Logo AND Algorithmique Numérique Distribuée

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