Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1c4fdb6af3dd2118299483e5ef3d9c8ae07648b0
[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 int addNode( Node host ) 
66         {
67                 int is = -1 ;
68                 if( host == null ) 
69                 {
70                         System.err.println( "In Register.addNode : host == null !" ) ;
71                         return 1 ;
72                 }
73                 
74                 is = existNodeOfStub( host.getStub() ) ;
75                 
76                 if (is != -1) {
77                         System.out.println("This node exists yet, I replace it");
78
79                         liste.setElementAt(host, is);
80                 } else {
81                         liste.add(host);
82                 }
83                 
84                 return 0 ;
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         {
188                 boolean removed = false ;
189                 int i = 0 ;
190                 // System.out.println("begin remove");
191                 // viewAll();
192                 while( i < liste.size() && ! removed ) 
193                 {
194                         // System.out.println("i="+i+" s="+s);
195                         if( ((Node) liste.get(i)).getName().equals(s) ) 
196                         {
197                                 liste.remove( i ) ;
198                                 removed = true ;
199                         } else {
200                                 i++ ;
201                         }
202                 }
203                 // System.out.println("end remove");
204                 return removed ;
205         }
206
207         public synchronized int getSize() {
208                 return liste.size();
209         }
210
211         public synchronized Vector<Node> getListe() {
212                 return liste;
213         }
214
215         public synchronized void setParams(String[] myParams) {
216                 params = myParams;
217         }
218
219         public synchronized void setAppliName(String name) {
220                 appliName = name;
221         }
222
223         public synchronized void setSpawnerStub(JaceSpawnerInterface ref) {
224                 spawnerStub = ref;
225         }
226
227         public synchronized void replaceBy(Register newReg) {
228                 Instance = null;
229                 liste.clear();
230                 Instance = newReg;
231         }
232
233         public synchronized void setListeOfTasks(ListeTask myListe) {
234                 listeOfTasks = myListe;
235         }
236
237         public synchronized ListeTask getListeOfTasks() {
238                 return listeOfTasks;
239         }
240
241         public synchronized int getNumBackupNeighbors() {
242                 return numBackupNeighbors;
243         }
244
245         public void setNumBackupNeighbors(int i) {
246                 numBackupNeighbors = i;
247         }
248
249         public synchronized JaceSpawnerInterface getSpawnerStub() {
250                 return spawnerStub;
251         }
252
253         public synchronized void purge() {
254                 for (int i = 0; i < liste.size(); i++)
255                         if (((Node) liste.elementAt(i)).getOutputStream() != null) {
256                                 try {
257                                         ((Node) liste.elementAt(i)).getOutputStream().close();
258                                 } catch (Exception e) {
259                                         System.err.println("Unable to close outputStream :" + e);
260                                 }
261                         }
262                 liste.clear();
263                 Instance = null;
264         }
265
266         public synchronized String[] getParams() {
267                 return params;
268         }
269
270         public synchronized Vector<Node> getListOfNodes() {
271                 return liste;
272         }
273
274         public void setInstance() {
275                 Instance = this;
276         }
277
278         public void setNbOfTasks(int nbOfTasks) {
279                 numOfTasks = nbOfTasks;
280         }
281
282         public int getNbOfTasks() {
283                 return numOfTasks;
284         }
285
286         // affiche la liste des nom (ou IP si pas de nom) des machine du Register
287         public synchronized void viewAll() {
288                 String aff = "Nb of Daemons registered: " + getSize();
289                 Node noeud = null;
290                 String inter = "\n\t";
291                 String count = "";
292                 if (liste.isEmpty()) {
293                         System.err.println("My Register is empty !!!!!");
294                 } else {
295                         for (int i = 0; i < liste.size(); i++) {
296                                 noeud = getNodeAt(i);
297                                 count = (i + 1) + " : ";
298                                 if (i > 0) {
299                                         // inter = "\n";
300                                 }
301                                 if (noeud.getName() != null) {
302                                         aff += inter + count + noeud.getName() + /*
303                                                                                                                          * " avec appli : "
304                                                                                                                          * +
305                                                                                                                          * noeud.getAppliName
306                                                                                                                          * () +
307                                                                                                                          */", alive : "
308                                                         + noeud.getAliveFlag() + ", count : "
309                                                         + noeud.getNbOfBeats();
310                                 } else {
311                                         aff += inter + count + noeud.getIP() + " "
312                                                         + noeud.getName() + " with appli : "
313                                                         + noeud.getAppliName() + ", alive : "
314                                                         + noeud.getAliveFlag();
315
316                                 }
317                                 /*
318                                  * if (noeud.getWorkerStub() != null) { aff +=
319                                  * ", stub = PAS null"; } else { aff += ", stub = NULL"; }
320                                  */
321                         }
322                         aff += "";
323                         System.out.println(aff);
324                 }
325                 System.out.println("\n");
326         }
327         
328 }