Logo AND Algorithmique Numérique Distribuée

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