Logo AND Algorithmique Numérique Distribuée

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