Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modification of mapping algorithm identifier mechanisms.
[jaceP2P.git] / src / jaceP2P / UpdateRegisterThread.java
1 package jaceP2P;
2
3 import java.util.Vector;
4
5 class UpdateRegisterThread extends Thread {
6         // Register reg = Register.Instance();
7         Register reg = null;
8         int requete;
9         int i, debut;
10         Task tache = null;
11         int nbOfDeamonsPerThread;
12         int nbOfDeamonsPerSpawner;
13
14         public UpdateRegisterThread(Task tache, Register r, int req, int i,
15                         int debut) {
16
17                 this.reg = r;
18                 this.requete = req;
19                 this.i = i;
20                 this.tache = tache;
21                 this.debut = debut;
22                 nbOfDeamonsPerThread = JaceSpawner.Instance().getNbOfDeamonsPerThread();
23                 nbOfDeamonsPerSpawner = JaceSpawner.Instance()
24                                 .getNbOfDeamonsPerSpawner();
25                 // requete=1 if beginning of appli to ping the spawner and not a
26                 // supernode
27                 // requete=0 if only update Register
28                 /*
29                  * told in another way : if requete = 1, update the Register AND the
30                  * beating Server on each node else, only update the Register on each
31                  * node
32                  */
33
34         }
35
36         // method launched by start()
37         public void run() {
38                 ListeTask listOfTasks = reg.getListeOfTasks();
39                 // System.out.println("Thread "+i+" start from "+(debut+i*nbOfDeamonsPerThread)+" till "+(debut+i*nbOfDeamonsPerThread+nbOfDeamonsPerThread)+" or "+(debut+nbOfDeamonsPerSpawner)+" or "+listOfTasks.getSize());
40                 for (int index = debut + i * nbOfDeamonsPerThread; index < debut + i
41                                 * nbOfDeamonsPerThread + nbOfDeamonsPerThread
42                                 && index < debut + nbOfDeamonsPerSpawner
43                                 && index < listOfTasks.getSize(); index++) {
44                         // Node node = Register.Instance().getNodeAt(index);
45                         // Node node = reg.getNodeAt(index);
46                         // Node voisin=reg.getNodeAt((index+1)%reg.getSize());
47                         Vector<Integer> dependencies = getDependencies(index, listOfTasks.getSize());
48                         // System.out.println("la liste des voisins de : "+index);
49                         // for(int z=0;z<dependencies.size();z++)
50                         // System.out.print(((Integer)dependencies.elementAt(z)).intValue()+" ");
51                         // System.out.println();
52                         System.out.println(dependencies.size());
53                         Register g = new Register();
54                         ListeTask newListOfTasks = new ListeTask();
55                         g.setAppliName(reg.getAppliName());
56                         g.setParams(reg.getParams());
57                         g.setSpawnerStub(reg.getSpawnerStub());
58                         g.setNbOfTasks(reg.getNbOfTasks());
59                         // g.setVersion(reg.getVersion());
60                         for (int j = 0; j < dependencies.size(); j++) {
61                                 TaskId id = listOfTasks.getTaskIdOfRank(((Integer) dependencies
62                                                 .elementAt(j)).intValue());
63                                 // System.out.println("got id of :"+((Integer)dependencies.elementAt(j)).intValue());
64                                 newListOfTasks.addTask(id);
65                                 if (id.getHostStub() != null) {
66                                         Node noeud = reg.getNodeOfStub(id.getHostStub());
67                                         g.addNode(noeud);
68                                 }
69                         }
70                         g.setListeOfTasks(newListOfTasks);
71                         // g.setInstance();
72                         TaskId neighborTask = listOfTasks.getTaskIdOfRank((index + 1)
73                                         % listOfTasks.getSize());
74                         // if (node == null) {
75                         // System.out.println("node NUULL");
76                         // }
77                         JaceInterface stubVoisin = neighborTask.getHostStub();
78                         JaceInterface stub = listOfTasks.getTaskIdOfRank(index)
79                                         .getHostStub();
80                         if (stub == null && stubVoisin == null) {
81                                 System.out.println("stub NULL");
82                         }
83
84                         String name = listOfTasks.getTaskIdOfRank(index).getHostName();
85
86                         try {
87
88                                 // whatever the case, uate the Register on each Node
89
90                                 // JaceSpawnerInterface ja=Register.Instance().getSpawnerStub();
91                                 // System.out.println("avant update register");
92
93                                 // 1 l'appli vient de commencer
94                                 stub.updateRegister(g, stubVoisin, requete);
95                                 // System.out.println("Envoi du registre a :"+index);
96                                 // g.viewAll();
97                                 // System.out.println("ancien registre");
98                                 // reg.viewAll();
99                                 // System.out.println("Le registre initial");
100                                 // Register.Instance().viewAll();
101                                 // System.out.println(" after update register");
102                                 // if updateRegister worked, that means Node is Alive
103                                 Node noeud = Register.Instance().getNodeOfStub(stub);
104                                 if (noeud != null) {
105                                         noeud.setAliveFlag(true);
106                                         noeud.setAliveTime();
107
108                                 }
109                         } catch (Exception e) {
110                                 System.err.println(name + " ("
111                                                 + LocalHost.Instance().resolve(name) + " rank=" + index
112                                                 + ") cannot be reached by UpdateRegisterThread   "
113                                                 + "     size : " + Register.Instance().getSize() + " :"
114                                                 + e);
115                                 try {
116                                         stub.suicide(" by Update");
117                                         // System.out.println("I suicide " + name + "\n\n");
118                                 } catch (Exception e2) {
119                                         // System.out.println("It is already Dead " + name +
120                                         // " : \n\n");
121                                 }
122                         }
123                 }
124
125         }
126
127         private Vector<Integer> getDependencies(int id, int jaceSize) {
128                 // get computing dependencies
129                 Vector<Integer> neighbors = new Vector<Integer>();
130                 int[] dep = null;
131                 // try{
132                 // if(tache==null)
133                 // System.out.println("tache = null");
134                 dep = tache.getDependencies(id);
135                 // }catch(Exception e)
136                 // {e.printStackTrace(System.out);}
137                 for (int z = 0; z < taille(dep); z++)
138                         neighbors.add(dep[z]);
139                 // System.out.println("la liste des voisins de calcul de: "+id);
140                 // for(int z=0;z<neighbors.size();z++)
141                 // System.out.print(((Integer)neighbors.elementAt(z)).intValue()+" ");
142                 // System.out.println();
143                 // get convergence neighbors
144                 int d = 0;
145                 while (Math.pow(2, d) < jaceSize) {
146                         if (id < Math.pow(2, d) && ((id + Math.pow(2, d)) < jaceSize))
147                                 if (!neighbors.contains((Object) ((int) (id + Math.pow(2, d)))))
148                                         neighbors.add((int) (id + Math.pow(2, d)));
149                         if (id < Math.pow(2, d + 1) && id >= Math.pow(2, d))
150                                 if (!neighbors.contains((Object) ((int) (id - Math.pow(2, d)))))
151                                         neighbors.add((int) (id - Math.pow(2, d)));
152                         d++;
153                 }
154                 // System.out.println("taille="+neighbors.size());
155                 // get backup neighbors
156                 int nb = reg.getNumBackupNeighbors();
157                 int rankOfBackTask;
158                 int tmp;
159                 for (int j = 1; j <= nb; j++) {
160                         // System.out.println("dans la boucle nb="+nb+" j="+j);
161                         // ------------ 1 - for backups "j + n" (to the right of j)
162                         rankOfBackTask = (id + j) % jaceSize;
163                         if (!neighbors.contains((Object) rankOfBackTask))
164                                 neighbors.add(rankOfBackTask);
165
166                         // ------------ 2 - for backups "j - n" (to the left of j)
167                         tmp = id - j;
168                         if (tmp >= 0) {
169                                 rankOfBackTask = tmp % jaceSize;
170                         } else {
171                                 rankOfBackTask = jaceSize - (Math.abs(tmp) % jaceSize);
172                         }
173                         if (!neighbors.contains((Object) rankOfBackTask))
174                                 neighbors.add(rankOfBackTask);
175                 }
176                 // System.out.println("taille="+neighbors.size());
177                 // adds itself
178                 neighbors.add(id);
179                 return neighbors;
180
181         }
182
183         public static int taille(int[] vect) {
184                 int taille = 0;
185                 int x = 0;
186                 while (x < vect.length && vect[x] >= 0) {
187                         taille++;
188                         x++;
189                 }
190                 return x;
191         }
192 }