import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.Naming;
+import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+import and.Mapping.GNode;
import and.Mapping.Utils;
public class JaceDaemon {
// attributes
public static JaceDaemon Instance = null;
private String snode_IP = null;
- private int snode_port = 1098;
- private int daemonPort = 1098;
+ private int snode_port = 1099;
+ private int daemonPort = 1099;
private int heartTime; // HeartBeat frequency
private String protocol;
private boolean running = false;
- public JaceDaemon(String superNode, int port, String comProtocol) {
- if (!superNode.equals("-d")) {
+ public JaceDaemon( String superNode, int port, String comProtocol ) {
+ if( !superNode.equals( "-d" ) )
+ {
// snode_IP = LocalHost.Instance().resolve(superNode); // get IP of
// the super node
- snode_IP = superNode;
+ snode_IP = superNode ;
}
- daemonPort = port;
- protocol = comProtocol;
- running = true;
- Instance = this;
+ daemonPort = port ;
+ protocol = comProtocol ;
+ running = true ;
+ Instance = this ;
// initialize();
}
*
**/
public void initialize() {
- LocalHost.Instance().setPort(daemonPort);
- exportObject(); // Iinstanciate the JaceServer localy
- reconnectSuperNode(); // Connect to one of the SuperNodes
- if (protocol.equals("socket")) {
- listenForRequests();
+ LocalHost.Instance().setPort( daemonPort ) ;
+ exportObject() ; // Instanciate the JaceServer localy
+ reconnectSuperNode() ; // Connect to one of the SuperNodes
+
+ if( protocol.equals( "socket" ) )
+ {
+ listenForRequests() ;
}
-
- /*
- * System.out.println("sleep de 5 secondes avt tuer Daemon"); try {
- * Thread.sleep(5000); } catch(Exception e1) {}
- *
- *
- * sortir();
- */
}
/**
*
**/
private void exportObject() {
- // if( protocol.equals( "rmi" ) ) {
- JaceInterface ref = null;
- JaceServer myServer = null;
-
- //System.out.println("Name of local machine is : "
- // + LocalHost.Instance().getName());
- //System.out.println("IP of local machine is : "
- // + LocalHost.Instance().getIP());
+ JaceInterface ref = null ;
+ JaceServer myServer = null ;
+ Registry reg ;
+
try {
+
+ while( true )
+ {
+ reg = LocateRegistry.getRegistry( daemonPort ) ;
+
+ String tab[] = reg.list() ;
+
+ System.out.println( "There is an existing RMI Registry on port "+daemonPort+" with "+tab.length+" entries!" ) ;
+ for( int i = 0 ; i < tab.length ; i++ )
+ {
+ try {
+ if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
+ {
+ System.out.println( "Register successfuly deleted!" ) ;
+ } else {
+ System.err.println( "Register undeleted !!!" ) ;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ if( UnicastRemoteObject.unexportObject( myServer, true ) )
+ {
+ System.out.println( "Register successfuly deleted!" ) ;
+ } else {
+ System.err.println( "Register undeleted !!!" ) ;
+ }
+ }
+ } catch( RemoteException e ) {
+ }
+
+
+ try {
+ if (System.getSecurityManager() == null) {
+ System.setSecurityManager(new SecurityManager());
+ }
+
// launch the JaceServer
myServer = new JaceServer( /* this */);
- java.rmi.registry.LocateRegistry.createRegistry(daemonPort);
- java.rmi.registry.LocateRegistry.getRegistry(daemonPort).rebind(
+ LocateRegistry.createRegistry(daemonPort);
+ LocateRegistry.getRegistry(daemonPort).rebind(
"JaceServer", myServer);
ref = (JaceInterface) Naming.lookup("rmi://"
+ LocalHost.Instance().getIP() + ":" + daemonPort
}
LocalHost.Instance().setStub(ref);
- // }
}
/**
}
}
-
+
+
/**
*
**/
} catch (Exception e) {
//System.out
// .println("Snode not launched, try another one (1/2s)");
- try {
- Thread.sleep(500);
- } catch (Exception e1) {
- // nothing
- }
+// try {
+// Thread.sleep(500);
+// } catch (Exception e1) {
+// // nothing
+// }
}
}
// LocalHost.Instance().getIP(),
// LocalHost.Instance().getName(),
// daemonPort ) ;
+
+ GNode me = Utils.createGNode() ;
+
+ if( me == null )
+ {
+ System.err.println( "Problem during the creation of my GNode !!" ) ;
+ System.exit( 1 ) ;
+ } else {
+ if( me.getIP().equals( "" ) || me.getIP() == null
+ || me.getIP().isEmpty()
+ || ! me.getIP().equals( LocalHost.Instance().getIP()))
+ {
+ System.err.println( "Problem with my IP address in my GNode !!" ) ;
+ System.exit( 2 ) ;
+ }
+ }
snodeStub.workerRegistering(LocalHost.Instance().getStub(),
LocalHost.Instance().getIP(), LocalHost.Instance()
- .getName(), daemonPort, Utils.createGNode());
+ .getName(), daemonPort, me ) ;
// Launching the heart beat thread
HeartBeatThread.Instance().setHeartTime(heartTime);
LocalHost.Instance().setStartedThreads(false);
System.out.println("Reinitialization of the Daemon");
- System.out.println("I kill the application if any exists");
+ System.out.println( "I kill the application if any exists" ) ;
// ScanThread.Instance().setScanning( false ) ;
// System.out.println( "Set ScanThread off" ) ;
// Sender.Instance().kill() ;
try {
- Thread.sleep(2500);
+ Thread.sleep( 2500 ) ;
} catch (Exception e) {
// nothing
}
Register.Instance().purge();
} catch (Exception e) {
- System.err.println("Crashed killing the application : " + e);
+ System.err.println( "Crashed killing the application : " + e ) ;
}
- System.out.println("Daemon reinitialized");
- // Runtime.getRuntime().gc() ;
+ System.out.println( "Daemon reinitialized" ) ;
+ System.gc() ;
reconnectSuperNode();
}