+/EE360P

distributed systems

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

vijaygarg1init99f4f39

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