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