Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some messages and comments.
[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         public synchronized int existNodeOfStub(JaceInterface stub) {
88                 // System.out.println("remote " + stub + "\n\n");
89                 if (stub == null) {
90                         System.err
91                                         .println("Stub = NULL in existNodeOfStub");
92                         return -1 ;
93                 }
94                 int existe = -1;
95                 int index = 0;
96                 while ((existe == -1) && (index < liste.size())) {
97
98                         if (stub.equals(((Node) liste.get(index)).getStub())) {
99                                 existe = index;
100                         } else
101                                 index++;
102                 }
103                 return existe;
104         }
105
106         public synchronized Node getNodeOfStub(JaceInterface stub) {
107                 int is = -1;
108                 if (liste.isEmpty()) {
109                         return null;
110                 } else {
111                         if (stub == null) {
112                                 System.err
113                                                 .println("In Register.getNodeOfStub : stub == null !");
114                         }
115                         is = existNodeOfStub(stub);
116                         if (is != -1) {
117                                 return (Node) liste.get(is);
118                         } else {
119                                 // System.out.println("stub : ce noeud existe poooooooooooo");
120                                 return null;
121                         }
122                 }
123         }
124
125         public synchronized Node getNodeOfName(String name) {
126                 int is = -1;
127                 if (liste.isEmpty()) {
128                         return null;
129                 } else {
130                         if (name == null)
131                                 System.err
132                                                 .println("In Register.getNodeOfName : name == null !");
133                         int index = 0;
134                         while ((is == -1) && (index < liste.size())) {
135
136                                 if (name.equals(((Node) liste.get(index)).getName())) {
137                                         is = index;
138                                 } else
139                                         index++;
140                         }
141
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
152         public synchronized Node getNodeAt(int index) {
153                 return (Node) liste.get(index);
154         }
155
156         public synchronized boolean removeNode(Node n) {
157                 return (liste.remove(n));
158         }
159
160         public synchronized boolean removeNodeOfName(String s) {
161                 boolean removed = false;
162                 int i = 0;
163                 // System.out.println("begin remove");
164                 // viewAll();
165                 while (i < liste.size() && !removed) {
166                         // System.out.println("i="+i+" s="+s);
167                         if (((Node) liste.get(i)).getName().equals(s)) {
168                                 liste.remove(i);
169                                 removed = true;
170                         } else
171                                 i++;
172                 }
173                 // System.out.println("end remove");
174                 return removed;
175         }
176
177         public synchronized int getSize() {
178                 return liste.size();
179         }
180
181         public synchronized Vector<Node> getListe() {
182                 return liste;
183         }
184
185         public synchronized void setParams(String[] myParams) {
186                 params = myParams;
187         }
188
189         public synchronized void setAppliName(String name) {
190                 appliName = name;
191         }
192
193         public synchronized void setSpawnerStub(JaceSpawnerInterface ref) {
194                 spawnerStub = ref;
195         }
196
197         public synchronized void replaceBy(Register newReg) {
198                 Instance = null;
199                 liste.clear();
200                 Instance = newReg;
201         }
202
203         public synchronized void setListeOfTasks(ListeTask myListe) {
204                 listeOfTasks = myListe;
205         }
206
207         public synchronized ListeTask getListeOfTasks() {
208                 return listeOfTasks;
209         }
210
211         public synchronized int getNumBackupNeighbors() {
212                 return numBackupNeighbors;
213         }
214
215         public void setNumBackupNeighbors(int i) {
216                 numBackupNeighbors = i;
217         }
218
219         public synchronized JaceSpawnerInterface getSpawnerStub() {
220                 return spawnerStub;
221         }
222
223         public synchronized void purge() {
224                 for (int i = 0; i < liste.size(); i++)
225                         if (((Node) liste.elementAt(i)).getOutputStream() != null) {
226                                 try {
227                                         ((Node) liste.elementAt(i)).getOutputStream().close();
228                                 } catch (Exception e) {
229                                         System.err.println("Unable to close outputStream :" + e);
230                                 }
231                         }
232                 liste.clear();
233                 Instance = null;
234         }
235
236         public synchronized String[] getParams() {
237                 return params;
238         }
239
240         public synchronized Vector<Node> getListOfNodes() {
241                 return liste;
242         }
243
244         public void setInstance() {
245                 Instance = this;
246         }
247
248         public void setNbOfTasks(int nbOfTasks) {
249                 numOfTasks = nbOfTasks;
250         }
251
252         public int getNbOfTasks() {
253                 return numOfTasks;
254         }
255
256         // affiche la liste des nom (ou IP si pas de nom) des machine du Register
257         public synchronized void viewAll() {
258                 String aff = "Nb of Daemons registered: " + getSize();
259                 Node noeud = null;
260                 String inter = "\n\t";
261                 String count = "";
262                 if (liste.isEmpty()) {
263                         System.err.println("My Register is empty !!!!!");
264                 } else {
265                         for (int i = 0; i < liste.size(); i++) {
266                                 noeud = getNodeAt(i);
267                                 count = (i + 1) + " : ";
268                                 if (i > 0) {
269                                         // inter = "\n";
270                                 }
271                                 if (noeud.getName() != null) {
272                                         aff += inter + count + noeud.getName() + /*
273                                                                                                                          * " avec appli : "
274                                                                                                                          * +
275                                                                                                                          * noeud.getAppliName
276                                                                                                                          * () +
277                                                                                                                          */", alive : "
278                                                         + noeud.getAliveFlag() + ", count : "
279                                                         + noeud.getNbOfBeats();
280                                 } else {
281                                         aff += inter + count + noeud.getIP() + " "
282                                                         + noeud.getName() + " with appli : "
283                                                         + noeud.getAppliName() + ", alive : "
284                                                         + noeud.getAliveFlag();
285
286                                 }
287                                 /*
288                                  * if (noeud.getWorkerStub() != null) { aff +=
289                                  * ", stub = PAS null"; } else { aff += ", stub = NULL"; }
290                                  */
291                         }
292                         aff += "";
293                         System.out.println(aff);
294                 }
295                 System.out.println("\n");
296         }
297         
298 }