Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b207bba7812a90120196185497b34c3efb111f36
[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         
50         /* Sébastien Miquée -- ajout de la lecture du fichier de config **/
51         
52         @SuppressWarnings("unchecked")
53         public void staticInitialization() {
54                 String adr = "";
55                 String home = System.getProperty("user.home");
56                 String fichier = home + "/supernode.conf";
57
58                 // lecture du fichier texte
59                 try {
60                         InputStream ips = new FileInputStream(fichier);
61                         InputStreamReader ipsr = new InputStreamReader(ips);
62                         BufferedReader br = new BufferedReader(ipsr);
63
64                         while ((adr = br.readLine()) != null) {
65                                 liste.add(new SuperNodeData(adr, 1098));
66                         }
67
68                         br.close();
69                 } catch (Exception e) {
70                         System.err.println(e.toString());
71                 }
72
73                 // String adr1 =
74                 // LocalHost.Instance().resolve("azur-38.sophia.grid5000.fr");
75                 // String adr2 =
76                 // LocalHost.Instance().resolve("cluster1.iut-bm.univ-fcomte.fr");
77                 // String adr3 =
78                 // LocalHost.Instance().resolve("smiths.iut-bm.univ-fcomte.fr");
79                 // String adr1="193.52.61.142";
80                 // String adr2="172.20.96.21";
81
82                 // String adr3="193.52.61.140";
83                 // System.out.println("adr3 = " + adr3);
84                 // liste.add(new SuperNodeData(adr1, 1098));
85                 // liste.add(new SuperNodeData(adr2, 1098));
86                 // liste.add(new SuperNodeData(adr3, 1098));
87         }
88
89         public void fileInitialization(String fileName) {
90                 // TODO modif JaceParser pr lire
91                 // donnees ds un fichier de conf
92         }
93
94         @SuppressWarnings("unchecked")
95         public Vector getListe() {
96                 return liste;
97         }
98
99         public int getSize() {
100                 return liste.size();
101         }
102
103         // Randomly returns a super Node of the liste
104         public SuperNodeData getASuperNodeData() {
105                 int index = r.nextInt() % liste.size();
106                 if (index < 0) {
107                         index = -index;
108                 }
109                 return (SuperNodeData) liste.elementAt(index);
110         }
111
112         public SuperNodeData getSuperNodeData(int i) {
113                 return (SuperNodeData) liste.elementAt(i);
114         }
115
116         // if returns 0, there are no nodes at all
117         public SuperNodeData getBestSuperNodeData() {
118                 int max = 0;
119                 SuperNodeData d = null;
120                 SuperNodeData return_d = null;
121
122                 for (int i = 0; i < liste.size(); i++) {
123                         d = (SuperNodeData) liste.elementAt(i);
124                         if (d.getNbOfNodes() > max) {
125                                 max = d.getNbOfNodes();
126                                 return_d = (SuperNodeData) liste.elementAt(i);
127                         }
128                 }
129                 return return_d;
130         }
131
132         public void addStub(int index, JaceSuperNodeInterface stub) {
133                 ((SuperNodeData) liste.elementAt(index)).setStub(stub);
134         }
135
136         @SuppressWarnings("unchecked")
137         public void addStubOf(String snode, int port, JaceSuperNodeInterface stub) {
138                 int is = -1;
139                 is = existSuperNode(snode);
140                 if (is != -1) {
141                         ((SuperNodeData) liste.elementAt(is)).setStub(stub);
142                 } else {
143                         System.err.println("This superNode does not exist : " + snode);
144                         System.out.println("Add the new SuperNode");
145                         SuperNodeData data = new SuperNodeData(snode, port);
146                         data.setStub(stub);
147                         data.setNbOfNodes(0);
148                         liste.add(data);
149
150                 }
151         }
152
153         public int getTotalDaemons() {
154                 int s = 0;
155
156                 for (int i = 0; i < liste.size(); i++) {
157                         s = s + ((SuperNodeData) liste.get(i)).getNbOfNodes();
158                         System.out.println("s=" + s + " " + Register.Instance().getSize());
159                 }
160                 return s;
161         }
162
163         public synchronized JaceSuperNodeInterface getStubOf(String superNode) {
164                 int is = -1;
165                 if (liste.isEmpty()) {
166                         return null;
167                 } else {
168                         if (superNode == null) {
169                                 System.err
170                                                 .println("In Register.getNode : host == null !!!!!!!!!!!!!!!");
171                         }
172                         is = existSuperNode(superNode);
173                         if (is != -1) {
174                                 return ((SuperNodeData) liste.get(is)).getStub();
175                         } else {
176                                 System.err.println("This superNode does not exist");
177                                 return null;
178                         }
179                 }
180         }
181
182         public synchronized int existSuperNode(String snode) {
183                 if (snode == null) {
184                         System.err
185                                         .println("HostIP in existNode = "
186                                                         + snode);
187                 }
188                 int existe = -1;
189                 int index = 0;
190                 while ((existe == -1) && (index < liste.size())) {
191                         if (snode.equals(((SuperNodeData) liste.get(index)).getIP())) {
192                                 existe = index;
193                         } else
194                                 index++;
195                 }
196                 return existe;
197         }
198
199         public void forwardCountNode() {
200                 SuperNodeData d = null;
201                 JaceSuperNodeInterface remoteStub = null;
202
203                 for (int i = 0; i < liste.size(); i++) {
204                         d = (SuperNodeData) liste.elementAt(i);
205                         if (!d.getIP().equals(LocalHost.Instance().getIP())) {
206                                 // if not me, I inform the other super nodes
207                                 remoteStub = d.getStub();
208                                 try {
209                                         remoteStub.updateCountNode(LocalHost.Instance().getIP(),
210                                                         Register.Instance().getSize());
211                                         // System.out.println("envoye : " +
212                                         // Register.Instance().getSize() + " a " + d.getIP());
213                                 } catch (Exception e) {
214                                         // System.out.println(d.getIP() + " is probably dead");
215                                         d.setNbOfNodes(0);
216                                 }
217                         }
218                 }
219         }
220
221         // TODO
222         // compare with stub rather than IP adresses
223         public void modifCountNode(String IP, int nb) {
224                 int is = -1;
225                 is = existSuperNode(IP);
226                 if (is != -1) {
227                         SuperNodeData d = (SuperNodeData) liste.elementAt(is);
228                         d.setNbOfNodes(nb);
229                 } else {
230                         System.err.println("This SuperNode does not exists in my list: " + IP);
231                 }
232         }
233
234         @SuppressWarnings("unchecked")
235         public void locateSuperNodes(JaceSuperNodeInterface stub) {
236                 String remoteIP;
237                 int remotePort;
238                 int i = 0;
239                 boolean connected = false;
240                 boolean inStaticList = false;
241                 while (i < liste.size() && !connected) {
242                         remoteIP = LocalHost.Instance().resolve(
243                                         ((SuperNodeData) liste.elementAt(i)).getIP());
244                         remotePort = ((SuperNodeData) liste.elementAt(i)).getPort();
245
246                         // If I'm not this superNode, I locate the others
247                         if (!remoteIP.equals(LocalHost.Instance().getIP())) {
248                                 JaceSuperNodeInterface remoteStub;
249                                 //int nb;
250                                 try {
251                                         // get the stub of remote SuperNodes
252                                         remoteStub = (JaceSuperNodeInterface) Naming
253                                                         .lookup("rmi://" + remoteIP + ":" + remotePort
254                                                                         + "/JaceSuperNode");
255                                         System.out.println("trying to contact " + remoteIP);
256                                         // add this stub in the local SuperNodeListe
257                                         // SuperNodeListe.Instance().addStub(i, remoteStub);
258
259                                         // Send my stub to this corresponding remote SuperNode
260                                         liste = (Vector<?>) remoteStub.sendStub(LocalHost.Instance()
261                                                         .getIP(), LocalHost.Instance().getPort(), stub);
262                                         System.out.println("Recieved List");
263                                         connected = true;
264                                         // tell this remote SuperNode that I have registered no
265                                         // Daemons yet
266                                         // remoteStub.updateCountNode(LocalHost.Instance().getIP(),
267                                         // 0);
268
269                                         // get the nb of Daemons the remote SuperNode has already
270                                         // registered
271                                         // nb = remoteStub.getNbOfNodes();
272                                         // ((SuperNodeData)liste.elementAt(i)).setNbOfNodes(nb);
273
274                                         // System.out.println("bien envoye mon stub a " + remoteIP);
275                                         System.out.println("size:" + liste.size());
276                                         viewAll();
277                                 } catch (Exception e) {
278                                         System.err.println("SuperNode " + remoteIP
279                                                         + " is down or not already launched: " + e);
280                                 }
281                         } else
282                                 inStaticList = true;
283                         i++;
284                 }
285                 if (!connected)
286                         if (inStaticList) {
287                                 liste.clear();
288                                 SuperNodeData data = new SuperNodeData(LocalHost.Instance()
289                                                 .getIP(), LocalHost.Instance().getPort());
290                                 data.setStub(stub);
291                                 data.setNbOfNodes(0);
292                                 liste.add(data);
293                                 TokenThread.Instance().setToken();
294                         } else {
295                                 System.err
296                                                 .println("No superNode alive in the static List, closing application");
297                                 System.exit(1);
298                         }
299
300         }
301
302         @SuppressWarnings("unchecked")
303         public void addSuperNode(SuperNodeData d) {
304                 int is = -1;
305                 String name = d.getIP();
306                 is = existSuperNode(name);
307                 if (is != -1) {
308                         liste.setElementAt(d, is);
309                 } else {
310                         liste.add(d);
311                 }
312         }
313
314         public void removeSuperNode(SuperNodeData d) {
315                 String name = d.getIP();
316                 int i = existSuperNode(name);
317                 if (i != -1) {
318                         // System.out.println("je remove le noeud de rang " + i);
319                         liste.remove(i);
320                 } else {
321                         System.err.println("The SuperNode does not exists!");
322                 }
323         }
324
325         public void viewAll() {
326                 System.out.println("\nConfig of the superNodes :");
327                 String msg = "";
328                 for (int i = 0; i < liste.size(); i++) {
329                         SuperNodeData d = (SuperNodeData) liste.elementAt(i);
330                         if (d.getIP().equals(LocalHost.Instance().getIP())) {
331                                 msg = "me : " + Register.Instance().getSize();
332                         } else {
333                                 System.out.println(d.getIP() + " : "
334                                                 + ((SuperNodeData) liste.elementAt(i)).getNbOfNodes());
335                         }
336                 }
337                 System.out.println(msg);
338                 // Register.Instance().viewAll();
339                 System.out.print("\n\n");
340         }
341         
342         
343         /****************************************/
344         /*********** Sébastien Miquée ***********/
345         /****************************************/
346         
347         public void addGNode( GNode _g )
348         {
349                 if( _g != null )
350                 {
351                         
352                         SuperNodeData d = null ;
353                         JaceSuperNodeInterface remoteStub = null ;
354
355                         for( int i = 0 ; i < liste.size() ; i++ ) 
356                         {
357                                 d = (SuperNodeData) liste.elementAt( i ) ;
358                                 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
359                                 {
360                                         // if not me, I inform the other super nodes
361                                         remoteStub = d.getStub() ;
362                                         try {
363                                                 remoteStub.addGNode( _g ) ;
364                                         } catch( Exception e ) {
365                                                 System.err.println( "Unable to add new GNode on SuperNode " + d.getIP() ) ;
366                                         }
367                                 }
368                         }
369                 }
370         }
371
372         public void removeGNode( GNode _g, int _mode, String _spawnerIP  )
373         {
374                 if( _g != null )
375                 {
376                         
377                         SuperNodeData d = null ;
378                         JaceSuperNodeInterface remoteStub = null ;
379
380                         for( int i = 0 ; i < liste.size() ; i++ ) 
381                         {
382                                 d = (SuperNodeData) liste.elementAt( i ) ;
383                                 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
384                                 {
385                                         // if not me, I inform the other super nodes
386                                         remoteStub = d.getStub() ;
387                                         try {
388                                                 remoteStub.delGNodeFromList( (Node)_g.getNode(), _mode, _spawnerIP ) ;
389                                         } catch( Exception e ) {
390                                                 System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
391                                         }
392                                 }
393                         }
394                 }
395         }
396         
397         
398         protected boolean workingOnGnodes()
399         {
400                 boolean ok = true ;     
401                 
402                 SuperNodeData d = null ;
403                 JaceSuperNodeInterface remoteStub = null ;
404
405                 for( int i = 0 ; i < liste.size() ; i++ ) 
406                 {
407                         d = (SuperNodeData) liste.elementAt( i ) ;
408                         if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
409                         {
410                                 // if not me, I inform the other super nodes
411                                 remoteStub = d.getStub() ;
412                                 try {
413                                         ok = ok && remoteStub.blockForMapping() ;
414                                 } catch( Exception e ) {
415                                         System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
416                                 }
417                         }
418                 }
419              
420             return ok ;
421         }
422
423         public void setMapping( Algo al ) 
424         {
425                 if( al != null )
426                 {
427                         SuperNodeData d = null ;
428                         JaceSuperNodeInterface remoteStub = null ;
429
430                         for( int i = 0 ; i < liste.size() ; i++ ) 
431                         {
432                                 d = (SuperNodeData) liste.elementAt( i ) ;
433                                 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
434                                 {
435                                         // if not me, I inform the other super nodes
436                                         remoteStub = d.getStub() ;
437                                         try {
438                                                 remoteStub.setMapping( al ) ;
439                                         } catch( Exception e ) {
440                                                 System.err.println( "Unable to set mapping data on SuperNode " + d.getIP() ) ;
441                                         }
442                                 }
443                         }
444                 }
445         }
446
447         public void propagateReplaceNode( GNode _dead, GNode _remp, String _spawnerIP ) 
448         {
449                 if( _dead != null && _remp != null )
450                 {
451                         SuperNodeData d = null ;
452                         JaceSuperNodeInterface remoteStub = null ;
453
454                         for( int i = 0 ; i < liste.size() ; i++ ) 
455                         {
456                                 d = (SuperNodeData) liste.elementAt( i ) ;
457                                 if( ! d.getIP().equals( LocalHost.Instance().getIP() ) ) 
458                                 {
459                                         // if not me, I inform the other super nodes
460                                         remoteStub = d.getStub() ;
461                                         try {
462                                                 remoteStub.updateMappedNode( _dead, _remp, _spawnerIP ) ;
463                                         } catch( Exception e ) {
464                                                 System.err.println( "Unable to propagate replacing node on SuperNode " + d.getIP() ) ;
465                                         }
466                                 }
467                         }
468                 }
469         }
470
471         
472         /**
473          * Send to all SuperNode the deletion of a mapping algorithm.
474          * @param id The mapping algorithm's identifier
475          */
476         public void removeAlgo( String _id ) 
477         {
478                 SuperNodeData d = null ;
479                 JaceSuperNodeInterface remoteStub = null ;
480
481                 for( int i = 0 ; i < liste.size() ; i++ ) 
482                 {
483                         d = (SuperNodeData) liste.elementAt( i ) ;
484                         if( ! d.getIP().equals( LocalHost.Instance().getIP() ) ) 
485                         {
486                                 // if not me, I inform the other super nodes
487                                 remoteStub = d.getStub() ;
488                                 try {
489                                         remoteStub.removeAlgo( _id, 1 ) ;
490                                 } catch( Exception e ) {
491                                         System.err.println( "Unable to propagate the mapping algorithm deletion " + d.getIP() ) ;
492                                 }
493                         }
494                 }               
495         }
496
497 }