Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New stable version with the add of the Maheve algorithm.
[mapping.git] / src / and / Mapping / Utils.java
index 7e05ca2..068789d 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.setCluster( names[1] ) ;
+               n.setSite( names[2] ) ;
+               n.setIP( ip ) ;
+               n.setMapped( false ) ;
                
                return n ;
        }
@@ -180,10 +196,10 @@ public class Utils
        /**
         * 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( "" ) )
                {
@@ -193,7 +209,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( "" ) )
@@ -230,10 +246,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( "" ) )
                {
@@ -243,7 +259,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( "" ) )
@@ -278,6 +294,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
@@ -294,7 +361,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 = "" ;
@@ -342,7 +409,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 = "" ;
@@ -373,6 +440,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 Grid" ) ;
+                       e.printStackTrace() ;
+                       return null ;
+               }
+               
+               return mp ;
+       }
+       
 }