+/EE360P

distributed systems

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

Wei-Lun Hungadd chap 6abf804d

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