Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
680c05d6929bf55fba358f1c986e6290baedc7ba
[jaceP2P.git] / src / jaceP2P / Register.java
1 package jaceP2P;
2
3 import java.util.Vector;
4
5
6 public class Register implements java.io.Serializable, java.lang.Cloneable {
7
8         private static final long serialVersionUID = 1L;
9
10         // a voir si on le met pas ds autre objet
11         final int MAX_COUNT_NOT_ALIVE = 3;
12
13         // attributes
14         public static Register Instance;
15         private String appliName; // name of the appli
16         private Vector<Node> liste; // list of the nodes of the Register
17         // private ListeTask listeOfTasks = null; //liste of the TaskId of the appli
18         // private int version=0;
19         private String[] params; // params of the appli
20         private int numBackupNeighbors = 3;
21         private int numOfTasks;
22         private JaceSpawnerInterface spawnerStub = null;
23         private ListeTask listeOfTasks = null; // liste of the TaskId of the appli
24
25         // constructors
26
27         public Register() {
28                 liste = new Vector<Node>();
29         }
30
31         public synchronized static Register Instance() {
32                 if (Instance == null) {
33                         Instance = new Register();
34                 }
35                 return Instance;
36         }
37
38         public synchronized String getAppliName() {
39                 return appliName;
40         }
41
42         public synchronized int existNode(Node node) {
43                 if (node == null) {
44                         System.err
45                                         .println("Node est null dans existNode");
46                         return -1 ;
47                 }
48                 int existe = -1;
49                 int index = 0;
50                 while ((existe == -1) && (index < liste.size())) {
51                         if (node.equals((Node) liste.get(index))) {
52                                 existe = index;
53                         } else
54                                 index++;
55                 }
56                 return existe;
57         }
58
59         /*
60          * public synchronized int getVersion(){ return version; }
61          * 
62          * public void setVersion(int version){ this.version=version; }
63          */
64
65         public synchronized void addNode(Node host) {
66                 int is = -1;
67                 if (host == null) {
68                         System.err
69                                         .println("In Register.addNode : host.getIP() == null !");
70                 }
71                 is = existNodeOfStub(host.getStub());
72                 if (is != -1) {
73                         System.out.println("This node exists yet, I replace it");
74                         // System.out.println("MERDE !!");
75                         // System.out.println("MERDE !!");
76                         // System.out.println("MERDE !!");
77                         // System.out.println("MERDE !!");
78                         // System.out.println("MERDE !!");
79                         // System.out.println("MERDE !!");
80                         // System.out.println("MERDE !!");
81                         liste.setElementAt(host, is);
82                 } else {
83                         liste.add(host);
84                 }
85         }
86
87         /**
88          * Add extra Nodes (for extra Spawners) at the beginning of the register.
89          * @param host The extra Node
90          */
91         public synchronized void addNodeBeg(Node host) {
92                 int is = -1;
93                 if (host == null) {
94                         System.err
95                                         .println("In Register.addNode : host.getIP() == null !");
96                 }
97                 is = existNodeOfStub(host.getStub());
98                 if (is != -1) {
99                         System.out.println("This node exists yet, I replace it");
100                         // System.out.println("MERDE !!");
101                         // System.out.println("MERDE !!");
102                         // System.out.println("MERDE !!");
103                         // System.out.println("MERDE !!");
104                         // System.out.println("MERDE !!");
105                         // System.out.println("MERDE !!");
106                         // System.out.println("MERDE !!");
107                         liste.setElementAt(host, is);
108                 } else {
109                         liste.add(0,host);
110                 }
111         }
112         
113         public synchronized int existNodeOfStub(JaceInterface stub) {
114                 // System.out.println("remote " + stub + "\n\n");
115                 if (stub == null) {
116                         System.err
117                                         .println("Stub = NULL in existNodeOfStub");
118                         return -1 ;
119                 }
120                 int existe = -1;
121                 int index = 0;
122                 while ((existe == -1) && (index < liste.size())) {
123
124                         if (stub.equals(((Node) liste.get(index)).getStub())) {
125                                 existe = index;
126                         } else
127                                 index++;
128                 }
129                 return existe;
130         }
131
132         public synchronized Node getNodeOfStub(JaceInterface stub) {
133                 int is = -1;
134                 if (liste.isEmpty()) {
135                         return null;
136                 } else {
137                         if (stub == null) {
138                                 System.err
139                                                 .println("In Register.getNodeOfStub : stub == null !");
140                         }
141                         is = existNodeOfStub(stub);
142                         if (is != -1) {
143                                 return (Node) liste.get(is);
144                         } else {
145                                 // System.out.println("stub : ce noeud existe poooooooooooo");
146                                 return null;
147                         }
148                 }
149         }
150
151         public synchronized Node getNodeOfName(String name) {
152                 int is = -1;
153                 if (liste.isEmpty()) {
154                         return null;
155                 } else {
156                         if (name == null)
157                                 System.err
158                                                 .println("In Register.getNodeOfName : name == null !");
159                         int index = 0;
160                         while ((is == -1) && (index < liste.size())) {
161
162                                 if (name.equals(((Node) liste.get(index)).getName())) {
163                                         is = index;
164                                 } else
165                                         index++;
166                         }
167
168                         if (is != -1) {
169                                 return (Node) liste.get(is);
170                         } else {
171                                 // System.out.println("stub : ce noeud existe poooooooooooo");
172                                 return null;
173                         }
174
175                 }
176         }
177
178         public synchronized Node getNodeAt(int index) {
179                 return (Node) liste.get(index);
180         }
181
182         public synchronized boolean removeNode(Node n) {
183                 return (liste.remove(n));
184         }
185
186         public synchronized boolean removeNodeOfName(String s) {
187                 boolean removed = false;
188                 int i = 0;
189                 // System.out.println("begin remove");
190                 // viewAll();
191                 while (i < liste.size() && !removed) {
192                         // System.out.println("i="+i+" s="+s);
193                         if (((Node) liste.get(i)).getName().equals(s)) {
194                                 liste.remove(i);
195                                 removed = true;
196                         } else
197                                 i++;
198                 }
199                 // System.out.println("end remove");
200                 return removed;
201         }
202
203         public synchronized int getSize() {
204                 return liste.size();
205         }
206
207         public synchronized Vector<Node> getListe() {
208                 return liste;
209         }
210
211         public synchronized void setParams(String[] myParams) {
212                 params = myParams;
213         }
214
215         public synchronized void setAppliName(String name) {
216                 appliName = name;
217         }
218
219         public synchronized void setSpawnerStub(JaceSpawnerInterface ref) {
220                 spawnerStub = ref;
221         }
222
223         public synchronized void replaceBy(Register newReg) {
224                 Instance = null;
225                 liste.clear();
226                 Instance = newReg;
227         }
228
229         public synchronized void setListeOfTasks(ListeTask myListe) {
230                 listeOfTasks = myListe;
231         }
232
233         public synchronized ListeTask getListeOfTasks() {
234                 return listeOfTasks;
235         }
236
237         public synchronized int getNumBackupNeighbors() {
238                 return numBackupNeighbors;
239         }
240
241         public void setNumBackupNeighbors(int i) {
242                 numBackupNeighbors = i;
243         }
244
245         public synchronized JaceSpawnerInterface getSpawnerStub() {
246                 return spawnerStub;
247         }
248
249         public synchronized void purge() {
250                 for (int i = 0; i < liste.size(); i++)
251                         if (((Node) liste.elementAt(i)).getOutputStream() != null) {
252                                 try {
253                                         ((Node) liste.elementAt(i)).getOutputStream().close();
254                                 } catch (Exception e) {
255                                         System.err.println("Unable to close outputStream :" + e);
256                                 }
257                         }
258                 liste.clear();
259                 Instance = null;
260         }
261
262         public synchronized String[] getParams() {
263                 return params;
264         }
265
266         public synchronized Vector<Node> getListOfNodes() {
267                 return liste;
268         }
269
270         public void setInstance() {
271                 Instance = this;
272         }
273
274         public void setNbOfTasks(int nbOfTasks) {
275                 numOfTasks = nbOfTasks;
276         }
277
278         public int getNbOfTasks() {
279                 return numOfTasks;
280         }
281
282         // affiche la liste des nom (ou IP si pas de nom) des machine du Register
283         public synchronized void viewAll() {
284                 String aff = "Nb of Daemons registered: " + getSize();
285                 Node noeud = null;
286                 String inter = "\n\t";
287                 String count = "";
288                 if (liste.isEmpty()) {
289                         System.err.println("My Register is empty !!!!!");
290                 } else {
291                         for (int i = 0; i < liste.size(); i++) {
292                                 noeud = getNodeAt(i);
293                                 count = (i + 1) + " : ";
294                                 if (i > 0) {
295                                         // inter = "\n";
296                                 }
297                                 if (noeud.getName() != null) {
298                                         aff += inter + count + noeud.getName() + /*
299                                                                                                                          * " avec appli : "
300                                                                                                                          * +
301                                                                                                                          * noeud.getAppliName
302                                                                                                                          * () +
303                                                                                                                          */", alive : "
304                                                         + noeud.getAliveFlag() + ", count : "
305                                                         + noeud.getNbOfBeats();
306                                 } else {
307                                         aff += inter + count + noeud.getIP() + " "
308                                                         + noeud.getName() + " with appli : "
309                                                         + noeud.getAppliName() + ", alive : "
310                                                         + noeud.getAliveFlag();
311
312                                 }
313                                 /*
314                                  * if (noeud.getWorkerStub() != null) { aff +=
315                                  * ", stub = PAS null"; } else { aff += ", stub = NULL"; }
316                                  */
317                         }
318                         aff += "";
319                         System.out.println(aff);
320                 }
321                 System.out.println("\n");
322         }
323         
324 }