+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
99f4f39
master
1package name ; 2import java .util .*;import java .net .*; 3public class NameTable { 4class NameEntry { 5public String procName ; 6public InetSocketAddress addr ; 7public NameEntry (String pName ,String host ,int port ){ 8procName =pName ; 9addr =new InetSocketAddress (host ,port ); 10 } 11 } 12ArrayList <NameEntry >table =new ArrayList <NameEntry >(); 13public synchronized InetSocketAddress search (String 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 ) { 20int retValue =1 ; 21for (NameEntry entry :table ) 22if (procName .equals (entry .procName )) { 23table .remove (entry ); 24retValue =0 ; 25 } 26table .add (new NameEntry (procName ,hostName ,portNumber )); 27notifyAll (); 28return retValue ; 29 } 30public synchronized InetSocketAddress blockingFind (String procName ) { 31InetSocketAddress addr =search (procName ); 32try { 33while (addr ==null ) { 34wait (); 35addr =search (procName ); 36 } 37 } 38catch (InterruptedException e ){System .out .println (e ); } 39return addr ; 40 } 41public synchronized void clear () { 42table .clear (); 43 } 44}