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;
9 public class JaceServer extends UnicastRemoteObject implements JaceInterface {
11 private static final long serialVersionUID = 1L;
13 public JaceServer() throws RemoteException {
17 public void reconnectSuperNode() throws RemoteException {
18 JaceDaemon.Instance().reconnectSuperNode();
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);
26 public void setSpawner(JaceSpawnerInterface spawnerStub)
27 throws RemoteException {
28 Register.Instance().setSpawnerStub(spawnerStub);
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();
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,
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();
59 public synchronized int updateRegister(Register newReg,
60 JaceInterface voisinStub, int req) throws RemoteException {
62 // If beginning of appli, tell nodes to beat the next neighbor
63 // and no longer the SuperNode
65 System.out.println("I change to ping a Daemon");
66 HeartBeatThread.Instance().setServer((Object) voisinStub);
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();
81 // initialise the BackupsManager if not exists (i.e. myRank = -1)
82 // which means that not init so first reload
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();
90 new InitiateAppli(req).start();
95 public int updateRegister( Node oldNode, Node node, int rank ) throws RemoteException {
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");
106 if( oldNode != null )
108 System.out.println("Old node name=" + oldNode.getName());
109 b = Register.Instance().removeNodeOfName(oldNode.getName());
113 System.out.println("Old node removed !");
115 System.out.println("Did not find the old Node !");
120 Register.Instance().addNode(node);
122 System.err.println( "The new node is NULL !" ) ;
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());
138 } catch (Exception e) {
139 System.err.println("Error in updateregister :" + e);
141 // e.printStackTrace();
145 public Vector<?> getIterationOfBackup(int remoteRank, int tag)
146 throws RemoteException {
147 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
149 Vector<?> result = b.getIterationStep();
153 public Backup getRemoteBackup(int remoteRank, int tag)
154 throws RemoteException {
155 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
160 public void suicide(String debugMsg) throws RemoteException {
161 // System.out.println("suiciiiiiiiiiiiide : "/* + debugMsg */);
162 new ReinitDaemon().start();
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);
172 public int getTimeStep() throws RemoteException {
173 return JaceSession.Instance().getTaskObject().timeStep;
176 public void saveTask(int rank, byte[] tsk, int iteration, int timeStep,
177 String appliName, int tag) throws RemoteException {
179 while (JaceSession.Instance().getTaskObject().reloading == true)
182 } catch (Exception e) {
184 Backup back = BackupsManager.Instance().getBackupTaskOfRank(rank, tag);
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);
196 * TaskId recev=null; recev =
197 * Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
198 * JaceInterface stub; stub=recev.getHostStub();
199 * stub.setSaved(true);
201 * }catch(Exception e){
202 * System.out.println("unable to acknowledge receiving the backup :"
207 System.out.println("No task at this rank");
208 // TODO : what to do ?
213 public void setSaved(boolean bool) throws RemoteException {
214 JaceSession.Instance().getTaskObject().setSaved(bool);
217 public boolean getReloading() throws RemoteException {
218 return JaceSession.Instance().getTaskObject().reloading;
221 public int getVerifNum() throws RemoteException {
222 return JaceSession.Instance().getTaskObject().verifNum;
225 public String getState() throws RemoteException {
226 return JaceSession.Instance().getTaskObject().state;
229 public void initializeVerif(int tag) throws RemoteException {
230 JaceSession.Instance().getTaskObject().initializeVerif(tag);
233 public void savOrFinOrRest(int tag, int step, boolean verd,
234 Vector<?> recievedValue) throws RemoteException {
235 JaceSession.Instance().getTaskObject().savOrFinOrRest(tag, step, verd,
239 public synchronized boolean setNbNeighboursNotConv(int tag, int idNeigh,
240 int neighborTimeStep) throws RemoteException {
241 return JaceSession.Instance().getTaskObject().setNbNeighboursNotConv(
242 tag, idNeigh, neighborTimeStep);
245 public synchronized int getNbNeighboursNotConv() throws RemoteException {
246 return JaceSession.Instance().getTaskObject().nb_not_recv;
249 public synchronized void response(int neighId, int tag, int response,
250 Vector<?> recievedValue) throws RemoteException {
251 JaceSession.Instance().getTaskObject().response(neighId, tag, response,
255 public boolean ping() throws RemoteException {
256 // System.out.println("pingggggggggggg");
260 public void updateHeart(JaceInterface stub) throws RemoteException {
261 System.out.println("I change to ping a Daemon");
262 HeartBeatThread.Instance().setServer((Object) stub);
265 public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
266 System.out.println("I change to ping a superNode");
267 HeartBeatThread.Instance().setServer((Object) stub);
270 public long beating(JaceInterface workerStub) throws RemoteException {
271 Node noeud = Register.Instance().getNodeOfStub(workerStub);
273 noeud.setAliveFlag(true);
274 noeud.setAliveTime();
275 // System.out.println("beating$$$$$$$$$");
276 return noeud.getAliveTime();
279 // System.out.println("le noeud " + workerIP +
280 // " est pas ds la liste des noeuds connectes");
285 public void setScanning(boolean bool) throws RemoteException {
288 ScanThread.Instance().setScanning(bool);
289 synchronized (ScanThread.Instance()) {
290 ScanThread.Instance().notify();
292 } catch (Exception e) {
293 System.err.println("Error in setScanning: " + e);
295 // System.out.println("before notify!!!!!!!!!");
296 // try{ScanThread.Instance().notify();}
297 // catch(Exception e){
298 // System.out.println("error in notify:"+e);
300 // System.out.println("notify!!!!!!!!!");
303 // ScanThread.Instance().setScanning(false);
306 class InitiateAppli extends Thread {
309 public InitiateAppli(int req) {
314 BackupsManager.Instance().initialize( req ) ;
319 class ReinitDaemon extends Thread {
321 public ReinitDaemon() {}
324 JaceDaemon.Instance().reinitDaemon() ;
329 public void suicide2( String mes ) throws RemoteException
331 System.out.println( "Killed because: " + mes ) ;
334 UnicastRemoteObject.unexportObject( this, true ) ;
336 catch( Exception e ) {
337 System.err.println( "Unable to unexport myself: " + e ) ;