Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[jaceP2P.git] / src / jaceP2P / JaceServer.java
1 package jaceP2P;
2
3 import java.rmi.RemoteException;
4 import java.rmi.server.UnicastRemoteObject;
5 import java.util.ArrayList;
6 import java.util.Calendar;
7 import java.util.GregorianCalendar;
8
9
10 public class JaceServer extends UnicastRemoteObject implements JaceInterface {
11
12         private static final long serialVersionUID = 1L;
13
14         public JaceServer() throws RemoteException {
15                 super();
16         }
17
18         public void reconnectSuperNode() throws RemoteException {
19                 JaceDaemon.Instance().reconnectSuperNode();
20         }
21
22         // when a daemon replaces a new one it asks for the backups
23         public void getBackupForNewNode(int rank) throws RemoteException {
24                 JaceSession.Instance().getTaskObject().getBackupForNewNode(rank);
25         }
26
27         public void setSpawner(JaceSpawnerInterface spawnerStub)
28                         throws RemoteException {
29                 Register.Instance().setSpawnerStub(spawnerStub);
30         }
31
32 //      public JaceSpawnerInterface transformIntoSpawner(String[] params,
33 //                      String appliName, Register reg, int nbTasks,
34 //                      JaceSuperNodeInterface snodeStub, int rank, int heartTime, int tag,
35 //                      int nbdc, int nbsdc, int nbDaemonPerSpawner, int nbDaemonPerThread)
36 //                      throws RemoteException {
37 //              System.out.println("Beginning the transformation ...");
38 //              new JaceSpawner(params, appliName, reg, nbTasks,
39 //                              snodeStub, rank, heartTime, tag, nbdc, nbsdc,
40 //                              nbDaemonPerSpawner, nbDaemonPerThread);
41 //              HeartBeatThread.Instance().kill();
42 //              return Register.Instance().getSpawnerStub();
43 //      }
44         
45         
46         public JaceSpawnerInterface transformIntoSpawner(String[] params,
47                         String appliName, Register reg, int nbTasks,
48                         JaceSuperNodeInterface snodeStub, int rank, int heartTime, int tag,
49                         int nbdc, int nbsdc, int nbDaemonPerSpawner, int nbDaemonPerThread,
50                         String idAlgo)
51                         throws RemoteException {
52                 System.out.println("Beginning the transformation ...");
53                 new JaceSpawner(params, appliName, reg, nbTasks,
54                                 snodeStub, rank, heartTime, tag, nbdc, nbsdc,
55                                 nbDaemonPerSpawner, nbDaemonPerThread, idAlgo);
56                 HeartBeatThread.Instance().kill();
57                 return Register.Instance().getSpawnerStub();
58         }
59
60         public synchronized int updateRegister(Register newReg,
61                         JaceInterface voisinStub, int req) throws RemoteException {
62
63                 // If beginning of appli, tell nodes to beat the next neighbor
64                 // and no longer the SuperNode
65
66                 System.out.println("I change to ping a Daemon");
67                 HeartBeatThread.Instance().setServer((Object) voisinStub);
68
69                 Calendar cal = new GregorianCalendar();
70                 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
71                                 + cal.get(Calendar.SECOND));
72                 System.out.println("name of spawner: "
73                                 + newReg.getSpawnerStub().getName());
74                 // 1 - replace the old Register by the new one
75                 System.out.println(" \n\n             NEW REGISTER            \n\n");
76                 // if(Register.Instance().getVersion()<=newReg.getVersion()){
77                 System.out.println("Replacing Register ...");
78                 Register.Instance().replaceBy(newReg);
79                 Register.Instance().viewAll();
80                 // }
81
82                 // initialise the BackupsManager if not exists (i.e. myRank = -1)
83                 // which means that not init so first reload
84                 // try {
85                 // if (BackupsManager.Instance().getMyRank() == -1) {
86                 // 1 - create the BackupsManager
87                 // 2 - get an eventual Backup for my Task
88                 // 3 - restart it if any
89                 // BackupsManager.Instance().initialize();
90
91                 new InitiateAppli(req).start();
92                 // }
93                 return 0;
94         }
95
96         public int updateRegister( Node oldNode, Node node, int rank ) throws RemoteException {
97
98                 // if(tag==1)
99                 // HeartBeatThread.Instance().setServer((Object)node.getStub());
100                 // 1 - replace the old Register by the new one
101                 Calendar cal = new GregorianCalendar();
102                 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
103                                 + cal.get(Calendar.SECOND));
104                 System.out.println("Modify REGISTER");
105                 try {
106                         boolean b = false ;
107                         if( oldNode != null )
108                         {
109                                 System.out.println("Old node name=" + oldNode.getName());
110                                 b = Register.Instance().removeNodeOfName(oldNode.getName());
111                         }
112                         if( b )
113                         {
114                                 System.out.println("Old node removed !");
115                         } else {
116                                 System.out.println("Did not find the old Node !");
117                         }
118
119                         if( node != null )
120                         {
121                                 Register.Instance().addNode(node);
122                         } else {
123                                 System.err.println( "The new node is NULL !" ) ;
124                                 return 1 ;
125                         }
126 //                      Register.Instance().viewAll();
127                         TaskId myTaskId = null;
128 //                      myTaskId = Register.Instance().getListeOfTasks()
129 //                                      .getTaskIdOfHostStub(oldNode.getStub());
130                         myTaskId = Register.Instance().getListeOfTasks()
131                         .getTaskIdOfRank( rank ) ;
132                         myTaskId.setHostIP(node.getIP());
133                         myTaskId.setHostName(node.getName());
134                         // Register.Instance().setVersion(version);
135                         myTaskId.setHostStub(node.getStub());
136                         
137                         return 0 ;
138
139                 } catch (Exception e) {
140                         System.err.println("Error in updateregister :" + e);
141                         return 1 ;
142                         // e.printStackTrace();
143                 }
144         }
145
146         public ArrayList<Integer> getIterationOfBackup(int remoteRank, int tag)
147                         throws RemoteException {
148                 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
149                                 tag);
150                 ArrayList<Integer> result = b.getIterationStep();
151                 return result;
152         }
153
154         public Backup getRemoteBackup(int remoteRank, int tag)
155                         throws RemoteException {
156                 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
157                                 tag);
158                 return b;
159         }
160
161         public void suicide(String debugMsg) throws RemoteException {
162 //              System.out.println("suiciiiiiiiiiiiide : "/* + debugMsg */);
163                 new ReinitDaemon().start();
164         }
165
166         public void iSendYou(Message msg) throws RemoteException {
167                 // on met le Message ds le MsgQueue correspondant ma tache
168                 // DS CAS ASYNC, on supprime un msg de meme tag ET meme sender
169                 // System.out.println("get msg from MSGQueue sent from "+msg.getSender().getRank());
170                 MsgQueue.Instance().add(msg);
171         }
172
173         public int getTimeStep() throws RemoteException {
174                 return JaceSession.Instance().getTaskObject().timeStep;
175         }
176
177         public void saveTask(int rank, byte[] tsk, int iteration, int timeStep,
178                         String appliName, int tag) throws RemoteException {
179
180                 while (JaceSession.Instance().getTaskObject().reloading == true)
181                         try {
182                                 Thread.sleep(10);
183                         } catch (Exception e) {
184                         }
185                 Backup back = BackupsManager.Instance().getBackupTaskOfRank(rank, tag);
186                 if (back != null) {
187                         if (back.getStep() < timeStep
188                                         || (back.getStep() == timeStep && back.getIteration() < iteration)) {
189                                 back.setIteration(iteration);
190                                 // System.out.print("\n\n they put in me a backup with iter="+back.getIteration()+" for the node of rank="+rank+"\n\n");
191                                 back.setStep(timeStep);
192                                 back.setData(tsk);
193
194                                 /*
195                                  * try{
196                                  * 
197                                  * TaskId recev=null; recev =
198                                  * Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
199                                  * JaceInterface stub; stub=recev.getHostStub();
200                                  * stub.setSaved(true);
201                                  * 
202                                  * }catch(Exception e){
203                                  * System.out.println("unable to acknowledge receiving the backup :"
204                                  * +e); }
205                                  */
206                         }
207                 } else {
208                         System.out.println("No task at this rank");
209                         // TODO : what to do ?
210                 }
211
212         }
213
214         public void setSaved(boolean bool) throws RemoteException {
215                 JaceSession.Instance().getTaskObject().setSaved(bool);
216         }
217
218         public boolean getReloading() throws RemoteException {
219                 return JaceSession.Instance().getTaskObject().reloading;
220         }
221
222         public int getVerifNum() throws RemoteException {
223                 return JaceSession.Instance().getTaskObject().verifNum;
224         }
225
226         public String getState() throws RemoteException {
227                 return JaceSession.Instance().getTaskObject().state;
228         }
229
230         public void initializeVerif(int tag) throws RemoteException {
231                 JaceSession.Instance().getTaskObject().initializeVerif(tag);
232         }
233
234         public void savOrFinOrRest(int tag, int step, boolean verd,
235                         ArrayList<Object> recievedValue) throws RemoteException {
236                 JaceSession.Instance().getTaskObject().savOrFinOrRest(tag, step, verd,
237                                 recievedValue);
238         }
239
240         public synchronized boolean setNbNeighboursNotConv(int tag, int idNeigh,
241                         int neighborTimeStep) throws RemoteException {
242                 return JaceSession.Instance().getTaskObject().setNbNeighboursNotConv(
243                                 tag, idNeigh, neighborTimeStep);
244         }
245
246         public synchronized int getNbNeighboursNotConv() throws RemoteException {
247                 return JaceSession.Instance().getTaskObject().nb_not_recv;
248         }
249
250         public synchronized void response(int neighId, int tag, int response,
251                         ArrayList<Object> recievedValue) throws RemoteException {
252                 JaceSession.Instance().getTaskObject().response(neighId, tag, response,
253                                 recievedValue);
254         }
255
256         public boolean ping() throws RemoteException {
257                 // System.out.println("pingggggggggggg");
258                 return true;
259         }
260
261         public void updateHeart(JaceInterface stub) throws RemoteException {
262                 System.out.println("I change to ping a Daemon");
263                 HeartBeatThread.Instance().setServer((Object) stub);
264         }
265
266         public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
267                 System.out.println("I change to ping a superNode");
268                 HeartBeatThread.Instance().setServer((Object) stub);
269         }
270
271         public long beating(JaceInterface workerStub) throws RemoteException {
272                 Node noeud = Register.Instance().getNodeOfStub(workerStub);
273                 if (noeud != null) {
274                         noeud.setAliveFlag(true);
275                         noeud.setAliveTime();
276                         // System.out.println("beating$$$$$$$$$");
277                         return noeud.getAliveTime();
278
279                 } else {
280                         // System.out.println("le noeud " + workerIP +
281                         // " est pas ds la liste des noeuds connectes");
282                         return -1;
283                 }
284         }
285
286         public void setScanning(boolean bool) throws RemoteException {
287                 // if(bool==true){
288                 try {
289                         ScanThread.Instance().setScanning(bool);
290                         synchronized (ScanThread.Instance()) {
291                                 ScanThread.Instance().notify();
292                         }
293                 } catch (Exception e) {
294                         System.err.println("Error in setScanning: " + e);
295                 }
296                 // System.out.println("before notify!!!!!!!!!");
297                 // try{ScanThread.Instance().notify();}
298                 // catch(Exception e){
299                 // System.out.println("error in notify:"+e);
300                 // }
301                 // System.out.println("notify!!!!!!!!!");
302                 // }
303                 // else
304                 // ScanThread.Instance().setScanning(false);
305         }
306
307         class InitiateAppli extends Thread {
308                 int req;
309
310                 public InitiateAppli(int req) {
311                         this.req = req;
312                 }
313
314                 public void run() {
315                         BackupsManager.Instance().initialize( req ) ;
316
317                 }
318         }
319
320         class ReinitDaemon extends Thread {
321
322                 public ReinitDaemon() {}
323
324                 public void run() {
325                         JaceDaemon.Instance().reinitDaemon() ;
326                 }
327         }
328
329         @Override
330         public void suicide2( String mes ) throws RemoteException 
331         {
332                 System.out.println( "Killed because: " + mes ) ;
333                 
334                 try{ 
335                 UnicastRemoteObject.unexportObject( this, true ) ; 
336             } 
337             catch( Exception e ) {
338                 System.err.println( "Unable to unexport myself: " + e ) ;
339             } 
340                                   
341                 System.exit( 0 ) ;
342         }
343 }