Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Cluster.java
index 588c26e..69323eb 100644 (file)
@@ -50,7 +50,7 @@ public class Cluster implements Serializable
         */
        public void addGNode( GNode _n )
        {
-               if( _n != null )
+               if( _n != null && _n.getClusterName().equalsIgnoreCase( name ) )
                {
                        _n.setInCluster( true ) ;
                        nodes.add( _n ) ;
@@ -73,6 +73,16 @@ public class Cluster implements Serializable
        }
        
        
+       /**
+        * Return the list of free computing nodes which are in the cluster.
+        * @return The list of free nodes
+        */
+       public ArrayList<GNode> getFreeGNodes()
+       {
+               return freenodes ;
+       }
+       
+       
        /**
         * Return cluster's name.
         * @return Cluster's name
@@ -114,7 +124,8 @@ public class Cluster implements Serializable
 
        
        /**
-        * Test if a computing node is in the cluster.
+        * Test if a computing node is in the cluster, and return its position (if
+        * it exists).
         * @param _g The node to be tested
         * @return The position of the node
         */
@@ -130,14 +141,36 @@ public class Cluster implements Serializable
                                {
                                        pos = i ;
                                        break ;
-                               }
-                               
+                               }                               
                        }
                }
                
                return pos ;
        }
        
+       
+       /**
+        * Test if a computing node is in the cluster, and return it (if
+        * it exists).
+        * @param _g The node to be tested
+        * @return The position of the node
+        */
+       public GNode exists( GNode _g )
+       {               
+               if( _g != null )
+               {
+                       for( int i = 0 ; i < nodes.size() ; i ++ )
+                       {
+                               if( nodes.get( i ).getId() == _g.getId() )
+                               {
+                                       return nodes.get( i ) ;
+                               }                       
+                       }
+               }
+               
+               return null ;
+       }
+       
        /**
         * Return the next available computing node in the cluster.
         * @return The next node in the cluster
@@ -196,7 +229,8 @@ public class Cluster implements Serializable
        {
                if( _dead != null )
                {
-                       if( _dead.getCluster().equals( name ) && _dead.getSite().equals( site )  )
+                       if( _dead.getClusterName().equalsIgnoreCase( name ) 
+                                       && _dead.getSiteName().equalsIgnoreCase( site )  )
                        {
                                int i = 0 ;
                                for( i = 0 ; i < nodes.size() ; i++ )
@@ -376,6 +410,68 @@ public class Cluster implements Serializable
                }
        }
        
+       
+       /**
+        * Replace a node in the cluster (in case of a reconnection for example).
+        * @param _g The node to be replaced
+        */
+       public void replaceGNode( GNode _g )
+       {
+               if( _g != null )
+               {
+                       removeGNode( _g ) ;
+                       addGNode( _g ) ;
+               }
+       }
+
+
+       /**
+        * Search and return the better (most powerful) available node 
+        * of the cluster.
+        * @return The best available node
+        */
+       public GNode getBetterFreeGNode() 
+       {
+               GNode ret = null ;
+               
+               if( freenodes.size() > 0 )
+               {
+                       ret = freenodes.get( 0 ) ;
+               }
+               
+               for( int i = 1 ; i < freenodes.size() ; i++ )
+               {
+                       if( freenodes.get( i ).getPower() > ret.getPower() )
+                       {
+                               ret = freenodes.get( i ) ;
+                       }
+               }
+               
+               return ret ;
+       }
+       
+       
+       /**
+        * Construct and return a copy of the current Cluster.
+        * @return A copy of the cluster
+        */
+       public Cluster clone()
+       {
+               Cluster copy = new Cluster() ;
+               
+               copy.setName( name ) ;
+               copy.setSite( site ) ;
+               
+               for( int i = 0 ; i < nodes.size() ; i++ )
+               {
+                       GNode newgn = (GNode) nodes.get( i ).clone() ;
+                       newgn.setCluster( copy ) ;
+                       copy.addGNode( newgn ) ;
+               }
+               
+               return copy ;
+       }
+       
 }
 
 /** La programmation est un art, respectons ceux qui la pratiquent !! **/