Logo AND Algorithmique Numérique Distribuée

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