Logo AND Algorithmique Numérique Distribuée

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