+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1included dependenciesde0e46f

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