Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New development version.
[jaceP2P.git] / src / jaceP2P / JaceDaemon.java
index 21a8c17..f894793 100644 (file)
@@ -3,30 +3,36 @@ package jaceP2P;
 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();
        }
@@ -66,20 +72,14 @@ public class JaceDaemon {
      *
      **/
        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();
-                */
        }
 
        /**
@@ -112,20 +112,54 @@ public class JaceDaemon {
      *
      **/
        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
@@ -139,7 +173,6 @@ public class JaceDaemon {
                }
 
                LocalHost.Instance().setStub(ref);
-               // }
        }
 
        /**
@@ -160,7 +193,8 @@ public class JaceDaemon {
                }
 
        }
-
+       
+       
        /**
      *
      **/
@@ -189,11 +223,11 @@ public class JaceDaemon {
                        } 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
+//                             }
                        }
                }
 
@@ -254,10 +288,26 @@ public class JaceDaemon {
                                // 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);
@@ -317,13 +367,13 @@ public class JaceDaemon {
                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
                }
@@ -339,11 +389,11 @@ public class JaceDaemon {
                        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();
        }