Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[jaceP2P.git] / src / jaceP2P / JaceSpawnerServer.java
1 package jaceP2P;
2
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;
8
9
10 public class JaceSpawnerServer extends UnicastRemoteObject implements
11                 JaceSpawnerInterface {
12
13         private static final long serialVersionUID = 1L;
14
15         int nbKilled = 0;
16         boolean finished = false;
17
18         // constructeur
19         public JaceSpawnerServer() throws RemoteException {
20                 super();
21         }
22
23         public void startProcess(ArrayList<Object> spawnersList) throws RemoteException {
24                 JaceSpawner.Instance().startProcess(spawnersList);
25         }
26
27         public Register getRegister(int rank) throws RemoteException {
28                 return JaceSpawner.Instance().getRegister(rank);
29         }
30
31         public void killApplication(JaceInterface stub) throws RemoteException {
32
33                 // Node noeud = Register.Instance().getNodeOfStub(stub);
34                 // noeud.setAppliName(null);
35                 // new ReconnectThread(stub,noeud.getName()).start();
36                 // Register.Instance().removeNode(noeud);
37                 // nbKilled++;
38                 /*
39                  * if (Register.Instance().getSize() < 1) { long finalTime =
40                  * RunningApplication.Instance().getChrono().getValue(); int nb =
41                  * RunningApplication.Instance().getNumberOfDisconnections();
42                  * System.out.println("Application finished successfully !!!!!!");
43                  * System.out.println("Application finished successfully !!!!!!");
44                  * System.out.println("Application finished successfully !!!!!!");
45                  * System.out.println("Application finished successfully !!!!!!");
46                  * System.out.println("Application finished successfully !!!!!!");
47                  * System.out.println("Application finished successfully !!!!!!");
48                  * System.out.println("Application finished successfully !!!!!!");
49                  * System.out.println("Application finished successfully !!!!!!");
50                  * System.out.println("Application finished successfully !!!!!!\n");
51                  * System.out.println("TOTAL TIME in s : " + (finalTime/1000));
52                  * System.out.println("nb of desconnections : " + nb);
53                  * 
54                  * //purger l'appli RunningApplication.Instance().purge(); }
55                  */
56         }
57
58         public long getChronoValue(String appliName) throws RemoteException {
59                 long res = -1;
60                 if (RunningApplication.Instance().getName().equals(appliName)) {
61                         res = RunningApplication.Instance().getChrono().getValue();
62                         // System.out.println("temps chrono : " + res + " ms\n");
63                 } else {
64                         System.err.println("No application of name "+appliName+" on this Spawner !");
65                 }
66                 return res;
67         }
68
69         public void setOver(boolean bool) throws RemoteException {
70                 JaceSpawner.Instance().broadcastFinished(bool);
71
72         }
73
74         public void setFinished(boolean bool) throws RemoteException {
75                 finished = bool;
76
77         }
78
79         public String getName() throws RemoteException {
80                 return LocalHost.Instance().getName();
81         }
82
83         public boolean getFinished() throws RemoteException {
84                 return finished;
85         }
86         
87         private class Signaler implements Runnable
88         {
89                 JaceInterface host ;
90                 int rankOfDead ;
91                 Signaler( JaceInterface _host, int _rankOfDead )
92                 {
93                         host = _host ;
94                         rankOfDead = _rankOfDead ;
95                 }
96                 
97                 @Override
98                 public void run() 
99                 {
100                         JaceSpawner.Instance().signalDeadNode( host, rankOfDead ) ;             
101                 }
102                 
103         }
104
105         public void signalDeadNode(JaceInterface host, int rankOfDead )
106                         throws RemoteException {
107                 System.out.println( "SignalDeadNode of rank " + rankOfDead ) ;
108         
109                 new Thread( new Signaler( host, rankOfDead ) ).start() ;
110 //              JaceSpawner.Instance().signalDeadNode( host, rankOfDead ) ;
111         }
112
113         // heartBeat that detects if a Spawner is dead or alive
114         public void beating() throws RemoteException {
115                 ScanThreadSpawner.Instance().setAliveTime();
116
117                 // System.out.println("spawner is pinging me");
118         }
119
120         public synchronized void replaceBy(JaceSpawnerInterface oldStub,
121                         JaceSpawnerInterface stub) throws RemoteException {
122                 Calendar cal = new GregorianCalendar();
123                 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
124                                 + cal.get(Calendar.SECOND));
125                 System.out.println("replacing a dead spawner by another");
126                 JaceSpawner.Instance().replaceBy(oldStub, stub);
127                 RunningApplication.Instance().incrementNumberOfSpawnerDisconnections();
128         }
129
130         public void updateHeart(JaceSpawnerInterface stub) throws RemoteException {
131                 HeartBeatSpawner.Instance().setServer(stub);
132         }
133
134         public boolean ping() throws RemoteException {
135                 return true;
136         }
137
138         public synchronized void replaceDeamonBy(Node oldNode, Node node, int rank)
139                         throws RemoteException {
140                 Calendar cal = new GregorianCalendar();
141                 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
142                                 + cal.get(Calendar.SECOND));
143                 if (Register.Instance().removeNodeOfName(oldNode.getName()))
144                         System.out.println("Node " + oldNode.getName() + " of rank " + rank
145                                         + " has been removed");
146                 else
147                         System.out.println("Node " + oldNode.getName() + " of rank " + rank
148                                         + " hasn't been removed");
149                 
150                 if( node != null )
151                 {
152                         Register.Instance().addNode(node);
153
154                         TaskId myTaskId = Register.Instance().getListeOfTasks()
155                                 .getTaskIdOfHostStub(oldNode.getStub());
156
157                         myTaskId.setHostIP(node.getIP());
158
159                         myTaskId.setHostName(node.getName());
160                         myTaskId.setHostStub(node.getStub());
161                         // Register.Instance().setVersion(Register.Instance().getVersion()+1);
162                         RunningApplication.Instance().incrementNumberOfDisconnections();
163                         System.out.println("Replacing node: " + oldNode.getName() + " with: "
164                                 + node.getName());
165                         // Register.Instance().getListeOfTasks().viewAll();
166                 } else {
167                         System.err.println( "ReplaceDaemon: The new node is null!" ) ;
168                 }
169         }
170
171 //      @Override
172 //      public void setIdAlgo( String _s ) throws RemoteException 
173 //      {
174 //              JaceSpawner.Instance().setIdAlgo( _s ) ;
175 //      }
176 }