Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Utils.java
index 25cd4a7..1cd073b 100644 (file)
@@ -69,7 +69,19 @@ public class Utils
                        e.printStackTrace();
                        System.exit( 1 ) ;
                } 
+               
+               /* Host IP */
+               String ip = null ;
+               try {
+                       InetAddress addr = InetAddress.getLocalHost() ;
+                       ip = addr.getHostAddress() ;
+               } catch( final Exception e ) {
+                       System.err.println( "Unalbe to retrieve host's name !" ) ;
+                       e.printStackTrace();
+                       System.exit( 1 ) ;
+               } 
 
+               String names[] = decodeG5Knames( name ) ;
                
                /* Updating node information */
                n.setFrequency( frequency ) ;
@@ -77,6 +89,10 @@ public class Utils
                n.setMemory( memory ) ;
                n.setNb_cores( nbCore ) ;
                n.setName( name ) ;
+               n.setClusterName( names[1] ) ;
+               n.setSiteName( names[2] ) ;
+               n.setIP( ip ) ;
+               n.setMapped( false ) ;
                
                return n ;
        }
@@ -94,53 +110,36 @@ public class Utils
                
                if( _an != null )
                {
-                       String temp ;
-                       String nom, cluster, site ;
-               
+                       String cluster = "", site = "" ;                
                        
                        ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
                        
                        for( int i = 0 ; i < _an.size() ; i++ )
                        {
-                               /* Variables edition */
-                               String tab[] = new String[ 5 ] ;                                
-                               
-                               temp = _an.get( i ).getName().trim().replace('.', '!' ) ;
-                               
-                               tab = temp.split( "!" ) ;
-                               
-                               nom = tab[ 0 ] ;
-                               site = tab[ 1 ] ;
-                               
-                               tab = nom.split( "-" ) ;
-                               
-                               // IUT
-                               //cluster = "iut" ;
-                               
-                               // G5K
-                               cluster = tab[ 0 ] ;
+                               /* Variables edition */         
                                
-                               //System.out.println( nom + " dans " + cluster + " du site " + site ) ;
+                               cluster = _an.get( i ).getClusterName() ; //names[ 1 ] ;
+                               site = _an.get( i ).getSiteName() ; //names[ 2 ] ;
                                
-                               
-                               /* Insertion du noeud dans son cluster */
-                               boolean trouve = false ;
+                               /* Insertion of the node in its cluster */
+                               boolean found = false ;
                                
                                for( int j = 0 ; j < clusters.size() ; j++ )
                                {
-                                       if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
+                                       if( clusters.get( j ).getName().equalsIgnoreCase( cluster ) 
+                                                       && clusters.get( j ).getSite().equalsIgnoreCase( site ))
                                        {
                                                _an.get( i ).setInCluster( true ) ;
-                                               _an.get( i ).setSite( clusters.get( j ).getSite() ) ;
-                                               _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
+                                               _an.get( i ).setMapped( false ) ;
+                                               _an.get( i ).setCluster( clusters.get( j ) ) ;
                                                clusters.get( j ).addGNode( _an.get( i ) ) ;
-                                               trouve  = true ;
+                                               found  = true ;
                                                
                                                break ;
                                        }
                                }
                                
-                               if( ! trouve )
+                               if( ! found )
                                {
                                        Cluster cl = new Cluster() ;
                                        
@@ -148,8 +147,8 @@ public class Utils
                                        cl.setSite( site ) ;
                                        
                                        _an.get( i ).setInCluster( true ) ;
-                                       _an.get( i ).setSite( site ) ;
-                                       _an.get( i ).setCluster( cluster ) ;
+                                       _an.get( i ).setMapped( false ) ;
+                                       _an.get( i ).setCluster( cl ) ;
                                        
                                        cl.addGNode( _an.get( i ) ) ;
                                        
@@ -165,13 +164,39 @@ public class Utils
        }
 
        
+       /**
+        * Return the three parts of the name of a node on Grid'5000.
+        * @param _name The full name of the G5K node
+        * @return The three parts of the name
+        */
+       public static String[] decodeG5Knames( String _name ) 
+       {
+               String temp = "" ;
+               String tab[] = new String[ 5 ] ;        
+               String ret[] = new String[ 3 ] ;
+               
+               temp = _name.replace('.', '!' ) ;
+               
+               tab = temp.split( "!" ) ;
+               
+               ret[0] = tab[ 0 ] ;   // node name
+               ret[2] = tab[ 1 ] ;   // site name
+               
+               tab = ret[0].split( "-" ) ;
+               
+               ret[ 1 ] = tab[ 0 ] ; // cluster name
+               
+               return ret ;
+       }
+
+       
        /**
         * Write the Grid object in an XML file.
         * @param _gl Grid graph to be write
-        * @param _file File's name 
         * @param _path File's path
+        * @param _file File's name 
         */
-       public static void writeGrid( Grid _gl, String _file, String _path )
+       public static void writeGrid( Grid _gl, String _path, String _file )
        {
                if( _file.equals( "" ) )
                {
@@ -181,7 +206,7 @@ public class Utils
                
                if ( ! _file.endsWith( ".xml" ) )
                {
-                       _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+                       _file = _file + ".xml"; // Adding xml extension to file
                }
                
                if( ! _file.equals( "" ) )
@@ -218,10 +243,10 @@ public class Utils
        /**
         * Write an application Graph in a file.
         * @param _gr Application Graph to be write
-        * @param _file File's name
         * @param _path File's path
+        * @param _file File's name
         */
-       public static void writeGraph( Graph _gr, String _file, String _path )
+       public static void writeGraph( Graph _gr, String _path, String _file )
        {
                if( _file.equals( "" ) )
                {
@@ -231,7 +256,7 @@ public class Utils
                
                if ( ! _file.endsWith( ".xml" ) )
                {
-                       _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+                       _file = _file + ".xml"; // Adding xml extension to file
                }
                
                if( ! _file.equals( "" ) )
@@ -266,6 +291,57 @@ public class Utils
        }
        
        
+       /**
+        * Write a mapping done in a file.
+        * @param _mp The mapping done to be write
+        * @param _path File's path
+        * @param _file File's name
+        */
+       public static void writeMapping( Mapping _mp, String _path, String _file )
+       {
+               if( _file.equals( "" ) )
+               {
+                       System.err.println( "No file's name !\n" ) ;
+                       return ;
+               }
+               
+               if ( ! _file.endsWith( ".xml" ) )
+               {
+                       _file = _file + ".xml"; // Adding xml extension to file
+               }
+               
+               if( ! _file.equals( "" ) )
+               {
+                       String path = "" ;
+                       
+                       if( _path.length() != 0 )
+                       {
+                               path = _path+"/"+_file ;
+                       } else {
+                               path = new String( "./" + _file ) ;
+                       }
+                       
+                       XStream xstream = new XStream( new DomDriver() ) ;
+                       
+                       String xml = xstream.toXML( _mp ) ;
+                       
+                       PrintWriter ecrivain = null ;
+
+                       try {
+                               ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
+
+                               ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
+                               ecrivain.println( xml ) ;
+
+                               ecrivain.close() ;
+                       } catch( Exception e ) {
+                               System.err.println( "\nError during the write opération !\n" ) ;
+                               e.printStackTrace() ;
+                       }
+               }
+       }
+       
+       
        /**
         * Read an application Graph from a file.
         * @param _file File's name
@@ -282,7 +358,7 @@ public class Utils
                
                if ( ! _file.endsWith( ".xml" ) )
                {
-                       _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+                       _file = _file + ".xml"; // Adding xml extension to file
                }
                
                String path = "" ;
@@ -330,7 +406,7 @@ public class Utils
                
                if ( ! _file.endsWith( ".xml" ) )
                {
-                       _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+                       _file = _file + ".xml"; // Adding xml extension to file
                }
 
                String path = "" ;
@@ -361,6 +437,54 @@ public class Utils
                return gr ;
        }
        
+       
+       /**
+        * Read a Mapping done from a file.
+        * @param _file File's name
+        * @param _path File's path
+        * @return The Mapping read
+        */
+       public static Mapping readMapping( String _path, String _file )
+       {       
+               if ( _file.equals( "" ) )
+               {
+                       System.err.println( "No file's name !\n" ) ;
+                       return null ;
+               }       
+               
+               if ( ! _file.endsWith( ".xml" ) )
+               {
+                       _file = _file + ".xml"; // Adding xml extension to file
+               }
+
+               String path = "" ;
+               
+               if( _path.length() != 0 )
+               {
+                       path = _path+"/"+_file ;
+               } else {
+                       path = new String( "./" + _file ) ;
+               }       
+               
+               Mapping mp = null ;
+               
+               XStream xstream = new XStream( new DomDriver() ) ;
+               
+               try {
+                       mp = (Mapping) xstream.fromXML( new FileInputStream( path ) ) ;
+               } catch( FileNotFoundException e ) {
+                       System.err.println( "File not found !\n" ) ;
+                       e.printStackTrace();
+                       return null ;
+               } catch( ClassCastException e ) {
+                       System.err.println( "The file does not contain a valid Mapping" ) ;
+                       e.printStackTrace() ;
+                       return null ;
+               }
+               
+               return mp ;
+       }
+       
 }