-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteCache.java
79 lines (67 loc) · 1.67 KB
/
RemoteCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.Vector;
public class RemoteCache implements ICache
{
ICache cache;
HashBalancer hb;
public RemoteCache(Vector<String> serverNames, int localId, ICache localCache)
{
String serverName = serverNames.get(localId);
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
URL url = new URL("http:" + serverName);
int port = 1099;
if (url.getPort() > 0)
port = url.getPort();
LocateRegistry.createRegistry(port);
System.err.println("RMI on port " + port + "...");
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
cache = localCache;
Naming.rebind(serverName, cache);
hb = new HashBalancer(serverNames);
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void clear()
{
for (String server : hb.getServers())
{
try {
((ICache)Naming.lookup(server)).clear();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public String get(String key) throws RemoteException
{
return getCacheServer(key).get(key);
}
public void set(String key, String val) throws RemoteException
{
getCacheServer(key).set(key, val);
}
private ICache getCacheServer(String key)
{
try {
return (ICache)Naming.lookup(hb.getServerForKey(key));
} catch (Exception e) {
e.printStackTrace();
}
return cache;
}
}