Logo AND Algorithmique Numérique Distribuée

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