Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Creating the JaceP2P repository.
[jaceP2P.git] / src / jaceP2P / SuperNodeListe.java
1 package jaceP2P;
2
3 import java.io.BufferedReader;
4 import java.io.FileInputStream;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.rmi.Naming;
8 import java.util.Random;
9 import java.util.Vector;
10
11 import and.Mapping.Algo;
12 import and.Mapping.GNode;
13
14 public class SuperNodeListe implements Cloneable {
15
16         // attributes
17         public static SuperNodeListe Instance;
18         private Random r;
19         @SuppressWarnings("unchecked")
20         private Vector liste = null;
21
22         // constructors
23         @SuppressWarnings("unchecked")
24         public SuperNodeListe() {
25                 liste = new Vector();
26                 r = new Random();
27         }
28
29         public synchronized static SuperNodeListe Instance() {
30                 if (Instance == null) {
31                         Instance = new SuperNodeListe();
32                 }
33                 return Instance;
34         }
35
36         @SuppressWarnings("unchecked")
37         public synchronized SuperNodeListe clone() {
38                 SuperNodeListe d = new SuperNodeListe();
39                 // d.r = (Random) r.clone();
40                 d.liste = (Vector) liste.clone();
41                 return d;
42         }
43
44         // methods
45
46         // TODO change to have non-static list
47         // using a config file
48         // in order to discover new superNodes dynamicaly
49         @SuppressWarnings("unchecked")
50         public void staticInitialization() {
51                 String adr = "";
52                 String home = System.getProperty("user.home");
53                 String fichier = home + "/supernode.conf";
54
55                 // lecture du fichier texte
56                 try {
57                         InputStream ips = new FileInputStream(fichier);
58                         InputStreamReader ipsr = new InputStreamReader(ips);
59                         BufferedReader br = new BufferedReader(ipsr);
60
61                         while ((adr = br.readLine()) != null) {
62                                 liste.add(new SuperNodeData(adr, 1098));
63                         }
64
65                         br.close();
66                 } catch (Exception e) {
67                         System.out.println(e.toString());
68                 }
69
70                 // String adr1 =
71                 // LocalHost.Instance().resolve("azur-38.sophia.grid5000.fr");
72                 // String adr2 =
73                 // LocalHost.Instance().resolve("cluster1.iut-bm.univ-fcomte.fr");
74                 // String adr3 =
75                 // LocalHost.Instance().resolve("smiths.iut-bm.univ-fcomte.fr");
76                 // String adr1="193.52.61.142";
77                 // String adr2="172.20.96.21";
78
79                 // String adr3="193.52.61.140";
80                 // System.out.println("adr3 = " + adr3);
81                 // liste.add(new SuperNodeData(adr1, 1098));
82                 // liste.add(new SuperNodeData(adr2, 1098));
83                 // liste.add(new SuperNodeData(adr3, 1098));
84         }
85
86         public void fileInitialization(String fileName) {
87                 // TODO modif JaceParser pr lire
88                 // donnees ds un fichier de conf
89         }
90
91         @SuppressWarnings("unchecked")
92         public Vector getListe() {
93                 return liste;
94         }
95
96         public int getSize() {
97                 return liste.size();
98         }
99
100         // Randomly returns a super Node of the liste
101         public SuperNodeData getASuperNodeData() {
102                 int index = r.nextInt() % liste.size();
103                 if (index < 0) {
104                         index = -index;
105                 }
106                 return (SuperNodeData) liste.elementAt(index);
107         }
108
109         public SuperNodeData getSuperNodeData(int i) {
110                 return (SuperNodeData) liste.elementAt(i);
111         }
112
113         // if returns 0, there are no nodes at all
114         public SuperNodeData getBestSuperNodeData() {
115                 int max = 0;
116                 SuperNodeData d = null;
117                 SuperNodeData return_d = null;
118
119                 for (int i = 0; i < liste.size(); i++) {
120                         d = (SuperNodeData) liste.elementAt(i);
121                         if (d.getNbOfNodes() > max) {
122                                 max = d.getNbOfNodes();
123                                 return_d = (SuperNodeData) liste.elementAt(i);
124                         }
125                 }
126                 return return_d;
127         }
128
129         public void addStub(int index, JaceSuperNodeInterface stub) {
130                 ((SuperNodeData) liste.elementAt(index)).setStub(stub);
131         }
132
133         @SuppressWarnings("unchecked")
134         public void addStubOf(String snode, int port, JaceSuperNodeInterface stub) {
135                 int is = -1;
136                 is = existSuperNode(snode);
137                 if (is != -1) {
138                         ((SuperNodeData) liste.elementAt(is)).setStub(stub);
139                 } else {
140                         System.out.println("this superNode does not exist : " + snode);
141                         System.out.println("add the new SuperNode");
142                         SuperNodeData data = new SuperNodeData(snode, port);
143                         data.setStub(stub);
144                         data.setNbOfNodes(0);
145                         liste.add(data);
146
147                 }
148         }
149
150         public int getTotalDaemons() {
151                 int s = 0;
152
153                 for (int i = 0; i < liste.size(); i++) {
154                         s = s + ((SuperNodeData) liste.get(i)).getNbOfNodes();
155                         System.out.println("s=" + s + " " + Register.Instance().getSize());
156                 }
157                 return s;
158         }
159
160         public synchronized JaceSuperNodeInterface getStubOf(String superNode) {
161                 int is = -1;
162                 if (liste.isEmpty()) {
163                         return null;
164                 } else {
165                         if (superNode == null) {
166                                 System.out
167                                                 .println("In Register.getNode : host == null !!!!!!!!!!!!!!!");
168                         }
169                         is = existSuperNode(superNode);
170                         if (is != -1) {
171                                 return ((SuperNodeData) liste.get(is)).getStub();
172                         } else {
173                                 System.out.println("this superNode does not exist");
174                                 return null;
175                         }
176                 }
177         }
178
179         public synchronized int existSuperNode(String snode) {
180                 if (snode == null) {
181                         System.out
182                                         .println("!!!!!!!!!!!!!!!!!!!!!!!!!   hostIP in existNode = "
183                                                         + snode);
184                 }
185                 int existe = -1;
186                 int index = 0;
187                 while ((existe == -1) && (index < liste.size())) {
188                         if (snode.equals(((SuperNodeData) liste.get(index)).getIP())) {
189                                 existe = index;
190                         } else
191                                 index++;
192                 }
193                 return existe;
194         }
195
196         public void forwardCountNode() {
197                 SuperNodeData d = null;
198                 JaceSuperNodeInterface remoteStub = null;
199
200                 for (int i = 0; i < liste.size(); i++) {
201                         d = (SuperNodeData) liste.elementAt(i);
202                         if (!d.getIP().equals(LocalHost.Instance().getIP())) {
203                                 // if not me, I inform the other super nodes
204                                 remoteStub = d.getStub();
205                                 try {
206                                         remoteStub.updateCountNode(LocalHost.Instance().getIP(),
207                                                         Register.Instance().getSize());
208                                         // System.out.println("envoye : " +
209                                         // Register.Instance().getSize() + " a " + d.getIP());
210                                 } catch (Exception e) {
211                                         // System.out.println(d.getIP() + " is probably dead");
212                                         d.setNbOfNodes(0);
213                                 }
214                         }
215                 }
216         }
217
218         // TODO
219         // compare with stub rather than IP adresses
220         public void modifCountNode(String IP, int nb) {
221                 int is = -1;
222                 is = existSuperNode(IP);
223                 if (is != -1) {
224                         SuperNodeData d = (SuperNodeData) liste.elementAt(is);
225                         d.setNbOfNodes(nb);
226                 } else {
227                         System.out.println("ce superNode existe pas ds ma liste : " + IP);
228                 }
229         }
230
231         @SuppressWarnings("unchecked")
232         public void locateSuperNodes(JaceSuperNodeInterface stub) {
233                 String remoteIP;
234                 int remotePort;
235                 int i = 0;
236                 boolean connected = false;
237                 boolean inStaticList = false;
238                 while (i < liste.size() && !connected) {
239                         remoteIP = LocalHost.Instance().resolve(
240                                         ((SuperNodeData) liste.elementAt(i)).getIP());
241                         remotePort = ((SuperNodeData) liste.elementAt(i)).getPort();
242
243                         // If I'm not this superNode, I locate the others
244                         if (!remoteIP.equals(LocalHost.Instance().getIP())) {
245                                 JaceSuperNodeInterface remoteStub;
246                                 //int nb;
247                                 try {
248                                         // get the stub of remote SuperNodes
249                                         remoteStub = (JaceSuperNodeInterface) Naming
250                                                         .lookup("rmi://" + remoteIP + ":" + remotePort
251                                                                         + "/JaceSuperNode");
252                                         System.out.println("trying to contact " + remoteIP);
253                                         // add this stub in the local SuperNodeListe
254                                         // SuperNodeListe.Instance().addStub(i, remoteStub);
255
256                                         // Send my stub to this corresponding remote SuperNode
257                                         liste = (Vector<?>) remoteStub.sendStub(LocalHost.Instance()
258                                                         .getIP(), LocalHost.Instance().getPort(), stub);
259                                         System.out.println("Recieved List");
260                                         connected = true;
261                                         // tell this remote SuperNode that I have registered no
262                                         // Daemons yet
263                                         // remoteStub.updateCountNode(LocalHost.Instance().getIP(),
264                                         // 0);
265
266                                         // get the nb of Daemons the remote SuperNode has already
267                                         // registered
268                                         // nb = remoteStub.getNbOfNodes();
269                                         // ((SuperNodeData)liste.elementAt(i)).setNbOfNodes(nb);
270
271                                         // System.out.println("bien envoye mon stub a " + remoteIP);
272                                         System.out.println("size:" + liste.size());
273                                         viewAll();
274                                 } catch (Exception e) {
275                                         System.out.println("SuperNode " + remoteIP
276                                                         + " is down or not already launched: " + e);
277                                 }
278                         } else
279                                 inStaticList = true;
280                         i++;
281                 }
282                 if (!connected)
283                         if (inStaticList) {
284                                 liste.clear();
285                                 SuperNodeData data = new SuperNodeData(LocalHost.Instance()
286                                                 .getIP(), LocalHost.Instance().getPort());
287                                 data.setStub(stub);
288                                 data.setNbOfNodes(0);
289                                 liste.add(data);
290                                 TokenThread.Instance().setToken();
291                         } else {
292                                 System.out
293                                                 .println("no superNode alive in the static List, closing application");
294                                 System.exit(1);
295                         }
296
297         }
298
299         @SuppressWarnings("unchecked")
300         public void addSuperNode(SuperNodeData d) {
301                 int is = -1;
302                 String name = d.getIP();
303                 is = existSuperNode(name);
304                 if (is != -1) {
305                         liste.setElementAt(d, is);
306                 } else {
307                         liste.add(d);
308                 }
309         }
310
311         public void removeSuperNode(SuperNodeData d) {
312                 String name = d.getIP();
313                 int i = existSuperNode(name);
314                 if (i != -1) {
315                         // System.out.println("je remove le noeud de rang " + i);
316                         liste.remove(i);
317                 } else {
318                         System.out.println("super node existe pas");
319                 }
320         }
321
322         public void viewAll() {
323                 System.out.println("\nConfig of the superNodes :");
324                 String msg = "";
325                 for (int i = 0; i < liste.size(); i++) {
326                         SuperNodeData d = (SuperNodeData) liste.elementAt(i);
327                         if (d.getIP().equals(LocalHost.Instance().getIP())) {
328                                 msg = "me : " + Register.Instance().getSize();
329                         } else {
330                                 System.out.println(d.getIP() + " : "
331                                                 + ((SuperNodeData) liste.elementAt(i)).getNbOfNodes());
332                         }
333                 }
334                 System.out.println(msg);
335                 // Register.Instance().viewAll();
336                 System.out.print("\n\n");
337         }
338         
339         
340         /****************************************/
341         /*********** Sébastien Miquée ***********/
342         /****************************************/
343         
344         public void addGNode( GNode _g )
345         {
346                 if( _g != null )
347                 {
348                         
349                         SuperNodeData d = null ;
350                         JaceSuperNodeInterface remoteStub = null ;
351
352                         for( int i = 0 ; i < liste.size() ; i++ ) 
353                         {
354                                 d = (SuperNodeData) liste.elementAt( i ) ;
355                                 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
356                                 {
357                                         // if not me, I inform the other super nodes
358                                         remoteStub = d.getStub() ;
359                                         try {
360                                                 remoteStub.addGNode( _g ) ;
361                                         } catch( Exception e ) {
362                                                 System.err.println( "Unable to add new GNode at SuperNode " + d.getIP() ) ;
363                                         }
364                                 }
365                         }
366                 }
367         }
368         
369         public void removeGNode( GNode _g )
370         {
371                 if( _g != null )
372                 {
373                         
374                         SuperNodeData d = null ;
375                         JaceSuperNodeInterface remoteStub = null ;
376
377                         for( int i = 0 ; i < liste.size() ; i++ ) 
378                         {
379                                 d = (SuperNodeData) liste.elementAt( i ) ;
380                                 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
381                                 {
382                                         // if not me, I inform the other super nodes
383                                         remoteStub = d.getStub() ;
384                                         try {
385                                                 remoteStub.removeGNode( _g ) ;
386                                         } catch( Exception e ) {
387                                                 System.err.println( "Unable to remove GNode at SuperNode " + d.getIP() ) ;
388                                         }
389                                 }
390                         }
391                 }
392         }
393
394         public void setMapping( Algo al ) 
395         {
396                 if( al != null )
397                 {
398                         SuperNodeData d = null ;
399                         JaceSuperNodeInterface remoteStub = null ;
400
401                         for( int i = 0 ; i < liste.size() ; i++ ) 
402                         {
403                                 d = (SuperNodeData) liste.elementAt( i ) ;
404                                 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
405                                 {
406                                         // if not me, I inform the other super nodes
407                                         remoteStub = d.getStub() ;
408                                         try {
409                                                 remoteStub.setMapping( al ) ;
410                                         } catch( Exception e ) {
411                                                 System.err.println( "Unable to set mapping data at SuperNode " + d.getIP() ) ;
412                                         }
413                                 }
414                         }
415                 }
416         }
417
418 }