+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

Wei-Lun Hungadd chap 6abf804d

master
1.6 KiB52 linesraw
1import java.util.*;
2import java.net.*; import java.io.*;
3public class Name {
4	Scanner din;
5	PrintStream pout;
6	Socket server;
7	public void getSocket() throws IOException {
8		server = new Socket(Symbols.nameServer, Symbols.ServerPort);
9		din = new Scanner(server.getInputStream());
10		pout = new PrintStream(server.getOutputStream());
11	}
12	public int insertName(String procName, String hostName, int portnum)
13			throws IOException {
14		getSocket();
15		pout.println("insert " + procName + " " + hostName + " " + portnum);
16		pout.flush();
17		int retValue = din.nextInt();
18		server.close();
19		return retValue;
20	}
21	public InetSocketAddress searchName(String procName, boolean isBlocking) 
22			throws IOException {
23		getSocket();
24		if (isBlocking) pout.println("blockingFind " + procName);
25		else pout.println("search " + procName);
26		pout.flush();
27		String result = din.nextLine();
28		System.out.println("NameServer returned" + result);
29		Scanner sc = new Scanner(result);
30		server.close();
31		int portnum = sc.nextInt();
32		String hostName = sc.next();
33		if (portnum == 0) return null; 
34		else return new InetSocketAddress(hostName, portnum);
35	}
36	public void clear() throws IOException {
37		getSocket();
38		pout.println("clear " );
39		pout.flush();
40		server.close();
41	}
42	public static void main(String[] args) {
43		Name myClient = new Name();
44		try {
45			myClient.insertName("hello1", "oak.ece.utexas.edu", 1000);
46			InetSocketAddress pa = myClient.searchName("hello1",false);
47			System.out.println(pa.getHostName() + ":" + pa.getPort());
48		} catch (Exception e) {
49			System.err.println("Server aborted:" + e);
50		}
51	}
52}