Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implementation of multiple mapping algorithm possibility.
[jaceP2P.git] / src / jaceP2P / JaceSuperNodeServer.java
index d2f5786..5f74bbb 100644 (file)
@@ -24,7 +24,7 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
        // Attributes
        private int beat;
        private ArrayList<GNode> gnodes = null;
-       private Algo al = null ;
+       private ArrayList<Algo> algos = null ;
        private int count = 0 ;
        private boolean inDemand = false, operating = false, authorized = false ;
 //     private boolean daemonListChange ;
@@ -35,6 +35,7 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                super() ;
                beat = timeBeat ;
                gnodes = new ArrayList<GNode>() ;
+               algos = new ArrayList<Algo>() ;
 //             daemonListChange = true ;
        }
 
@@ -220,14 +221,14 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                        int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
 
                // Have we the correct application ?
-               if (t == null) {
-                       System.err.println( "Problem of class transmission !" ) ;
+               if( t == null ) {
+                       System.err.println( "Problem of class transmission!" ) ;
                        return null ;
                }
                
                if( t.getDependencies( 0 ) == null )
                {
-                       System.err.println( "No redifinition of getDependencies() function!" ) ;
+                       System.err.println( "No redifinition of getDependencies() function!" ) ;
                        return null ;
                }
                
@@ -241,29 +242,33 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                Grid grid = Utils.createGridG5k(gnodes);
 //             grid.initClusters();
                
+               
+               /** Informing about the grid heterogeneity **/
+               System.out.println( "\nHeterogeneity of the grid : " + grid.getHeterogenityDegre() + "\n" ) ;
+               
 
                /** Creation of tasks GTask **/
                ArrayList<GTask> ts = new ArrayList<GTask>();
-               for (int i = 0; i < nbTasks; i++) {
-                       ts.add(new GTask( i ) ) ;
+               for( int i = 0; i < nbTasks; i++ ) {
+                       ts.add( new GTask( i ) ) ;
                }
 
                /** Research of dependencies **/
-               for (int i = 0; i < nbTasks; i++) {
-                       int dep[] = null;
-                       dep = t.getDependencies(i);
+               for( int i = 0; i < nbTasks; i++ ) {
+                       int dep[] = null ;
+                       dep = t.getDependencies( i ) ;
 
                        /** Adding dependencies to tasks **/
-                       for (int j = 0; j < dep.length; j++) {
-                               if (dep[j] != -1) {
-                                       ts.get(i).addDependance(ts.get(dep[j]));
+                       for( int j = 0; j < dep.length; j++ ) {
+                               if( dep[j] != -1 ) {
+                                       ts.get( i ).addDependance( ts.get( dep[ j ] ) ) ;
                                } else {
-                                       break;
+                                       break ;
                                }
                        }
                }
                
-
+               /** Creation of the application interaction graph **/
                Graph graph = new Graph();
 
                for( int i = 0 ; i < ts.size() ; i++) 
@@ -285,9 +290,9 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                // } catch( Exception e ) {}
 
                /** Selection of the mapping algorithm **/
-               al = null ;
+               Algo al = null ;
 
-               switch (algo) {
+               switch( algo ) {
                case 0:
                        al = new Simple(graph, grid);
                        break;
@@ -301,7 +306,8 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                        al = new DefaultMapping( graph, grid, gnodes ) ;
                }
 
-               if (al != null) {
+               if (al != null) 
+               {
                        /** Launching the Mapping **/
                        al.map();
 
@@ -313,7 +319,7 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                        
                        for( int i = 0; i < ag.size(); i++ ) {
                                reg.addNode((Node) ag.get(i).getNode());
-                               delGNodeFromList( (Node) ag.get(i).getNode(), 2 ) ;
+                               delGNodeFromList( (Node) ag.get(i).getNode(), 2, spawnerIP ) ;
 //                             gnodes.remove(ag.get(i));
 //                             Register.Instance().removeNode((Node) ag.get(i).getNode());
                        }
@@ -329,7 +335,7 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                                {
                                        sup = al.getOtherGNode( gnodes ) ;
                                        reg.addNodeBeg( (Node) sup.getNode() ) ;
-                                       delGNodeFromList( (Node) sup.getNode(), 2 ) ;
+                                       delGNodeFromList( (Node) sup.getNode(), 2, spawnerIP ) ;
                                }
                        }
                        
@@ -338,6 +344,12 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                        if (ag.size() != 0) {
                                SuperNodeListe.Instance().forwardCountNode();
                        }
+                       
+                       /** Setting the algorithm's identifier **/
+                       al.setIdS( spawnerIP ) ;
+                       
+                       /** Adding the algorithm in the list **/
+                       algos.add( al ) ;
 
                } 
                
@@ -364,7 +376,7 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
         * @author S&eacute;bastien Miqu&eacute;e        
         */
        @Override
-       public GNode delGNodeFromList( Node _n, int _mode ) throws RemoteException
+       public GNode delGNodeFromList( Node _n, int _mode, String _spawnerIp ) throws RemoteException
        {
                GNode removedGNode = null ;
                
@@ -395,7 +407,16 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                        /** The dead node is in the mapping **/
                        if( _mode == 1 )
                        {
-                               ArrayList<GNode> temp = al.getMapping().getMappedGNodes() ;
+                               /** Searching the algorithm **/ 
+                               int idAlgo = searchAlgo( _spawnerIp ) ;
+                               
+                               if( idAlgo == -1 )
+                               {
+                                       System.err.println( "No algorithm found for this Spawner!" ) ;
+                                       return null ;
+                               }
+                               
+                               ArrayList<GNode> temp = algos.get( idAlgo ).getMapping().getMappedGNodes() ;
                                
                                for( int i = 0 ; i < temp.size() ; i++ )
                                {
@@ -410,11 +431,16 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                        
                        /** Removing the dead node from the Grid **/
                        if( _mode == 0 || _mode == 1 ) {
-                               al.getGrid().removeGNode( removedGNode ) ;
+                               int idAlgo = searchAlgo( _spawnerIp ) ;
+                               
+                               if( idAlgo != -1 && _mode == 1 )
+                               {
+                                       algos.get( idAlgo ).getGrid().removeGNode( removedGNode ) ;
+                               }
                        }
                        
                        /** Propagation of the deletion **/
-                       SuperNodeListe.Instance().removeGNode( removedGNode, _mode ) ;
+                       SuperNodeListe.Instance().removeGNode( removedGNode, _mode, _spawnerIp ) ;
                        
                        if( free )
                        {
@@ -427,6 +453,27 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
        }
        
        
+       /**
+        * Search the position of the mapping algorithm in the list.
+        * @param _id Identifier of the algorithm
+        * @return The position in the list
+        */
+       private int searchAlgo( String _id ) 
+       {
+               int id = -1 ;
+               
+               for( int i = 0 ; i < algos.size() ; i++ )
+               {
+                       if( algos.get( i ).getIdS().equals( _id ) )
+                       {
+                               id = i ; 
+                               break ;
+                       }
+               }
+               
+               return id ;
+       }
+
        /**
         * Determine if this SuperNode can operate on the gnodes list.
         * It asks other SuperNodes to know if their are working on this list
@@ -479,6 +526,14 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
        {
                Node node = null ;
                GNode remp = null, gnode = null ;
+
+               int idAlgo = searchAlgo( _spawnerIP ) ;
+               
+               if( idAlgo == -1 )
+               {
+                       System.err.println( "No algorithm found for this Spawner!" ) ;
+                       return null ;
+               }
                
                /** Can we use gnodes ?**/
                workingOnGnodes() ;
@@ -487,9 +542,9 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                {
                        authorized = true ;
                        
-                       gnode = delGNodeFromList( _deadNode, 1 ) ;
+                       gnode = delGNodeFromList( _deadNode, 1, _spawnerIP ) ;
 
-                       remp = al.replaceNode( gnode, gnodes ) ;
+                       remp = algos.get( idAlgo ).replaceNode( gnode, gnodes ) ;
                
                        if( remp != null )
                        {
@@ -497,7 +552,7 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                                
                                remp.setMapped( true ) ;
                                
-                               delGNodeFromList( (Node) remp.getNode(), 2 ) ;
+                               delGNodeFromList( (Node) remp.getNode(), 2, _spawnerIP ) ;
                                
                                authorized = false ;
                                
@@ -506,7 +561,7 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                                System.err.println( "Replacing node not found !!" ) ;
                        }
                } else {
-                       remp = al.getOtherGNode( gnodes ) ;
+                       remp = algos.get( idAlgo ).getOtherGNode( gnodes ) ;
                        
                        if( remp != null )
                        {
@@ -516,7 +571,7 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                                
                                authorized = true ;
                                
-                               delGNodeFromList( (Node) remp.getNode(), 2 ) ;
+                               delGNodeFromList( (Node) remp.getNode(), 2, _spawnerIP ) ;
                                
                                authorized = false ;
                                
@@ -526,10 +581,15 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                        }
                }
                
+               if( remp != null )
+               {
+                       node = (Node) remp.getNode() ;
+               }
+               
                
                /** Updating all mapping **/
-               updateMappedNode( gnode, remp ) ;
-               propagateReplaceNode( gnode, remp ) ;
+               updateMappedNode( gnode, remp, _spawnerIP ) ;
+               propagateReplaceNode( gnode, remp, _spawnerIP ) ;
                
                
                /** Free the gnodes use **/
@@ -548,26 +608,34 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
         * @author S&eacute;bastien Miqu&eacute;e
         */
        @Override
-       public void updateMappedNode( GNode _dead, GNode _remp ) 
+       public void updateMappedNode( GNode _dead, GNode _remp, String _spawnerIP ) 
        {
                if( _remp != null )
                {
                        int pos = 0 ;
+                       
+                       int idAlgo = searchAlgo( _spawnerIP ) ;
+                       
+                       if( idAlgo == -1 )
+                       {
+                               System.err.println( "No algorithm found for this Spawner!" ) ;
+                               return ;
+                       }
                
-                       pos = al.getMapping().getIdOfAssociation( _dead ) ;
+                       pos = algos.get( idAlgo ).getMapping().getIdOfAssociation( _dead ) ;
                
                        if( pos != -1 )
                        {
                                /** Changing the node in the mapping **/
-                               al.getMapping().getMapping().get( pos ).setGNode( _remp ) ;
+                               algos.get( idAlgo ).getMapping().getMapping().get( pos ).setGNode( _remp ) ;
                                
                                System.out.println( "Succesfully replacing the fallen node in the mapping." ) ;
                        }               
                        
                        /** Changing the status in the grid **/
-                       al.getGrid().setMappedStatus( _remp, true ) ;
+                       algos.get( idAlgo ).getGrid().setMappedStatus( _remp, true ) ;
                
-                       al.updateGrid() ;
+                       algos.get( idAlgo ).updateGrid() ;
                } else {
                        System.err.println( "The new GNode is null!" ) ;
                }
@@ -581,11 +649,11 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
         * 
         * @author S&eacute;bastien Miqu&eacute;e        
         */
-       private void propagateReplaceNode( GNode _dead, GNode _remp ) throws RemoteException
+       private void propagateReplaceNode( GNode _dead, GNode _remp, String _spawnerIP ) throws RemoteException
        {
                if( _remp != null )
                {       
-                       SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp ) ;
+                       SuperNodeListe.Instance().propagateReplaceNode( _dead, _remp, _spawnerIP ) ;
                }
        }
 
@@ -659,7 +727,8 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
        @Override
        public void setMapping( Algo _al ) throws RemoteException 
        {
-               al = _al ;              
+               if( searchAlgo( _al.getIdS() ) == -1 )
+                       algos.add( _al ) ;                      
        }
 
        
@@ -702,6 +771,31 @@ public class JaceSuperNodeServer extends UnicastRemoteObject implements
                return gnodes ;
        }
        
+       
+       /**
+        * Remove a mapping algorithm of the algorithms list.
+        * @param _id The algorithm identifier
+        * @param _mode Indicate if the information should be transmitted
+        * 
+        * @author S&eacute;bastien Miqu&eacute;e
+        */
+       public void removeAlgo( String _id, int _mode ) throws RemoteException
+       {
+               int i ;
+               
+               i = searchAlgo( _id ) ;
+               
+               if( i != -1 )
+               {
+                       algos.remove( i ) ;
+                       
+                       if( _mode == 0 )
+                       {
+                               SuperNodeListe.Instance().removeAlgo( _id ) ;
+                       }
+               }
+       }
+       
 }
 
 /** ! **/