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