Logo AND Algorithmique Numérique Distribuée

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