Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
21a8c179a021a265dcae5a779c4497170a4102d8
[jaceP2P.git] / src / jaceP2P / JaceDaemon.java
1 package jaceP2P;
2
3 import java.net.ServerSocket;
4 import java.net.Socket;
5 import java.rmi.Naming;
6
7 import and.Mapping.Utils;
8
9 public class JaceDaemon {
10         // attributes
11         public static JaceDaemon Instance = null;
12         private String snode_IP = null;
13         private int snode_port = 1098;
14         private int daemonPort = 1098;
15         private int heartTime; // HeartBeat frequency
16         private String protocol;
17         private boolean running = false;
18
19         public JaceDaemon(String superNode, int port, String comProtocol) {
20                 if (!superNode.equals("-d")) {
21                         // snode_IP = LocalHost.Instance().resolve(superNode); // get IP of
22                         // the super node
23                         snode_IP = superNode;
24                 }
25
26                 daemonPort = port;
27                 protocol = comProtocol;
28                 running = true;
29                 Instance = this;
30
31                 // initialize();
32         }
33
34         /**
35      *
36      **/
37         public JaceDaemon() {
38         }
39
40         /**
41      *
42      **/
43         public String getProtocol() {
44                 return protocol;
45         }
46
47         /**
48      *
49      **/
50         public synchronized static JaceDaemon Instance() {
51                 if (Instance == null) {
52                         Instance = new JaceDaemon();
53                 }
54
55                 return Instance;
56         }
57
58         /**
59      *
60      **/
61         public boolean isRunning() {
62                 return running;
63         }
64
65         /**
66      *
67      **/
68         public void initialize() {
69                 LocalHost.Instance().setPort(daemonPort);
70                 exportObject(); // Iinstanciate the JaceServer localy
71                 reconnectSuperNode(); // Connect to one of the SuperNodes
72                 if (protocol.equals("socket")) {
73                         listenForRequests();
74                 }
75
76                 /*
77                  * System.out.println("sleep de 5 secondes avt tuer Daemon"); try {
78                  * Thread.sleep(5000); } catch(Exception e1) {}
79                  * 
80                  * 
81                  * sortir();
82                  */
83         }
84
85         /**
86      *
87      **/
88         public void listenForRequests() {
89                 ServerSocket ss = null;
90
91                 try {
92
93                         ss = new ServerSocket(LocalHost.Instance().getSocketPort());
94
95                 } catch (Exception e) {
96                         System.err.println("Error initializing ServerSocket: " + e);
97                 }
98
99                 while (true) {
100                         Socket s = null;
101                         try {
102                                 // System.out.println( "Nouveau message" ) ;
103                                 s = ss.accept();
104                                 new HandleClient(s).start();
105                         } catch (Exception e) {
106                                 System.err.println("Error handling Client: " + e);
107                         }
108                 }
109         }
110
111         /**
112      *
113      **/
114         private void exportObject() {
115                 // if( protocol.equals( "rmi" ) ) {
116                 JaceInterface ref = null;
117                 JaceServer myServer = null;
118
119                 //System.out.println("Name of local machine is : "
120                 //              + LocalHost.Instance().getName());
121                 //System.out.println("IP of local machine is : "
122                 //              + LocalHost.Instance().getIP());
123
124                 try {
125                         // launch the JaceServer
126                         myServer = new JaceServer( /* this */);
127                         java.rmi.registry.LocateRegistry.createRegistry(daemonPort);
128                         java.rmi.registry.LocateRegistry.getRegistry(daemonPort).rebind(
129                                         "JaceServer", myServer);
130                         ref = (JaceInterface) Naming.lookup("rmi://"
131                                         + LocalHost.Instance().getIP() + ":" + daemonPort
132                                         + "/JaceServer");
133                 } catch (Exception e) {
134                         System.err
135                                         .println("JaceP2P_Error in JaceRuntime.exportObject() when creating the local JaceServer "
136                                                         + e);
137                         System.err.println("Exit from JaceRuntime.exportObject");
138                         System.exit(1);
139                 }
140
141                 LocalHost.Instance().setStub(ref);
142                 // }
143         }
144
145         /**
146      *
147      **/
148         @SuppressWarnings("unused")
149         private void sortir() {
150                 if (protocol.equals("rmi")) {
151                         try {
152                                 java.rmi.registry.LocateRegistry.getRegistry(daemonPort)
153                                                 .unbind("JaceServer");
154                                 //System.out.println("Unbind done !!!!!!!!!!!1");
155                         } catch (Exception e) {
156                                 System.err
157                                                 .println("JaceP2P_Error in JaceRuntime.sortir() when unbind "
158                                                                 + e);
159                         }
160                 }
161
162         }
163
164         /**
165      *
166      **/
167         public void reconnectSuperNode() {
168
169                 //System.out.println("I'm looking for a JaceP2P Super Node");
170                 //System.out.println(protocol);
171                 // if( protocol.equals( "rmi" ) ) {
172                 JaceSuperNodeInterface snodeStub = null;
173                 boolean connected = false;
174
175                 // while( connected == false ) {
176                 if (snode_IP != null) {
177                         try {
178                                 //System.out.println("Trying to invoke Super Node " + snode_IP);
179                                 snodeStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
180                                                 + snode_IP + ":" + snode_port + "/JaceSuperNode");
181                                 //System.out.println("Succesfully located " + snode_IP);
182
183                                 // Add stub and IP in LocalHost to store it until super node
184                                 // death
185                                 LocalHost.Instance().setSuperNodeStub(snodeStub);
186                                 LocalHost.Instance().setSuperNodeIP(snode_IP);
187                                 connected = true;
188
189                         } catch (Exception e) {
190                                 //System.out
191                                 //              .println("Snode not launched, try another one (1/2s)");
192                                 try {
193                                         Thread.sleep(500);
194                                 } catch (Exception e1) {
195                                         // nothing
196                                 }
197                         }
198                 }
199
200                 if (connected == false) {
201                         int i = 0;
202
203                         SuperNodeListe.Instance().staticInitialization();
204
205                         while (connected == false
206                                         && i < SuperNodeListe.Instance().getListe().size()) {
207                                 SuperNodeData d = null;
208                                 d = SuperNodeListe.Instance().getSuperNodeData(i);
209                                 snode_IP = LocalHost.Instance().resolve(d.getIP());
210                                 snode_port = d.getPort();
211
212                                 try {
213                                         //System.out.println("Trying to invoke super node "
214                                         //              + snode_IP);
215                                         snodeStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
216                                                         + snode_IP + ":" + snode_port + "/JaceSuperNode");
217                                         // System.out.println( "succesfully located " + snode_IP ) ;
218
219                                         // Add stub and IP in LocalHost to store it until super node
220                                         // death
221                                         LocalHost.Instance().setSuperNodeStub(snodeStub);
222                                         LocalHost.Instance().setSuperNodeIP(snode_IP);
223                                         connected = true;
224
225                                 } catch (Exception e) {
226                                         //System.out.println("Snode " + snode_IP
227                                         //              + " not launched, try another one (1/2s)");
228                                         i++;
229
230                                         try {
231                                                 Thread.sleep(500);
232                                         } catch (Exception e1) {
233                                                 // nothing
234                                         }
235
236                                         // If error, exit the loop and reenter in order to find
237                                         // another Snode
238                                         // continue ;
239                                 }
240                         }
241                 }
242
243                 if (connected == false) {
244                         System.err
245                                         .println("All the SuperNodes in the list are dead, unable to connect to the platform");
246                         System.exit(1);
247                 } else {
248
249                         // Registering to the Super Node
250                         try {
251                                 heartTime = snodeStub.getSuperNodeBeat();
252
253                                 // snodeStub.workerRegistering( LocalHost.Instance().getStub(),
254                                 // LocalHost.Instance().getIP(),
255                                 // LocalHost.Instance().getName(),
256                                 // daemonPort ) ;
257
258                                 snodeStub.workerRegistering(LocalHost.Instance().getStub(),
259                                                 LocalHost.Instance().getIP(), LocalHost.Instance()
260                                                                 .getName(), daemonPort, Utils.createGNode());
261
262                                 // Launching the heart beat thread
263                                 HeartBeatThread.Instance().setHeartTime(heartTime);
264
265                                 //System.out.println("Starting to ping the Super Node");
266                                 HeartBeatThread.Instance().setServer((Object) snodeStub);
267
268                                 if (HeartBeatThread.Instance().isAlive() == false) {
269                                         // HeartBeatThread.Instance().setPriority(Thread.MAX_PRIORITY);
270                                         HeartBeatThread.Instance().start(); // Starting the beating
271                                 }
272
273                                 //System.out.println("Succesfully registered to Super Node "
274                                 //              + snode_IP);
275
276                         } catch (Exception e) {
277                                 System.err
278                                                 .println("JaceP2P_Error in JaceDamon reconnectSuperNode  when invoking function workerRegistering() : "
279                                                                 + e);
280                                 reconnectSuperNode();
281                         }
282                 }
283                 // }
284                 // }
285         }
286
287         /**
288      *
289      **/
290         public void reinitDaemon() {
291
292                 if (JaceDaemon.Instance().getProtocol().equals("rmi")) {
293                         SenderRmi.Instance().getBuffer().stopGet = true;
294
295                         try {
296                                 SenderRmi.Instance().notify();
297                         } catch (Exception e) {
298                                 // nothing
299                         }
300
301                         SenderRmi.Instance().kill();
302
303                 } else {
304                         SenderSocket.Instance().getBuffer().stopGet = true;
305
306                         try {
307                                 SenderSocket.Instance().notify();
308                         } catch (Exception e) {
309                                 // nothing
310                         }
311
312                         SenderSocket.Instance().kill();
313                 }
314
315                 //System.out.println("$$$$$$$$$$ Killed Sender $$$$$$$$$$");
316                 ScanThread.Instance().kill();
317                 LocalHost.Instance().setStartedThreads(false);
318                 System.out.println("Reinitialization of the Daemon");
319
320                 System.out.println("I kill the application if any exists");
321                 // ScanThread.Instance().setScanning( false ) ;
322                 // System.out.println( "Set ScanThread off" ) ;
323                 // Sender.Instance().kill() ;
324
325                 try {
326                         Thread.sleep(2500);
327                 } catch (Exception e) {
328                         // nothing
329                 }
330
331                 // Sender.Instance().running = false ;
332                 try {
333                         // Cleaning JaceBuffer, MsgQueue, Register
334                         // and deleting taskObject and taskThread
335                         JaceSession.Instance().kill(); // Also to do when a node has been
336                         // down, in the case he is back
337                         System.out.println("Application killed properly");
338                         BackupsManager.Instance().purge();
339                         Register.Instance().purge();
340
341                 } catch (Exception e) {
342                         System.err.println("Crashed killing the application : " + e);
343                 }
344
345                 System.out.println("Daemon reinitialized");
346                 // Runtime.getRuntime().gc() ;
347                 reconnectSuperNode();
348         }
349
350 }
351
352 /** ! **/
353