Logo AND Algorithmique Numérique Distribuée

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