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