Logo AND Algorithmique Numérique Distribuée

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