1 7 package org.objectweb.proactive.p2p.registry; 8 9 import java.io.IOException ; 10 import java.io.Serializable ; 11 import java.rmi.RMISecurityManager ; 12 import java.rmi.RemoteException ; 13 import java.util.HashMap ; 15 import java.util.LinkedList ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.objectweb.proactive.Body; 20 import org.objectweb.proactive.ProActive; 21 import org.objectweb.proactive.RunActive; 22 import org.objectweb.proactive.Service; 23 import org.objectweb.proactive.core.body.rmi.*; 24 import org.objectweb.proactive.core.config.ProActiveConfiguration; 25 27 28 import net.jini.discovery.LookupDiscovery; 29 30 36 public class P2PRegistryImpl implements Serializable , P2PRegistry, RunActive { 38 39 private Map register; 40 private Map underloaded; 41 42 public P2PRegistryImpl() throws RemoteException { 43 register = new HashMap (); 44 underloaded = new HashMap (); 45 } 46 47 48 49 public void register(String key, Object o) throws RemoteException { 50 51 register.put(key,o); 52 System.out.println("[P2PRegistry] Register: "+key); 53 } 54 55 public void unregister(String key, Object o) throws RemoteException { 56 register.remove(key); 57 System.out.println("[P2PRegistry] Unregister: "+key); 58 } 59 60 public void registerOverloaded(String key, Object o) throws RemoteException { 61 } 62 63 public void unregisterOverloaded(String key, Object o) throws RemoteException { 64 } 65 66 public void registerUnderloaded(String key, Object o) throws RemoteException { 67 underloaded.put(key, o); 68 System.out.println("[P2PRegistry] Underload Register: "+key); 69 } 70 71 public void unregisterUnderloaded(String key, Object o) throws RemoteException { 72 underloaded.remove(key); 73 System.out.println("[P2PRegistry] Underload Unregister: "+key); 74 } 75 76 public Object getUnderloaded(String key) throws RemoteException { 77 78 Object o = null; 79 80 if (underloaded.size() < 1) return o; 81 82 Object [] keys = underloaded.keySet().toArray(); 83 return underloaded.get(keys[0]); 84 } 85 86 87 public static void main(String [] args) throws Exception { 88 89 90 P2PRegistryImpl Areg = (P2PRegistryImpl) ProActive.newActive(P2PRegistryImpl.class.getName(),new Object []{}); 91 System.out.println("[P2PRegistry] Started."); 92 93 } 94 95 96 public int getNumberOfAvailables() throws RemoteException { 97 return underloaded.size(); 98 } 99 100 public Object [] getFullyAvailables(int n) throws RemoteException { 101 if (n > underloaded.size()) n = underloaded.size(); 102 List l = new LinkedList (underloaded.values()); 103 return l.subList(0,n-1).toArray(); 104 } 105 106 public Object [] getAvailables(int n) throws RemoteException { 107 if (n > register.size()) n = register.size(); 108 List l = new LinkedList (register.values()); 109 return l.subList(0,n-1).toArray(); 110 } 111 112 113 114 private void registerJini(RemoteBodyAdapter body) throws IOException { 115 116 System.setSecurityManager(new RMISecurityManager ()); 117 String [] groups = new String [] {""}; 118 LookupDiscovery ld = new LookupDiscovery(groups); 119 Object reg = body.getRemoteAdapter(); 120 122 ServerListener sl = new ServerListener(ld,reg); 124 ld.addDiscoveryListener(sl); 125 } 126 127 public void register () { 128 try { 129 ProActive.register(ProActive.getStubOnThis(), "//localhost/P2PRegistry"); 130 } 131 catch (IOException e) { 132 System.err.println("[Robin Hood] Cannot register!"); 133 e.printStackTrace(); 134 } 135 } 136 137 public void unregister () { 138 try { 139 ProActive.unregister("//localhost/P2PRegistry"); 140 } 141 catch (IOException e) { e.printStackTrace(); } 142 } 143 144 public void runActivity(Body body) { 145 146 ProActiveConfiguration.load(); 147 Service service = new Service(body); 148 register(); 149 150 while (body.isActive()) { 151 service.blockingServeOldest(); 152 }; 153 154 unregister(); 155 } 156 157 158 } 159 | Popular Tags |