+package jaceP2P;
+
+import java.rmi.Naming;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Vector;
+
+public class JaceSpawner {
+ private Class<?> c;
+ private Loader load;
+ private Task tache = null;
+ public static JaceSpawner Instance;
+ private static String superNode_IP = null;
+ private int superNode_port = 1098;
+ private static int spawnerPort = 1099;
+ private static JaceSuperNodeInterface centralServer = null;
+ private JaceSpawnerInterface spawnerRef = null;
+ private int nbTasks;
+ private String appliName;
+ private String[] params = null;
+ @SuppressWarnings("unused")
+ private String protocol;
+ // private int registerVersion=0;
+ final int NB_HEART_DECONNECT = 3;
+ private int heartTime; // frequency of heartBeat
+ @SuppressWarnings("unused")
+ private int timeBeforeKill; // wait 3 non-response of heartBeat before
+ // considering de Daemon as dead
+ private boolean broadcasting = false;
+ @SuppressWarnings("unused")
+ private int z = 0;
+ private static int nbOfDaemonsPerSpawner;
+ private static int nbOfDeamonsPerThread;
+ private Vector<Object> spawnersList;
+ private int rank;
+ private int nbSavingNodes;
+
+ // Variables for Mapping
+ private int algo;
+ private double paramAlgo ;
+
+ public JaceSpawner(String superNode, int port, String comProtocol,
+ String[] args, int nbDaemonPerSpawner, int nbDaemonPerThread,
+ int nbSavingNodes, int _algo, double _paramAlgo) {
+ // superNode_IP = LocalHost.Instance().resolve(superNode);
+ algo = _algo;
+ paramAlgo = _paramAlgo ;
+
+ superNode_IP = superNode;
+ spawnerPort = port;
+ protocol = comProtocol;
+ nbOfDaemonsPerSpawner = nbDaemonPerSpawner;
+ nbOfDeamonsPerThread = nbDaemonPerThread;
+ this.nbSavingNodes = nbSavingNodes;
+ if (args.length < 2)
+ {
+ // if less than 2 params (nb of tasks and name of the appli), error
+ System.err.println( "Parameters error !" ) ;
+ System.exit( 1 ) ;
+ } else {
+ try {
+ nbTasks = new Integer(args[0]).intValue(); // nb of tasks
+ // launched by the
+ // spawner
+ } catch (Exception e) {
+ System.err.println("Number format exception :" + e ) ;
+ System.exit( 1 ) ;
+ }
+ appliName = args[1]; // name of the class to launch
+ if (args.length > 2) { // get the eventual param of the appli
+ params = new String[args.length - 2];
+ for (int i = 0; i < params.length; i++) {
+ params[i] = args[2 + i];
+ }
+ }
+ load = new Loader();
+ c = load.load(appliName);
+ try {
+ tache = ((Task) c.newInstance());
+ tache.setParam(params);
+ tache.setJaceSize(nbTasks);
+
+ // ****************//
+ //tache.printDep();
+ } catch (Exception e) {
+ System.err.println( "Unable to instantiate the class " + e ) ;
+ System.exit( 1 ) ;
+ }
+ }
+
+ Instance = this;
+ }
+
+
+
+ public JaceSpawner(String[] params, String appliName, Register reg,
+ int nbTasks, JaceSuperNodeInterface snodeStub, int rank,
+ int heartTime, int tag, int nbdc, int nbsdc,
+ int nbDaemonPerSpawner, int nbDaemonPerThread) {
+ try {
+ nbOfDaemonsPerSpawner = nbDaemonPerSpawner;
+ nbOfDeamonsPerThread = nbDaemonPerThread;
+ if (params.length != 0) {
+ this.params = new String[params.length];
+ for (int i = 0; i < params.length; i++)
+ this.params[i] = params[i];
+ } else {
+ params = null;
+ System.err.println( "There is no parameter !" ) ;
+ }
+ } catch (Exception e) {
+ System.err.println("Error in copying the parameters: " + e ) ;
+ }
+ // System.out.println("xxxxxxxxxxxxxxx reg size="+reg.getSize()+" xxxxxxxxxxxxxx");
+ this.appliName = appliName;
+
+ this.nbTasks = nbTasks;
+ this.heartTime = heartTime;
+ LocalHost.Instance().setSuperNodeStub(snodeStub);
+ centralServer = snodeStub;
+ exportObject();
+ Register.Instance().replaceBy(reg);
+ Register.Instance().setSpawnerStub(this.spawnerRef);
+ Register.Instance().getListeOfTasks().viewAll();
+
+ this.rank = rank;
+ load = new Loader();
+ c = load.load(appliName);
+ try {
+ tache = ((Task) c.newInstance());
+ tache.setParam(params);
+ tache.setJaceSize(nbTasks);
+ // ****************//
+ tache.printDep();
+ } catch (Exception e) {
+ System.err.println("Unable to instantiate the class " + e);
+ }
+ RunningApplication.Instance().getChrono().start();
+
+ RunningApplication.Instance().setName(appliName);
+ RunningApplication.Instance().setNbTasks(nbTasks);
+ RunningApplication.Instance().setRunning(true);
+ RunningApplication.Instance().setNumberOfDisconnections(nbdc);
+ RunningApplication.Instance().setNumberOfSpawnerDisconnections(nbsdc);
+ // System.out.println("+++++++++++++++++++++++++");
+ Instance = this;
+ // if(tag==0)
+ broadcastRegister(1);
+ /*
+ * else{ int
+ * x=Register.Instance().getListeOfTasks().getSize()/nbOfDaemonsPerSpawner
+ * ; int s; if(rank==x)
+ * s=(reg.getListeOfTasks().getSize()%nbOfDaemonsPerSpawner
+ * )/nbOfDeamonsPerThread; else
+ * s=nbOfDaemonsPerSpawner/nbOfDeamonsPerThread;
+ *
+ * int debut=nbOfDaemonsPerSpawnerrank;
+ *
+ *
+ * for(int i=0;i<s+1;i++){
+ *
+ * new BroadcastSpawner(i,
+ * debut,nbOfDaemonsPerSpawner,nbOfDeamonsPerThread).start(); }
+ *
+ * }
+ */
+ System.out.println("########################");
+ }
+
+ public synchronized static JaceSpawner Instance() {
+ return Instance;
+ }
+
+ public int getNbOfDeamonsPerThread() {
+ return nbOfDeamonsPerThread;
+ }
+
+ public int getNbOfDeamonsPerSpawner() {
+ return nbOfDaemonsPerSpawner;
+ }
+
+ public void startProcess(Vector<Object> spawnersList) {
+ this.spawnersList = spawnersList;
+ int is = spawnersList.indexOf((Object) Register.Instance()
+ .getSpawnerStub());
+ if (is != -1) {
+ int nextNeighbour;
+ if (is == spawnersList.size() - 1)
+ nextNeighbour = 0;
+ else
+ nextNeighbour = is + 1;
+ /*
+ * while((spawnersList.elementAt(nextNeighbour) instanceof Node))
+ * try{
+ * System.out.println("waiting till transform of spawner "+nextNeighbour
+ * +" is finished, for setServer"); Thread.sleep(20);
+ * }catch(Exception e1){}
+ */
+ HeartBeatSpawner.Instance().setServer(
+ (JaceSpawnerInterface) spawnersList.get(nextNeighbour));
+ HeartBeatSpawner.Instance().setHeartTime(heartTime);
+ HeartBeatSpawner.Instance().start();
+ int previousNeighbour;
+ if (is == 0)
+ previousNeighbour = spawnersList.size() - 1;
+ else
+ previousNeighbour = is - 1;
+ ScanThreadSpawner.Instance().setHeartTime(heartTime);
+ ScanThreadSpawner.Instance().setServer(
+ (JaceSpawnerInterface) spawnersList.get(previousNeighbour));
+ ScanThreadSpawner.Instance().start();
+
+ broadcastScanning();
+// System.out.println("apres broadcastScanning");
+ new StartScanning().start();
+ } else {
+ System.err.println("Cannot find myself in the spawnersList !");
+ }
+
+ }
+
+ public void setBroadcasting(boolean bool) {
+ broadcasting = bool;
+ }
+
+ public void initialize() {
+ // if(protocol.equals("rmi")){
+ // launch the JaceSpawnerServer
+ exportObject();
+
+ connectSuperNode();
+
+ // get a Register on the Super Node
+ // completed with the required number of Daemons
+ getRegisterOnSuperNode();
+
+ createAppli();
+ createSpawnerNetwork();
+
+ // }
+ }
+
+ public void startScanning() {
+ //int res;
+ long time = RunningApplication.Instance().getChrono().getValue() / 1000;
+ System.out.println("Start scanning at time: " + time + "s");
+ // lancer le chrono qui gere les heart beat
+ while (RunningApplication.Instance().isRunning() == true) {
+ // 1 etape : scaner tous les "heartTime" milisecondes si les noeuds
+ // enregistes sont encore vivants
+ // res = scanConnectedHosts();
+
+ // 2 etape : a garder ou pas !!!!! regarder si l'appli est en
+ // attente de noeud pr lui en attribuer 1 nvx
+ scanAppliNodes();
+ try {
+ Thread.sleep(heartTime);
+ } catch (Exception e) {
+ }
+ }
+ // /System.out.println("is running = false");
+ if (!JaceDaemon.Instance().isRunning())
+ System.exit(0);
+ }
+
+ public synchronized void signalDeadNode(JaceInterface host, int rankOfDead) {
+ try {
+ TaskId myTaskId = null;
+ int nb = 0;
+ int nbC = 0;
+ long time = 0;
+ RunningApplication.Instance().incrementNumberOfDisconnections();
+
+ time = RunningApplication.Instance().getChrono().getValue() / 1000;
+ nb = RunningApplication.Instance().getNumberOfDisconnections();
+ nbC = RunningApplication.Instance().getNumberOfCouilles();
+ System.out.println("At time = " + time + "s, NbDisconnection = "
+ + nb + ", NbProblem = " + nbC);
+
+ // !!!!!!!!!!!!!!actualiser le ListeTask ds le Register
+ myTaskId = Register.Instance().getListeOfTasks()
+ .getTaskIdOfHostStub(host);
+ if (myTaskId == null) {
+ Register.Instance.getListeOfTasks().viewAll();
+ myTaskId = Register.Instance().getListeOfTasks()
+ .getTaskIdOfRank(rankOfDead);
+ JaceInterface deadStub = myTaskId.getHostStub();
+ deadStub.suicide("Not doing a good work");
+ }
+ myTaskId.setHostIP(null);
+ myTaskId.setHostName(null);
+ Node noeud = Register.Instance().getNodeOfStub(
+ myTaskId.getHostStub());
+ myTaskId.setHostStub(null);
+ int rankDeaD = myTaskId.getRank();
+
+ String nomNoeud = noeud.getName();
+ // Register.Instance().removeNodeAt(i);
+ // Register.Instance().removeNode(host.getIP());
+ // System.out.println("fait le remove : taille = " +
+ // Register.Instance().getSize());
+
+ boolean b = Register.Instance().removeNodeOfName(noeud.getName());
+
+ if (b == true) {
+ System.out.println("Removing Node of rank "
+ + rankDeaD + " : size = "
+ + Register.Instance().getSize());
+ } else {
+ System.err
+ .println("Cannot remove the Node, it doesn't exist anymore: size = "
+ + Register.Instance().getSize());
+ }
+
+ Calendar cal = new GregorianCalendar();
+ System.out.println("At time=" + cal.get(Calendar.MINUTE) + ":"
+ + cal.get(Calendar.SECOND));
+
+ // retrouver SI POSSIBLE un autre libre pr remplacer celui la pr
+ // cette tache
+
+ /**** Sébastien Miquée **/
+ //Node tmpNode = foundToReplaceThisNode(rankDeaD, nomNoeud);
+ Node tmpNode = foundToReplaceThisNode(rankDeaD, nomNoeud, noeud);
+ try {
+ // broadcastRegister(0);
+ updateConcernedNodes(rankDeaD, noeud, tmpNode);
+
+ Thread.sleep(500);
+ System.out.println("Set scanning on %%%%%%");
+ tmpNode.getStub().setScanning(true);
+ } catch (Exception e) {
+ System.err.println("Unable to setScannig on for the new node: "
+ + e);
+ }
+
+ // Register.Instance().getListeOfTasks().viewAll();
+ for (int z = 0; z < spawnersList.size(); z++)
+ if (!((JaceSpawnerInterface) spawnersList.get(z))
+ .equals(Register.Instance().getSpawnerStub()))
+ try {
+ ((JaceSpawnerInterface) spawnersList.get(z))
+ .replaceDeamonBy(noeud, tmpNode, rankDeaD);
+
+ } catch (Exception e) {
+ System.err
+ .println("Unable to broadcast the modifications to all the spawners: "
+ + e);
+ }
+ } catch (Exception ee) {
+ System.err.println("Error in signalDeadNode() :" + ee);
+ }
+ }
+
+ // verifie si les noeud notes vivant ds le Register.Instance() du SuperNode
+ // le sont encore
+ // retourne 0 si erreur, 1 sinon
+ /*
+ * private synchronized int scanConnectedHosts() { long time = 0; Node host;
+ * Node tmpNode; long workerTime; long currentTime; int rank; int restempo;
+ * int nb = 0; int nbC = 0; boolean changed = false; int index=0; try{
+ * JaceSpawnerInterface spawnerStub=Register.Instance().getSpawnerStub();
+ * if(spawnerStub.getFinished()==true){
+ * System.out.println("nbre de taches="+Register.Instance().getSize());
+ * ListeTask t=Register.Instance().getListeOfTasks();
+ * for(index=z;index<t.getSize();index++){ TaskId recev = null;
+ * System.out.println("deleting Task************"+index);
+ *
+ * recev = t.get(index); JaceInterface stub=recev.getHostStub();
+ * spawnerStub.killApplication(stub); }
+ *
+ *
+ *
+ * } }catch(Exception e){
+ * System.out.println("w aiiiiiiiiiiiiiirrrr"+e+" index="+index); z=index;
+ * }
+ *
+ * if (Register.Instance().getSize() == 0) {
+ * System.out.println("aucun noeuds a scanner");
+ * RunningApplication.Instance().purge(); System.exit(0);
+ *
+ * }
+ *
+ * return 1; }
+ */
+
+ // trouver un noeud sur les superNode
+ // pr les requisitionner
+
+ /*** Sébastien Miquée ***/
+
+ //private synchronized Node foundToReplaceThisNode(int theRank, String nom) {
+ private synchronized Node foundToReplaceThisNode(int theRank, String nom, Node _n) {
+ // int i = 0;
+ boolean found = false;
+ Node node = null;
+
+ while (found == false) {
+ try {
+
+ //node = centralServer.getNewNode(LocalHost.Instance().getIP());
+ node = centralServer.getNewNode(LocalHost.Instance().getIP(), _n);
+
+ if( node != null )
+ {
+ found = true ;
+ }
+ } catch (Exception e) {
+ // trouver un autre superNode et lui demander le noeud a lui
+
+ System.err.println("Cannot localize SuperNode ! " + e);
+
+ connectSuperNode();
+ }
+ }
+
+
+ if (node != null) {
+ System.out.println("Using Node " + node.getName() + " ("
+ + node.getIP() + ") in order to replace " + nom
+ + " size before add: " + Register.Instance().getSize()
+ + "\n\n");
+ node.setAliveFlag(true);
+ node.setAliveTime();
+
+ // rajouter le noeud ds le Register
+ node.setAppliName(RunningApplication.Instance().getName());
+
+ // lui envoyer mon stub pr qu'il commence a me pinguer des
+ // maintenant
+ // TODO a mettre ds un thread ????
+
+ /*
+ * TaskId
+ * neighborTask=Register.Instance().getListeOfTasks().getTaskIdOfRank
+ * ((theRank+1)%Register.Instance().getListeOfTasks().getSize());
+ * try{ node.getStub().updateHeart(neighborTask.getHostStub()); }
+ * catch(Exception e) {
+ * System.out.println("nvx noeud deja plu dispo2"); //node = null; }
+ */
+ // TODO verif pourkoi superNode me le redonne
+ // alors qu'il fait deja du calcul
+ // int is = Register.Instance().existNode(node.getIP());
+ int is = Register.Instance().existNode(node);
+ if (is != -1) {
+ System.out.println("The Node is already in the register ! I don't add it.");
+ System.out.println("Node " + node.getName() + " not added !") ;
+ node = null;
+ } else {
+ Register.Instance().addNode(node);
+
+ // !!!!!!!!!!!!!!actualiser le ListeTask
+ TaskId myTaskId = Register.Instance().getListeOfTasks()
+ .getTaskIdOfRank(theRank);
+ myTaskId.setHostIP(node.getIP());
+ myTaskId.setHostName(node.getName());
+ myTaskId.setHostStub(node.getStub());
+
+ // Register.Instance().getListeOfTasks().viewAll();
+ int neighborRank;
+ if (theRank == 0)
+ neighborRank = Register.Instance().getSize() - 1;
+ else
+ neighborRank = theRank - 1;
+ TaskId neighborTask2 = Register.Instance().getListeOfTasks()
+ .getTaskIdOfRank(neighborRank);
+ try {
+ JaceInterface jaceStub = neighborTask2.getHostStub();
+ jaceStub.updateHeart(node.getStub());
+ } catch (Exception e) {
+ System.err.println("Next node unreachable ! " + e);
+ // node = null;
+ }
+
+ }
+
+ } else {
+ System.out.println("I didn't receive a new Node !");
+ }
+ return node;
+ }
+
+ public void replaceBy(JaceSpawnerInterface oldStub,
+ JaceSpawnerInterface stub) {
+ int index = spawnersList.indexOf((Object) oldStub);
+ if (index != -1)
+ spawnersList.setElementAt(stub, index);
+ else
+ System.err.println("Spawner's stub not foud in spawnersList !");
+ }
+
+ public void getNewSpawner(JaceSpawnerInterface previousSpawner) {
+ //boolean found = false;
+ Node node = null;
+ int index;
+ JaceSpawnerInterface spawnerStub = null;
+
+ // while (found == false) {
+ try {
+ // TODO : trouver l'erreur !!!
+ // msg d'erreur :
+ // "pas localise le super node java.lang.NullPointerException"
+ if (centralServer == null) {
+ System.err.println("Central Server not localized !");
+ }
+ node = centralServer.getNewNode( LocalHost.Instance().getIP(), null ) ;
+ RunningApplication.Instance()
+ .incrementNumberOfSpawnerDisconnections();
+ //found = true;
+ } catch (Exception e) {
+ // trouver un autre superNode et lui demander le noeud a lui
+ System.err.println("Super Node not localized !\n " + e);
+// System.out.println("pas localise le super node " + e);
+// System.out.println("pas localise le super node " + e);
+// System.out.println("pas localise le super node " + e);
+// System.out.println("pas localise le super node " + e);
+// System.out.println("pas localise le super node " + e);
+// System.out.println("pas localise le super node " + e);
+// System.out.println("pas localise le super node " + e);
+ System.err.println("My IP : " + LocalHost.Instance().getIP());
+ if (centralServer == null) {
+ System.err.println("CentralServer is NULL !");
+ }
+ connectSuperNode();
+ }
+ // }
+ if (node != null) {
+ index = spawnersList.indexOf((Object) previousSpawner);
+ if (index != -1) {
+ System.out.println("Using Node " + node.getName()
+ + " ("
+ + LocalHost.Instance().resolve(node.getName())
+ + ") to replace a dead spawner\n\n");
+ try {
+ // Register.Instance().viewAll();
+ // Register.Instance().getListeOfTasks().viewAll();
+ spawnerStub = node.getStub().transformIntoSpawner(
+ params,
+ appliName,
+ Register.Instance(),
+ nbTasks,
+ centralServer,
+ index,
+ heartTime,
+ 1,
+ RunningApplication.Instance()
+ .getNumberOfDisconnections(),
+ RunningApplication.Instance()
+ .getNumberOfSpawnerDisconnections(),
+ nbOfDaemonsPerSpawner, nbOfDeamonsPerThread);
+ spawnersList.setElementAt(spawnerStub, index);
+ new StartProcessThread(index).start();
+ // spawnerStub.startProcess( spawnersList);
+ } catch (Exception e) {
+ System.err.println("Unable to reach the new spawner: " + e);
+ }
+ for (int j = 0; j < spawnersList.size(); j++)
+ try {
+ if (!((JaceSpawnerInterface) spawnersList.get(j))
+ .equals(Register.Instance().getSpawnerStub())
+ && !((JaceSpawnerInterface) spawnersList.get(j))
+ .equals(spawnerStub)) {
+ System.out
+ .println("Trying to broadcast to spawner of rank "
+ + j);
+
+ ((JaceSpawnerInterface) spawnersList.get(j))
+ .replaceBy(previousSpawner, spawnerStub);
+ }
+ } catch (Exception e) {
+ System.err
+ .println("Unable to broadcast to spawner of rank: "
+ + j + ". Error:" + e);
+ }
+ ScanThreadSpawner.Instance().setServer(spawnerStub);
+
+ int previous;
+ if (index == 0)
+ previous = spawnersList.size() - 1;
+ else
+ previous = index - 1;
+ try {
+ ((JaceSpawnerInterface) spawnersList.get(previous))
+ .updateHeart(spawnerStub);
+ } catch (Exception e) {
+ System.err
+ .println("unable to change the server of the heartbeatThread for the previous node of rank "
+ + previous + ". error:" + e);
+ }
+ }
+ } else {
+ System.err.println("Node is null !");
+ }
+
+ }
+
+ public void broadcastFinished(boolean bool) {
+ for (int i = 0; i < spawnersList.size(); i++)
+ try {
+ ((JaceSpawnerInterface) spawnersList.get(i)).setFinished(bool);
+ } catch (Exception e) {
+ System.err
+ .println("Unable to propagate the end of the application :"
+ + e);
+ }
+ }
+
+ private synchronized void scanAppliNodes() {
+
+ //Node node = null;
+ //ListeTask tskList = null;
+ //int cptReplaced;
+ int index = 0;
+ try {
+ JaceSpawnerInterface spawnerStub = Register.Instance()
+ .getSpawnerStub();
+ if (spawnerStub.getFinished() == true) {
+ System.out.println("Number of tasks ="
+ + Register.Instance().getSize());
+
+ int x = Register.Instance().getListeOfTasks().getSize()
+ / nbOfDaemonsPerSpawner;
+ int s;
+ if (rank == x)
+ s = (Register.Instance().getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
+ / nbOfDeamonsPerThread;
+ else
+ s = nbOfDaemonsPerSpawner / nbOfDeamonsPerThread;
+
+ int debut = nbOfDaemonsPerSpawner * rank;
+
+ // for(int i=debut;i<nbOfDaemonsPerSpawner*(rank+1) &&
+ // i<reg.getSize();i++)
+ // System.out.println(((Node)nodes.elementAt(i)).getName());
+
+ ListeTask t = Register.Instance().getListeOfTasks();
+ ScanThreadSpawner.Instance().kill();
+ HeartBeatSpawner.Instance().kill();
+ for (int i = 0; i < s + 1; i++) {
+
+ new KillThread(i, debut, nbOfDaemonsPerSpawner,
+ nbOfDeamonsPerThread, t).start();
+ }
+
+ Thread.sleep(2000);
+
+ long finalTime = RunningApplication.Instance().getChrono()
+ .getValue();
+
+ int nbe = RunningApplication.Instance()
+ .getNumberOfDisconnections();
+
+ int nbsdc = RunningApplication.Instance()
+ .getNumberOfSpawnerDisconnections();
+ System.out.println("Application finished successfully !");
+// System.out.println("Application finished successfully !!!!!!");
+// System.out.println("Application finished successfully !!!!!!");
+// System.out.println("Application finished successfully !!!!!!");
+// System.out.println("Application finished successfully !!!!!!");
+// System.out.println("Application finished successfully !!!!!!");
+// System.out
+// .println("Application finished successfully !!!!!!\n");
+ System.out.println("TOTAL TIME in s : " + (finalTime / 1000));
+ System.out.println("nb of desconnections: " + nbe);
+ System.out.println("nb of spawners desconnections: " + nbsdc);
+ if (JaceDaemon.Instance().isRunning()) {
+ JaceDaemon.Instance().reconnectSuperNode();
+
+ RunningApplication.Instance().purge();
+
+ } else {
+ // purger l'appli
+
+ RunningApplication.Instance().purge();
+ // System.exit(1);
+ }
+ }
+ } catch (Exception e) {
+ System.out
+ .println("w aiiiiiiiiiiiiiirrrr" + e + " index=" + index);
+ z = index;
+ }
+ /*
+ * if (Register.Instance().getSize() == 0) {
+ * System.out.println("aucun noeuds a scanner");
+ * RunningApplication.Instance().purge(); System.exit(0); return 0;
+ *
+ * } else{ tskList = Register.Instance().getListeOfTasks();
+ *
+ * //si mon appli a besoin d'un noeud //sinon, on fait rien if ((nbTasks
+ * - Register.Instance().getSize()) > 0) { cptReplaced = 0;
+ *
+ * //TODO demander des paquet de nodes, pas qu'un //on scanne toutes les
+ * taches de cette appli for (int ind = 0; ind < tskList.getSize();
+ * ind++) { //si 1 tache a pas de noeud, on trouve 1 remplacant
+ *
+ * //if (tskList.get(ind).getHostIP() == null) { if
+ * (tskList.get(ind).getHostStub() == null) { rank =
+ * tskList.get(ind).getRank(); node = foundToReplaceThisNodeTMP(rank);
+ * if (node != null) { cptReplaced++; }
+ *
+ * } }
+ *
+ * //qd fini de scanner taches, envoyer Register //si remplacement de
+ * noeud (c a d si Register modifier) if (cptReplaced != 0) {
+ * broadcastRegister(0); } try { Thread.currentThread().yield(); } catch
+ * (Exception e) {}
+ *
+ * }// fin if(appli.getNeededNodes() > 0) {
+ * //System.out.println("SCAN APPLI : taille : " +
+ * Register.Instance().getSize()); return 1; }
+ */
+ }
+
+// @SuppressWarnings("unused")
+// private synchronized Node foundToReplaceThisNodeTMP(int theRank) {
+// // int i = 0;
+// boolean found = false;
+// Node node = null;
+// // while (found == false) {
+// try {
+// // TODO : trouver l'erreur !!!
+// // msg d'erreur :
+// // "pas localise le super node java.lang.NullPointerException"
+// if (centralServer == null) {
+// System.out.println("centralServer est NUUUUUUUUULL");
+// }
+// node = centralServer.getNewNode(LocalHost.Instance().getIP());
+//
+// found = true;
+// } catch (Exception e) {
+// // trouver un autre superNode et lui demander le noeud a lui
+// System.out.println("TMP pas localise le super node " + e);
+// System.out.println("TMP pas localise le super node " + e);
+// System.out.println("TMP pas localise le super node " + e);
+// System.out.println("TMP pas localise le super node " + e);
+// System.out.println("TMP pas localise le super node " + e);
+// System.out.println("TMP pas localise le super node " + e);
+// System.out.println("TMP pas localise le super node " + e);
+// System.out.println("mon IP : " + LocalHost.Instance().getIP());
+// if (centralServer == null) {
+// System.out.println("centralServer : NULL");
+// }
+// connectSuperNode();
+// }
+// // }
+// if (node != null) {
+// System.out.println("COOOOOOOOOOOOOOOOOOOOOOL : requisition de "
+// + node.getName() + " taille avt add: "
+// + Register.Instance().getSize() + "\n\n");
+// node.setAliveFlag(true);
+// node.setAliveTime();
+//
+// // rajouter le noeud ds le Register
+// System.out.println("ds Register, manque "
+// + (nbTasks - Register.Instance().getSize()));
+// node.setAppliName(RunningApplication.Instance().getName());
+//
+// // lui envoyer mon stub pr qu'il commence a me pinguer des
+// // maintenant
+// // TODO a mettre ds un thread ????
+// try {
+// TaskId neighborTask = Register.Instance().getListeOfTasks()
+// .getTaskIdOfRank(
+// (theRank + 1)
+// % Register.Instance().getListeOfTasks()
+// .getSize());
+// node.getStub().updateHeart(neighborTask.getHostStub());
+// // node.getStub().updateHeart(this.spawnerRef);
+//
+// // int is = Register.Instance().existNode(node.getIP());
+// int is = Register.Instance().existNode(node);
+// // TODO verif pourkoi superNode me le redonne
+// // alors qu'il fait deja du calcul
+// if (is != -1) {
+// System.out.println("j'ajoute pas le noeud, il y est deja");
+// System.out.println("PAS AJOUTEE TMP " + node.getName());
+// System.out.println("PAS AJOUTEE TMP " + node.getName());
+// System.out.println("PAS AJOUTEE TMP " + node.getName());
+// System.out.println("PAS AJOUTEE TMP " + node.getName());
+// System.out.println("PAS AJOUTEE TMP " + node.getName());
+// node = null;
+// } else {
+// Register.Instance().addNode(node);
+//
+// // !!!!!!!!!!!!!!actualiser le ListeTask
+// TaskId myTaskId = Register.Instance().getListeOfTasks()
+// .getTaskIdOfRank(theRank);
+// myTaskId.setHostIP(node.getIP());
+// myTaskId.setHostName(node.getName());
+// myTaskId.setHostStub(node.getStub());
+// // Register.Instance().getListeOfTasks().getTaskIdOfRank(theRank).setHostIP(node.getIP());
+// // Register.Instance().getListeOfTasks().getTaskIdOfRank(theRank).setHostName(node.getName());
+// // Register.Instance().getListeOfTasks().getTaskIdOfRank(theRank).setHostStub(node.getStub());
+// }
+// } catch (Exception e) {
+// System.out.println("nvx noeud deja plu dispo");
+// node = null;
+// }
+// } else {
+// System.out.println("RADINNNNNNNNNNNNNN TMP ");
+// }
+// return node;
+// }
+
+ private void exportObject() {
+
+ JaceSpawnerServer spawnerServer = null;
+
+ System.out.println("Name of local machine is: "
+ + LocalHost.Instance().getName());
+ System.out.println("IP of local machine is: "
+ + LocalHost.Instance().getIP());
+ try {
+ // launch the JaceSpawnerServer
+ spawnerServer = new JaceSpawnerServer();
+ java.rmi.registry.LocateRegistry.createRegistry(spawnerPort);
+ java.rmi.registry.LocateRegistry.getRegistry(spawnerPort).rebind(
+ "JaceSpawnerServer", spawnerServer);
+ spawnerRef = (JaceSpawnerInterface) Naming.lookup("rmi://"
+ + LocalHost.Instance().getIP() + ":" + spawnerPort
+ + "/JaceSpawnerServer");
+
+ } catch (Exception e) {
+ System.err
+ .println("JaceP2P_Error in JaceSpawner.exportObject() when creating the local JaceSpawnerServer "
+ + e);
+// System.err.println("exit ds JaceSpawner.exportObject");
+ System.exit(0);
+ }
+
+ }
+
+ public void connectSuperNode() {
+ System.out.println("I'm looking for a super node");
+ boolean connected = false;
+ if (!(superNode_IP == null)) {
+ try {
+ System.out.println("Trying to invoke super node "
+ + superNode_IP);
+ centralServer = (JaceSuperNodeInterface) Naming.lookup("rmi://"
+ + superNode_IP + ":" + superNode_port
+ + "/JaceSuperNode");
+ System.out.println("succesfully located " + superNode_IP);
+
+ // add stub and IP in LocalHost to store it until super node
+ // death
+ LocalHost.Instance().setSuperNodeStub(centralServer);
+ LocalHost.Instance().setSuperNodeIP(superNode_IP);
+ heartTime = centralServer.getSuperNodeBeat();
+ timeBeforeKill = NB_HEART_DECONNECT * heartTime;
+ connected = true;
+
+ } catch (Exception e) {
+ System.err.println("Super Node not accessible, try another one (1/2s)");
+ try {
+ Thread.sleep(500);
+ } catch (Exception e1) {
+ }
+
+ }
+ }
+ if (connected == false) {
+ int i = 0;
+ SuperNodeListe.Instance().staticInitialization();
+ while (connected == false
+ && i < SuperNodeListe.Instance().getListe().size()) {
+ SuperNodeData d = null;
+ d = SuperNodeListe.Instance().getSuperNodeData(i);
+
+ superNode_IP = LocalHost.Instance().resolve(d.getIP());
+ superNode_port = d.getPort();
+ // superNode_port = d.getPort();
+ try {
+ System.out.println("Trying to invoke Super Node "
+ + superNode_IP);
+ centralServer = (JaceSuperNodeInterface) Naming
+ .lookup("rmi://" + superNode_IP + ":"
+ + superNode_port + "/JaceSuperNode");
+ System.out.println("Succesfully located SuperNode "
+ + superNode_IP);
+ LocalHost.Instance().setSuperNodeStub(centralServer);
+ LocalHost.Instance().setSuperNodeIP(superNode_IP);
+ heartTime = centralServer.getSuperNodeBeat();
+ timeBeforeKill = NB_HEART_DECONNECT * heartTime;
+
+ connected = true;
+ } catch (Exception e) {
+ System.err
+ .println("SuperNode "
+ + superNode_IP
+ + " not accessible, trying to locate another one in 0.5s\n");
+ i++;
+ try {
+ Thread.sleep(500);
+ } catch (Exception e1) {
+ }
+
+ }
+ }
+ }
+ if (connected == false) {
+ System.err.println("All the Super Nodes in the list are not accessible. I'm unable to connect to the platform !");
+ System.exit(1);
+ }
+
+ }
+
+ // get a Register on the SuperNode
+ // completed with the required number of Daemons
+ // or gets NULL
+ public synchronized void getRegisterOnSuperNode() {
+ Register registerSpawner = null;
+ Node noeud = null;
+ boolean recieved = false;
+
+ System.out.println("Trying to get a Register on the SuperNode");
+ int nbExtraSpawners = 0;
+ if (nbTasks > nbOfDaemonsPerSpawner) {
+ nbExtraSpawners = (nbTasks - 1) / nbOfDaemonsPerSpawner;
+
+ }
+ while (!recieved) {
+ try {
+ registerSpawner = centralServer.getRegisterSpawner(LocalHost
+ .Instance().getIP(), nbTasks, (Task) tache, nbTasks
+ + nbExtraSpawners, algo, paramAlgo);
+ recieved = true;
+ } catch (Exception e) {
+ System.err
+ .println("Unable to recieve a register from superNode "
+ + e);
+ connectSuperNode();
+ }
+ }
+ if (registerSpawner.getSize() != (nbTasks + nbExtraSpawners)) {
+ System.err.println("I did not recieve enough nodes from superNode!!!! \n killing application !!!!");
+ for (int i = 0; i < registerSpawner.getSize(); i++) {
+ try {
+ registerSpawner.getNodeAt(i).getStub().reconnectSuperNode();
+ } catch (Exception e) {
+ System.err.println("The reserved node was unable to reconnect to the super node");
+ }
+ }
+ System.exit(0);
+ }
+
+ spawnersList = new Vector<Object>();
+ for (int i = 0; i < nbExtraSpawners && i < registerSpawner.getSize(); i++) {
+ spawnersList.add(registerSpawner.getNodeAt(i
+ * nbOfDaemonsPerSpawner));
+ registerSpawner.removeNode(registerSpawner.getNodeAt(i
+ * nbOfDaemonsPerSpawner));
+ }
+
+ registerSpawner.setNbOfTasks(nbTasks);
+ registerSpawner.setNumBackupNeighbors(nbSavingNodes);
+ /*
+ * System.out.println("Trying to connect another SuperNode");
+ * connectSuperNode(); try { registerSpawner =
+ * centralServer.getRegisterSpawner(LocalHost.Instance().getIP(),
+ * nbTasks); } catch(Exception e1) {}
+ */
+
+ if (registerSpawner != null) {
+ System.out.println("I received the register");
+ // registerSpawner.setVersion(registerVersion);
+ // registerVersion++;
+ Register.Instance().replaceBy(registerSpawner);
+ System.out.println("It contains " + Register.Instance().getSize()
+ + " Nodes" + " " + nbExtraSpawners + " ExtraSpawners");
+
+ // set each Node aliveTime value to the Spawner current time
+ for (int i = 0; i < Register.Instance().getSize(); i++) {
+ noeud = Register.Instance().getNodeAt(i);
+ noeud.setAliveFlag(true);
+ noeud.setAliveTime();
+ }
+
+ } else {
+ System.err.println("\n---------------WARNING--------------");
+ System.err.println("No Daemon available on the SuperNode dispo, try later, please");
+ System.exit(0);
+ }
+ }
+
+ public class TransformThread extends Thread {
+ int i;
+ Node n;
+
+ public TransformThread(int i, Node n) {
+ this.i = i;
+ this.n = n;
+ }
+
+ public void run() {
+
+ try {
+ System.out.println("Trying to transform the spawner ("
+ + n.getName() + ") of rank " + i);
+ spawnersList.setElementAt(n.getStub().transformIntoSpawner(
+ params, appliName, Register.Instance(), nbTasks,
+ centralServer, i, heartTime, 0, 0, 0,
+ nbOfDaemonsPerSpawner, nbOfDeamonsPerThread), i);
+ } catch (Exception e) {
+ System.err.println("Error while contacting newly acquired spawner ("
+ + n.getName() + "): " + e);
+ try {
+ n = centralServer.getNewNode( LocalHost.Instance().getIP(), null ) ;
+
+ new TransformThread(i, n).start();
+ } catch (Exception e1) {
+ System.err.println("The Super Node is maybe dead: " + e1) ;
+ for (int z = 0; z < Register.Instance().getSize(); z++) {
+ try {
+ Register.Instance().getNodeAt(z).getStub()
+ .reconnectSuperNode();
+ } catch (Exception ez) {
+ System.err.println("The reserved node was unable to reconnect to the super node: \n"
+ + ez);
+ }
+ }
+ System.exit(0);
+ }
+ }
+ }
+ }
+
+ public class StartProcessThread extends Thread {
+ int i;
+
+ public StartProcessThread(int i) {
+ this.i = i;
+ }
+
+ public void run() {
+ try {
+
+ /*
+ * while((spawnersList.elementAt(i) instanceof Node)) try{
+ * System.out.println("waiting till transform of spawner "+i+
+ * " is finished"); Thread.sleep(20); }catch(Exception e1){}
+ */
+
+ // System.out.println("start process on spawner of rank "+i);
+ JaceSpawnerInterface spawnerStub = (JaceSpawnerInterface) spawnersList
+ .elementAt(i);
+ spawnerStub.startProcess(spawnersList);
+ } catch (Exception e) {
+ e.printStackTrace(System.out);
+ System.err.println("Unable to start the process on the spawner of rank "
+ + i + ".error: " + e);
+ }
+ }
+ }
+
+ public void createSpawnerNetwork() {
+ Node n;
+ int i;
+ for (i = 0; i < spawnersList.size(); i++) {
+ n = (Node) spawnersList.elementAt(i);
+
+ // Register.Instance().getListeOfTasks().viewAll();
+ // spawnersList.setElementAt(n.getStub().transformIntoSpawner(
+ // params, appliName, Register.Instance(),nbTasks, centralServer,i,
+ // heartTime,0,0),i);
+ new TransformThread(i, n).start();
+
+ }
+ // broadcast the Register.Instance() to all the JaceServer
+ // in order to start each task on the Daemons
+
+ spawnersList.add(Register.Instance().getSpawnerStub());
+ System.out.println(" rank=spawnersList.size()=" + spawnersList.size());
+ rank = spawnersList.size() - 1;
+ broadcastRegister(1);
+ for (int j = 0; j < spawnersList.size(); j++) {
+ System.out.println("waiting till transform of spawner " + j
+ + " is finished");
+ while ((spawnersList.elementAt(j) instanceof Node))
+ try {
+
+ Thread.sleep(20);
+ } catch (Exception e) {
+ }
+ System.out
+ .println("End Transformation of all spawners. Beginning the computing processes");
+ }
+ for (i = 0; i < spawnersList.size(); i++) {
+
+ // while(!(spawnersList.elementAt(i) instanceof
+ // JaceSpawnerInterface))
+
+ new StartProcessThread(i).start();
+
+ }
+ System.out.println("End create Spawner Network!!!!!!!!!");
+ }
+
+ public JaceSpawnerInterface getSpawnerResponsibleOn(int rank) {
+ int id = rank / nbOfDaemonsPerSpawner;
+ return (JaceSpawnerInterface) spawnersList.get(id);
+ }
+
+ public void createAppli() {
+ int count = 0;
+ int i = 0;
+ String nodeName;
+ String nodeIP;
+ ListeTask tsk = new ListeTask();
+ Node tmpNode;
+ JaceInterface nodeStub = null;
+ TaskId myTask = null;
+
+ System.out.println("appli launched, starting the chrono");
+ RunningApplication.Instance().getChrono().start();
+
+ RunningApplication.Instance().setName(appliName);
+ RunningApplication.Instance().setNbTasks(nbTasks);
+ // RunningApplication.Instance().setRegister(Register.Instance());
+
+ Register.Instance().setParams(params);
+ Register.Instance().setAppliName(appliName);
+ Register.Instance().setSpawnerStub(this.spawnerRef);
+
+ // assign a TaskId to each Node of the Register
+ // and insert the TaskId in tke ListTask
+ while (i < Register.Instance().getSize() && count < nbTasks) {
+ tmpNode = Register.Instance().getNodeAt(i);
+ if (tmpNode.getAliveFlag() == true) {
+ tmpNode.setAppliName(appliName);
+ nodeStub = tmpNode.getStub();
+ nodeName = tmpNode.getName();
+ nodeIP = tmpNode.getIP();
+
+ myTask = new TaskId(appliName, count, nodeStub);
+ myTask.setHostIP(nodeIP);
+ myTask.setHostName(nodeName);
+
+ tsk.addTask(myTask);
+ count++;
+ }
+ i++;
+ }
+
+ // if not enough Nodes in the Register,
+ // insert not assigned TaskId in the ListTask
+ if (count < nbTasks) {
+ for (int j = count; j < nbTasks; j++) {
+ tsk.addTask(new TaskId(appliName, j, null));
+ }
+ System.out.println("in Register, misses "
+ + (nbTasks - Register.Instance().getSize()) + " nodes");
+ }
+
+ // insert the ListeTask in the Register of the appli
+ Register.Instance().setListeOfTasks(tsk);
+ // Register.Instance().getListeOfTasks().viewAll();
+ RunningApplication.Instance().setRunning(true);
+ System.out.println("fin create appli");
+ }
+
+ public class BroadcastSpawner extends Thread {
+ int debut;
+ int i;
+ int nbOfDaemonsPerThread, nbOfDeamonsPerSpawner;
+
+ public BroadcastSpawner(int i, int debut, int nbOfDeamonsPerSpawner,
+ int nbOfDaemonsPerThread) {
+ this.debut = debut;
+ this.i = i;
+ this.nbOfDaemonsPerThread = nbOfDaemonsPerThread;
+ this.nbOfDeamonsPerSpawner = nbOfDeamonsPerSpawner;
+ }
+
+ public void run() {
+
+ for (int index = debut + i * nbOfDaemonsPerThread; index < debut
+ + i * nbOfDeamonsPerThread + nbOfDeamonsPerThread
+ && index < debut + nbOfDeamonsPerSpawner
+ && index < Register.Instance().getListeOfTasks().getSize(); index++) {
+ try {
+ Register.Instance().getNodeAt(index).getStub().setSpawner(
+ Register.Instance().getSpawnerStub());
+ } catch (Exception e) {
+ System.out.println("can't change spawner stub on node: "
+ + Register.Instance().getNodeAt(i).getName()
+ + ". error: " + e);
+ }
+ }
+ }
+ }
+
+ public class KillThread extends Thread {
+ int debut;
+ int i;
+ int nbOfDaemonsPerThread, nbOfDeamonsPerSpawner;
+ ListeTask t;
+
+ public KillThread(int i, int debut, int nbOfDeamonsPerSpawner,
+ int nbOfDaemonsPerThread, ListeTask t) {
+ this.debut = debut;
+ this.i = i;
+ this.nbOfDaemonsPerThread = nbOfDaemonsPerThread;
+ this.nbOfDeamonsPerSpawner = nbOfDeamonsPerSpawner;
+ this.t = t;
+ }
+
+ public void run() {
+
+ // t.viewAll();
+ for (int index = debut + i * nbOfDaemonsPerThread; index < debut
+ + i * nbOfDeamonsPerThread + nbOfDeamonsPerThread
+ && index < debut + nbOfDeamonsPerSpawner
+ && index < t.getSize(); index++) {
+ Node noeud = null;
+ try {
+ TaskId recev = null;
+ System.out.println("deleting Task" + index);
+
+ recev = t.getTaskIdOfRank(index);
+
+ JaceInterface stub = recev.getHostStub();
+ System.out.println("name=" + recev.getHostName());
+ noeud = Register.Instance().getNodeOfStub(stub);
+ noeud.setAppliName(null);
+ new ReconnectThread(stub, noeud.getName()).start();
+ Register.Instance().removeNode(noeud);
+ // LocalHost.Instance().getSpawnerStub().killApplication(stub);
+
+ } catch (Exception e) {
+ try {
+ System.err.println("error in killThread on node "
+ + noeud.getName() + ". " + e);
+ } catch (Exception e2) {
+ System.err.println("error in error :" + e2);
+ }
+ }
+ }
+ }
+
+ class ReconnectThread extends Thread {
+ JaceInterface stub = null;
+ String name;
+
+ public ReconnectThread(JaceInterface s, String name) {
+ stub = s;
+ this.name = name;
+ }
+
+ public void run() {
+ try {
+ // System.out.println("reconnexion SuperNode");
+ // Register.Instance().getNode(workerIP).getWorkerStub().reconnectSuperNode();
+
+ // stub.reconnectSuperNode();
+ stub.suicide("fin d'appli");
+ } catch (Exception e) {
+ System.err.println("can't kill node " + name);
+ }
+ yield();
+ }
+ }
+
+ }
+
+ // faire une copie du Register et l'envoyer aux noeuds qui le compose
+ // car si il est modif en meme tmp, on envoi pas un truc coherent
+ private synchronized void broadcastRegister(int requete) {
+ // Register reg = Register.Instance().clone();
+ Register reg = Register.Instance();
+
+ try {
+ System.out.println("name of spawner: "
+ + Register.Instance().getSpawnerStub().getName());
+ // launch 1 thread to send the Register to all the nodes
+ while (broadcasting == true)
+ Thread.sleep(5);
+ broadcasting = true;
+ Register.Instance().setSpawnerStub(
+ Register.Instance().getSpawnerStub());
+ int x = reg.getListeOfTasks().getSize() / nbOfDaemonsPerSpawner;
+ int s;
+ if (rank == x)
+ if ((reg.getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
+ % nbOfDeamonsPerThread == 0)
+ s = (reg.getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
+ / nbOfDeamonsPerThread;
+ else
+ s = (reg.getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
+ / nbOfDeamonsPerThread + 1;
+ else if ((nbOfDaemonsPerSpawner % nbOfDeamonsPerThread) == 0)
+ s = nbOfDaemonsPerSpawner / nbOfDeamonsPerThread;
+ else
+ s = nbOfDaemonsPerSpawner / nbOfDeamonsPerThread + 1;
+ int debut = nbOfDaemonsPerSpawner * rank;
+ System.out.println("rank=" + rank + " debut=" + debut + " s=" + s
+ + " nbOfDaemonsPerSpawner=" + nbOfDaemonsPerSpawner
+ + " nbOfDeamonsPerThread=" + nbOfDeamonsPerThread + " x="
+ + x);
+ for (int i = 0; i < s; i++)
+ new UpdateRegisterThread(tache, reg, requete, i, debut).start();
+ /*
+ * This thread : -updates the goal of the Node beats if necessary
+ * (stub.updateHeart) -updates the Register on each Node
+ * (stub.updateRegister)
+ */
+ JaceSpawner.Instance().setBroadcasting(false);
+ try {
+ Thread.sleep(10);
+ } catch (Exception e) {
+ }
+
+ } catch (Exception e) {
+ System.out
+ .println("\n1 node has died during JaceSpawner.broadcastRegister()");
+ }
+ }
+
+ private synchronized void broadcastScanning() {
+ Register reg = Register.Instance();
+ while (broadcasting == true)
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) {
+ }
+ // Register.Instance().viewAll();
+ Vector<?> nodes = (Vector<?>) Register.Instance().getListOfNodes().clone();
+ int x = reg.getListeOfTasks().getSize() / nbOfDaemonsPerSpawner;
+ int s;
+ if (rank == x)
+ s = (reg.getListeOfTasks().getSize() % nbOfDaemonsPerSpawner)
+ / nbOfDeamonsPerThread;
+ else
+ s = nbOfDaemonsPerSpawner / nbOfDeamonsPerThread;
+
+ int debut = nbOfDaemonsPerSpawner * rank;
+
+ // for(int i=debut;i<nbOfDaemonsPerSpawner*(rank+1) &&
+ // i<reg.getSize();i++)
+ // System.out.println(((Node)nodes.elementAt(i)).getName());
+
+ for (int i = 0; i < s + 1; i++) {
+
+ new StartScanThread(i, nodes, debut).start();
+ }
+
+ }
+
+ public Register getRegister(int rank) {
+
+ ListeTask listOfTasks = Register.Instance().getListeOfTasks();
+ Vector<Integer> dependencies = getDependencies(rank, listOfTasks.getSize());
+ Register g = new Register();
+ ListeTask newListOfTasks = new ListeTask();
+ g.setAppliName(Register.Instance().getAppliName());
+ g.setParams(Register.Instance().getParams());
+ g.setSpawnerStub(Register.Instance().getSpawnerStub());
+ g.setNbOfTasks(Register.Instance().getNbOfTasks());
+ // g.setVersion(reg.getVersion());
+ for (int j = 0; j < dependencies.size(); j++) {
+ TaskId id = listOfTasks.getTaskIdOfRank(((Integer) dependencies
+ .elementAt(j)).intValue());
+ newListOfTasks.addTask(id);
+ if (id.getHostStub() != null) {
+ Node noeud = Register.Instance()
+ .getNodeOfStub(id.getHostStub());
+ g.addNode(noeud);
+ }
+ }
+ g.setListeOfTasks(newListOfTasks);
+ return g;
+ }
+
+ private void updateConcernedNodes(int rank, Node oldNode, Node node) {
+ ListeTask listOfTasks = Register.Instance().getListeOfTasks();
+ Vector<?> dependencies = getDependencies(rank, listOfTasks.getSize());
+ System.out.println("la liste des voisins concernes de : " + rank);
+ for (int z = 0; z < dependencies.size(); z++)
+ System.out.print(((Integer) dependencies.elementAt(z)).intValue()
+ + " ");
+ System.out.println();
+ // Register.Instance().setVersion(registerVersion);
+ // registerVersion++;
+ Register.Instance()
+ .setSpawnerStub(Register.Instance().getSpawnerStub());
+ int s;
+ if ((dependencies.size() % nbOfDeamonsPerThread) == 0)
+ s = dependencies.size() / nbOfDeamonsPerThread;
+ else
+ s = dependencies.size() / nbOfDeamonsPerThread + 1;
+ Register reg = Register.Instance();
+
+ for (int j = 0; j < s; j++) {
+ new UpdateRegisterConcernedThread(dependencies, reg, j, rank,
+ oldNode, node).start();
+ }
+ }
+
+ private Vector<Integer> getDependencies(int id, int jaceSize) {
+ // get computing dependencies
+ Vector<Integer> neighbors = new Vector<Integer>();
+ int[] dep = tache.getDependencies(id);
+ for (int z = 0; z < taille(dep); z++)
+ neighbors.add(dep[z]);
+ // System.out.println("la liste des voisins de calcul de: "+id+" concerne");
+ // for(int z=0;z<neighbors.size();z++)
+ // System.out.print(((Integer)neighbors.elementAt(z)).intValue()+" ");
+ // System.out.println();
+
+ // get convergence neighbors
+ int d = 0;
+ while (Math.pow(2, d) < jaceSize) {
+ if (id < Math.pow(2, d) && ((id + Math.pow(2, d)) < jaceSize))
+ if (!neighbors.contains((Object) ((int) (id + Math.pow(2, d)))))
+ neighbors.add((int) (id + Math.pow(2, d)));
+ if (id < Math.pow(2, d + 1) && id >= Math.pow(2, d))
+ if (!neighbors.contains((Object) ((int) (id - Math.pow(2, d)))))
+ neighbors.add((int) (id - Math.pow(2, d)));
+ d++;
+ }
+
+ // get backup neighbors
+ int nb = Register.Instance().getNumBackupNeighbors();
+ int rankOfBackTask;
+ int tmp;
+ for (int j = 1; j <= nb; j++) {
+ // ------------ 1 - for backups "j + n" (to the right of j)
+ rankOfBackTask = (id + j) % jaceSize;
+ if (!neighbors.contains((Object) rankOfBackTask))
+ neighbors.add(rankOfBackTask);
+
+ // ------------ 2 - for backups "j - n" (to the left of j)
+ tmp = id - j;
+ if (tmp >= 0) {
+ rankOfBackTask = tmp % jaceSize;
+ } else {
+ rankOfBackTask = jaceSize - (Math.abs(tmp) % jaceSize);
+ }
+ if (!neighbors.contains((Object) rankOfBackTask))
+ neighbors.add(rankOfBackTask);
+ }
+ // adds itself
+ neighbors.add(id);
+ return neighbors;
+
+ }
+
+ public static int taille(int[] vect) {
+ int taille = 0;
+ int x = 0;
+ while (x < vect.length && vect[x] >= 0) {
+ taille++;
+ x++;
+ }
+ return x;
+ }
+
+ class StartScanning extends Thread {
+
+ public StartScanning() {
+ }
+
+ public void run() {
+ startScanning();
+ }
+ }
+
+}
+
+class StartScanThread extends Thread {
+ int i, debut;
+ Vector<?> nodes;
+ int nbOfDeamonsPerThread, nbOfDeamonsPerSpawner;
+
+ StartScanThread(int i, Vector<?> nodes, int debut) {
+ this.i = i;
+ this.nodes = nodes;
+ this.debut = debut;
+ nbOfDeamonsPerThread = JaceSpawner.Instance().getNbOfDeamonsPerThread();
+ nbOfDeamonsPerSpawner = JaceSpawner.Instance()
+ .getNbOfDeamonsPerSpawner();
+ }
+
+ public void run() {
+ int index;
+ for (index = debut + i * nbOfDeamonsPerThread; index < debut + i
+ * nbOfDeamonsPerThread + nbOfDeamonsPerThread
+ && index < debut + nbOfDeamonsPerSpawner
+ && index < nodes.size(); index++) {
+
+ Node node = (Node) nodes.elementAt(index);
+ JaceInterface stub = node.getStub();
+ String name = node.getName();
+ try {
+
+ stub.setScanning(true);
+ // System.out.println("modify scanning to "+name);
+
+ } catch (Exception e) {
+ System.out.println("unable to modify scanning to " + name + ":"
+ + e);
+ }
+ }
+ // for(int x=0;x<nodes.size();x++)
+ // System.out.println(((Node)nodes.elementAt(x)).getName());
+ // System.out.println("nbre total: "+(index-1));
+ }
+}