3 import java.net.ServerSocket;
4 import java.net.Socket;
5 import java.rmi.Naming;
7 import and.Mapping.Utils;
9 public class JaceDaemon {
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;
19 public JaceDaemon(String superNode, int port, String comProtocol) {
20 if (!superNode.equals("-d")) {
21 // snode_IP = LocalHost.Instance().resolve(superNode); // get IP of
27 protocol = comProtocol;
43 public String getProtocol() {
50 public synchronized static JaceDaemon Instance() {
51 if (Instance == null) {
52 Instance = new JaceDaemon();
61 public boolean isRunning() {
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")) {
77 * System.out.println("sleep de 5 secondes avt tuer Daemon"); try {
78 * Thread.sleep(5000); } catch(Exception e1) {}
88 public void listenForRequests() {
89 ServerSocket ss = null;
93 ss = new ServerSocket(LocalHost.Instance().getSocketPort());
95 } catch (Exception e) {
96 System.err.println("Error initializing ServerSocket: " + e);
102 // System.out.println( "Nouveau message" ) ;
104 new HandleClient(s).start();
105 } catch (Exception e) {
106 System.err.println("Error handling Client: " + e);
114 private void exportObject() {
115 // if( protocol.equals( "rmi" ) ) {
116 JaceInterface ref = null;
117 JaceServer myServer = null;
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());
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
133 } catch (Exception e) {
135 .println("JaceP2P_Error in JaceRuntime.exportObject() when creating the local JaceServer "
137 System.err.println("Exit from JaceRuntime.exportObject");
141 LocalHost.Instance().setStub(ref);
148 @SuppressWarnings("unused")
149 private void sortir() {
150 if (protocol.equals("rmi")) {
152 java.rmi.registry.LocateRegistry.getRegistry(daemonPort)
153 .unbind("JaceServer");
154 //System.out.println("Unbind done !!!!!!!!!!!1");
155 } catch (Exception e) {
157 .println("JaceP2P_Error in JaceRuntime.sortir() when unbind "
167 public void reconnectSuperNode() {
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;
175 // while( connected == false ) {
176 if (snode_IP != null) {
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);
183 // Add stub and IP in LocalHost to store it until super node
185 LocalHost.Instance().setSuperNodeStub(snodeStub);
186 LocalHost.Instance().setSuperNodeIP(snode_IP);
189 } catch (Exception e) {
191 // .println("Snode not launched, try another one (1/2s)");
194 } catch (Exception e1) {
200 if (connected == false) {
203 SuperNodeListe.Instance().staticInitialization();
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();
213 //System.out.println("Trying to invoke super node "
215 snodeStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
216 + snode_IP + ":" + snode_port + "/JaceSuperNode");
217 // System.out.println( "succesfully located " + snode_IP ) ;
219 // Add stub and IP in LocalHost to store it until super node
221 LocalHost.Instance().setSuperNodeStub(snodeStub);
222 LocalHost.Instance().setSuperNodeIP(snode_IP);
225 } catch (Exception e) {
226 //System.out.println("Snode " + snode_IP
227 // + " not launched, try another one (1/2s)");
232 } catch (Exception e1) {
236 // If error, exit the loop and reenter in order to find
243 if (connected == false) {
245 .println("All the SuperNodes in the list are dead, unable to connect to the platform");
249 // Registering to the Super Node
251 heartTime = snodeStub.getSuperNodeBeat();
253 // snodeStub.workerRegistering( LocalHost.Instance().getStub(),
254 // LocalHost.Instance().getIP(),
255 // LocalHost.Instance().getName(),
258 snodeStub.workerRegistering(LocalHost.Instance().getStub(),
259 LocalHost.Instance().getIP(), LocalHost.Instance()
260 .getName(), daemonPort, Utils.createGNode());
262 // Launching the heart beat thread
263 HeartBeatThread.Instance().setHeartTime(heartTime);
265 //System.out.println("Starting to ping the Super Node");
266 HeartBeatThread.Instance().setServer((Object) snodeStub);
268 if (HeartBeatThread.Instance().isAlive() == false) {
269 // HeartBeatThread.Instance().setPriority(Thread.MAX_PRIORITY);
270 HeartBeatThread.Instance().start(); // Starting the beating
273 //System.out.println("Succesfully registered to Super Node "
276 } catch (Exception e) {
278 .println("JaceP2P_Error in JaceDamon reconnectSuperNode when invoking function workerRegistering() : "
280 reconnectSuperNode();
290 public void reinitDaemon() {
292 if (JaceDaemon.Instance().getProtocol().equals("rmi")) {
293 SenderRmi.Instance().getBuffer().stopGet = true;
296 SenderRmi.Instance().notify();
297 } catch (Exception e) {
301 SenderRmi.Instance().kill();
304 SenderSocket.Instance().getBuffer().stopGet = true;
307 SenderSocket.Instance().notify();
308 } catch (Exception e) {
312 SenderSocket.Instance().kill();
315 //System.out.println("$$$$$$$$$$ Killed Sender $$$$$$$$$$");
316 ScanThread.Instance().kill();
317 LocalHost.Instance().setStartedThreads(false);
318 System.out.println("Reinitialization of the Daemon");
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() ;
327 } catch (Exception e) {
331 // Sender.Instance().running = false ;
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();
341 } catch (Exception e) {
342 System.err.println("Crashed killing the application : " + e);
345 System.out.println("Daemon reinitialized");
346 // Runtime.getRuntime().gc() ;
347 reconnectSuperNode();