Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d63f6ee19c56161e974e4c0363762ae944f3c9e1
[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         public synchronized int updateRegister(Register newReg,
45                         JaceInterface voisinStub, int req) throws RemoteException {
46
47                 // If beginning of appli, tell nodes to beat the next neighbor
48                 // and no longer the SuperNode
49
50                 System.out.println("I change to ping a Daemon");
51                 HeartBeatThread.Instance().setServer((Object) voisinStub);
52
53                 Calendar cal = new GregorianCalendar();
54                 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
55                                 + cal.get(Calendar.SECOND));
56                 System.out.println("name of spawner: "
57                                 + newReg.getSpawnerStub().getName());
58                 // 1 - replace the old Register by the new one
59                 System.out.println(" \n\n             NEW REGISTER            \n\n");
60                 // if(Register.Instance().getVersion()<=newReg.getVersion()){
61                 System.out.println("Replacing Register ...");
62                 Register.Instance().replaceBy(newReg);
63                 Register.Instance().viewAll();
64                 // }
65
66                 // initialise the BackupsManager if not exists (i.e. myRank = -1)
67                 // which means that not init so first reload
68                 // try {
69                 // if (BackupsManager.Instance().getMyRank() == -1) {
70                 // 1 - create the BackupsManager
71                 // 2 - get an eventual Backup for my Task
72                 // 3 - restart it if any
73                 // BackupsManager.Instance().initialize();
74
75                 new InitiateAppli(req).start();
76                 // }
77                 return 0;
78         }
79
80         public void updateRegister(Node oldNode, Node node) throws RemoteException {
81
82                 // if(tag==1)
83                 // HeartBeatThread.Instance().setServer((Object)node.getStub());
84                 // 1 - replace the old Register by the new one
85                 Calendar cal = new GregorianCalendar();
86                 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
87                                 + cal.get(Calendar.SECOND));
88                 System.out.println("Modify  REGISTER");
89                 try {
90                         // System.out.println("blablablablabalablablabalbalab");
91                         System.out.println("oldName=" + oldNode.getName());
92                         boolean b = Register.Instance().removeNodeOfName(oldNode.getName());
93                         if (b)
94                                 System.out.println("removed old Node!!!!!");
95                         else
96                                 System.out.println("didn't find old Node!!!!");
97
98                         Register.Instance().addNode(node);
99                         Register.Instance().viewAll();
100                         TaskId myTaskId = null;
101                         myTaskId = Register.Instance().getListeOfTasks()
102                                         .getTaskIdOfHostStub(oldNode.getStub());
103                         myTaskId.setHostIP(node.getIP());
104                         myTaskId.setHostName(node.getName());
105                         // Register.Instance().setVersion(version);
106                         myTaskId.setHostStub(node.getStub());
107
108                 } catch (Exception e) {
109                         System.err.println("Error in updateregister :" + e);
110                         // e.printStackTrace();
111                 }
112         }
113
114         public Vector<?> getIterationOfBackup(int remoteRank, int tag)
115                         throws RemoteException {
116                 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
117                                 tag);
118                 Vector<?> result = b.getIterationStep();
119                 return result;
120         }
121
122         public Backup getRemoteBackup(int remoteRank, int tag)
123                         throws RemoteException {
124                 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
125                                 tag);
126                 return b;
127         }
128
129         public void suicide(String debugMsg) throws RemoteException {
130 //              System.out.println("suiciiiiiiiiiiiide : "/* + debugMsg */);
131                 new ReinitDaemon().start();
132         }
133
134         public void iSendYou(Message msg) throws RemoteException {
135                 // on met le Message ds le MsgQueue correspondant ma tache
136                 // DS CAS ASYNC, on supprime un msg de meme tag ET meme sender
137                 // System.out.println("get msg from MSGQueue sent from "+msg.getSender().getRank());
138                 MsgQueue.Instance().add(msg);
139         }
140
141         public int getTimeStep() throws RemoteException {
142                 return JaceSession.Instance().getTaskObject().timeStep;
143         }
144
145         public void saveTask(int rank, byte[] tsk, int iteration, int timeStep,
146                         String appliName, int tag) throws RemoteException {
147
148                 while (JaceSession.Instance().getTaskObject().reloading == true)
149                         try {
150                                 Thread.sleep(10);
151                         } catch (Exception e) {
152                         }
153                 Backup back = BackupsManager.Instance().getBackupTaskOfRank(rank, tag);
154                 if (back != null) {
155                         if (back.getStep() < timeStep
156                                         || (back.getStep() == timeStep && back.getIteration() < iteration)) {
157                                 back.setIteration(iteration);
158                                 // System.out.print("\n\n they put in me a backup with iter="+back.getIteration()+" for the node of rank="+rank+"\n\n");
159                                 back.setStep(timeStep);
160                                 back.setData(tsk);
161
162                                 /*
163                                  * try{
164                                  * 
165                                  * TaskId recev=null; recev =
166                                  * Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
167                                  * JaceInterface stub; stub=recev.getHostStub();
168                                  * stub.setSaved(true);
169                                  * 
170                                  * }catch(Exception e){
171                                  * System.out.println("unable to acknowledge receiving the backup :"
172                                  * +e); }
173                                  */
174                         }
175                 } else {
176                         System.out.println("No task at this rank");
177                         // TODO : what to do ?
178                 }
179
180         }
181
182         public void setSaved(boolean bool) throws RemoteException {
183                 JaceSession.Instance().getTaskObject().setSaved(bool);
184         }
185
186         public boolean getReloading() throws RemoteException {
187                 return JaceSession.Instance().getTaskObject().reloading;
188         }
189
190         public int getVerifNum() throws RemoteException {
191                 return JaceSession.Instance().getTaskObject().verifNum;
192         }
193
194         public String getState() throws RemoteException {
195                 return JaceSession.Instance().getTaskObject().state;
196         }
197
198         public void initializeVerif(int tag) throws RemoteException {
199                 JaceSession.Instance().getTaskObject().initializeVerif(tag);
200         }
201
202         public void savOrFinOrRest(int tag, int step, boolean verd,
203                         Vector<?> recievedValue) throws RemoteException {
204                 JaceSession.Instance().getTaskObject().savOrFinOrRest(tag, step, verd,
205                                 recievedValue);
206         }
207
208         public synchronized boolean setNbNeighboursNotConv(int tag, int idNeigh,
209                         int neighborTimeStep) throws RemoteException {
210                 return JaceSession.Instance().getTaskObject().setNbNeighboursNotConv(
211                                 tag, idNeigh, neighborTimeStep);
212         }
213
214         public synchronized int getNbNeighboursNotConv() throws RemoteException {
215                 return JaceSession.Instance().getTaskObject().nb_not_recv;
216         }
217
218         public synchronized void response(int neighId, int tag, int response,
219                         Vector<?> recievedValue) throws RemoteException {
220                 JaceSession.Instance().getTaskObject().response(neighId, tag, response,
221                                 recievedValue);
222         }
223
224         public boolean ping() throws RemoteException {
225                 // System.out.println("pingggggggggggg");
226                 return true;
227         }
228
229         public void updateHeart(JaceInterface stub) throws RemoteException {
230                 System.out.println("I change to ping a Daemon");
231                 HeartBeatThread.Instance().setServer((Object) stub);
232         }
233
234         public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
235                 System.out.println("I change to ping a superNode");
236                 HeartBeatThread.Instance().setServer((Object) stub);
237         }
238
239         public long beating(JaceInterface workerStub) throws RemoteException {
240                 Node noeud = Register.Instance().getNodeOfStub(workerStub);
241                 if (noeud != null) {
242                         noeud.setAliveFlag(true);
243                         noeud.setAliveTime();
244                         // System.out.println("beating$$$$$$$$$");
245                         return noeud.getAliveTime();
246
247                 } else {
248                         // System.out.println("le noeud " + workerIP +
249                         // " est pas ds la liste des noeuds connectes");
250                         return -1;
251                 }
252         }
253
254         public void setScanning(boolean bool) throws RemoteException {
255                 // if(bool==true){
256                 try {
257                         ScanThread.Instance().setScanning(bool);
258                         synchronized (ScanThread.Instance()) {
259                                 ScanThread.Instance().notify();
260                         }
261                 } catch (Exception e) {
262                         System.err.println("Error in setScanning: " + e);
263                 }
264                 // System.out.println("before notify!!!!!!!!!");
265                 // try{ScanThread.Instance().notify();}
266                 // catch(Exception e){
267                 // System.out.println("error in notify:"+e);
268                 // }
269                 // System.out.println("notify!!!!!!!!!");
270                 // }
271                 // else
272                 // ScanThread.Instance().setScanning(false);
273         }
274
275         class InitiateAppli extends Thread {
276                 int req;
277
278                 public InitiateAppli(int req) {
279                         this.req = req;
280                 }
281
282                 public void run() {
283                         BackupsManager.Instance().initialize(req);
284
285                 }
286         }
287
288         class ReinitDaemon extends Thread {
289
290                 public ReinitDaemon() {
291                 }
292
293                 public void run() {
294                         JaceDaemon.Instance().reinitDaemon();
295                 }
296         }
297 }