Logo AND Algorithmique Numérique Distribuée

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