Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7c64bed216636454911493f02b2dc673f4c209af
[jaceP2P.git] / src / jaceP2P / JaceSpawner.java
1 package jaceP2P;
2
3 import java.io.FileOutputStream;
4 import java.io.OutputStreamWriter;
5 import java.io.PrintWriter;
6 import java.rmi.Naming;
7 import java.rmi.RemoteException;
8 import java.rmi.UnmarshalException;
9 import java.util.Calendar;
10 import java.util.GregorianCalendar;
11 import java.util.Random;
12 import java.util.Vector;
13
14 import and.Mapping.Algo;
15 import and.Mapping.Utils;
16
17 public class JaceSpawner {
18         private Class<?> c;
19         private Loader load;
20         private Task tache = null;
21         public static JaceSpawner Instance;
22         private static String superNode_IP = null;
23         private int superNode_port = 1098;
24         private static int spawnerPort = 1098;
25         protected static JaceSuperNodeInterface centralServer = null;
26         private JaceSpawnerInterface spawnerRef = null;
27         private int nbTasks;
28         private String appliName;
29         private String[] params = null;
30         @SuppressWarnings("unused")
31         private String protocol;
32         // private int registerVersion=0;
33         final int NB_HEART_DECONNECT = 3;
34         private int heartTime; // frequency of heartBeat
35         @SuppressWarnings("unused")
36         private int timeBeforeKill; // wait 3 non-response of heartBeat before
37         // considering de Daemon as dead
38         private boolean broadcasting = false;
39         @SuppressWarnings("unused")
40         private int z = 0;
41         private static int nbOfDaemonsPerSpawner;
42         private static int nbOfDeamonsPerThread;
43         private Vector<Object> spawnersList;
44         private int rank;
45         private int nbSavingNodes;
46
47         // Variables for Mapping
48         private int algo;
49         private double paramAlgo ;
50         private String idAlgo ;
51         
52         // ** Test ** //
53         private int test;
54         private double dh ;
55         protected int nbFault ;
56         protected int faultTime ;
57
58         public JaceSpawner(String superNode, int port, String comProtocol,
59                         String[] args, int nbDaemonPerSpawner, int nbDaemonPerThread,
60                         int nbSavingNodes, int _algo, double _paramAlgo, int _test, int _nbF, int _fT) {
61                 // superNode_IP = LocalHost.Instance().resolve(superNode);
62                 algo = _algo;
63                 paramAlgo = _paramAlgo ;
64                 
65                 
66                 test = _test ;
67                 nbFault = _nbF ;
68                 faultTime = _fT ;
69                 
70                 
71                 superNode_IP = superNode;
72                 spawnerPort = port;
73                 protocol = comProtocol;
74                 nbOfDaemonsPerSpawner = nbDaemonPerSpawner;
75                 nbOfDeamonsPerThread = nbDaemonPerThread;
76                 this.nbSavingNodes = nbSavingNodes;
77                 if (args.length < 2)
78                 {
79                         // if less than 2 params (nb of tasks and name of the appli), error
80                         System.err.println( "Parameters error !" ) ;
81                         System.exit( 1 ) ;
82                 } else {
83                         try {
84                                 nbTasks = new Integer(args[0]).intValue(); // nb of tasks
85                                 // launched by the
86                                 // spawner
87                         } catch (Exception e) {
88                                 System.err.println("Number format exception :" + e ) ;
89                                 System.exit( 1 ) ;
90                         }
91                         appliName = args[1]; // name of the class to launch
92                         if (args.length > 2) { // get the eventual param of the appli
93                                 params = new String[args.length - 2];
94                                 for (int i = 0; i < params.length; i++) {
95                                         params[i] = args[2 + i];
96                                 }
97                         }
98                         load = new Loader();
99                         c = load.load(appliName);
100                         try {
101                                 tache = ((Task) c.newInstance());
102                                 tache.setParam(params);
103                                 tache.setJaceSize(nbTasks);
104
105                                 // ****************//
106                                 //tache.printDep();
107                         } catch (Exception e) {
108                                 System.err.println( "Unable to instantiate the class " + e ) ;
109                                 System.exit( 1 ) ;
110                         }
111                 }
112
113                 Instance = this;
114         }
115         
116         
117
118         public JaceSpawner(String[] params, String appliName, Register reg,
119                         int nbTasks, JaceSuperNodeInterface snodeStub, int rank,
120                         int heartTime, int tag, int nbdc, int nbsdc,
121                         int nbDaemonPerSpawner, int nbDaemonPerThread, String _idAlgo) {
122                 try {
123                         nbOfDaemonsPerSpawner = nbDaemonPerSpawner;
124                         nbOfDeamonsPerThread = nbDaemonPerThread;
125                         if (params.length != 0) {
126                                 this.params = new String[params.length];
127                                 for (int i = 0; i < params.length; i++)
128                                         this.params[i] = params[i];
129                         } else {
130                                 params = null;
131                                 System.err.println( "There is no parameter !" ) ;
132                         }
133                 } catch (Exception e) {
134                         System.err.println("Error in copying the parameters: " + e ) ;
135                 }
136                 
137                 idAlgo = _idAlgo ;
138                 
139                 this.appliName = appliName;
140
141                 this.nbTasks = nbTasks;
142                 this.heartTime = heartTime;
143                 LocalHost.Instance().setSuperNodeStub(snodeStub);
144                 centralServer = snodeStub;
145                 exportObject();
146                 Register.Instance().replaceBy(reg);
147                 Register.Instance().setSpawnerStub(this.spawnerRef);
148                 Register.Instance().getListeOfTasks().viewAll();
149
150                 this.rank = rank;
151                 load = new Loader();
152                 c = load.load(appliName);
153                 try {
154                         tache = ((Task) c.newInstance());
155                         tache.setParam(params);
156                         tache.setJaceSize(nbTasks);
157                         // ****************//
158                         tache.printDep();
159                 } catch (Exception e) {
160                         System.err.println("Unable to instantiate the class " + e);
161                 }
162                 RunningApplication.Instance().getChrono().start();
163
164                 RunningApplication.Instance().setName(appliName);
165                 RunningApplication.Instance().setNbTasks(nbTasks);
166                 RunningApplication.Instance().setRunning(true);
167                 RunningApplication.Instance().setNumberOfDisconnections(nbdc);
168                 RunningApplication.Instance().setNumberOfSpawnerDisconnections(nbsdc);
169                 
170                 Instance = this;
171                 // if(tag==0)
172                 broadcastRegister(1);
173                 /*
174                  * else{ int
175                  * x=Register.Instance().getListeOfTasks().getSize()/nbOfDaemonsPerSpawner
176                  * ; int s; if(rank==x)
177                  * s=(reg.getListeOfTasks().getSize()%nbOfDaemonsPerSpawner
178                  * )/nbOfDeamonsPerThread; else
179                  * s=nbOfDaemonsPerSpawner/nbOfDeamonsPerThread;
180                  * 
181                  * int debut=nbOfDaemonsPerSpawnerrank;
182                  * 
183                  * 
184                  * for(int i=0;i<s+1;i++){
185                  * 
186                  * new BroadcastSpawner(i,
187                  * debut,nbOfDaemonsPerSpawner,nbOfDeamonsPerThread).start(); }
188                  * 
189                  * }
190                  */
191                 System.out.println("########################");
192         }
193
194         public synchronized static JaceSpawner Instance() {
195                 return Instance;
196         }
197
198         public int getNbOfDeamonsPerThread() {
199                 return nbOfDeamonsPerThread;
200         }
201
202         public int getNbOfDeamonsPerSpawner() {
203                 return nbOfDaemonsPerSpawner;
204         }
205
206         public void startProcess(Vector<Object> spawnersList) {
207                 this.spawnersList = spawnersList;
208
209                 int is = spawnersList.indexOf((Object) Register.Instance()
210                                 .getSpawnerStub());
211
212                 if (is != -1) {
213                         int nextNeighbour;
214                         if (is == spawnersList.size() - 1)
215                                 nextNeighbour = 0;
216                         else
217                                 nextNeighbour = is + 1;
218                         /*
219                          * while((spawnersList.elementAt(nextNeighbour) instanceof Node))
220                          * try{
221                          * System.out.println("waiting till transform of spawner "+nextNeighbour
222                          * +" is finished, for setServer"); Thread.sleep(20);
223                          * }catch(Exception e1){}
224                          */
225                         HeartBeatSpawner.Instance().setServer(
226                                         (JaceSpawnerInterface) spawnersList.get(nextNeighbour));
227                         HeartBeatSpawner.Instance().setHeartTime(heartTime);
228                         HeartBeatSpawner.Instance().start();
229                         int previousNeighbour;
230                         if (is == 0)
231                                 previousNeighbour = spawnersList.size() - 1;
232                         else
233                                 previousNeighbour = is - 1;
234                         ScanThreadSpawner.Instance().setHeartTime(heartTime);
235                         ScanThreadSpawner.Instance().setServer(
236                                         (JaceSpawnerInterface) spawnersList.get(previousNeighbour));
237                         ScanThreadSpawner.Instance().start();
238
239                         broadcastScanning();
240
241                         new StartScanning().start();
242                 } else {
243                         System.err.println("Cannot find myself in the spawnersList !");
244                 }
245
246         }
247
248         public void setBroadcasting(boolean bool) {
249                 broadcasting = bool;
250         }
251
252         public void initialize() {
253                 // if(protocol.equals("rmi")){
254                 // launch the JaceSpawnerServer
255                 if (System.getSecurityManager() == null) {
256             System.setSecurityManager(new SecurityManager());
257         }
258
259 //              signal = false ;
260                 
261                 exportObject();
262
263                 connectSuperNode();
264
265                 // get a Register on the Super Node
266                 // completed with the required number of Daemons
267                 getRegisterOnSuperNode();
268
269                 createAppli();
270                 createSpawnerNetwork();
271
272                 // ** Tests ** //
273                 new FaultMake().run() ;
274                 // }
275         }
276
277         public void startScanning() {
278                 //int res;
279                 long time = RunningApplication.Instance().getChrono().getValue() / 1000;
280                 System.out.println("Start scanning at time: " + time + "s");
281                 // lancer le chrono qui gere les heart beat
282                 while (RunningApplication.Instance().isRunning() == true) {
283                         // 1 etape : scaner tous les "heartTime" milisecondes si les noeuds
284                         // enregistes sont encore vivants
285                         // res = scanConnectedHosts();
286
287                         // 2 etape : a garder ou pas !!!!! regarder si l'appli est en
288                         // attente de noeud pr lui en attribuer 1 nvx
289                         scanAppliNodes();
290                         try {
291                                 Thread.sleep(heartTime);
292                         } catch (Exception e) {
293                         }
294                 }
295                 // /System.out.println("is running = false");
296                 if (!JaceDaemon.Instance().isRunning())
297                         System.exit( 1 ) ;
298         }
299
300         public synchronized void signalDeadNode(JaceInterface host, int rankOfDead) {
301                 
302
303                         TaskId myTaskId = null;
304                         int nb = 0;
305                         int nbC = 0;
306                         long time = 0;
307                         RunningApplication.Instance().incrementNumberOfDisconnections();
308
309                         time = RunningApplication.Instance().getChrono().getValue() / 1000;
310                         nb = RunningApplication.Instance().getNumberOfDisconnections();
311                         nbC = RunningApplication.Instance().getNumberOfCouilles();
312                         System.out.println("At time = " + time + "s,      NbDisconnection = "
313                                         + nb + ",     NbProblem =  " + nbC);
314                         
315                         if( host == null )
316                                 System.err.println( "SIGNAL DE NODE NULL" ) ;
317
318                         // !!!!!!!!!!!!!!actualiser le ListeTask ds le Register
319                         try {
320                                 myTaskId = Register.Instance().getListeOfTasks()
321                                         .getTaskIdOfHostStub(host);
322                         } catch( Exception e ) {}
323                         
324                         if (myTaskId == null) {
325                                 myTaskId = Register.Instance().getListeOfTasks()
326                                                 .getTaskIdOfRank(rankOfDead);
327                                 if( myTaskId == null )
328                                 {
329                                         System.err.println("Houston we have a serious problem!!");
330                                         return ;
331                                 }
332                                 
333                                 JaceInterface deadStub = myTaskId.getHostStub();
334                                 
335                                 if( deadStub != null )
336                                 {
337                                         try{
338                                                 deadStub.suicide2("Not doing a good work");
339                                         }catch(Exception e){}
340                                 } else {
341                                         System.err.println( "Dead node stub unavailable!" );
342                                 }
343                         }
344
345                         Node noeud = Register.Instance().getNodeOfName( myTaskId.getHostName() ) ;
346
347                         int rankDeaD = myTaskId.getRank();
348
349                         String nomNoeud = "" ;
350                         
351                         if( noeud != null )
352                         {
353                                 nomNoeud = noeud.getName();
354                         }
355
356                         boolean b = false ;
357                         
358                         if( ! nomNoeud.equals( "" ) )
359                         {
360                                 b = Register.Instance().removeNodeOfName( nomNoeud ) ;
361                         } else {
362                                 System.err.println( "Dead node name unknown!!" ) ;
363                         }
364                                 
365                         if( ! b )
366                         {
367                                 b = Register.Instance().removeNode( noeud ) ;
368                         }
369
370                         if( b == true ) 
371                         {
372                                 System.out.println("Removing Node of rank "
373                                                 + rankDeaD + "  : size = "
374                                                 + Register.Instance().getSize());
375                         } else {
376                                 System.err
377                                                 .println("Cannot remove the Node, it doesn't exist anymore: size = "
378                                                                 + Register.Instance().getSize());
379                         }
380
381                         Calendar cal = new GregorianCalendar();
382                         System.out.println("At time=" + cal.get(Calendar.MINUTE) + ":"
383                                         + cal.get(Calendar.SECOND));
384
385                         /**** Sébastien Miquée **/
386                         Node tmpNode = null ;
387                         int retry = 0, retryMax = 4 ;
388                         
389                         while( tmpNode == null )
390                         {
391                                 tmpNode = foundToReplaceThisNode(rankDeaD, nomNoeud, noeud);
392                                 
393                                 if( tmpNode == null )
394                                 {
395                                         try{
396                                                 Thread.sleep( 1000 ) ;
397                                         } catch( Exception e ) {}
398                                         
399                                         retry++ ;
400                                         
401                                         if( retry > retryMax )
402                                         {
403                                                 System.err.println( "Unable to replace the dead node "+nomNoeud ) ;
404                                                 return ;
405                                         }
406                                 }
407                         }
408                         
409                         try {
410                                 updateConcernedNodes(rankDeaD, noeud, tmpNode);
411
412                                 Thread.sleep(500);
413                                 System.out.println("Set scanning on "+tmpNode.getName());
414                                 tmpNode.getStub().setScanning( true ) ;
415                         } catch (Exception e) {
416                                 try {
417                                         Thread.sleep( 2000 ) ;
418                                         
419                                         tmpNode.getStub().setScanning( true ) ;
420                                 } catch (InterruptedException e1) {
421                                 } catch (RemoteException e2) {
422                                         System.err.println( "Unable to setScanning on for the new node: " + e2 ) ;
423                                 }
424                                 System.err.println("Unable to setScanning on for the new node: "
425                                                 + e) ;
426                         }
427
428                         for (int z = 0; z < spawnersList.size(); z++)
429                         {
430                                 if (!((JaceSpawnerInterface) spawnersList.get(z))
431                                                 .equals(Register.Instance().getSpawnerStub()))
432                                 {
433                                         try {
434                                                 ((JaceSpawnerInterface) spawnersList.get(z))
435                                                                 .replaceDeamonBy(noeud, tmpNode, rankDeaD);
436
437                                         } catch (Exception e) {
438                                                 System.err
439                                                                 .println("Unable to broadcast the modifications to all the spawners: "
440                                                                                 + e);
441                                         }
442                                 }
443                         }                                       
444         }
445
446         // verifie si les noeud notes vivant ds le Register.Instance() du SuperNode
447         // le sont encore
448         // retourne 0 si erreur, 1 sinon
449         /*
450          * private synchronized int scanConnectedHosts() { long time = 0; Node host;
451          * Node tmpNode; long workerTime; long currentTime; int rank; int restempo;
452          * int nb = 0; int nbC = 0; boolean changed = false; int index=0; try{
453          * JaceSpawnerInterface spawnerStub=Register.Instance().getSpawnerStub();
454          * if(spawnerStub.getFinished()==true){
455          * System.out.println("nbre de taches="+Register.Instance().getSize());
456          * ListeTask t=Register.Instance().getListeOfTasks();
457          * for(index=z;index<t.getSize();index++){ TaskId recev = null;
458          * System.out.println("deleting Task************"+index);
459          * 
460          * recev = t.get(index); JaceInterface stub=recev.getHostStub();
461          * spawnerStub.killApplication(stub); }
462          * 
463          * 
464          * 
465          * } }catch(Exception e){
466          * System.out.println("w aiiiiiiiiiiiiiirrrr"+e+"  index="+index); z=index;
467          * }
468          * 
469          * if (Register.Instance().getSize() == 0) {
470          * System.out.println("aucun noeuds a scanner");
471          * RunningApplication.Instance().purge(); System.exit(0);
472          * 
473          * }
474          * 
475          * return 1; }
476          */
477
478         // trouver un noeud sur les superNode
479         // pr les requisitionner
480         
481         /*** Sébastien Miquée ***/
482         private synchronized Node foundToReplaceThisNode(int theRank, String nom, Node _n) 
483         {
484                 boolean found = false ;
485                 Node node = null ;
486                 int nbNull = 0 ;
487
488                 while( found == false ) {
489                         try {
490                                 node = null ;
491                                 
492                                 node = centralServer.getNewNode( idAlgo, theRank ) ;
493
494                                 if( node != null && ! node.getIP().isEmpty() && node.getIP() != null
495                                         && ! node.getIP().equals( "" ) )
496                                 {
497                                         found = true ;
498                                 } else {
499                                         System.err.println("Returned node is null!");
500                                         nbNull++ ;
501                                         try {
502                                                 Thread.sleep( 2000 ) ;
503                                         } catch( Exception e ) {}
504                                 }
505                         } catch (Exception e) {
506                                 // trouver un autre superNode et lui demander le noeud a lui
507
508                                 System.err.println("Cannot localize SuperNode ! " + e);
509
510                                 connectSuperNode();
511                         }
512                         
513                         if( Register.Instance().existNode( node ) != -1 ) 
514                         {
515                                 found = false ;
516                         }
517                 }
518                 
519                 
520                 if( node != null ) 
521                 {
522                         System.out.println( "Using Node " + node.getName() + " in order to replace " + nom + "\n" ) ;
523                                         
524                         node.setAliveFlag(true);
525                         node.setAliveTime();
526
527                         // rajouter le noeud ds le Register
528                         node.setAppliName(RunningApplication.Instance().getName());
529
530                         // lui envoyer mon stub pr qu'il commence a me pinguer des
531                         // maintenant
532                         // TODO a mettre ds un thread ????
533
534                         /*
535                          * TaskId
536                          * neighborTask=Register.Instance().getListeOfTasks().getTaskIdOfRank
537                          * ((theRank+1)%Register.Instance().getListeOfTasks().getSize());
538                          * try{ node.getStub().updateHeart(neighborTask.getHostStub()); }
539                          * catch(Exception e) {
540                          * System.out.println("nvx noeud deja plu dispo2"); //node = null; }
541                          */
542                         // TODO verif pourkoi superNode me le redonne
543                         // alors qu'il fait deja du calcul
544                         // int is = Register.Instance().existNode(node.getIP());
545                         int is = Register.Instance().existNode( node ) ;
546                 
547                         if( is != -1 ) 
548                         {
549                                 System.out.println("The Node is already in the register ! I don't add it.");
550                                 System.out.println("Node " + node.getName() + " not added !") ;
551                                 node = null;
552                         } else {
553                                 Register.Instance().addNode(node);
554
555                                 // !!!!!!!!!!!!!!actualiser le ListeTask
556                                 TaskId myTaskId = Register.Instance().getListeOfTasks()
557                                                 .getTaskIdOfRank(theRank);
558                                 myTaskId.setHostIP(node.getIP());
559                                 myTaskId.setHostName(node.getName());
560                                 myTaskId.setHostStub(node.getStub());
561
562                                 int neighborRank;
563                                 if (theRank == 0)
564                                 {
565                                         neighborRank = Register.Instance().getSize() - 1;
566                                 } else {
567                                         neighborRank = theRank - 1;
568                                 }
569                                 
570                                 TaskId neighborTask2 = Register.Instance().getListeOfTasks()
571                                                 .getTaskIdOfRank( neighborRank ) ;
572                                 
573                                 int loop = 0, MAX_LOOP = 1 ; 
574                                 boolean ok = false ;
575                                 JaceInterface jaceStub = null ;
576                                 
577                                 while( !ok && loop < MAX_LOOP )
578                                 {
579                                         try {
580                                                 jaceStub = neighborTask2.getHostStub();
581                                                 jaceStub.updateHeart(node.getStub());
582                                                 
583                                                 ok = true ;
584                                         } catch( Exception e ) {
585                                                 loop++ ;
586                                                 
587                                                 if( loop < MAX_LOOP )
588                                                 {
589                                                         try {
590                                                                 Thread.sleep( loop * 2000 ) ;
591                                                         } catch (InterruptedException e1) {}
592                                                 }
593                                                 
594                                                 if( loop == MAX_LOOP )
595                                                 {
596                                                         System.err.println( "Next node unreachable! " + e ) ;
597                                                 }
598                                         }
599                                 }
600                                 
601                                 if( loop == MAX_LOOP )
602                                 {
603                                         
604                                         try {
605                                                 node.getStub().suicide2( "Not reachable!" ) ;
606                                         } catch (RemoteException e1) {
607                                                 System.err.println( "Unable to suicide the node!\n"+e1 );
608                                                 signalDeadNode( null, neighborTask2.getRank() ) ;
609                                         }
610                                 }
611
612                         }
613
614                 } else {
615                         System.out.println("I didn't receive a new Node !");
616                 }
617                 return node;
618         }
619
620         public void replaceBy(JaceSpawnerInterface oldStub,
621                         JaceSpawnerInterface stub) {
622                 int index = spawnersList.indexOf((Object) oldStub);
623                 if (index != -1)
624                         spawnersList.setElementAt(stub, index);
625                 else
626                         System.err.println("Spawner's stub not foud in spawnersList !");
627         }
628
629         public void getNewSpawner(JaceSpawnerInterface previousSpawner) {
630                 //boolean found = false;
631                 Node node = null;
632                 int index;
633                 JaceSpawnerInterface spawnerStub = null;
634
635                 // while (found == false) {
636                 try {
637                         // TODO : trouver l'erreur !!!
638                         // msg d'erreur :
639                         // "pas localise le super node java.lang.NullPointerException"
640                         if (centralServer == null) {
641                                 System.err.println("Central Server not localized !");
642                         }
643                         node = centralServer.getNewNode( idAlgo, -2 ) ;
644                         RunningApplication.Instance()
645                                         .incrementNumberOfSpawnerDisconnections();
646                         //found = true;
647                 } catch (Exception e) {
648                         // trouver un autre superNode et lui demander le noeud a lui
649                         System.err.println("Super Node not localized !\n " + e);
650                         System.err.println("My IP : " + LocalHost.Instance().getIP());
651                         if (centralServer == null) {
652                                 System.err.println("CentralServer is NULL !");
653                         }
654                         connectSuperNode();
655                 }
656                 // }
657                 if (node != null) {
658                         index = spawnersList.indexOf((Object) previousSpawner);
659                         if (index != -1) {
660                                 System.out.println("Using Node " + node.getName()
661                                                 + "       ("
662                                                 + LocalHost.Instance().resolve(node.getName())
663                                                 + ") to replace a dead spawner\n\n");
664                                 try {
665                                         spawnerStub = node.getStub().transformIntoSpawner(
666                                                         params,
667                                                         appliName,
668                                                         Register.Instance(),
669                                                         nbTasks,
670                                                         centralServer,
671                                                         index,
672                                                         heartTime,
673                                                         1,
674                                                         RunningApplication.Instance()
675                                                                         .getNumberOfDisconnections(),
676                                                         RunningApplication.Instance()
677                                                                         .getNumberOfSpawnerDisconnections(),
678                                                         nbOfDaemonsPerSpawner, nbOfDeamonsPerThread
679                                                         , idAlgo);
680                                         spawnersList.setElementAt(spawnerStub, index);
681                                         
682                                         
683                                         new StartProcessThread(index).start();
684
685                                 } catch (Exception e) {
686                                         System.err.println("Unable to reach the new spawner: " + e);
687                                 }
688                                 for (int j = 0; j < spawnersList.size(); j++)
689                                         try {
690                                                 if (!((JaceSpawnerInterface) spawnersList.get(j))
691                                                                 .equals(Register.Instance().getSpawnerStub())
692                                                                 && !((JaceSpawnerInterface) spawnersList.get(j))
693                                                                                 .equals(spawnerStub)) {
694                                                         System.out
695                                                                         .println("Trying to broadcast to spawner of rank "
696                                                                                         + j);
697
698                                                         ((JaceSpawnerInterface) spawnersList.get(j))
699                                                                         .replaceBy(previousSpawner, spawnerStub);
700                                                 }
701                                         } catch (Exception e) {
702                                                 System.err
703                                                                 .println("Unable to broadcast to spawner of rank: "
704                                                                                 + j + ". Error:" + e);
705                                         }
706                                 ScanThreadSpawner.Instance().setServer(spawnerStub);
707
708                                 int previous;
709                                 if (index == 0)
710                                         previous = spawnersList.size() - 1;
711                                 else
712                                         previous = index - 1;
713                                 try {
714                                         ((JaceSpawnerInterface) spawnersList.get(previous))
715                                                         .updateHeart(spawnerStub);
716                                 } catch (Exception e) {
717                                         System.err
718                                                         .println("Unable to change the server of the heartbeatThread  for the previous node of rank "
719                                                                         + previous + ". error:" + e);
720                                 }
721                         }
722                 } else {
723                         System.err.println("Node is null !");
724                 }
725
726         }
727
728         public void broadcastFinished(boolean bool) {
729                 for (int i = 0; i < spawnersList.size(); i++)
730                         try {
731                                 ((JaceSpawnerInterface) spawnersList.get(i)).setFinished(bool);
732                         } catch (Exception e) {
733                                 System.err
734                                                 .println("Unable to propagate the end of the application :"
735                                                                 + e);
736                         }
737         }
738
739         private synchronized void scanAppliNodes() {
740
741                 //Node node = null;
742                 //ListeTask tskList = null;
743                 //int cptReplaced;
744                 int index = 0;
745                 try {
746                         JaceSpawnerInterface spawnerStub = Register.Instance()
747                                         .getSpawnerStub();
748                         if (spawnerStub.getFinished() == true) {
749                                 System.out.println("Number of tasks ="
750                                                 + Register.Instance().getSize());
751
752                                 int x = Register.Instance().getListeOfTasks().getSize()
753                                                 / nbOfDaemonsPerSpawner;
754                                 int s;
755                                 if (rank == x)
756                                         s = (Register.Instance().getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
757                                                         / nbOfDeamonsPerThread;
758                                 else
759                                         s = nbOfDaemonsPerSpawner / nbOfDeamonsPerThread;
760
761                                 int debut = nbOfDaemonsPerSpawner * rank;
762
763                                 // for(int i=debut;i<nbOfDaemonsPerSpawner*(rank+1) &&
764                                 // i<reg.getSize();i++)
765                                 // System.out.println(((Node)nodes.elementAt(i)).getName());
766
767                                 ListeTask t = Register.Instance().getListeOfTasks();
768                                 ScanThreadSpawner.Instance().kill();
769                                 HeartBeatSpawner.Instance().kill();
770                                 
771                                 for (int i = 0; i < s + 1; i++) {
772
773                                         new KillThread(i, debut, nbOfDaemonsPerSpawner,
774                                                         nbOfDeamonsPerThread, t).start();
775                                 }
776
777                                 Thread.sleep(2000);
778
779                                 long finalTime = RunningApplication.Instance().getChrono()
780                                                 .getValue();
781
782                                 int nbe = RunningApplication.Instance()
783                                                 .getNumberOfDisconnections();
784
785                                 int nbsdc = RunningApplication.Instance()
786                                                 .getNumberOfSpawnerDisconnections();
787                                 
788                                 System.out.println("Application finished successfully !");
789
790                                 System.out.println("TOTAL TIME in s : " + (finalTime / 1000));
791                                 System.out.println("nb of desconnections: " + nbe);
792                                 System.out.println("nb of spawners desconnections: " + nbsdc);
793                                 
794                                 // ** Tests ** //
795                                 String path = "/home/lyon/smiquee/resultats/execTime_"+algo+"_"+test ;
796                                 PrintWriter ecrivain = null ;
797                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
798
799                                 ecrivain.println( "TOTAL TIME in s : " + (finalTime / 1000));
800                                 ecrivain.println( "nb of desconnections: " + nbe);
801                                 ecrivain.println( "nb of spawners desconnections: " + nbsdc);
802                                 ecrivain.println( "DH = "+dh ) ;
803                                 
804                                 ecrivain.flush() ;
805                                 ecrivain.close() ;
806                                 
807                                 
808                                 if (JaceDaemon.Instance().isRunning()) {
809                                         JaceDaemon.Instance().reconnectSuperNode();
810
811                                         RunningApplication.Instance().purge();
812
813                                 } else {
814                                         // purger l'appli
815
816                                         RunningApplication.Instance().purge();
817                                 }
818                                 
819                                 /** Suprresion of the mapping algorithm on the SuperNode **/
820                                 centralServer.removeAlgo( idAlgo, 0 ) ;
821                         }
822                 } catch( Exception e ) {
823                         System.err.println( "Error the application nodes scan!\n " + e ) ;
824                         z = index;
825                 }
826                 /*
827                  * if (Register.Instance().getSize() == 0) {
828                  * System.out.println("aucun noeuds a scanner");
829                  * RunningApplication.Instance().purge(); System.exit(0); return 0;
830                  * 
831                  * } else{ tskList = Register.Instance().getListeOfTasks();
832                  * 
833                  * //si mon appli a besoin d'un noeud //sinon, on fait rien if ((nbTasks
834                  * - Register.Instance().getSize()) > 0) { cptReplaced = 0;
835                  * 
836                  * //TODO demander des paquet de nodes, pas qu'un //on scanne toutes les
837                  * taches de cette appli for (int ind = 0; ind < tskList.getSize();
838                  * ind++) { //si 1 tache a pas de noeud, on trouve 1 remplacant
839                  * 
840                  * //if (tskList.get(ind).getHostIP() == null) { if
841                  * (tskList.get(ind).getHostStub() == null) { rank =
842                  * tskList.get(ind).getRank(); node = foundToReplaceThisNodeTMP(rank);
843                  * if (node != null) { cptReplaced++; }
844                  * 
845                  * } }
846                  * 
847                  * //qd fini de scanner taches, envoyer Register //si remplacement de
848                  * noeud (c a d si Register modifier) if (cptReplaced != 0) {
849                  * broadcastRegister(0); } try { Thread.currentThread().yield(); } catch
850                  * (Exception e) {}
851                  * 
852                  * }// fin if(appli.getNeededNodes() > 0) {
853                  * //System.out.println("SCAN APPLI : taille : " +
854                  * Register.Instance().getSize()); return 1; }
855                  */
856         }
857
858 //      @SuppressWarnings("unused")
859 //      private synchronized Node foundToReplaceThisNodeTMP(int theRank) {
860 //              // int i = 0;
861 //              boolean found = false;
862 //              Node node = null;
863 //              // while (found == false) {
864 //              try {
865 //                      // TODO : trouver l'erreur !!!
866 //                      // msg d'erreur :
867 //                      // "pas localise le super node java.lang.NullPointerException"
868 //                      if (centralServer == null) {
869 //                              System.out.println("centralServer est NUUUUUUUUULL");
870 //                      }
871 //                      node = centralServer.getNewNode(LocalHost.Instance().getIP());
872 //
873 //                      found = true;
874 //              } catch (Exception e) {
875 //                      // trouver un autre superNode et lui demander le noeud a lui
876 //                      System.out.println("TMP   pas localise le super node " + e);
877 //                      System.out.println("TMP   pas localise le super node " + e);
878 //                      System.out.println("TMP   pas localise le super node " + e);
879 //                      System.out.println("TMP   pas localise le super node " + e);
880 //                      System.out.println("TMP   pas localise le super node " + e);
881 //                      System.out.println("TMP   pas localise le super node " + e);
882 //                      System.out.println("TMP   pas localise le super node " + e);
883 //                      System.out.println("mon IP : " + LocalHost.Instance().getIP());
884 //                      if (centralServer == null) {
885 //                              System.out.println("centralServer : NULL");
886 //                      }
887 //                      connectSuperNode();
888 //              }
889 //              // }
890 //              if (node != null) {
891 //                      System.out.println("COOOOOOOOOOOOOOOOOOOOOOL : requisition de "
892 //                                      + node.getName() + "  taille avt add: "
893 //                                      + Register.Instance().getSize() + "\n\n");
894 //                      node.setAliveFlag(true);
895 //                      node.setAliveTime();
896 //
897 //                      // rajouter le noeud ds le Register
898 //                      System.out.println("ds Register, manque "
899 //                                      + (nbTasks - Register.Instance().getSize()));
900 //                      node.setAppliName(RunningApplication.Instance().getName());
901 //
902 //                      // lui envoyer mon stub pr qu'il commence a me pinguer des
903 //                      // maintenant
904 //                      // TODO a mettre ds un thread ????
905 //                      try {
906 //                              TaskId neighborTask = Register.Instance().getListeOfTasks()
907 //                                              .getTaskIdOfRank(
908 //                                                              (theRank + 1)
909 //                                                                              % Register.Instance().getListeOfTasks()
910 //                                                                                              .getSize());
911 //                              node.getStub().updateHeart(neighborTask.getHostStub());
912 //                              // node.getStub().updateHeart(this.spawnerRef);
913 //
914 //                              // int is = Register.Instance().existNode(node.getIP());
915 //                              int is = Register.Instance().existNode(node);
916 //                              // TODO verif pourkoi superNode me le redonne
917 //                              // alors qu'il fait deja du calcul
918 //                              if (is != -1) {
919 //                                      System.out.println("j'ajoute pas le noeud, il y est deja");
920 //                                      System.out.println("PAS AJOUTEE   TMP  " + node.getName());
921 //                                      System.out.println("PAS AJOUTEE   TMP  " + node.getName());
922 //                                      System.out.println("PAS AJOUTEE   TMP  " + node.getName());
923 //                                      System.out.println("PAS AJOUTEE   TMP  " + node.getName());
924 //                                      System.out.println("PAS AJOUTEE   TMP  " + node.getName());
925 //                                      node = null;
926 //                              } else {
927 //                                      Register.Instance().addNode(node);
928 //
929 //                                      // !!!!!!!!!!!!!!actualiser le ListeTask
930 //                                      TaskId myTaskId = Register.Instance().getListeOfTasks()
931 //                                                      .getTaskIdOfRank(theRank);
932 //                                      myTaskId.setHostIP(node.getIP());
933 //                                      myTaskId.setHostName(node.getName());
934 //                                      myTaskId.setHostStub(node.getStub());
935 //                                      // Register.Instance().getListeOfTasks().getTaskIdOfRank(theRank).setHostIP(node.getIP());
936 //                                      // Register.Instance().getListeOfTasks().getTaskIdOfRank(theRank).setHostName(node.getName());
937 //                                      // Register.Instance().getListeOfTasks().getTaskIdOfRank(theRank).setHostStub(node.getStub());
938 //                              }
939 //                      } catch (Exception e) {
940 //                              System.out.println("nvx noeud deja plu dispo");
941 //                              node = null;
942 //                      }
943 //              } else {
944 //                      System.out.println("RADINNNNNNNNNNNNNN  TMP ");
945 //              }
946 //              return node;
947 //      }
948
949         private void exportObject() {
950
951                 JaceSpawnerServer spawnerServer = null;
952
953                 System.out.println("Name of local machine is: "
954                                 + LocalHost.Instance().getName());
955                 System.out.println("IP of local machine is: "
956                                 + LocalHost.Instance().getIP());
957                 try {
958                         // launch the JaceSpawnerServer
959                         spawnerServer = new JaceSpawnerServer();
960                         java.rmi.registry.LocateRegistry.createRegistry(spawnerPort);
961                         java.rmi.registry.LocateRegistry.getRegistry(spawnerPort).rebind(
962                                         "JaceSpawnerServer", spawnerServer);
963                         spawnerRef = (JaceSpawnerInterface) Naming.lookup("rmi://"
964                                         + LocalHost.Instance().getIP() + ":" + spawnerPort
965                                         + "/JaceSpawnerServer");
966
967                 } catch (Exception e) {
968                         System.err
969                                         .println("JaceP2P_Error in JaceSpawner.exportObject() when creating the local JaceSpawnerServer "
970                                                         + e);
971 //                      System.err.println("exit ds JaceSpawner.exportObject");
972                         System.exit( 1 ) ;
973                 }
974
975         }
976
977         public void connectSuperNode() {
978                 System.out.println("I'm looking for a super node");
979                 boolean connected = false;
980                 if (!(superNode_IP == null)) {
981                         try {
982                                 centralServer = null ; 
983                                 System.gc() ;
984                                 
985                                 System.out.println("Trying to invoke super node "
986                                                 + superNode_IP);
987                                 centralServer = (JaceSuperNodeInterface) Naming.lookup("rmi://"
988                                                 + superNode_IP + ":" + superNode_port
989                                                 + "/JaceSuperNode");
990                                 System.out.println("Succesfully located " + superNode_IP);
991
992                                 // add stub and IP in LocalHost to store it until super node
993                                 // death
994                                 LocalHost.Instance().setSuperNodeStub(centralServer);
995                                 LocalHost.Instance().setSuperNodeIP(superNode_IP);
996                                 heartTime = centralServer.getSuperNodeBeat();
997                                 timeBeforeKill = NB_HEART_DECONNECT * heartTime;
998                                 connected = true;
999
1000                         } catch (Exception e) {
1001                                 System.err.println("Super Node not accessible, try another one (1/2s)");
1002                                 try {
1003                                         Thread.sleep(500);
1004                                 } catch (Exception e1) {
1005                                 }
1006
1007                         }
1008                 }
1009                 if (connected == false) {
1010                         int i = 0;
1011                         SuperNodeListe.Instance().staticInitialization();
1012                         while (connected == false
1013                                         && i < SuperNodeListe.Instance().getListe().size()) {
1014                                 SuperNodeData d = null;
1015                                 d = SuperNodeListe.Instance().getSuperNodeData(i);
1016
1017                                 superNode_IP = LocalHost.Instance().resolve(d.getIP());
1018                                 superNode_port = d.getPort();
1019                                 // superNode_port = d.getPort();
1020                                 try {
1021                                         System.out.println("Trying to invoke Super Node "
1022                                                         + superNode_IP);
1023                                         centralServer = (JaceSuperNodeInterface) Naming
1024                                                         .lookup("rmi://" + superNode_IP + ":"
1025                                                                         + superNode_port + "/JaceSuperNode");
1026                                         System.out.println("Succesfully located SuperNode "
1027                                                         + superNode_IP);
1028                                         LocalHost.Instance().setSuperNodeStub(centralServer);
1029                                         LocalHost.Instance().setSuperNodeIP(superNode_IP);
1030                                         heartTime = centralServer.getSuperNodeBeat();
1031                                         timeBeforeKill = NB_HEART_DECONNECT * heartTime;
1032
1033                                         connected = true;
1034                                 } catch (Exception e) {
1035                                         System.err
1036                                                         .println("SuperNode "
1037                                                                         + superNode_IP
1038                                                                         + " not accessible, trying to locate another one in 0.5s\n");
1039                                         i++;
1040                                         try {
1041                                                 Thread.sleep(500);
1042                                         } catch (Exception e1) {
1043                                         }
1044
1045                                 }
1046                         }
1047                 }
1048                 if (connected == false) {
1049                         System.err.println("All the Super Nodes in the list are not accessible. I'm unable to connect to the platform !");
1050                         System.exit(1);
1051                 }
1052
1053         }
1054
1055         // get a Register on the SuperNode
1056         // completed with the required number of Daemons
1057         // or gets NULL
1058         public synchronized void getRegisterOnSuperNode() {
1059                 Register registerSpawner = null;
1060                 Node noeud = null;
1061                 boolean recieved = false;
1062
1063                 idAlgo = LocalHost.Instance().getIP() + ":" + LocalHost.Instance().getPort() ;
1064                 
1065                 System.out.println("Trying to get a Register on the SuperNode");
1066                 int nbExtraSpawners = 0;
1067                 if (nbTasks > nbOfDaemonsPerSpawner) {
1068                         nbExtraSpawners = (nbTasks - 1) / nbOfDaemonsPerSpawner;
1069
1070                 }
1071                 while (!recieved) {
1072                         try {
1073                                 registerSpawner = centralServer.getRegisterSpawner(idAlgo, 
1074                                                 nbTasks, (Task) tache, nbTasks
1075                                                 + nbExtraSpawners, algo, paramAlgo);
1076                                 recieved = true;
1077                         } catch (Exception e) {
1078                                 System.err
1079                                                 .println("Unable to recieve a register from superNode "
1080                                                                 + e);
1081                                 connectSuperNode();
1082                         }
1083                 }
1084                 
1085                 // ** Tests ** //
1086                 try {
1087                         Algo al = centralServer.getAlgo( idAlgo ) ;
1088                         
1089                         dh = al.getGrid().getHeterogenityDegre() ;
1090                         System.out.println( "### DH = " + dh ) ;
1091                         
1092                         Utils.writeGraph( al.getGraph(), "/home/lyon/smiquee/resultats/", 
1093                                         "graph_"+algo+"_"+test+".xml" ) ;
1094                         Utils.writeGrid( al.getGrid(), "/home/lyon/smiquee/resultats/", 
1095                                         "grid_"+algo+"_"+test+".xml") ;
1096                         Utils.writeMapping( al.getMapping(), "/home/lyon/smiquee/resultats/",
1097                                         "mapping_"+algo+"_"+test+".xml" ) ;
1098                 } catch (RemoteException e1) {
1099                         System.err.println( "Problème avec Algo dans Spawner !" ) ;
1100                         e1.printStackTrace();
1101                 }
1102                 
1103                 if (registerSpawner.getSize() != (nbTasks + nbExtraSpawners)) {
1104                         System.err.println("I did not recieve enough nodes from superNode!!!! \n killing application !!!!");
1105                         for (int i = 0; i < registerSpawner.getSize(); i++) {
1106                                 try {
1107                                         registerSpawner.getNodeAt(i).getStub().suicide( "Not enough nodes for the application" ) ;
1108
1109                                 } catch (Exception e) {
1110                                         System.err.println("The reserved node was unable to reconnect to the super node");
1111                                 }
1112                         }
1113                         System.exit( 1 ) ;
1114                 }
1115
1116                 spawnersList = new Vector<Object>();
1117                 for (int i = 0; i < nbExtraSpawners && i < registerSpawner.getSize(); i++) {
1118                         spawnersList.add(registerSpawner.getNodeAt(0));
1119
1120                         registerSpawner.removeNodeOfName(registerSpawner.getNodeAt(0).getName());
1121
1122                 }
1123
1124                 registerSpawner.setNbOfTasks(nbTasks);
1125                 registerSpawner.setNumBackupNeighbors(nbSavingNodes);
1126                 /*
1127                  * System.out.println("Trying to connect another SuperNode");
1128                  * connectSuperNode(); try { registerSpawner =
1129                  * centralServer.getRegisterSpawner(LocalHost.Instance().getIP(),
1130                  * nbTasks); } catch(Exception e1) {}
1131                  */
1132
1133                 if (registerSpawner != null) {
1134                         System.out.println("I received the register");
1135                         // registerSpawner.setVersion(registerVersion);
1136                         // registerVersion++;
1137                         Register.Instance().replaceBy(registerSpawner);
1138                         System.out.println("It contains " + Register.Instance().getSize()
1139                                         + " Nodes" + " " + nbExtraSpawners + " ExtraSpawners");
1140
1141                         // set each Node aliveTime value to the Spawner current time
1142                         for (int i = 0; i < Register.Instance().getSize(); i++) {
1143                                 noeud = Register.Instance().getNodeAt(i);
1144                                 noeud.setAliveFlag(true);
1145                                 noeud.setAliveTime();
1146                         }
1147
1148                 } else {
1149                         System.err.println("\n---------------WARNING--------------");
1150                         System.err.println("No Daemon available on the SuperNode dispo, try later, please");
1151                         System.exit(0);
1152                 }
1153         }
1154         
1155         
1156         public class TransformThread extends Thread {
1157                 int i;
1158                 Node n;
1159
1160                 public TransformThread(int i, Node n) {
1161                         this.i = i;
1162                         this.n = n;
1163                 }
1164
1165                 public void run() {
1166
1167                         try {
1168                                 System.out.println("Trying to transform the spawner ("
1169                                                 + n.getName() + ") of rank " + i);
1170                                 spawnersList.setElementAt(n.getStub().transformIntoSpawner(
1171                                                 params, appliName, Register.Instance(), nbTasks,
1172                                                 centralServer, i, heartTime, 0, 0, 0,
1173                                                 nbOfDaemonsPerSpawner, nbOfDeamonsPerThread, idAlgo), i);
1174                         } catch (Exception e) {
1175                                 System.err.println("Error while contacting newly acquired spawner ("
1176                                                 + n.getName() + "): " + e);
1177                                 try {
1178                                         n = centralServer.getNewNode( LocalHost.Instance().getIP(), null ) ;
1179
1180                                         new TransformThread(i, n).start();
1181                                 } catch (Exception e1) {
1182                                         System.err.println("The Super Node is maybe dead: " + e1) ;
1183                                         for (int z = 0; z < Register.Instance().getSize(); z++) {
1184                                                 try {
1185                                                         Register.Instance().getNodeAt(z).getStub().suicide2("Problem") ;
1186
1187                                                 } catch (Exception ez) {
1188                                                         System.err.println("The reserved node was unable to reconnect to the super node: \n"
1189                                                                                         + ez);
1190                                                 }
1191                                         }
1192                                         System.exit(1);
1193                                 }
1194                         }
1195                 }
1196         }
1197         
1198         
1199         public class FaultMake extends Thread {
1200
1201                 public FaultMake(){}
1202
1203                 public void run()
1204                 {
1205                         ListeTask t = null ;
1206                         Random r = null ;
1207                         int next, old = -1 ;
1208                         
1209                         System.out.println( "Starting fault tolerance !" ) ;
1210
1211                         while( ! RunningApplication.Instance().isRunning() )
1212                         {
1213                                 /** Waiting some time the beginning of the computation **/
1214                                 try {
1215                                         sleep( 1000 ) ;
1216                                 } catch (InterruptedException e) {                              
1217                                 }
1218                         }
1219
1220                         while( RunningApplication.Instance().isRunning() )
1221                         {
1222                                 /** Waiting some time ... **/
1223                                 try {
1224                                         sleep( faultTime * 1000 ) ;
1225                                 } catch (InterruptedException e) {                              
1226                                 }
1227
1228                                 /** ... and kill some daemons ! **/
1229                                 t = Register.Instance().getListeOfTasks();
1230                                 r = new Random() ;
1231                                 
1232                                 for( int i = 0 ; i < nbFault ; i++ )
1233                                 {
1234                                         next = r.nextInt( t.getSize() ) ;
1235                 
1236                                         while( next == old )
1237                                         {
1238                                                 next = r.nextInt( t.getSize() ) ;
1239                                         }
1240                                         old = next ;
1241                                         
1242                                         Node noeud = null;
1243                                         try {
1244                                                 TaskId recev = null;
1245                                                 System.out.println("Making fault on Task" + next);
1246                                                 
1247                                                 recev = t.getTaskIdOfRank( next );
1248                                                 
1249                                                 JaceInterface stub = recev.getHostStub();
1250                                                 System.out.println("name = " + recev.getHostName());
1251                                                 noeud = Register.Instance().getNodeOfStub( stub ) ;
1252                                                 
1253                                                 stub.suicide2( "Test fault tolerance" ) ;
1254                                                 
1255                                         } catch (UnmarshalException ue ){
1256                                         } catch (Exception e) {
1257                                                 try {
1258                                                         System.err.println("Error in FaultMake on node: "
1259                                                                         + noeud.getName() + ". " + e);
1260                                                 } catch (Exception e2) {
1261                                                         System.err.println("(Fault)Error in error:" + e2);
1262                                                 }
1263                                         }
1264                                         
1265                                         try {
1266                                                 sleep( 500 ) ;
1267                                         } catch (InterruptedException e) {                              
1268                                         }
1269                                 }
1270                         }
1271                 }
1272         }
1273         
1274         
1275
1276         public class StartProcessThread extends Thread {
1277                 int i;
1278
1279                 public StartProcessThread(int i) {
1280                         this.i = i;
1281                 }
1282
1283                 public void run() {
1284                         try {
1285
1286                                 /*
1287                                  * while((spawnersList.elementAt(i) instanceof Node)) try{
1288                                  * System.out.println("waiting till transform of spawner "+i+
1289                                  * " is finished"); Thread.sleep(20); }catch(Exception e1){}
1290                                  */
1291                                 // System.out.println("start process on spawner of rank "+i);
1292                                 JaceSpawnerInterface spawnerStub = (JaceSpawnerInterface) spawnersList.get(i);
1293                                 spawnerStub.startProcess(spawnersList);
1294                         } catch (Exception e) {
1295                                 e.printStackTrace(System.out);
1296                                 System.err.println("Unable to start the process on the spawner of rank "
1297                                                                 + i + ".error: " + e);
1298                         }
1299                 }
1300         }
1301
1302         public void createSpawnerNetwork() {
1303                 Node n;
1304                 int i;
1305                 for (i = 0; i < spawnersList.size(); i++) {
1306                         n = (Node) spawnersList.elementAt(i);
1307
1308                         // Register.Instance().getListeOfTasks().viewAll();
1309                         // spawnersList.setElementAt(n.getStub().transformIntoSpawner(
1310                         // params, appliName, Register.Instance(),nbTasks, centralServer,i,
1311                         // heartTime,0,0),i);
1312                         new TransformThread(i, n).start();
1313
1314                 }
1315                 // broadcast the Register.Instance() to all the JaceServer
1316                 // in order to start each task on the Daemons
1317
1318                 spawnersList.add(Register.Instance().getSpawnerStub());
1319                 System.out.println("    rank="+rank+" spawnersList.size()=" + spawnersList.size());
1320                 rank = spawnersList.size() - 1;
1321                 
1322                 broadcastRegister(1);
1323                 
1324                 for (int j = 0; j < spawnersList.size(); j++) {
1325                         System.out.println("waiting till transform of spawner " + j
1326                                         + " is finished");
1327                         while ((spawnersList.elementAt(j) instanceof Node))
1328                         {
1329                                 try {
1330
1331                                         Thread.sleep(20);
1332                                 } catch (Exception e) {
1333                                 }
1334                         }
1335                 }
1336                 
1337 //              for (int k = 0; k < spawnersList.size(); k++)
1338 //              {
1339 //                      try {
1340 //                              ((JaceSpawnerInterface) spawnersList.get( k )).setIdAlgo( idAlgo ) ;
1341 //                      } catch (Exception e) {
1342 //                              System.err.println("Unable to propagate the mapping algorithm identifier:" + e) ;
1343 //                      }
1344 //              }
1345
1346                 System.out.println("End Transformation of all spawners. Beginning the computing processes");
1347                 
1348                 for (i = 0; i < spawnersList.size(); i++) {
1349
1350                         // while(!(spawnersList.elementAt(i) instanceof
1351                         // JaceSpawnerInterface))
1352
1353                         new StartProcessThread(i).start();
1354
1355                 }
1356                 System.out.println("End create Spawner Network!!!!!!!!!");
1357         }
1358
1359         public JaceSpawnerInterface getSpawnerResponsibleOn(int rank) {
1360                 int id = rank / nbOfDaemonsPerSpawner;
1361                 return (JaceSpawnerInterface) spawnersList.get(id);
1362         }
1363
1364         public void createAppli() {
1365                 int count = 0;
1366                 int i = 0;
1367                 String nodeName;
1368                 String nodeIP;
1369                 ListeTask tsk = new ListeTask();
1370                 Node tmpNode;
1371                 JaceInterface nodeStub = null;
1372                 TaskId myTask = null;
1373
1374                 System.out.println("Application launched, starting the chronometer");
1375                 RunningApplication.Instance().getChrono().start();
1376
1377                 RunningApplication.Instance().setName(appliName);
1378                 RunningApplication.Instance().setNbTasks(nbTasks);
1379                 // RunningApplication.Instance().setRegister(Register.Instance());
1380
1381                 Register.Instance().setParams(params);
1382                 Register.Instance().setAppliName(appliName);
1383                 Register.Instance().setSpawnerStub(this.spawnerRef);
1384
1385                 // assign a TaskId to each Node of the Register
1386                 // and insert the TaskId in tke ListTask
1387                 while (i < Register.Instance().getSize() && count < nbTasks) {
1388                         tmpNode = Register.Instance().getNodeAt(i);
1389                         if (tmpNode.getAliveFlag() == true) {
1390                                 tmpNode.setAppliName(appliName);
1391                                 nodeStub = tmpNode.getStub();
1392                                 nodeName = tmpNode.getName();
1393                                 nodeIP = tmpNode.getIP();
1394
1395                                 myTask = new TaskId(appliName, count, nodeStub);
1396                                 myTask.setHostIP(nodeIP);
1397                                 myTask.setHostName(nodeName);
1398
1399                                 tsk.addTask(myTask);
1400                                 count++;
1401                         }
1402                         i++;
1403                 }
1404
1405                 // if not enough Nodes in the Register,
1406                 // insert not assigned TaskId in the ListTask
1407                 if (count < nbTasks) {
1408                         for (int j = count; j < nbTasks; j++) {
1409                                 tsk.addTask(new TaskId(appliName, j, null));
1410                         }
1411                         System.out.println("in Register, misses "
1412                                         + (nbTasks - Register.Instance().getSize()) + " nodes");
1413                 }
1414
1415                 // insert the ListeTask in the Register of the appli
1416                 Register.Instance().setListeOfTasks(tsk);
1417                 // Register.Instance().getListeOfTasks().viewAll();
1418                 RunningApplication.Instance().setRunning(true);
1419                 System.out.println("fin create appli");
1420         }
1421
1422         public class BroadcastSpawner extends Thread {
1423                 int debut;
1424                 int i;
1425                 int nbOfDaemonsPerThread, nbOfDeamonsPerSpawner;
1426
1427                 public BroadcastSpawner(int i, int debut, int nbOfDeamonsPerSpawner,
1428                                 int nbOfDaemonsPerThread) {
1429                         this.debut = debut;
1430                         this.i = i;
1431                         this.nbOfDaemonsPerThread = nbOfDaemonsPerThread;
1432                         this.nbOfDeamonsPerSpawner = nbOfDeamonsPerSpawner;
1433                 }
1434
1435                 public void run() {
1436
1437                         for (int index = debut + i * nbOfDaemonsPerThread; index < debut
1438                                         + i * nbOfDeamonsPerThread + nbOfDeamonsPerThread
1439                                         && index < debut + nbOfDeamonsPerSpawner
1440                                         && index < Register.Instance().getListeOfTasks().getSize(); index++) {
1441                                 try {
1442                                         Register.Instance().getNodeAt(index).getStub().setSpawner(
1443                                                         Register.Instance().getSpawnerStub());
1444                                 } catch (Exception e) {
1445                                         System.out.println("can't change spawner stub on node: "
1446                                                         + Register.Instance().getNodeAt(i).getName()
1447                                                         + ". error: " + e);
1448                                 }
1449                         }
1450                 }
1451         }
1452
1453         public class KillThread extends Thread {
1454                 int debut;
1455                 int i;
1456                 int nbOfDaemonsPerThread, nbOfDeamonsPerSpawner;
1457                 ListeTask t;
1458
1459                 public KillThread(int i, int debut, int nbOfDeamonsPerSpawner,
1460                                 int nbOfDaemonsPerThread, ListeTask t) {
1461                         this.debut = debut;
1462                         this.i = i;
1463                         this.nbOfDaemonsPerThread = nbOfDaemonsPerThread;
1464                         this.nbOfDeamonsPerSpawner = nbOfDeamonsPerSpawner;
1465                         this.t = t;
1466                 }
1467
1468                 public void run() {
1469
1470                         // t.viewAll();
1471                         for (int index = debut + i * nbOfDaemonsPerThread; index < debut
1472                                         + i * nbOfDeamonsPerThread + nbOfDeamonsPerThread
1473                                         && index < debut + nbOfDeamonsPerSpawner
1474                                         && index < t.getSize(); index++) {
1475                                 Node noeud = null;
1476                                 try {
1477                                         TaskId recev = null;
1478                                         System.out.println("deleting Task" + index);
1479
1480                                         recev = t.getTaskIdOfRank(index);
1481
1482                                         JaceInterface stub = recev.getHostStub();
1483                                         System.out.println("name=" + recev.getHostName());
1484                                         noeud = Register.Instance().getNodeOfStub(stub);
1485                                         noeud.setAppliName(null);
1486                                         new ReconnectThread(stub, noeud.getName()).start();
1487                                         Register.Instance().removeNode(noeud);
1488                                         // LocalHost.Instance().getSpawnerStub().killApplication(stub);
1489
1490                                 } catch (Exception e) {
1491                                         try {
1492                                                 System.err.println("error in killThread on node "
1493                                                                 + noeud.getName() + ". " + e);
1494                                         } catch (Exception e2) {
1495                                                 System.err.println("error in error :" + e2);
1496                                         }
1497                                 }
1498                         }
1499                 }
1500
1501                 class ReconnectThread extends Thread {
1502                         JaceInterface stub = null;
1503                         String name;
1504
1505                         public ReconnectThread(JaceInterface s, String name) {
1506                                 stub = s;
1507                                 this.name = name;
1508                         }
1509
1510                         public void run() {
1511                                 try {
1512                                         // System.out.println("reconnexion SuperNode");
1513                                         // Register.Instance().getNode(workerIP).getWorkerStub().reconnectSuperNode();
1514
1515                                         // stub.reconnectSuperNode();
1516                                         stub.suicide("End of application");
1517                                 } catch (Exception e) {
1518                                         System.err.println("Cannot kill node " + name);
1519                                 }
1520                                 yield();
1521                         }
1522                 }
1523
1524         }
1525
1526         // faire une copie du Register et l'envoyer aux noeuds qui le compose
1527         // car si il est modif en meme tmp, on envoi pas un truc coherent
1528         private synchronized void broadcastRegister(int requete) {
1529                 // Register reg = Register.Instance().clone();
1530                 Register reg = Register.Instance();
1531
1532                 try {
1533                         System.out.println("name of spawner: "
1534                                         + Register.Instance().getSpawnerStub().getName());
1535                         // launch 1 thread to send the Register to all the nodes
1536                         while (broadcasting == true)
1537                                 Thread.sleep(5);
1538                         broadcasting = true;
1539 //                      Register.Instance().setSpawnerStub(
1540 //                                      Register.Instance().getSpawnerStub());
1541                         int x = reg.getListeOfTasks().getSize() / nbOfDaemonsPerSpawner;
1542                         int s;
1543                         if (rank == x)
1544                                 if ((reg.getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
1545                                                 % nbOfDeamonsPerThread == 0)
1546                                         s = (reg.getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
1547                                                         / nbOfDeamonsPerThread;
1548                                 else
1549                                         s = (reg.getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
1550                                                         / nbOfDeamonsPerThread + 1;
1551                         else if ((nbOfDaemonsPerSpawner % nbOfDeamonsPerThread) == 0)
1552                                 s = nbOfDaemonsPerSpawner / nbOfDeamonsPerThread;
1553                         else
1554                                 s = nbOfDaemonsPerSpawner / nbOfDeamonsPerThread + 1;
1555                         int debut = nbOfDaemonsPerSpawner * rank;
1556                         System.out.println("rank=" + rank + " debut=" + debut + " s=" + s
1557                                         + " nbOfDaemonsPerSpawner=" + nbOfDaemonsPerSpawner
1558                                         + " nbOfDeamonsPerThread=" + nbOfDeamonsPerThread + " x="
1559                                         + x);
1560                         for (int i = 0; i < s; i++)
1561                                 new UpdateRegisterThread(tache, reg, requete, i, debut).start();
1562                         /*
1563                          * This thread : -updates the goal of the Node beats if necessary
1564                          * (stub.updateHeart) -updates the Register on each Node
1565                          * (stub.updateRegister)
1566                          */
1567                         JaceSpawner.Instance().setBroadcasting(false);
1568                         try {
1569                                 Thread.sleep(10);
1570                         } catch (Exception e) {
1571                         }
1572
1573                 } catch (Exception e) {
1574                         System.out
1575                                         .println("\n1 node has died during JaceSpawner.broadcastRegister()");
1576                 }
1577         }
1578
1579         private synchronized void broadcastScanning() {
1580                 Register reg = Register.Instance();
1581                 while (broadcasting == true)
1582                         try {
1583                                 Thread.sleep(500);
1584                         } catch (Exception e) {
1585                         }
1586                 // Register.Instance().viewAll();
1587                 Vector<?> nodes = (Vector<?>) Register.Instance().getListOfNodes().clone();
1588                 int x = reg.getListeOfTasks().getSize() / nbOfDaemonsPerSpawner;
1589                 int s;
1590                 if (rank == x)
1591                         s = (reg.getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
1592                                         / nbOfDeamonsPerThread;
1593                 else
1594                         s = nbOfDaemonsPerSpawner / nbOfDeamonsPerThread;
1595
1596                 int debut = nbOfDaemonsPerSpawner * rank;
1597
1598                 // for(int i=debut;i<nbOfDaemonsPerSpawner*(rank+1) &&
1599                 // i<reg.getSize();i++)
1600                 // System.out.println(((Node)nodes.elementAt(i)).getName());
1601
1602                 for (int i = 0; i < s + 1; i++) {
1603
1604                         new StartScanThread(i, nodes, debut).start();
1605                 }
1606
1607         }
1608
1609         @SuppressWarnings("unchecked")
1610         public synchronized Register getRegister( int rank ) {
1611
1612                 ListeTask listOfTasks = Register.Instance().getListeOfTasks();
1613                 Vector dependencies = getDependencies(rank, listOfTasks.getSize());
1614                 Register g = new Register();
1615                 ListeTask newListOfTasks = new ListeTask();
1616                 
1617                 g.setAppliName(Register.Instance().getAppliName());
1618                 g.setParams(Register.Instance().getParams());
1619                 g.setSpawnerStub(Register.Instance().getSpawnerStub());
1620                 g.setNbOfTasks(Register.Instance().getNbOfTasks());
1621                 // g.setVersion(reg.getVersion());
1622                 
1623                 for (int j = 0; j < dependencies.size(); j++) {
1624                         TaskId id = listOfTasks.getTaskIdOfRank(((Integer) dependencies
1625                                         .elementAt(j)).intValue());
1626                         newListOfTasks.addTask(id);
1627                         if (id.getHostStub() != null) {
1628                                 Node noeud = Register.Instance()
1629                                                 .getNodeOfName( id.getHostName() ) ;// (id.getHostStub());
1630                                 if( noeud != null )
1631                                         g.addNode( noeud ) ;
1632                                 else
1633                                         System.err.println( "PAS BON DU TOUT" ) ;
1634                         }
1635                 }
1636                 g.setListeOfTasks(newListOfTasks);
1637                 return g;
1638         }
1639
1640         @SuppressWarnings("unchecked")
1641         private void updateConcernedNodes(int rank, Node oldNode, Node node) {
1642                 ListeTask listOfTasks = Register.Instance().getListeOfTasks();
1643                 Vector dependencies = getDependencies(rank, listOfTasks.getSize());
1644                 System.out.println("List of concerned neighbors of task " + rank+": ");
1645                 for (int z = 0; z < dependencies.size(); z++)
1646                         System.out.print(((Integer) dependencies.elementAt(z)).intValue()
1647                                         + " ");
1648                 System.out.println();
1649                 // Register.Instance().setVersion(registerVersion);
1650                 // registerVersion++;
1651 //              Register.Instance()
1652 //                              .setSpawnerStub(Register.Instance().getSpawnerStub());
1653                 int s;
1654                 if ((dependencies.size() % nbOfDeamonsPerThread) == 0)
1655                         s = dependencies.size() / nbOfDeamonsPerThread;
1656                 else
1657                         s = dependencies.size() / nbOfDeamonsPerThread + 1;
1658                 Register reg = Register.Instance();
1659
1660                 for (int j = 0; j < s; j++) {
1661                         new UpdateRegisterConcernedThread(dependencies, reg, j, rank,
1662                                         oldNode, node).start();
1663                 }
1664         }
1665
1666         private Vector<Integer> getDependencies(int id, int jaceSize) {
1667                 // get computing dependencies
1668                 Vector<Integer> neighbors = new Vector<Integer>();
1669                 int[] dep = tache.getDependencies(id);
1670                 for (int z = 0; z < taille(dep); z++)
1671                         neighbors.add(dep[z]);
1672                 // System.out.println("la liste des voisins de calcul de: "+id+" concerne");
1673                 // for(int z=0;z<neighbors.size();z++)
1674                 // System.out.print(((Integer)neighbors.elementAt(z)).intValue()+" ");
1675                 // System.out.println();
1676
1677                 // get convergence neighbors
1678                 int d = 0;
1679                 while (Math.pow(2, d) < jaceSize) {
1680                         if (id < Math.pow(2, d) && ((id + Math.pow(2, d)) < jaceSize))
1681                                 if (!neighbors.contains((Object) ((int) (id + Math.pow(2, d)))))
1682                                         neighbors.add((int) (id + Math.pow(2, d)));
1683                         if (id < Math.pow(2, d + 1) && id >= Math.pow(2, d))
1684                                 if (!neighbors.contains((Object) ((int) (id - Math.pow(2, d)))))
1685                                         neighbors.add((int) (id - Math.pow(2, d)));
1686                         d++;
1687                 }
1688
1689                 // get backup neighbors
1690                 int nb = Register.Instance().getNumBackupNeighbors();
1691                 int rankOfBackTask;
1692                 int tmp;
1693                 for (int j = 1; j <= nb; j++) {
1694                         // ------------ 1 - for backups "j + n" (to the right of j)
1695                         rankOfBackTask = (id + j) % jaceSize;
1696                         if (!neighbors.contains((Object) rankOfBackTask))
1697                                 neighbors.add(rankOfBackTask);
1698
1699                         // ------------ 2 - for backups "j - n" (to the left of j)
1700                         tmp = id - j;
1701                         if (tmp >= 0) {
1702                                 rankOfBackTask = tmp % jaceSize;
1703                         } else {
1704                                 rankOfBackTask = jaceSize - (Math.abs(tmp) % jaceSize);
1705                         }
1706                         if (!neighbors.contains((Object) rankOfBackTask))
1707                                 neighbors.add(rankOfBackTask);
1708                 }
1709                 // adds itself
1710                 neighbors.add(id);
1711                 return neighbors;
1712
1713         }
1714
1715         public static int taille(int[] vect) {
1716                 int taille = 0;
1717                 int x = 0;
1718                 while (x < vect.length && vect[x] >= 0) {
1719                         taille++;
1720                         x++;
1721                 }
1722                 return x;
1723         }
1724
1725         class StartScanning extends Thread {
1726
1727                 public StartScanning() {
1728                 }
1729
1730                 public void run() {
1731                         startScanning();
1732                 }
1733         }
1734
1735 }
1736
1737 class StartScanThread extends Thread {
1738         int i, debut;
1739         Vector<?> nodes;
1740         int nbOfDeamonsPerThread, nbOfDeamonsPerSpawner;
1741
1742         StartScanThread(int i, Vector<?> nodes, int debut) {
1743                 this.i = i;
1744                 this.nodes = nodes;
1745                 this.debut = debut;
1746                 nbOfDeamonsPerThread = JaceSpawner.Instance().getNbOfDeamonsPerThread();
1747                 nbOfDeamonsPerSpawner = JaceSpawner.Instance()
1748                                 .getNbOfDeamonsPerSpawner();
1749         }
1750
1751         public void run() {
1752                 int index;
1753                 for (index = debut + i * nbOfDeamonsPerThread; index < debut + i
1754                                 * nbOfDeamonsPerThread + nbOfDeamonsPerThread
1755                                 && index < debut + nbOfDeamonsPerSpawner
1756                                 && index < nodes.size(); index++) {
1757
1758                         Node node = (Node) nodes.elementAt(index);
1759                         JaceInterface stub = node.getStub();
1760                         String name = node.getName();
1761                         try {
1762
1763                                 stub.setScanning(true);
1764                                 // System.out.println("modify scanning to "+name);
1765
1766                         } catch (Exception e) {
1767                                 System.err.println("Unable to modify scanning to " + name + ":"
1768                                                 + e);
1769                         }
1770                 }
1771                 // for(int x=0;x<nodes.size();x++)
1772                 // System.out.println(((Node)nodes.elementAt(x)).getName());
1773                 // System.out.println("nbre total: "+(index-1));
1774         }
1775 }