Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implementation of fault tolerance functions in all algorithms.
[mapping.git] / src / and / Mapping / QM.java
index 70799f2..6483cd8 100644 (file)
@@ -512,16 +512,116 @@ public class QM extends Algo
        @Override
        public GNode replaceNode(GNode _dead, ArrayList<GNode> _ag ) 
        {
+               GNode ret = null ;
+               
+               /** Updating the grid if there is any modification **/
+               if( _ag != null && _ag.size() != 0 )
+               {
+                       gl.updateGrid( _ag ) ;
+               }
+               
+               /** If something has to be done **/
+               if( _dead != null )
+               {
+                       ArrayList<GNode> ac = new ArrayList<GNode>() ;
+                       GNode tmp = null ;
+               
+                       /** Searching if clusters have some free nodes **/
+                       for( int i = 0 ; i < gl.getNbCluster() ; i++ )
+                       {
+                               if( gl.getClusters().get( i ).getNbFreeNodes() > 0 )
+                               {
+                                       tmp = gl.getClusters().get( i ).nextGNode() ;
+                                       
+                                       if( tmp != null )
+                                       {
+                                               ac.add( tmp ) ;
+                                       }
+                               }
+                       }
+                       
+                       /** If there some nodes in clusters **/
+                       if( ac.size() > 0 )
+                       {
+                               double power = -1, best = -1 ;
+                               int bestNode = -1 ;
+                               
+                               /** Searching the replacing node with the higher 
+                                * computing power
+                                */
+                               for( int i = 0 ; i < ac.size() ; i++ )
+                               {
+                                       power = ac.get( i ).getPower() ;
+                                       
+                                       if( best == -1 )
+                                       {
+                                               best = power ;
+                                               bestNode = i ;
+                                       } else {
+                                               if( power < best )
+                                               {
+                                                       best = power ;
+                                                       bestNode = i ;
+                                               }
+                                       }
+                               }
+                               
+                               /** Is there any node candidate ? **/
+                               if( bestNode != -1 )
+                               {
+                                       ret = ac.get( bestNode ) ;
+                               }
+                       }                       
+               }
+               
                /** Update in cluster the status of nodes **/
                updateGrid() ;
-               return null;
+               
+               return ret ;
        }
 
 
        @Override
-       public GNode getOtherGNode( ArrayList<GNode> _ag ) {
-               // TODO Auto-generated method stub
-               return null;
+       public GNode getOtherGNode( ArrayList<GNode> _ag ) 
+       {
+               GNode ret = null ;
+               
+               /** Updating the grid if there is any modification **/
+               if( _ag != null && _ag.size() != 0 )
+               {
+                       gl.updateGrid( _ag ) ;
+               }
+               
+               int free[] = new int[ gl.getNbCluster() ] ;
+               
+               /** Searching if clusters have some free nodes **/
+               for( int i = 0 ; i < gl.getNbCluster() ; i++ )
+               {
+                       free[ i ] = gl.getClusters().get( i ).getNbFreeNodes() ;
+               }
+               
+               /** Returning a node from the cluster which has the most
+                * available nodes
+                */
+               int most = -1, max = 0 ;
+               
+               for( int i = 0 ; i < free.length ; i++ )
+               {
+                       if( free[ i ] > max )
+                       {
+                               max = free[ i ] ;
+                               most = i ;
+                       }
+               }
+               
+               /** Is there any cluster candidate ? **/
+               if( most != -1 )
+               {
+                       ret = gl.getClusters().get( most ).nextGNode() ;
+               }
+               
+               
+               return ret ;
        }
 }