+/EE360P

distributed systems

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

vijaygarg1init99f4f39

master
1.3 KiB42 linesraw
1package name;
2import java.net.*; import java.io.*; import java.util.*;
3public class ServerThread extends Thread {
4	NameTable table;
5	Socket theClient;
6	public ServerThread(NameTable table, Socket s) {
7		this.table = table;
8		theClient = s;
9	}
10	public void run() {
11		try {
12			Scanner sc = new Scanner(theClient.getInputStream());
13			PrintWriter pout = new PrintWriter(theClient.getOutputStream());
14			String command = sc.nextLine();
15			System.out.println("received:" + command);
16			Scanner st = new Scanner(command);          
17			String tag = st.next();
18			if (tag.equals("search")) {
19				InetSocketAddress addr = table.search(st.next());
20				if (addr == null) pout.println(0 + " " + "nullhost");
21				else pout.println(addr.getPort() + " " + addr.getHostName());
22			} else if (tag.equals("insert")) {
23				String name = st.next();
24				String hostName = st.next();
25				int port = st.nextInt();
26				int retValue = table.insert(name, hostName, port);
27				pout.println(retValue);
28			} else if (tag.equals("blockingFind")) {
29				InetSocketAddress addr = table.blockingFind(st.next());
30				pout.println(addr.getPort() + " " + addr.getHostName());
31			} else if (tag.equals("clear")) {
32				table.clear();
33			}
34			pout.flush();
35			theClient.close();
36		} catch (IOException e) {
37			System.err.println(e);
38		}
39
40	}
41}
42