Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[jaceP2P.git] / src / jaceP2P / SuperNodeListe.java
index 82facfc..107d9b7 100644 (file)
@@ -5,8 +5,8 @@ import java.io.FileInputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.rmi.Naming;
+import java.util.ArrayList;
 import java.util.Random;
-import java.util.Vector;
 
 import and.Mapping.Algo;
 import and.Mapping.GNode;
@@ -16,13 +16,11 @@ public class SuperNodeListe implements Cloneable {
        // attributes
        public static SuperNodeListe Instance;
        private Random r;
-       @SuppressWarnings("unchecked")
-       private Vector liste = null;
+       private ArrayList<Object> liste = null;
 
        // constructors
-       @SuppressWarnings("unchecked")
        public SuperNodeListe() {
-               liste = new Vector();
+               liste = new ArrayList<Object>() ;
                r = new Random();
        }
 
@@ -32,13 +30,13 @@ public class SuperNodeListe implements Cloneable {
                }
                return Instance;
        }
-
+       
        @SuppressWarnings("unchecked")
        public synchronized SuperNodeListe clone() {
                SuperNodeListe d = new SuperNodeListe();
                // d.r = (Random) r.clone();
-               d.liste = (Vector) liste.clone();
-               return d;
+               d.liste = (ArrayList<Object>) liste.clone() ;
+               return d ;
        }
 
        // methods
@@ -48,8 +46,6 @@ public class SuperNodeListe implements Cloneable {
        // in order to discover new superNodes dynamicaly
        
        /* Sébastien Miquée -- ajout de la lecture du fichier de config **/
-       
-       @SuppressWarnings("unchecked")
        public void staticInitialization() {
                String adr = "";
                String home = System.getProperty("user.home");
@@ -91,8 +87,8 @@ public class SuperNodeListe implements Cloneable {
                // donnees ds un fichier de conf
        }
 
-       @SuppressWarnings("unchecked")
-       public Vector getListe() {
+       
+       public ArrayList<Object> getListe() {
                return liste;
        }
 
@@ -106,11 +102,11 @@ public class SuperNodeListe implements Cloneable {
                if (index < 0) {
                        index = -index;
                }
-               return (SuperNodeData) liste.elementAt(index);
+               return (SuperNodeData) liste.get(index);
        }
 
        public SuperNodeData getSuperNodeData(int i) {
-               return (SuperNodeData) liste.elementAt(i);
+               return (SuperNodeData) liste.get(i);
        }
 
        // if returns 0, there are no nodes at all
@@ -120,25 +116,24 @@ public class SuperNodeListe implements Cloneable {
                SuperNodeData return_d = null;
 
                for (int i = 0; i < liste.size(); i++) {
-                       d = (SuperNodeData) liste.elementAt(i);
+                       d = (SuperNodeData) liste.get(i);
                        if (d.getNbOfNodes() > max) {
                                max = d.getNbOfNodes();
-                               return_d = (SuperNodeData) liste.elementAt(i);
+                               return_d = (SuperNodeData) liste.get(i);
                        }
                }
                return return_d;
        }
 
        public void addStub(int index, JaceSuperNodeInterface stub) {
-               ((SuperNodeData) liste.elementAt(index)).setStub(stub);
+               ((SuperNodeData) liste.get(index)).setStub(stub);
        }
 
-       @SuppressWarnings("unchecked")
        public void addStubOf(String snode, int port, JaceSuperNodeInterface stub) {
                int is = -1;
                is = existSuperNode(snode);
                if (is != -1) {
-                       ((SuperNodeData) liste.elementAt(is)).setStub(stub);
+                       ((SuperNodeData) liste.get(is)).setStub(stub);
                } else {
                        System.err.println("This superNode does not exist : " + snode);
                        System.out.println("Add the new SuperNode");
@@ -201,7 +196,7 @@ public class SuperNodeListe implements Cloneable {
                JaceSuperNodeInterface remoteStub = null;
 
                for (int i = 0; i < liste.size(); i++) {
-                       d = (SuperNodeData) liste.elementAt(i);
+                       d = (SuperNodeData) liste.get(i);
                        if (!d.getIP().equals(LocalHost.Instance().getIP())) {
                                // if not me, I inform the other super nodes
                                remoteStub = d.getStub();
@@ -224,14 +219,13 @@ public class SuperNodeListe implements Cloneable {
                int is = -1;
                is = existSuperNode(IP);
                if (is != -1) {
-                       SuperNodeData d = (SuperNodeData) liste.elementAt(is);
+                       SuperNodeData d = (SuperNodeData) liste.get(is);
                        d.setNbOfNodes(nb);
                } else {
                        System.err.println("This SuperNode does not exists in my list: " + IP);
                }
        }
 
-       @SuppressWarnings("unchecked")
        public void locateSuperNodes(JaceSuperNodeInterface stub) {
                String remoteIP;
                int remotePort;
@@ -240,8 +234,8 @@ public class SuperNodeListe implements Cloneable {
                boolean inStaticList = false;
                while (i < liste.size() && !connected) {
                        remoteIP = LocalHost.Instance().resolve(
-                                       ((SuperNodeData) liste.elementAt(i)).getIP());
-                       remotePort = ((SuperNodeData) liste.elementAt(i)).getPort();
+                                       ((SuperNodeData) liste.get(i)).getIP());
+                       remotePort = ((SuperNodeData) liste.get(i)).getPort();
 
                        // If I'm not this superNode, I locate the others
                        if (!remoteIP.equals(LocalHost.Instance().getIP())) {
@@ -257,7 +251,7 @@ public class SuperNodeListe implements Cloneable {
                                        // SuperNodeListe.Instance().addStub(i, remoteStub);
 
                                        // Send my stub to this corresponding remote SuperNode
-                                       liste = (Vector<?>) remoteStub.sendStub(LocalHost.Instance()
+                                       liste = (ArrayList<Object>) remoteStub.sendStub(LocalHost.Instance()
                                                        .getIP(), LocalHost.Instance().getPort(), stub);
                                        System.out.println("Recieved List");
                                        connected = true;
@@ -269,7 +263,7 @@ public class SuperNodeListe implements Cloneable {
                                        // get the nb of Daemons the remote SuperNode has already
                                        // registered
                                        // nb = remoteStub.getNbOfNodes();
-                                       // ((SuperNodeData)liste.elementAt(i)).setNbOfNodes(nb);
+                                       // ((SuperNodeData)liste.get(i)).setNbOfNodes(nb);
 
                                        // System.out.println("bien envoye mon stub a " + remoteIP);
                                        System.out.println("size:" + liste.size());
@@ -299,13 +293,12 @@ public class SuperNodeListe implements Cloneable {
 
        }
 
-       @SuppressWarnings("unchecked")
        public void addSuperNode(SuperNodeData d) {
                int is = -1;
                String name = d.getIP();
                is = existSuperNode(name);
                if (is != -1) {
-                       liste.setElementAt(d, is);
+                       liste.set( is, d ) ;
                } else {
                        liste.add(d);
                }
@@ -326,12 +319,12 @@ public class SuperNodeListe implements Cloneable {
                System.out.println("\nConfig of the superNodes :");
                String msg = "";
                for (int i = 0; i < liste.size(); i++) {
-                       SuperNodeData d = (SuperNodeData) liste.elementAt(i);
+                       SuperNodeData d = (SuperNodeData) liste.get(i);
                        if (d.getIP().equals(LocalHost.Instance().getIP())) {
                                msg = "me : " + Register.Instance().getSize();
                        } else {
                                System.out.println(d.getIP() + " : "
-                                               + ((SuperNodeData) liste.elementAt(i)).getNbOfNodes());
+                                               + ((SuperNodeData) liste.get(i)).getNbOfNodes());
                        }
                }
                System.out.println(msg);
@@ -344,7 +337,7 @@ public class SuperNodeListe implements Cloneable {
        /*********** Sébastien Miquée ***********/
        /****************************************/
        
-       public void addGNode( GNode _g )
+       public synchronized void addGNode( GNode _g )
        {
                if( _g != null )
                {
@@ -354,7 +347,7 @@ public class SuperNodeListe implements Cloneable {
 
                        for( int i = 0 ; i < liste.size() ; i++ ) 
                        {
-                               d = (SuperNodeData) liste.elementAt( i ) ;
+                               d = (SuperNodeData) liste.get( i ) ;
                                if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
                                {
                                        // if not me, I inform the other super nodes
@@ -368,8 +361,33 @@ public class SuperNodeListe implements Cloneable {
                        }
                }
        }
+
+       public synchronized void removeGNode( GNode _g, int _mode, String _spawnerIP  )
+       {
+               if( _g != null )
+               {
+                       
+                       SuperNodeData d = null ;
+                       JaceSuperNodeInterface remoteStub = null ;
+
+                       for( int i = 0 ; i < liste.size() ; i++ ) 
+                       {
+                               d = (SuperNodeData) liste.get( i ) ;
+                               if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
+                               {
+                                       // if not me, I inform the other super nodes
+                                       remoteStub = d.getStub() ;
+                                       try {
+                                               remoteStub.delGNodeFromList( (Node)_g.getNode(), _mode, _spawnerIP ) ;
+                                       } catch( Exception e ) {
+                                               System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
+                                       }
+                               }
+                       }
+               }
+       }
        
-       public void removeGNode( GNode _g )
+       public synchronized void removeGNode( GNode _g, int _mode  )
        {
                if( _g != null )
                {
@@ -379,13 +397,13 @@ public class SuperNodeListe implements Cloneable {
 
                        for( int i = 0 ; i < liste.size() ; i++ ) 
                        {
-                               d = (SuperNodeData) liste.elementAt( i ) ;
+                               d = (SuperNodeData) liste.get( i ) ;
                                if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
                                {
                                        // if not me, I inform the other super nodes
                                        remoteStub = d.getStub() ;
                                        try {
-                                               remoteStub.removeGNode( _g ) ;
+                                               remoteStub.removeMappedGNode( _g, _mode ) ;
                                        } catch( Exception e ) {
                                                System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
                                        }
@@ -394,6 +412,31 @@ public class SuperNodeListe implements Cloneable {
                }
        }
        
+       public synchronized void setMappedGNode( GNode _g, int _mode )
+       {
+               if( _g != null )
+               {
+                       
+                       SuperNodeData d = null ;
+                       JaceSuperNodeInterface remoteStub = null ;
+
+                       for( int i = 0 ; i < liste.size() ; i++ ) 
+                       {
+                               d = (SuperNodeData) liste.get( i ) ;
+                               if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
+                               {
+                                       // if not me, I inform the other super nodes
+                                       remoteStub = d.getStub() ;
+                                       try {
+                                               remoteStub.setMapped( _g, _mode ) ; 
+                                       } catch( Exception e ) {
+                                               System.err.println( "Unable to set mapped GNode on SuperNode " + d.getIP() ) ;
+                                       }
+                               }
+                       }
+               }
+       }
+       
        
        protected boolean workingOnGnodes()
        {
@@ -404,7 +447,7 @@ public class SuperNodeListe implements Cloneable {
 
                for( int i = 0 ; i < liste.size() ; i++ ) 
                {
-                       d = (SuperNodeData) liste.elementAt( i ) ;
+                       d = (SuperNodeData) liste.get( i ) ;
                        if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
                        {
                                // if not me, I inform the other super nodes
@@ -412,7 +455,7 @@ public class SuperNodeListe implements Cloneable {
                                try {
                                        ok = ok && remoteStub.blockForMapping() ;
                                } catch( Exception e ) {
-                                       System.err.println( "Unable to remove GNode at SuperNode " + d.getIP() ) ;
+                                       System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
                                }
                        }
                }
@@ -429,7 +472,7 @@ public class SuperNodeListe implements Cloneable {
 
                        for( int i = 0 ; i < liste.size() ; i++ ) 
                        {
-                               d = (SuperNodeData) liste.elementAt( i ) ;
+                               d = (SuperNodeData) liste.get( i ) ;
                                if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
                                {
                                        // if not me, I inform the other super nodes
@@ -437,14 +480,14 @@ public class SuperNodeListe implements Cloneable {
                                        try {
                                                remoteStub.setMapping( al ) ;
                                        } catch( Exception e ) {
-                                               System.err.println( "Unable to set mapping data at SuperNode " + d.getIP() ) ;
+                                               System.err.println( "Unable to set mapping data on SuperNode " + d.getIP() ) ;
                                        }
                                }
                        }
                }
        }
 
-       public void propagateReplaceNode( GNode _dead, GNode _remp ) 
+       public synchronized void propagateReplaceNode( GNode _dead, GNode _remp, String _spawnerIP ) 
        {
                if( _dead != null && _remp != null )
                {
@@ -453,19 +496,45 @@ public class SuperNodeListe implements Cloneable {
 
                        for( int i = 0 ; i < liste.size() ; i++ ) 
                        {
-                               d = (SuperNodeData) liste.elementAt( i ) ;
-                               if( ! d.getIP().equals(LocalHost.Instance().getIP() ) ) 
+                               d = (SuperNodeData) liste.get( i ) ;
+                               if( ! d.getIP().equals( LocalHost.Instance().getIP() ) ) 
                                {
                                        // if not me, I inform the other super nodes
                                        remoteStub = d.getStub() ;
                                        try {
-                                               remoteStub.updateMappedNode( _dead, _remp ) ;
+                                               remoteStub.updateMappedNode( _dead, _remp, _spawnerIP ) ;
                                        } catch( Exception e ) {
-                                               System.err.println( "Unable to set mapping data at SuperNode " + d.getIP() ) ;
+                                               System.err.println( "Unable to propagate replacing node on SuperNode " + d.getIP() ) ;
                                        }
                                }
                        }
                }
        }
 
+       
+       /**
+        * Send to all SuperNode the deletion of a mapping algorithm.
+        * @param id The mapping algorithm's identifier
+        */
+       public void removeAlgo( String _id ) 
+       {
+               SuperNodeData d = null ;
+               JaceSuperNodeInterface remoteStub = null ;
+
+               for( int i = 0 ; i < liste.size() ; i++ ) 
+               {
+                       d = (SuperNodeData) liste.get( i ) ;
+                       if( ! d.getIP().equals( LocalHost.Instance().getIP() ) ) 
+                       {
+                               // if not me, I inform the other super nodes
+                               remoteStub = d.getStub() ;
+                               try {
+                                       remoteStub.removeAlgo( _id, 1 ) ;
+                               } catch( Exception e ) {
+                                       System.err.println( "Unable to propagate the mapping algorithm deletion " + d.getIP() ) ;
+                               }
+                       }
+               }               
+       }
+
 }