3 import java.net.ServerSocket;
4 import java.net.Socket;
5 import java.rmi.Naming;
6 import java.rmi.RemoteException;
7 import java.rmi.registry.LocateRegistry;
8 import java.rmi.registry.Registry;
9 import java.rmi.server.UnicastRemoteObject;
11 import and.Mapping.GNode;
12 import and.Mapping.Utils;
14 public class JaceDaemon {
16 public static JaceDaemon Instance = null;
17 private String snode_IP = null;
18 private int snode_port = 1098;
19 private int daemonPort = 1098;
20 private int heartTime; // HeartBeat frequency
21 private String protocol;
22 private boolean running = false;
24 public JaceDaemon( String superNode, int port, String comProtocol ) {
25 if( !superNode.equals( "-d" ) )
27 // snode_IP = LocalHost.Instance().resolve(superNode); // get IP of
29 snode_IP = superNode ;
33 protocol = comProtocol ;
49 public String getProtocol() {
56 public synchronized static JaceDaemon Instance() {
57 if (Instance == null) {
58 Instance = new JaceDaemon();
67 public boolean isRunning() {
74 public void initialize() {
75 LocalHost.Instance().setPort( daemonPort ) ;
76 exportObject() ; // Instanciate the JaceServer localy
77 reconnectSuperNode() ; // Connect to one of the SuperNodes
79 if( protocol.equals( "socket" ) )
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 JaceInterface ref = null ;
116 JaceServer myServer = null ;
124 reg = LocateRegistry.getRegistry( daemonPort ) ;
126 String tab[] = reg.list() ;
128 System.out.println( "There is an existing RMI Registry on port "+daemonPort+" with "+tab.length+" entries!" ) ;
129 for( int i = 0 ; i < tab.length ; i++ )
132 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
134 System.out.println( "Register successfuly deleted!" ) ;
136 System.err.println( "Register undeleted !!!" ) ;
138 } catch (Exception e) {
143 if( UnicastRemoteObject.unexportObject( myServer, true ) )
145 System.out.println( "Register successfuly deleted!" ) ;
147 System.err.println( "Register undeleted !!!" ) ;
150 } catch( RemoteException e ) {
155 if (System.getSecurityManager() == null) {
156 System.setSecurityManager(new SecurityManager());
159 // launch the JaceServer
160 myServer = new JaceServer( /* this */);
161 LocateRegistry.createRegistry(daemonPort);
162 LocateRegistry.getRegistry(daemonPort).rebind(
163 "JaceServer", myServer);
164 ref = (JaceInterface) Naming.lookup("rmi://"
165 + LocalHost.Instance().getIP() + ":" + daemonPort
167 } catch (Exception e) {
169 .println("JaceP2P_Error in JaceRuntime.exportObject() when creating the local JaceServer "
171 System.err.println("Exit from JaceRuntime.exportObject");
175 LocalHost.Instance().setStub(ref);
181 @SuppressWarnings("unused")
182 private void sortir() {
183 if (protocol.equals("rmi")) {
185 java.rmi.registry.LocateRegistry.getRegistry(daemonPort)
186 .unbind("JaceServer");
187 //System.out.println("Unbind done !!!!!!!!!!!1");
188 } catch (Exception e) {
190 .println("JaceP2P_Error in JaceRuntime.sortir() when unbind "
201 public void reconnectSuperNode() {
203 //System.out.println("I'm looking for a JaceP2P Super Node");
204 //System.out.println(protocol);
205 // if( protocol.equals( "rmi" ) ) {
206 JaceSuperNodeInterface snodeStub = null;
207 boolean connected = false;
209 // while( connected == false ) {
210 if (snode_IP != null) {
212 //System.out.println("Trying to invoke Super Node " + snode_IP);
213 snodeStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
214 + snode_IP + ":" + snode_port + "/JaceSuperNode");
215 //System.out.println("Succesfully located " + snode_IP);
217 // Add stub and IP in LocalHost to store it until super node
219 LocalHost.Instance().setSuperNodeStub(snodeStub);
220 LocalHost.Instance().setSuperNodeIP(snode_IP);
223 } catch (Exception e) {
225 // .println("Snode not launched, try another one (1/2s)");
227 // Thread.sleep(500);
228 // } catch (Exception e1) {
234 if (connected == false) {
237 SuperNodeListe.Instance().staticInitialization();
239 while (connected == false
240 && i < SuperNodeListe.Instance().getListe().size()) {
241 SuperNodeData d = null;
242 d = SuperNodeListe.Instance().getSuperNodeData(i);
243 snode_IP = LocalHost.Instance().resolve(d.getIP());
244 snode_port = d.getPort();
247 //System.out.println("Trying to invoke super node "
249 snodeStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
250 + snode_IP + ":" + snode_port + "/JaceSuperNode");
251 // System.out.println( "succesfully located " + snode_IP ) ;
253 // Add stub and IP in LocalHost to store it until super node
255 LocalHost.Instance().setSuperNodeStub(snodeStub);
256 LocalHost.Instance().setSuperNodeIP(snode_IP);
259 } catch (Exception e) {
260 //System.out.println("Snode " + snode_IP
261 // + " not launched, try another one (1/2s)");
266 } catch (Exception e1) {
270 // If error, exit the loop and reenter in order to find
277 if (connected == false) {
279 .println("All the SuperNodes in the list are dead, unable to connect to the platform");
283 // Registering to the Super Node
285 heartTime = snodeStub.getSuperNodeBeat();
287 // snodeStub.workerRegistering( LocalHost.Instance().getStub(),
288 // LocalHost.Instance().getIP(),
289 // LocalHost.Instance().getName(),
292 GNode me = Utils.createGNode() ;
296 System.err.println( "Problem during the creation of my GNode !!" ) ;
299 if( me.getIP().equals( "" ) || me.getIP() == null
300 || me.getIP().isEmpty()
301 || ! me.getIP().equals( LocalHost.Instance().getIP()))
303 System.err.println( "Problem with my IP address in my GNode !!" ) ;
308 snodeStub.workerRegistering(LocalHost.Instance().getStub(),
309 LocalHost.Instance().getIP(), LocalHost.Instance()
310 .getName(), daemonPort, me ) ;
312 // Launching the heart beat thread
313 HeartBeatThread.Instance().setHeartTime(heartTime);
315 //System.out.println("Starting to ping the Super Node");
316 HeartBeatThread.Instance().setServer((Object) snodeStub);
318 if (HeartBeatThread.Instance().isAlive() == false) {
319 // HeartBeatThread.Instance().setPriority(Thread.MAX_PRIORITY);
320 HeartBeatThread.Instance().start(); // Starting the beating
323 //System.out.println("Succesfully registered to Super Node "
326 } catch (Exception e) {
328 .println("JaceP2P_Error in JaceDamon reconnectSuperNode when invoking function workerRegistering() : "
330 reconnectSuperNode();
340 public void reinitDaemon() {
342 if (JaceDaemon.Instance().getProtocol().equals("rmi")) {
343 SenderRmi.Instance().getBuffer().stopGet = true;
346 SenderRmi.Instance().notify();
347 } catch (Exception e) {
351 SenderRmi.Instance().kill();
354 SenderSocket.Instance().getBuffer().stopGet = true;
357 SenderSocket.Instance().notify();
358 } catch (Exception e) {
362 SenderSocket.Instance().kill();
365 //System.out.println("$$$$$$$$$$ Killed Sender $$$$$$$$$$");
366 ScanThread.Instance().kill();
367 LocalHost.Instance().setStartedThreads(false);
368 System.out.println("Reinitialization of the Daemon");
370 System.out.println( "I kill the application if any exists" ) ;
371 // ScanThread.Instance().setScanning( false ) ;
372 // System.out.println( "Set ScanThread off" ) ;
373 // Sender.Instance().kill() ;
376 Thread.sleep( 2500 ) ;
377 } catch (Exception e) {
381 // Sender.Instance().running = false ;
383 // Cleaning JaceBuffer, MsgQueue, Register
384 // and deleting taskObject and taskThread
385 JaceSession.Instance().kill(); // Also to do when a node has been
386 // down, in the case he is back
387 System.out.println("Application killed properly");
388 BackupsManager.Instance().purge();
389 Register.Instance().purge();
391 } catch (Exception e) {
392 System.err.println( "Crashed killing the application : " + e ) ;
395 System.out.println( "Daemon reinitialized" ) ;
397 reconnectSuperNode();