Logo AND Algorithmique Numérique Distribuée

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