+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
de0e46f
master
1import java .util .*;import java .net .*; 2public class NameTable { 3class NameEntry { 4public String procName ; 5public InetSocketAddress addr ; 6public NameEntry (String pName ,String host ,int port ){ 7procName =pName ; 8addr =new InetSocketAddress (host ,port ); 9 } 10 } 11ArrayList <NameEntry >table =new ArrayList <NameEntry >(); 12public synchronized InetSocketAddress search (String procName ) { 13System .out .println ("Searching " +procName ); 14for (NameEntry entry :table ) 15if (procName .equals (entry .procName ))return entry .addr ; 16return null ; 17 } 18// returns 0 if old value replaced, otherwise 1 19public synchronized int insert (String procName ,String hostName ,int portNumber ) { 20System .out .println ("Inserting " +procName ); 21int retValue =1 ; 22for (NameEntry entry :table ) 23if (procName .equals (entry .procName )) { 24table .remove (entry ); 25retValue =0 ; 26 } 27table .add (new NameEntry (procName ,hostName ,portNumber )); 28notifyAll (); 29return retValue ; 30 } 31public synchronized InetSocketAddress blockingFind (String procName ) { 32System .out .println ("blockingFind " +procName ); 33InetSocketAddress addr =search (procName ); 34while (addr ==null ) { 35Util .myWait (this ); 36addr =search (procName ); 37 } 38return addr ; 39 } 40public synchronized void clear () { 41table .clear (); 42 } 43}