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 )
{
- ac.add( gl.getClusters().get( i ).nextGNode() ) ;
+ tmp = gl.getClusters().get( i ).nextGNode() ;
+
+ if( tmp != null )
+ {
+ ac.add( tmp ) ;
+ }
}
}
+
+ /** If there some nodes in clusters **/
+ if( ac.size() > 0 )
+ {
+ double dist = -1, best = -1 ;
+ int bestNode = -1 ;
+
+ /** Searching the replacing node with the lower distance
+ * between it and the failed node
+ */
+ for( int i = 0 ; i < ac.size() ; i++ )
+ {
+ dist = gl.getDistance( _dead, ac.get( i ) ) ;
+
+ if( best == -1 )
+ {
+ best = dist ;
+ bestNode = i ;
+ } else {
+ if( dist < best )
+ {
+ best = dist ;
+ bestNode = i ;
+ }
+ }
+ }
+
+ /** Is there any node candidate ? **/
+ if( bestNode != -1 )
+ {
+ ret = ac.get( bestNode ) ;
+ }
+ }
}
/** Update in cluster the status of nodes **/
@Override
public GNode getOtherGNode( ArrayList<GNode> _ag )
{
+ GNode ret = null ;
- return 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 ;
}
}