Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Cluster.java
index 712f17c..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
@@ -168,15 +201,24 @@ public class Cluster implements Serializable
                {
                        if( moreNode >= freenodes.size() )
                        {
-                               moreNode = 0 ;
+                               System.err.println( "No more free node available" );
+                       } else {
+                               ret = freenodes.get( moreNode ) ;
+                               moreNode++ ;
                        }
-                       
-                       ret = freenodes.get( moreNode ) ;
-                       moreNode++ ;
                }
                
                return ret ;
        }
+       
+       
+       /**
+        * (Re-)Initialize the moreNode counter. 
+        */
+       public void initMoreGNode()
+       {
+               moreNode = 0 ;
+       }
 
 
        /**
@@ -187,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++ )
@@ -270,9 +313,8 @@ public class Cluster implements Serializable
        
        
        /**
-        * Compute and return the real available computing power of the cluster,
-        * including the heterogeneity degree of the platform. 
-        * @return The real available computing power
+        * Compute and return the available computing power of the cluster.
+        * @return The available computing power
         */
        public double getAvailablePower()
        {
@@ -289,7 +331,65 @@ public class Cluster implements Serializable
                
                return ret ;
        }
+       
+       
+       /**
+        * Compute and return the average available computing power of the cluster.
+        * @return The available available computing power
+        */
+       public double getAvgAvailablePower()
+       {
+               double ret = 0 ;
+               
+               /** If there is some available nodes **/
+               if( freenodes.size() > 0 )
+               {                                               
+                       for( int i = 0 ; i < freenodes.size() ; i++ )
+                       {
+                               ret += freenodes.get( i ).getPower() ;
+                       }
+                       
+                       ret = ret / freenodes.size() ;
+               }
+               
+               return ret ;
+       }
 
+       
+       public double getAvgPower()
+       {
+               double ret = 0 ;
+               
+               /** If there is some available nodes **/
+               if( nodes.size() > 0 )
+               {                                               
+                       for( int i = 0 ; i < nodes.size() ; i++ )
+                       {
+                               ret += nodes.get( i ).getPower() ;
+                       }
+                       
+                       ret = ret / nodes.size() ;
+               }
+               
+               return ret ;
+       }
+       
+       
+       public double getPower()
+       {
+               double ret = 0 ;
+               
+               /** If there is some available nodes **/
+               if( nodes.size() > 0 )
+               {                                               
+                       for( int i = 0 ; i < nodes.size() ; i++ )
+                       {
+                               ret += nodes.get( i ).getPower() ;
+                       }
+               }
+               
+               return ret ;
+       }
 
        /**
         * Initialization of computing nodes in this cluster. Set all
@@ -310,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 !! **/