Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modification of mapping algorithm identifier mechanisms.
[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 void updateRegister(Node oldNode, Node node) 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                         // System.out.println("blablablablabalablablabalbalab");
106                         System.out.println("oldName=" + oldNode.getName());
107                         boolean b = Register.Instance().removeNodeOfName(oldNode.getName());
108                         if (b)
109                                 System.out.println("removed old Node!!!!!");
110                         else
111                                 System.out.println("didn't find old Node!!!!");
112
113                         Register.Instance().addNode(node);
114                         Register.Instance().viewAll();
115                         TaskId myTaskId = null;
116                         myTaskId = Register.Instance().getListeOfTasks()
117                                         .getTaskIdOfHostStub(oldNode.getStub());
118                         myTaskId.setHostIP(node.getIP());
119                         myTaskId.setHostName(node.getName());
120                         // Register.Instance().setVersion(version);
121                         myTaskId.setHostStub(node.getStub());
122
123                 } catch (Exception e) {
124                         System.err.println("Error in updateregister :" + e);
125                         // e.printStackTrace();
126                 }
127         }
128
129         public Vector<?> getIterationOfBackup(int remoteRank, int tag)
130                         throws RemoteException {
131                 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
132                                 tag);
133                 Vector<?> result = b.getIterationStep();
134                 return result;
135         }
136
137         public Backup getRemoteBackup(int remoteRank, int tag)
138                         throws RemoteException {
139                 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
140                                 tag);
141                 return b;
142         }
143
144         public void suicide(String debugMsg) throws RemoteException {
145 //              System.out.println("suiciiiiiiiiiiiide : "/* + debugMsg */);
146                 new ReinitDaemon().start();
147         }
148
149         public void iSendYou(Message msg) throws RemoteException {
150                 // on met le Message ds le MsgQueue correspondant ma tache
151                 // DS CAS ASYNC, on supprime un msg de meme tag ET meme sender
152                 // System.out.println("get msg from MSGQueue sent from "+msg.getSender().getRank());
153                 MsgQueue.Instance().add(msg);
154         }
155
156         public int getTimeStep() throws RemoteException {
157                 return JaceSession.Instance().getTaskObject().timeStep;
158         }
159
160         public void saveTask(int rank, byte[] tsk, int iteration, int timeStep,
161                         String appliName, int tag) throws RemoteException {
162
163                 while (JaceSession.Instance().getTaskObject().reloading == true)
164                         try {
165                                 Thread.sleep(10);
166                         } catch (Exception e) {
167                         }
168                 Backup back = BackupsManager.Instance().getBackupTaskOfRank(rank, tag);
169                 if (back != null) {
170                         if (back.getStep() < timeStep
171                                         || (back.getStep() == timeStep && back.getIteration() < iteration)) {
172                                 back.setIteration(iteration);
173                                 // System.out.print("\n\n they put in me a backup with iter="+back.getIteration()+" for the node of rank="+rank+"\n\n");
174                                 back.setStep(timeStep);
175                                 back.setData(tsk);
176
177                                 /*
178                                  * try{
179                                  * 
180                                  * TaskId recev=null; recev =
181                                  * Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
182                                  * JaceInterface stub; stub=recev.getHostStub();
183                                  * stub.setSaved(true);
184                                  * 
185                                  * }catch(Exception e){
186                                  * System.out.println("unable to acknowledge receiving the backup :"
187                                  * +e); }
188                                  */
189                         }
190                 } else {
191                         System.out.println("No task at this rank");
192                         // TODO : what to do ?
193                 }
194
195         }
196
197         public void setSaved(boolean bool) throws RemoteException {
198                 JaceSession.Instance().getTaskObject().setSaved(bool);
199         }
200
201         public boolean getReloading() throws RemoteException {
202                 return JaceSession.Instance().getTaskObject().reloading;
203         }
204
205         public int getVerifNum() throws RemoteException {
206                 return JaceSession.Instance().getTaskObject().verifNum;
207         }
208
209         public String getState() throws RemoteException {
210                 return JaceSession.Instance().getTaskObject().state;
211         }
212
213         public void initializeVerif(int tag) throws RemoteException {
214                 JaceSession.Instance().getTaskObject().initializeVerif(tag);
215         }
216
217         public void savOrFinOrRest(int tag, int step, boolean verd,
218                         Vector<?> recievedValue) throws RemoteException {
219                 JaceSession.Instance().getTaskObject().savOrFinOrRest(tag, step, verd,
220                                 recievedValue);
221         }
222
223         public synchronized boolean setNbNeighboursNotConv(int tag, int idNeigh,
224                         int neighborTimeStep) throws RemoteException {
225                 return JaceSession.Instance().getTaskObject().setNbNeighboursNotConv(
226                                 tag, idNeigh, neighborTimeStep);
227         }
228
229         public synchronized int getNbNeighboursNotConv() throws RemoteException {
230                 return JaceSession.Instance().getTaskObject().nb_not_recv;
231         }
232
233         public synchronized void response(int neighId, int tag, int response,
234                         Vector<?> recievedValue) throws RemoteException {
235                 JaceSession.Instance().getTaskObject().response(neighId, tag, response,
236                                 recievedValue);
237         }
238
239         public boolean ping() throws RemoteException {
240                 // System.out.println("pingggggggggggg");
241                 return true;
242         }
243
244         public void updateHeart(JaceInterface stub) throws RemoteException {
245                 System.out.println("I change to ping a Daemon");
246                 HeartBeatThread.Instance().setServer((Object) stub);
247         }
248
249         public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
250                 System.out.println("I change to ping a superNode");
251                 HeartBeatThread.Instance().setServer((Object) stub);
252         }
253
254         public long beating(JaceInterface workerStub) throws RemoteException {
255                 Node noeud = Register.Instance().getNodeOfStub(workerStub);
256                 if (noeud != null) {
257                         noeud.setAliveFlag(true);
258                         noeud.setAliveTime();
259                         // System.out.println("beating$$$$$$$$$");
260                         return noeud.getAliveTime();
261
262                 } else {
263                         // System.out.println("le noeud " + workerIP +
264                         // " est pas ds la liste des noeuds connectes");
265                         return -1;
266                 }
267         }
268
269         public void setScanning(boolean bool) throws RemoteException {
270                 // if(bool==true){
271                 try {
272                         ScanThread.Instance().setScanning(bool);
273                         synchronized (ScanThread.Instance()) {
274                                 ScanThread.Instance().notify();
275                         }
276                 } catch (Exception e) {
277                         System.err.println("Error in setScanning: " + e);
278                 }
279                 // System.out.println("before notify!!!!!!!!!");
280                 // try{ScanThread.Instance().notify();}
281                 // catch(Exception e){
282                 // System.out.println("error in notify:"+e);
283                 // }
284                 // System.out.println("notify!!!!!!!!!");
285                 // }
286                 // else
287                 // ScanThread.Instance().setScanning(false);
288         }
289
290         class InitiateAppli extends Thread {
291                 int req;
292
293                 public InitiateAppli(int req) {
294                         this.req = req;
295                 }
296
297                 public void run() {
298                         BackupsManager.Instance().initialize(req);
299
300                 }
301         }
302
303         class ReinitDaemon extends Thread {
304
305                 public ReinitDaemon() {
306                 }
307
308                 public void run() {
309                         JaceDaemon.Instance().reinitDaemon();
310                 }
311         }
312 }