Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
df8c03613d886353cb89c4029605ac184d4f4c96
[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                                 int ret = 1 ;
95                                 int loop = 0 ;
96                                 int LOOP_MAX = 3 ;
97                                 
98                                 while( ret == 1 && loop < LOOP_MAX )
99                                 {
100                                         ret = stub.updateRegister(g, stubVoisin, requete);
101                                         loop++ ;
102                                 }
103                                 
104                                 if( loop == LOOP_MAX )
105                                 {
106                                         System.err.println( "Problem during register update!" ) ;
107                                 } else {
108                                 // System.out.println("Envoi du registre a :"+index);
109                                 // g.viewAll();
110                                 // System.out.println("ancien registre");
111                                 // reg.viewAll();
112                                 // System.out.println("Le registre initial");
113                                 // Register.Instance().viewAll();
114                                 // System.out.println(" after update register");
115                                 // if updateRegister worked, that means Node is Alive
116                                         Node noeud = Register.Instance().getNodeOfStub(stub);
117                                         if (noeud != null) {
118                                                 noeud.setAliveFlag(true);
119                                                 noeud.setAliveTime();
120                                         }
121                                 }
122                         } catch (Exception e) {
123                                 System.err.println(name + " ("
124                                                 + LocalHost.Instance().resolve(name) + " rank=" + index
125                                                 + ") cannot be reached by UpdateRegisterThread   "
126                                                 + "     size : " + Register.Instance().getSize() + " :"
127                                                 + e);
128                                 try {
129                                         stub.suicide(" by Update");
130                                         // System.out.println("I suicide " + name + "\n\n");
131                                 } catch (Exception e2) {
132                                         // System.out.println("It is already Dead " + name +
133                                         // " : \n\n");
134                                 }
135                         }
136                 }
137
138         }
139
140         private Vector<Integer> getDependencies(int id, int jaceSize) {
141                 // get computing dependencies
142                 Vector<Integer> neighbors = new Vector<Integer>();
143                 int[] dep = null;
144                 // try{
145                 // if(tache==null)
146                 // System.out.println("tache = null");
147                 dep = tache.getDependencies(id);
148                 // }catch(Exception e)
149                 // {e.printStackTrace(System.out);}
150                 for (int z = 0; z < taille(dep); z++)
151                         neighbors.add(dep[z]);
152                 // System.out.println("la liste des voisins de calcul de: "+id);
153                 // for(int z=0;z<neighbors.size();z++)
154                 // System.out.print(((Integer)neighbors.elementAt(z)).intValue()+" ");
155                 // System.out.println();
156                 // get convergence neighbors
157                 int d = 0;
158                 while (Math.pow(2, d) < jaceSize) {
159                         if (id < Math.pow(2, d) && ((id + Math.pow(2, d)) < jaceSize))
160                                 if (!neighbors.contains((Object) ((int) (id + Math.pow(2, d)))))
161                                         neighbors.add((int) (id + Math.pow(2, d)));
162                         if (id < Math.pow(2, d + 1) && id >= Math.pow(2, d))
163                                 if (!neighbors.contains((Object) ((int) (id - Math.pow(2, d)))))
164                                         neighbors.add((int) (id - Math.pow(2, d)));
165                         d++;
166                 }
167                 // System.out.println("taille="+neighbors.size());
168                 // get backup neighbors
169                 int nb = reg.getNumBackupNeighbors();
170                 int rankOfBackTask;
171                 int tmp;
172                 for (int j = 1; j <= nb; j++) {
173                         // System.out.println("dans la boucle nb="+nb+" j="+j);
174                         // ------------ 1 - for backups "j + n" (to the right of j)
175                         rankOfBackTask = (id + j) % jaceSize;
176                         if (!neighbors.contains((Object) rankOfBackTask))
177                                 neighbors.add(rankOfBackTask);
178
179                         // ------------ 2 - for backups "j - n" (to the left of j)
180                         tmp = id - j;
181                         if (tmp >= 0) {
182                                 rankOfBackTask = tmp % jaceSize;
183                         } else {
184                                 rankOfBackTask = jaceSize - (Math.abs(tmp) % jaceSize);
185                         }
186                         if (!neighbors.contains((Object) rankOfBackTask))
187                                 neighbors.add(rankOfBackTask);
188                 }
189                 // System.out.println("taille="+neighbors.size());
190                 // adds itself
191                 neighbors.add(id);
192                 return neighbors;
193
194         }
195
196         public static int taille(int[] vect) {
197                 int taille = 0;
198                 int x = 0;
199                 while (x < vect.length && vect[x] >= 0) {
200                         taille++;
201                         x++;
202                 }
203                 return x;
204         }
205 }