Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of the start application time setup.
[hpcvm.git] / src / and / hpcvm / Client.java
index 7d58da8..e3233eb 100644 (file)
@@ -932,29 +932,20 @@ public class Client extends UnicastRemoteObject implements ServicesClient
                
                if( ! lastSaveOk )
                {
-                       String arch1 = "", arch2 = "" ;
-                       File file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar" )  ;
+                       String arch = "" ;
+                       File file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ) ;
                        if( file.exists() )
                        {
-                               arch1 = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar" ;
+                               arch = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
                        }
                        
                        file = null ;
                        
-                       file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ) ;
-                       if( file.exists() )
-                       {
-                               arch2 = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
-                       }
-                       
-                       file = null ;
-                       
-                       if( arch1.length() > 0 || arch2.length() > 0 )
+                       if( arch.length() > 0 )
                        {                       
                                System.out.println( "Deletion of last nok archive ... " ) ;
                        
-                               command = new String[]{ "/bin/rm", "-rf", 
-                                       arch1, arch2 } ;
+                               command = new String[]{ "/bin/rm", "-rf", arch } ;
                                                        
                                try {
                                        procSave = Runtime.getRuntime().exec( command ) ;
@@ -986,9 +977,9 @@ public class Client extends UnicastRemoteObject implements ServicesClient
                        System.out.print( "Creation of the archive ... " ) ;
                        
                        /** Archive creation **/
-                       command = new String[]{ "/bin/tar", "-cf", 
-                                       machine.getName() + "_new_" + machine.getComputationId() + ".tar",
-                                       machine.getDirectory(), "-C", working_directory } ;
+                       command = new String[]{ "/bin/tar", "-cz", "-C", working_directory,
+                                       "-f", working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz",
+                                       machine.getDirectory() } ;
                                
                        if( emergencyStop )
                        {
@@ -1019,42 +1010,6 @@ public class Client extends UnicastRemoteObject implements ServicesClient
                                e.printStackTrace() ;
                                error = true ;
                        }
-               
-                       /** Compression of the archive **/
-                       if( ! error )
-                       {
-                               System.out.print( "Compression of the archive ... " ) ;
-                               command = new String[]{ "/bin/gzip", 
-                                               working_directory + "/" + machine.getName() 
-                                               + "_new_" + machine.getComputationId() + ".tar" } ;
-                               
-                               if( emergencyStop )
-                               {
-                                       return 1 ;
-                               }
-                               
-                               try {
-                                       procSave = Runtime.getRuntime().exec( command ) ;
-                                       procSave.waitFor() ;
-                       
-                                       if( procSave.exitValue() == 0 )
-                                       {
-                                               System.out.println( "Archive successfully compressed." ) ;
-                                       } else {
-                                               System.err.println( "Archive not compressed!" ) ;
-                                               printProcessError( procSave ) ;
-                                               
-                                               error = true ;
-                                       }
-                               } catch( IOException e ) {
-                                       System.err.println( "Error during archive compression command: " ) ;
-                                       e.printStackTrace() ;
-                                       error = true ;
-                               } catch( InterruptedException e ) {
-                                       e.printStackTrace() ;
-                                       error = true ;
-                               }
-                       }
                        
                        long fin = System.currentTimeMillis() ;
                        
@@ -1191,7 +1146,7 @@ public class Client extends UnicastRemoteObject implements ServicesClient
 //             date_last_save = 0 ;
                
                isRestartedSave = false ;
-               lastSaveOk = true ;
+               lastSaveOk = false ;
                
                /** Connection to server **/
                try {
@@ -1290,7 +1245,7 @@ public class Client extends UnicastRemoteObject implements ServicesClient
                                while( (line = br.readLine()) != null )
                                {
                                        System.err.println( line ) ;
-                                       if( line.contains( "egmentation" ) ) 
+                                       if( line.contains( "egmentation" ) || _p.exitValue() == 139 
                                        {
                                                ret = true ;
                                        }
@@ -1303,6 +1258,13 @@ public class Client extends UnicastRemoteObject implements ServicesClient
                return ret ;
        }
        
+       
+       @Override
+       public int echo()
+       {
+               return 0 ;
+       }
+       
 
        @Override
        public int start() 
@@ -1354,31 +1316,33 @@ public class Client extends UnicastRemoteObject implements ServicesClient
        
        private class PingServer extends Thread
        {
-               private boolean run ;
+               protected boolean go ;
                
                PingServer()
                {
-                       run = true ;
+                       go = true ;
                }
                
-               protected void stopPing() { run = false ; }
+               protected void stopPing() { go = false ; }
                
                @Override
                public void run() 
                {
-                       while( run )
+                       while( go )
                        {
                                try {
                                        LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
                                } catch( RemoteException e1 ) {
                                        System.err.println( "Unable to ping the server!" ) ;
                                        e1.printStackTrace() ;
+                                       yield() ;
                                }
                                
                                try {
-                                       Thread.sleep( 2000 ) ;
+                                       sleep( 2000 ) ;
                                } catch( InterruptedException e ) {
                                        e.printStackTrace() ;
+                                       yield() ;
                                }
                        }
                }
@@ -1387,31 +1351,26 @@ public class Client extends UnicastRemoteObject implements ServicesClient
        
        private class DialogVMServer extends Thread
        {
-               private boolean run ;
+               protected boolean go ;
                private Socket socket ;
-               private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
+//             private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
                
                DialogVMServer()
                {
-                       run = true ;
+                       go = true ;
                }
                
                protected void stopDialogVMServer() 
                { 
-                       run = false ;
+                       go = false ;
                        
                        if( serverSocket != null )
                        {
                                try {
-                                       serverSocket.close() ;
-                                       
-                                       for( int i = 0 ; i < dialogs.size() ; i++ )
-                                       {
-                                               dialogs.get( i ).stopDialogVM() ;
-                                       }
-                                       
+                                       serverSocket.close() ;                                  
                                } catch( IOException e ) {
                                        e.printStackTrace() ;
+                                       yield() ;
                                }
                        }                       
                }
@@ -1428,20 +1387,20 @@ public class Client extends UnicastRemoteObject implements ServicesClient
                        } catch( IOException e ) {
                                System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
                                e.printStackTrace() ;
-                               
-                               run = false ;
+                               System.exit( 1 ) ;
                        }       
                        
-                       while( run )
+                       while( go )
                        {                               
                                try {
                                        socket = serverSocket.accept() ;
                                        
-                                       dialogs.add( new DialogVM( socket ) ) ;
-                                       dialogs.get( dialogs.size() - 1 ).start() ;     
+                                       new DialogVM( socket ).start() ;
+
                                } catch( IOException e ) {
                                        System.err.println( "Problem with the accept function!" ) ;
                                        e.printStackTrace() ;
+                                       yield() ;
                                }
                        }
                }
@@ -1450,24 +1409,11 @@ public class Client extends UnicastRemoteObject implements ServicesClient
        
        private class DialogVM extends Thread
        {
-               private boolean run ;
                private Socket socket ;
                private BufferedReader reader ;
                private String line ;
                
-               DialogVM( Socket _socket ) { run = true ; socket = _socket ; }
-       
-               protected void stopDialogVM()
-               {
-                       run = false ;
-
-                       try {
-                               reader.close() ; reader = null ;
-                               socket.close() ; socket = null ;
-                       } catch( IOException e ) {
-                               e.printStackTrace() ;
-                       }
-               }
+               DialogVM( Socket _socket ) { socket = _socket ; }
        
                @Override
                public void run() 
@@ -1476,105 +1422,129 @@ public class Client extends UnicastRemoteObject implements ServicesClient
                                reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
                        } catch( IOException e ) {
                                System.err.println( "Unable to open a dialog socket with the VM!" ) ;
-                               e.printStackTrace();
-                               stopDialogVM() ;
+                               e.printStackTrace() ;
+                               yield() ;
                        }
-                       
-                       while( run )
-                       {
-                               try {
-                                       line = null ;
-                                       
-                                       if( reader != null )
+
+                       try {
+                               line = null ;
+
+                               if( reader != null )
+                               {
+                                       line = reader.readLine() ;
+                               }
+
+                               /** VM is starting -- retrieving informations **/
+                               if( line != null &&  line.equalsIgnoreCase( "infos" ) )
+                               {
+                                       /* Receiving name */
+                                       machine.setName( reader.readLine() ) ;
+
+                                       /* Receiving IP */
+                                       String ip = reader.readLine() ;
+                                       if( ! ip.equalsIgnoreCase( machine.getIp() ) )
                                        {
-                                               line = reader.readLine() ;
+                                               System.err.println( "VM IP not well configured!!" ) ;
                                        }
-                                       
-                                       /** VM is starting -- retrieving informations **/
-                                       if( run && line != null &&  line.equalsIgnoreCase( "infos" ) )
-                                       {
-                                               /* Receiving name */
-                                               machine.setName( reader.readLine() ) ;
-                                               
-                                               /* Receiving IP */
-                                               String ip = reader.readLine() ;
-                                               if( ! ip.equalsIgnoreCase( machine.getIp() ) )
-                                               {
-                                                       System.err.println( "VM IP not well configured!!" ) ;
-                                               }
-                                               
-                                               /* Close streams */
-                                               reader.close() ; reader = null ;
-                                               socket.close() ; socket = null ;
-                                               
-                                               run = false ;
-                                       }
-                                       
+
+                                       /* Close streams */
+                                       reader.close() ; reader = null ;
+                                       socket.close() ; socket = null ;
+
+                               } else if( line != null &&  line.equalsIgnoreCase( "save" ) ) {
                                        /** It's time to do a save **/
-                                       if( run && line != null &&  line.equalsIgnoreCase( "save" ) )
-                                       {                                               
+
+                                       try {
+                                               machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
+                                       } catch( Exception e ) {
+                                               System.err.println( "Problem while reading the computation id!" ) ;
+                                               e.printStackTrace() ;
+                                       }
+
+                                       synchronized( saveRequest ) 
+                                       {
                                                try {
-                                                       machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
-                                               } catch( Exception e ) {
-                                                       System.err.println( "Problem while reading the computation id!" ) ;
+                                                       LocalHost.Instance().getServerStub().requestSave( LocalHost.Instance().getIP() ) ;
+                                               } catch( RemoteException e ) {
+                                                       System.err.println( "Unable to request save to server!" ) ;
                                                        e.printStackTrace() ;
                                                }
-                                               
-                                               synchronized( saveRequest ) 
-                                               {
-                                                       try {
-                                                               LocalHost.Instance().getServerStub().requestSave( LocalHost.Instance().getIP() ) ;
-                                                       } catch( RemoteException e ) {
-                                                               System.err.println( "Unable to request save to server!" ) ;
-                                                               e.printStackTrace() ;
-                                                       }
-                                       
-                                                       try {
-                                                               saveRequest.wait() ;
-                                                       } catch( InterruptedException e ) {
-                                                               e.printStackTrace();
-                                                       }
-                                               }
-                                               
-                                               if( saveRequest.getStatus() )
-                                               {
-                                                       /* Close streams */
-                                                       reader.close() ; reader = null ;
-                                                       socket.close() ; socket = null ;
-                                                       
-                                                       run = false ;
-                                                       
-                                                       saveRequest.setStatus( false ) ;
-                                               
-                                                       /* Requesting the VM save */
-                                                       saveVM() ;
-                                               } else {
-                                                       sendSaveOkVM() ;
-                                               }
-                                       }
-                                       
-                                       
-                                       /** Computation is done, we can shutdown the VM **/
-                                       if( run && line != null &&  line.equalsIgnoreCase( "quit" ) )
-                                       {
+
                                                try {
-                                                       Thread.sleep( 5000 ) ;
+                                                       saveRequest.wait() ;
                                                } catch( InterruptedException e ) {
-                                                       e.printStackTrace() ;
+                                                       e.printStackTrace();
                                                }
-                                               
+                                       }
+
+                                       if( saveRequest.getStatus() )
+                                       {
                                                /* Close streams */
                                                reader.close() ; reader = null ;
                                                socket.close() ; socket = null ;
-                                               
-                                               run = false ;
-                                               
-                                               stopVM() ;
+
+                                               saveRequest.setStatus( false ) ;
+
+                                               /* Requesting the VM save */
+                                               if( saveVM() == 1 )
+                                               {
+                                                       System.err.println( "Problem while saving. Exiting ..." ) ;
+                                                       pingServer.stopPing() ;
+                                                       emergencyStop() ;
+                                                       System.exit( 1 ) ;
+                                               }
+                                       } else {
+                                               sendSaveOkVM() ;
+                                       }
+                               } else if( line != null &&  line.equalsIgnoreCase( "quit" ) ) {
+                                       /** Computation is done, we can shutdown the VM **/
+
+                                       try {
+                                               Thread.sleep( 5000 ) ;
+                                       } catch( InterruptedException e ) {
+                                               e.printStackTrace() ;
+                                       }
+
+                                       /* Close streams */
+                                       reader.close() ; reader = null ;
+                                       socket.close() ; socket = null ;
+
+                                       stopVM() ;
+
+                                       machine.setStatus( "connected" ) ;
+                                       try {
+                                               LocalHost.Instance().getServerStub().changeStatus( 
+                                                               LocalHost.Instance().getIP(), "connected" ) ;
+                                       } catch( RemoteException e ) {
+                                               System.err.println( "Unable to inform the server of the VM status!" ) ;
+                                               e.printStackTrace() ;
                                        }
                                        
-                               } catch( IOException e ) {
-                                       e.printStackTrace() ;
+                                       try {
+                                               LocalHost.Instance().getServerStub().endApplication() ;
+                                       } catch( RemoteException e ) {
+                                               System.err.println( "Unable to inform the server of the end of application!" ) ;
+                                               e.printStackTrace() ;
+                                       }
+                               } else if( line != null &&  line.equalsIgnoreCase( "start" ) ) {
+                                       /** Computation is starting **/
+
+                                       /* Close streams */
+                                       reader.close() ; reader = null ;
+                                       socket.close() ; socket = null ;
+                                       System.out.println( "Application is starting." ) ;
+                                       
+                                       try {
+                                               LocalHost.Instance().getServerStub().goApplication() ;
+                                       } catch( RemoteException e ) {
+                                               System.err.println( "Unable to inform the server of the end of application!" ) ;
+                                               e.printStackTrace() ;
+                                       }
                                }
+
+                       } catch( IOException e ) {
+                               e.printStackTrace() ;
+                               yield() ;
                        }
                }
        }