3 import java.rmi.RemoteException;
4 import java.rmi.server.UnicastRemoteObject;
5 import java.util.ArrayList;
6 import java.util.Calendar;
7 import java.util.GregorianCalendar;
10 public class JaceServer extends UnicastRemoteObject implements JaceInterface {
12 private static final long serialVersionUID = 1L;
14 public JaceServer() throws RemoteException {
18 public void reconnectSuperNode() throws RemoteException {
19 JaceDaemon.Instance().reconnectSuperNode();
22 // when a daemon replaces a new one it asks for the backups
23 public void getBackupForNewNode(int rank) throws RemoteException {
24 JaceSession.Instance().getTaskObject().getBackupForNewNode(rank);
27 public void setSpawner(JaceSpawnerInterface spawnerStub)
28 throws RemoteException {
29 Register.Instance().setSpawnerStub(spawnerStub);
32 // public JaceSpawnerInterface transformIntoSpawner(String[] params,
33 // String appliName, Register reg, int nbTasks,
34 // JaceSuperNodeInterface snodeStub, int rank, int heartTime, int tag,
35 // int nbdc, int nbsdc, int nbDaemonPerSpawner, int nbDaemonPerThread)
36 // throws RemoteException {
37 // System.out.println("Beginning the transformation ...");
38 // 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();
46 public JaceSpawnerInterface transformIntoSpawner(String[] params,
47 String appliName, Register reg, int nbTasks,
48 JaceSuperNodeInterface snodeStub, int rank, int heartTime, int tag,
49 int nbdc, int nbsdc, int nbDaemonPerSpawner, int nbDaemonPerThread,
51 throws RemoteException {
52 System.out.println("Beginning the transformation ...");
53 new JaceSpawner(params, appliName, reg, nbTasks,
54 snodeStub, rank, heartTime, tag, nbdc, nbsdc,
55 nbDaemonPerSpawner, nbDaemonPerThread, idAlgo);
56 HeartBeatThread.Instance().kill();
57 return Register.Instance().getSpawnerStub();
60 public synchronized int updateRegister(Register newReg,
61 JaceInterface voisinStub, int req) throws RemoteException {
63 // If beginning of appli, tell nodes to beat the next neighbor
64 // and no longer the SuperNode
66 System.out.println("I change to ping a Daemon");
67 HeartBeatThread.Instance().setServer((Object) voisinStub);
69 Calendar cal = new GregorianCalendar();
70 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
71 + cal.get(Calendar.SECOND));
72 System.out.println("name of spawner: "
73 + newReg.getSpawnerStub().getName());
74 // 1 - replace the old Register by the new one
75 System.out.println(" \n\n NEW REGISTER \n\n");
76 // if(Register.Instance().getVersion()<=newReg.getVersion()){
77 System.out.println("Replacing Register ...");
78 Register.Instance().replaceBy(newReg);
79 Register.Instance().viewAll();
82 // initialise the BackupsManager if not exists (i.e. myRank = -1)
83 // which means that not init so first reload
85 // if (BackupsManager.Instance().getMyRank() == -1) {
86 // 1 - create the BackupsManager
87 // 2 - get an eventual Backup for my Task
88 // 3 - restart it if any
89 // BackupsManager.Instance().initialize();
91 new InitiateAppli(req).start();
96 public int updateRegister( Node oldNode, Node node, int rank ) throws RemoteException {
99 // HeartBeatThread.Instance().setServer((Object)node.getStub());
100 // 1 - replace the old Register by the new one
101 Calendar cal = new GregorianCalendar();
102 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
103 + cal.get(Calendar.SECOND));
104 System.out.println("Modify REGISTER");
107 if( oldNode != null )
109 System.out.println("Old node name=" + oldNode.getName());
110 b = Register.Instance().removeNodeOfName(oldNode.getName());
114 System.out.println("Old node removed !");
116 System.out.println("Did not find the old Node !");
121 Register.Instance().addNode(node);
123 System.err.println( "The new node is NULL !" ) ;
126 // Register.Instance().viewAll();
127 TaskId myTaskId = null;
128 // myTaskId = Register.Instance().getListeOfTasks()
129 // .getTaskIdOfHostStub(oldNode.getStub());
130 myTaskId = Register.Instance().getListeOfTasks()
131 .getTaskIdOfRank( rank ) ;
132 myTaskId.setHostIP(node.getIP());
133 myTaskId.setHostName(node.getName());
134 // Register.Instance().setVersion(version);
135 myTaskId.setHostStub(node.getStub());
139 } catch (Exception e) {
140 System.err.println("Error in updateregister :" + e);
142 // e.printStackTrace();
146 public ArrayList<Integer> getIterationOfBackup(int remoteRank, int tag)
147 throws RemoteException {
148 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
150 ArrayList<Integer> result = b.getIterationStep();
154 public Backup getRemoteBackup(int remoteRank, int tag)
155 throws RemoteException {
156 Backup b = BackupsManager.Instance().getBackupTaskOfRank(remoteRank,
161 public void suicide(String debugMsg) throws RemoteException {
162 // System.out.println("suiciiiiiiiiiiiide : "/* + debugMsg */);
163 new ReinitDaemon().start();
166 public void iSendYou(Message msg) throws RemoteException {
167 // on met le Message ds le MsgQueue correspondant ma tache
168 // DS CAS ASYNC, on supprime un msg de meme tag ET meme sender
169 // System.out.println("get msg from MSGQueue sent from "+msg.getSender().getRank());
170 MsgQueue.Instance().add(msg);
173 public int getTimeStep() throws RemoteException {
174 return JaceSession.Instance().getTaskObject().timeStep;
177 public void saveTask(int rank, byte[] tsk, int iteration, int timeStep,
178 String appliName, int tag) throws RemoteException {
180 while (JaceSession.Instance().getTaskObject().reloading == true)
183 } catch (Exception e) {
185 Backup back = BackupsManager.Instance().getBackupTaskOfRank(rank, tag);
187 if (back.getStep() < timeStep
188 || (back.getStep() == timeStep && back.getIteration() < iteration)) {
189 back.setIteration(iteration);
190 // System.out.print("\n\n they put in me a backup with iter="+back.getIteration()+" for the node of rank="+rank+"\n\n");
191 back.setStep(timeStep);
197 * TaskId recev=null; recev =
198 * Register.Instance().getListeOfTasks().getTaskIdOfRank(rank);
199 * JaceInterface stub; stub=recev.getHostStub();
200 * stub.setSaved(true);
202 * }catch(Exception e){
203 * System.out.println("unable to acknowledge receiving the backup :"
208 System.out.println("No task at this rank");
209 // TODO : what to do ?
214 public void setSaved(boolean bool) throws RemoteException {
215 JaceSession.Instance().getTaskObject().setSaved(bool);
218 public boolean getReloading() throws RemoteException {
219 return JaceSession.Instance().getTaskObject().reloading;
222 public int getVerifNum() throws RemoteException {
223 return JaceSession.Instance().getTaskObject().verifNum;
226 public String getState() throws RemoteException {
227 return JaceSession.Instance().getTaskObject().state;
230 public void initializeVerif(int tag) throws RemoteException {
231 JaceSession.Instance().getTaskObject().initializeVerif(tag);
234 public void savOrFinOrRest(int tag, int step, boolean verd,
235 ArrayList<Object> recievedValue) throws RemoteException {
236 JaceSession.Instance().getTaskObject().savOrFinOrRest(tag, step, verd,
240 public synchronized boolean setNbNeighboursNotConv(int tag, int idNeigh,
241 int neighborTimeStep) throws RemoteException {
242 return JaceSession.Instance().getTaskObject().setNbNeighboursNotConv(
243 tag, idNeigh, neighborTimeStep);
246 public synchronized int getNbNeighboursNotConv() throws RemoteException {
247 return JaceSession.Instance().getTaskObject().nb_not_recv;
250 public synchronized void response(int neighId, int tag, int response,
251 ArrayList<Object> recievedValue) throws RemoteException {
252 JaceSession.Instance().getTaskObject().response(neighId, tag, response,
256 public boolean ping() throws RemoteException {
257 // System.out.println("pingggggggggggg");
261 public void updateHeart(JaceInterface stub) throws RemoteException {
262 System.out.println("I change to ping a Daemon");
263 HeartBeatThread.Instance().setServer((Object) stub);
266 public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
267 System.out.println("I change to ping a superNode");
268 HeartBeatThread.Instance().setServer((Object) stub);
271 public long beating(JaceInterface workerStub) throws RemoteException {
272 Node noeud = Register.Instance().getNodeOfStub(workerStub);
274 noeud.setAliveFlag(true);
275 noeud.setAliveTime();
276 // System.out.println("beating$$$$$$$$$");
277 return noeud.getAliveTime();
280 // System.out.println("le noeud " + workerIP +
281 // " est pas ds la liste des noeuds connectes");
286 public void setScanning(boolean bool) throws RemoteException {
289 ScanThread.Instance().setScanning(bool);
290 synchronized (ScanThread.Instance()) {
291 ScanThread.Instance().notify();
293 } catch (Exception e) {
294 System.err.println("Error in setScanning: " + e);
296 // System.out.println("before notify!!!!!!!!!");
297 // try{ScanThread.Instance().notify();}
298 // catch(Exception e){
299 // System.out.println("error in notify:"+e);
301 // System.out.println("notify!!!!!!!!!");
304 // ScanThread.Instance().setScanning(false);
307 class InitiateAppli extends Thread {
310 public InitiateAppli(int req) {
315 BackupsManager.Instance().initialize( req ) ;
320 class ReinitDaemon extends Thread {
322 public ReinitDaemon() {}
325 JaceDaemon.Instance().reinitDaemon() ;
330 public void suicide2( String mes ) throws RemoteException
332 System.out.println( "Killed because: " + mes ) ;
335 UnicastRemoteObject.unexportObject( this, true ) ;
337 catch( Exception e ) {
338 System.err.println( "Unable to unexport myself: " + e ) ;