KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > p2p > registry > P2PRegistryImpl


1 /*
2  * Created on Jan 29, 2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.objectweb.proactive.p2p.registry;
8
9 import java.io.IOException JavaDoc;
10 import java.io.Serializable JavaDoc;
11 import java.rmi.RMISecurityManager JavaDoc;
12 import java.rmi.RemoteException JavaDoc;
13 // import java.rmi.server.UnicastRemoteObject;
14
import java.util.HashMap JavaDoc;
15 import java.util.LinkedList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 // import org.objectweb.proactive.core.node.*;
26

27
28 import net.jini.discovery.LookupDiscovery;
29
30 /**
31  * @author jbustos
32  *
33  * To change the template for this generated type comment go to
34  * Window>Preferences>Java>Code Generation>Code and Comments
35  */

36 // public class P2PRegisterImpl extends UnicastRemoteObject implements P2PRegister {
37
public class P2PRegistryImpl implements Serializable JavaDoc, P2PRegistry, RunActive {
38
39     private Map JavaDoc register;
40     private Map JavaDoc underloaded;
41         
42     public P2PRegistryImpl() throws RemoteException JavaDoc {
43         register = new HashMap JavaDoc();
44         underloaded = new HashMap JavaDoc();
45     }
46
47 /****************** External Messages **************/
48
49     public void register(String JavaDoc key, Object JavaDoc o) throws RemoteException JavaDoc {
50         
51         register.put(key,o);
52         System.out.println("[P2PRegistry] Register: "+key);
53     }
54
55     public void unregister(String JavaDoc key, Object JavaDoc o) throws RemoteException JavaDoc {
56         register.remove(key);
57         System.out.println("[P2PRegistry] Unregister: "+key);
58     }
59
60     public void registerOverloaded(String JavaDoc key, Object JavaDoc o) throws RemoteException JavaDoc {
61     }
62
63     public void unregisterOverloaded(String JavaDoc key, Object JavaDoc o) throws RemoteException JavaDoc {
64     }
65
66     public void registerUnderloaded(String JavaDoc key, Object JavaDoc o) throws RemoteException JavaDoc {
67             underloaded.put(key, o);
68         System.out.println("[P2PRegistry] Underload Register: "+key);
69     }
70
71     public void unregisterUnderloaded(String JavaDoc key, Object JavaDoc o) throws RemoteException JavaDoc {
72             underloaded.remove(key);
73         System.out.println("[P2PRegistry] Underload Unregister: "+key);
74     }
75
76     public Object JavaDoc getUnderloaded(String JavaDoc key) throws RemoteException JavaDoc {
77         
78         Object JavaDoc o = null;
79         
80             if (underloaded.size() < 1) return o;
81
82             Object JavaDoc[] keys = underloaded.keySet().toArray();
83             return underloaded.get(keys[0]);
84     }
85 /******************* Internal use *********************************/
86
87 public static void main(String JavaDoc[] args) throws Exception JavaDoc {
88
89         
90     P2PRegistryImpl Areg = (P2PRegistryImpl) ProActive.newActive(P2PRegistryImpl.class.getName(),new Object JavaDoc[]{});
91     System.out.println("[P2PRegistry] Started.");
92     
93     }
94 /************** Job Submitter use *****************************/
95
96 public int getNumberOfAvailables() throws RemoteException JavaDoc {
97     return underloaded.size();
98     }
99
100 public Object JavaDoc[] getFullyAvailables(int n) throws RemoteException JavaDoc {
101         if (n > underloaded.size()) n = underloaded.size();
102         List JavaDoc l = new LinkedList JavaDoc(underloaded.values());
103         return l.subList(0,n-1).toArray();
104         }
105
106 public Object JavaDoc[] getAvailables(int n) throws RemoteException JavaDoc {
107     if (n > register.size()) n = register.size();
108     List JavaDoc l = new LinkedList JavaDoc(register.values());
109     return l.subList(0,n-1).toArray();
110     }
111
112 /*************** Active object methods ***************************/
113
114 private void registerJini(RemoteBodyAdapter body) throws IOException JavaDoc {
115
116     System.setSecurityManager(new RMISecurityManager JavaDoc());
117     String JavaDoc[] groups = new String JavaDoc[] {""};
118     LookupDiscovery ld = new LookupDiscovery(groups);
119     Object JavaDoc reg = body.getRemoteAdapter();
120     // Debug
121

122     // end Debug
123
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 JavaDoc 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 JavaDoc 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