Logo AND Algorithmique Numérique Distribuée

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