Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding mechanism for application execution time retrievement plus bugs correction.
[hpcvm.git] / src / and / hpcvm / Server.java
index 2bc7ab8..8f35386 100644 (file)
@@ -81,6 +81,12 @@ public class Server extends UnicastRemoteObject implements ServicesServer
        private String working_directory ;
        private long save_interleave ;
        private Semaphore semaSave ;
+       private OperatingClients startingClients ;
+       private OperatingClients deployingClients ;
+       private LimitThread limitThread ;
+       private int maxThread ;
+       private int ind ;
+       private boolean running ;
        
        
        protected Server() throws RemoteException 
@@ -95,49 +101,52 @@ public class Server extends UnicastRemoteObject implements ServicesServer
        {
                if( _stub != null )
                {
-                       String ip = "" ;
-                       try {
-                               ip = _stub.getIPHost() ;
-                       } catch (RemoteException e) {
-                               e.printStackTrace() ;
-                               return 1 ;
-                       }
-                       
-                       boolean exists = false ;
-                       int i ;
-                       
-                       for( i = 0 ; i < clients.size() ; i++ )
+                       synchronized( clients )
                        {
-                               if( ip.equals( clients.get( i ).getIP() ) )
+                               String ip = "" ;
+                               try {
+                                       ip = _stub.getIPHost() ;
+                               } catch (RemoteException e) {
+                                       e.printStackTrace() ;
+                                       return 1 ;
+                               }
+
+                               boolean exists = false ;
+                               int i ;
+
+                               for( i = 0 ; i < clients.size() ; i++ )
                                {
-                                       exists = true ;
-                                       System.out.println( "Client already connected!" ) ;
-                                       break ;
+                                       if( ip.equals( clients.get( i ).getIP() ) )
+                                       {
+                                               exists = true ;
+                                               System.out.println( "Client already connected!" ) ;
+                                               break ;
+                                       }
                                }
-                       }
-                       
-                       if( exists )
-                       {
-                               System.out.println( "The client stub will be replaced." ) ;
-                               clients.get( i ).setStub( _stub ) ;
-                               System.out.println( "(reconnection of " + clients.get( i ).getName() + ")" ) ;
-                               return 2 ;
-                       } else {
-                               System.out.println( "New connection!" ) ;
-                               clients.add( new ConnectedClient( _stub ) ) ;
-                               System.out.println( "(connection of " + clients.get( clients.size() - 1 ).getName() + ")" ) ;
-                               generateVmIP( ip ) ;
-                               
-                               if( clients.size() == 0 )
+
+                               if( exists )
                                {
-                                       System.out.println( "There is no client connected." ) ;
-                               } else if( clients.size() == 1 ) {
-                                       System.out.println( "There is one client connected." ) ;
+                                       System.out.println( "The client stub will be replaced." ) ;
+                                       clients.get( i ).setStub( _stub ) ;
+                                       System.out.println( "(reconnection of " + clients.get( i ).getName() + ")" ) ;
+                                       return 2 ;
                                } else {
-                                       System.out.println( "There are " + clients.size() + " clients connected." ) ;
-                               }
+                                       System.out.println( "New connection!" ) ;
+                                       clients.add( new ConnectedClient( _stub ) ) ;
+                                       System.out.println( "(connection of " + clients.get( clients.size() - 1 ).getName() + ")" ) ;
+                                       generateVmIP( ip ) ;
 
-                               return 0 ;
+                                       if( clients.size() == 0 )
+                                       {
+                                               System.out.println( "There is no client connected." ) ;
+                                       } else if( clients.size() == 1 ) {
+                                               System.out.println( "There is one client connected." ) ;
+                                       } else {
+                                               System.out.println( "There are " + clients.size() + " clients connected." ) ;
+                                       }
+
+                                       return 0 ;
+                               }
                        }
                }
                
@@ -187,9 +196,10 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                        for( int i = 0 ; i < clients.size() ; i++ )
                        {
                                if( _ip.equals( clients.get( i ).getIP() ) ) 
-                               {
+                               {                                       
                                        clients.get( i ).setStatus( _status ) ;
                                        System.out.println( "Client " + clients.get( i ).getName() + " changed its status to: " + _status ) ;
+                                       
                                        break ;
                                }
                        }
@@ -207,6 +217,14 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                applications = new ArrayList<RunningApplication>() ;
                monitor = null ;
                
+               startingClients = new OperatingClients() ;
+               deployingClients = new OperatingClients() ;
+               limitThread = new LimitThread() ;
+               maxThread = 20 ;
+               
+               ind = -1 ;
+               running = false ;
+               
                working_directory = "/localhome/vmware" ;
                
                save_interleave  = 30 * 60 * 1000 ;
@@ -417,11 +435,11 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                                                }
                                                                
                                                                // Replacing in RunningApplication
-                                                               applications.get( 0 ).replaceComputingClient( cc, ccl ) ;
+                                                               applications.get( ind ).replaceComputingClient( cc, ccl ) ;
                                                                
-                                                               for( int l = 0 ; l < applications.get(0).getComputingClients().size() ; l++ )
+                                                               for( int l = 0 ; l < applications.get( ind ).getComputingClients().size() ; l++ )
                                                                {
-                                                                       applications.get(0).getComputingClients().get( l ).setSaveRequest( false ) ;
+                                                                       applications.get( ind ).getComputingClients().get( l ).setSaveRequest( false ) ;
                                                                }
                                                                
                                                                        
@@ -430,10 +448,6 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                                                System.err.println( "Unable to deploy the save on the new computing client!" ) ;
                                                        }
                                                }
-//                                             } else {
-//                                                     System.err.println( "Problem while launching the VM on " 
-//                                                                     + clients.get(i).getName() + "!" ) ;
-//                                             }
                                                        
                                                if( ok )
                                                {
@@ -524,7 +538,10 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                                        System.out.println( "Maybe it will come back later :)" ) ;
                                                }
                                                
-                                               it.remove() ;
+                                               synchronized( clients )
+                                               {
+                                                       it.remove() ;
+                                               }
                                                nb_disconnections++ ;
                                                change = true ;
                                        }
@@ -532,13 +549,16 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                
                                if( change )
                                {
-                                       if( clients.size() == 0 )
+                                       synchronized( clients )
                                        {
-                                               System.out.println( "There is no client connected." ) ;
-                                       } else if( clients.size() == 1 ) {
-                                               System.out.println( "There is one client connected." ) ;
-                                       } else {
-                                               System.out.println( "There are " + clients.size() + " clients connected." ) ;
+                                               if( clients.size() == 0 )
+                                               {
+                                                       System.out.println( "There is no client connected." ) ;
+                                               } else if( clients.size() == 1 ) {
+                                                       System.out.println( "There is one client connected." ) ;
+                                               } else {
+                                                       System.out.println( "There are " + clients.size() + " clients connected." ) ;
+                                               }
                                        }
                                }
                                
@@ -580,11 +600,11 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                                }
                                        }
                                        
-                                       for( int  i = 0 ; i < applications.get(0).getComputingClients().size() ; i++ )
+                                       for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
                                        {
-                                               final ServicesClient sc = applications.get(0).getComputingClients().get( i ).getClient().getStub() ;
+                                               final ServicesClient sc = applications.get( ind ).getComputingClients().get( i ).getClient().getStub() ;
                                                
-                                               applications.get( 0 ).getComputingClients().get( i ).setRestartOk( false ) ;
+                                               applications.get( ind ).getComputingClients().get( i ).setRestartOk( false ) ;
                                                
                                                new Thread( new Runnable() {
                                                        
@@ -647,7 +667,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                computingClients.get( i ).setSaveStatus( false ) ;
                        }
                        
-                       applications.get( 0 ).setLastSaveDate( System.currentTimeMillis() ) ;
+                       applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
                }
                
                return 0 ;
@@ -663,7 +683,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) ) 
                                {
                                        computingClients.get( i ).setLastSave( _saveName ) ;
-                                       System.out.println( "Save name successfully change for " + _ip ) ;
+                                       System.out.println( "Save name successfully changed on " + _ip ) ;
                                        return 0 ;
                                }
                        }
@@ -677,37 +697,98 @@ public class Server extends UnicastRemoteObject implements ServicesServer
        {
                int nb = clients.size() - computingClients.size() ;
                
-               if( nb > _nb )
+               if( nb > _nb && ! running )
                {
-                       ArrayList<ServicesClient> ac = new ArrayList<ServicesClient>() ;
-                       ArrayList<ComputingClient> tmp = new ArrayList<ComputingClient>() ;
+                       running = true ;
+                       
+                       final ArrayList<ServicesClient> ac = new ArrayList<ServicesClient>() ;
+                       final ArrayList<ComputingClient> tmp = new ArrayList<ComputingClient>() ;
                        
                        RunningApplication app = new RunningApplication( "Test" ) ;
                        
                        int i = 0 ;
+                       boolean ok ;
                        
                        while( i < clients.size() && ac.size() < _nb )
                        {
-                               if( clients.get(i).getStatus().equalsIgnoreCase( "connected" ) ) 
+                               ok = false ;
+                               if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) ) 
                                {
-                                       int res = 1 ;
-                                       try {
-                                               res = clients.get( i ).getStub().startVM( 0 ) ;
-                                       } catch( RemoteException e ) {
-                                               e.printStackTrace();
+                                       synchronized( startingClients )
+                                       {
+                                               while( ac.size() + startingClients.getNb() >= _nb )
+                                               {
+                                                       if( ac.size() == _nb ) break ;
+                                                       
+                                                       try {
+                                                               startingClients.wait() ;
+                                                       } catch (InterruptedException e) {
+                                                               e.printStackTrace();
+                                                       }
+                                               }
+                                               
+                                               if( ac.size() < _nb )
+                                               {
+                                                       startingClients.inc() ;
+                                                       ok = true ;
+                                               }
                                        }
                                        
-                                       if( res == 0 )
+                                       if( ok )
                                        {
-                                               ac.add( clients.get( i ).getStub() ) ;
-                                               clients.get( i ).setStatus( "running" ) ;
-                                               ComputingClient cl = new ComputingClient( clients.get( i ) ) ;
-                                               clients.get( i ).setComputingClient( cl ) ;
-                                               computingClients.add( cl ) ;
-                                               tmp.add( cl ) ;
-                                       } else {
-                                               System.err.println( "Problem while launching the VM on " 
-                                                               + clients.get(i).getName() + "!" ) ;
+                                               synchronized( limitThread )
+                                               {
+                                                       while( limitThread.getNb() >= maxThread )
+                                                       {
+                                                               try {
+                                                                       limitThread.wait() ;
+                                                               } catch (InterruptedException e) {
+                                                                       e.printStackTrace();
+                                                               }
+                                                       } 
+                                                       
+                                                       limitThread.inc() ;
+                                               }
+                                               
+                                               final int indice = i ;
+                                               new Thread( new Runnable(){
+                                                       
+                                                       @Override
+                                                       public void run() 
+                                                       {
+                                                               int res = 1 ;
+                                                               try {
+                                                                       res = clients.get( indice ).getStub().startVM( 0 ) ;
+                                                               } catch( RemoteException e ) {
+                                                                       e.printStackTrace();
+                                                               }
+                                                               
+                                                               if( res == 0 )
+                                                               {
+                                                                       ac.add( clients.get( indice ).getStub() ) ;
+                                                                       clients.get( indice ).setStatus( "running" ) ;
+                                                                       ComputingClient cl = new ComputingClient( clients.get( indice ) ) ;
+                                                                       clients.get( indice ).setComputingClient( cl ) ;
+                                                                       computingClients.add( cl ) ;
+                                                                       tmp.add( cl ) ;
+                                                               } else {
+                                                                       System.err.println( "Problem while launching the VM on " 
+                                                                                       + clients.get( indice ).getName() + "!" ) ;
+                                                               }
+                                                               
+                                                               synchronized( limitThread )
+                                                               {
+                                                                       limitThread.dec() ;
+                                                                       limitThread.notifyAll() ;
+                                                               }
+                                                               
+                                                               synchronized( startingClients )
+                                                               {
+                                                                       startingClients.dec() ;
+                                                                       startingClients.notifyAll() ;
+                                                               }
+                                                       }
+                                               }).start() ;
                                        }
                                }
                                
@@ -718,7 +799,7 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                        {
                                app.setComputingClients( tmp ) ;
                                app.setRunning( true ) ;
-                               app.setStartTime( System.currentTimeMillis() ) ;
+//                             app.setStartTime( System.currentTimeMillis() ) ;
                                
                                int index, index2 ;
                                /* Choosing save neighbors */
@@ -761,6 +842,8 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                        
                        applications.add( app ) ;
                        
+                       ind = applications.indexOf( app ) ;
+                       
                        return ac ;
                }
                
@@ -795,14 +878,14 @@ public class Server extends UnicastRemoteObject implements ServicesServer
        {
                if( applications.size() > 0 && _ip != null && _ip.length() > 0 )
                {
-                       if( (System.currentTimeMillis() - applications.get( 0 ).getLastSaveDate()) > save_interleave )
+                       if( (System.currentTimeMillis() - applications.get( ind ).getLastSaveDate()) > save_interleave )
                        {
                                // Mark it as a requester
-                               for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ )
+                               for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
                                {
-                                       if( applications.get( 0 ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
+                                       if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
                                        {
-                                               applications.get( 0 ).getComputingClients().get( i ).setSaveRequest( true ) ;
+                                               applications.get( ind ).getComputingClients().get( i ).setSaveRequest( true ) ;
                                                                                                
                                                break ;
                                        }
@@ -812,13 +895,13 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                
                                // Is every body ok?
                                boolean ok = false ;
-                               for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ )
+                               for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
                                {
                                        if( i == 0 )
                                        {
-                                               ok = applications.get( 0 ).getComputingClients().get( i ).getSaveRequest() ;
+                                               ok = applications.get( ind ).getComputingClients().get( i ).getSaveRequest() ;
                                        } else {
-                                               ok = ok & applications.get( 0 ).getComputingClients().get( i ).getSaveRequest() ;       
+                                               ok = ok & applications.get( ind ).getComputingClients().get( i ).getSaveRequest() ;     
                                        }
                                        
                                        if( ! ok )
@@ -835,33 +918,33 @@ public class Server extends UnicastRemoteObject implements ServicesServer
 //                                             e1.printStackTrace() ;
 //                                     }
                                        
-                                       for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ )
+                                       for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
                                        {
                                                try {
-                                                       applications.get( 0 ).getComputingClients().get( i ).getClient().getStub().responseSave( true ) ;
-                                                       applications.get( 0 ).getComputingClients().get( i ).setSaveRequest( false ) ;                                                  
+                                                       applications.get( ind ).getComputingClients().get( i ).getClient().getStub().responseSave( true ) ;
+                                                       applications.get( ind ).getComputingClients().get( i ).setSaveRequest( false ) ;                                                        
                                                } catch( RemoteException e ) {
                                                        System.err.println( "Unable to send the save request response to " +
-                                                                       applications.get( 0 ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
+                                                                       applications.get( ind ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
                                                        e.printStackTrace() ; 
                                                }
                                        }
                                        
-                                       applications.get( 0 ).setLastSaveDate( System.currentTimeMillis() ) ;
+                                       applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
                                }
                                
                        } else {
                                semaSave.release() ;
                                
-                               for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ )
+                               for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
                                {
-                                       if( applications.get( 0 ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
+                                       if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
                                        {
                                                try {
-                                                       applications.get( 0 ).getComputingClients().get( i ).getClient().getStub().responseSave( false ) ;
+                                                       applications.get( ind ).getComputingClients().get( i ).getClient().getStub().responseSave( false ) ;
                                                } catch( RemoteException e ) {
                                                        System.err.println( "Unable to send the save request response to " +
-                                                                       applications.get( 0 ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
+                                                                       applications.get( ind ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
                                                        e.printStackTrace() ; 
                                                }
                                                break ;
@@ -880,15 +963,15 @@ public class Server extends UnicastRemoteObject implements ServicesServer
        {
                if( applications.size() > 0 && _ip != null && _ip.length() > 0 )
                {
-                       System.out.println( "Client " + _ip + " has finished to restart ("+applications.get(0).getComputingClients().size()+") ... " ) ;
-                       if( (System.currentTimeMillis() - applications.get( 0 ).getLastSaveDate()) > save_interleave )
+                       System.out.println( "Client " + _ip + " has finished to restart ("+applications.get( ind ).getComputingClients().size()+") ... " ) ;
+                       if( (System.currentTimeMillis() - applications.get( ind ).getLastSaveDate()) > save_interleave )
                        {
                                // Has it already finished?
-                               for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ )
+                               for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
                                {
-                                       if( applications.get( 0 ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
+                                       if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
                                        {
-                                               applications.get( 0 ).getComputingClients().get( i ).setRestartOk( true ) ;
+                                               applications.get( ind ).getComputingClients().get( i ).setRestartOk( true ) ;
                                                                                                
                                                break ;
                                        }
@@ -896,13 +979,13 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                
                                // Is every body ok?
                                boolean ok = false ;
-                               for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ )
+                               for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
                                {
                                        if( i == 0 )
                                        {
-                                               ok = applications.get( 0 ).getComputingClients().get( i ).getRestartOk() ;
+                                               ok = applications.get( ind ).getComputingClients().get( i ).getRestartOk() ;
                                        } else {
-                                               ok = ok & applications.get( 0 ).getComputingClients().get( i ).getRestartOk() ; 
+                                               ok = ok & applications.get( ind ).getComputingClients().get( i ).getRestartOk() ;       
                                        }
                                        
                                        if( ! ok )
@@ -913,11 +996,11 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                                
                                if( ok )
                                {
-                                       applications.get( 0 ).setLastSaveDate( System.currentTimeMillis() ) ;
+                                       applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
                                        
-                                       for( int i = 0 ; i < applications.get( 0 ).getComputingClients().size() ; i++ )
+                                       for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
                                        {
-                                               applications.get( 0 ).getComputingClients().get( i ).setRestartOk( false ) ;
+                                               applications.get( ind ).getComputingClients().get( i ).setRestartOk( false ) ;
                                        }
                                }
                                
@@ -925,32 +1008,54 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                }
        }       
        
+       
+       @Override
+       public void goApplication()
+       {
+               synchronized( applications ) {
+               if( running && applications.get( ind ).getStartTime() != 0 )
+               {
+                       applications.get( ind ).setStartTime( System.currentTimeMillis() ) ;
+                       applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
+               }}
+       }
+       
 
        @Override
        public void endApplication() 
        {
-               Iterator<ComputingClient> it = computingClients.iterator() ;
-               
-               while( it.hasNext() )
+               synchronized( applications )
+               {
+               if( running )
                {
-                       ComputingClient cl = it.next() ;
+                       Iterator<ComputingClient> it = computingClients.iterator() ;
+               
+                       while( it.hasNext() )
+                       {
+                               ComputingClient cl = it.next() ;
 
-                       try {
-                               cl.getClient().getStub().emergencyStop() ;
-                       } catch (RemoteException e) {
-                               e.printStackTrace();
-                       }
+                               try {
+                                       cl.getClient().getStub().emergencyStop() ;
+                               } catch (RemoteException e) {
+                                       e.printStackTrace();
+                               }
                        
-                       cl.getClient().setStatus( "connected" ) ;
-                       cl.getClient().setComputingClient( null ) ;
-                       it.remove() ;
-                       cl = null ;
-               }
-               
-               applications.get( 0 ).setEndTime( System.currentTimeMillis() ) ;
-               applications.get( 0 ).setRunning( false ) ;
-               applications.get( 0 ).clear() ;
+                               cl.getClient().setStatus( "connected" ) ;
+                               cl.getClient().setComputingClient( null ) ;
+                               it.remove() ;
+                               cl = null ;
+                       }
                
+                       applications.get( ind ).setEndTime( System.currentTimeMillis() ) ;
+                       applications.get( ind ).setRunning( false ) ;
+                       applications.get( ind ).clear() ;
+//                     applications.remove( ind ) ;
+                       
+                       running = false ;
+                       
+                       System.out.println( "Application " + applications.get( ind ).getName() + " ends in " + 
+                                       applications.get( ind ).getExecutionTime() + " seconds." ) ;
+               }}
        }
 
 
@@ -973,10 +1078,9 @@ public class Server extends UnicastRemoteObject implements ServicesServer
        }
        
        
-       public Integer deployVM( String _name, String _archive, String _directory )
+       public Integer deployVM( final String _name, final String _archive, final String _directory )
        {
-               int pb = -1 ;
-               int nb = 0 ;
+               int nb = 0, pb = 0 ;
                
                if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 
                                && _directory != null && _directory.length() > 0 )
@@ -998,112 +1102,156 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                        file = null ;
                        
                        // TODO do a better deployment !!
-                       int ret ;
-                       boolean error ;
+//                     int ret ;
+//                     boolean error, ok, server ;
+//                     ArrayList<ConnectedClient> deployed = new ArrayList<ConnectedClient>() ;
                        
-                       pb = 0 ;
+//                     boolean server = true ;
+//                     boolean ok ;
                        
                        for( int i = 0 ; i < clients.size() ; i++ )
                        {
-                               ret = 1 ;
-                               error = false ;
+//                             ret = 1 ;
+                               nb++ ;
+//                             error = false ;
+//                             ok = false ;
                                if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
-                               {
-                                       try {
-                                               ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ;
-                                       } catch( RemoteException e ) {
-                                               System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ;
-                                               e.printStackTrace() ;
+                               {                                       
+                                       synchronized( limitThread )
+                                       {
+                                               while( limitThread.getNb() >= maxThread )
+                                               {
+                                                       try {
+                                                               limitThread.wait() ;
+                                                       } catch (InterruptedException e) {
+                                                               e.printStackTrace();
+                                                       }
+                                               } 
+       
+                                               limitThread.inc() ;
                                        }
                                        
-                                       // The client does not have the archive, we have to send it.
-                                       if( ret == 2 )
-                                       {
-                                               System.out.print( "Sending VM archive to " + clients.get( i ).getName() + " ... " ) ;
+                                       final int indice = i ;
+                                       new Thread( new Runnable() {
+
+                                               @Override
+                                               public void run() {
+                                                       int ret = -1 ;
+                                                       boolean error = true  ;
+                                                       
+                                                       try {
+                                                               ret = clients.get( indice ).getStub().deployVM( _name, _archive, _directory ) ;
+                                                       } catch( RemoteException e ) {
+                                                               System.err.println( "Unable to deploy the VM on " + clients.get( indice ).getName() + "!" ) ;
+                                                               e.printStackTrace() ;
+                                                       }
                                        
-                                               String wd = "" ;
-                                               String snIP = "" ;
-                                               error = false ;
+                                                       // The client does not have the archive, we have to send it.
+                                                       if( ret == 2 )
+                                                       {
+                                                               // Attention au multi-envois !!!
+                                                               System.out.print( "Sending VM archive to " + clients.get( indice ).getName() + " ... " ) ;
                                        
-                                               try {
-                                                       wd = clients.get( i ).getStub().getWorkingDirectory() ;
-                                                       snIP = clients.get( i ).getStub().getIPHost() ;
-                                               } catch (RemoteException e2) {
-                                                       System.err.println( "Unable to retrieve information on " + clients.get( i ).getName() + "!" ) ;
-                                                       e2.printStackTrace() ;
-                                                       error = true ;
-                                                       pb++ ;
-                                               }
+                                                               String wd = "" ;
+                                                               String snIP = "" ;
+                                                               error = false ;
                                        
-                                               String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive,
-                                                               snIP + ":" + wd } ;
+                                                               try {
+                                                                       wd = clients.get( indice ).getStub().getWorkingDirectory() ;
+                                                                       snIP = clients.get( indice ).getStub().getIPHost() ;
+                                                               } catch (RemoteException e2) {
+                                                                       System.err.println( "Unable to retrieve information on " + clients.get( indice ).getName() + "!" ) ;
+                                                                       e2.printStackTrace() ;
+                                                                       error = true ;
+                                                               }
+                                       
+                                                               String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive,
+                                                                               snIP + ":" + wd } ;
                                
-                                               if( ! error )
-                                               {
-                                                       try {
-                                                               Process proc = Runtime.getRuntime().exec( command ) ;
-                                                               proc.waitFor() ;
-                       
-                                                               if( proc.exitValue() == 0 )
+                                                               if( ! error )
                                                                {
-                                                                       System.out.println( "Initial VM archive successfully sent." ) ;
-                                                               } else {
-                                                                       System.err.println( "Initial VM archive not sent!" ) ;
-                                                                       System.err.println( "Error: " + proc.exitValue() ) ;
-                                                                       BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ;
-                                                       
-                                                                       String l ;
                                                                        try {
-                                                                               while( (l = b.readLine()) != null )
+                                                                               Process proc = Runtime.getRuntime().exec( command ) ;
+                                                                               proc.waitFor() ;
+                       
+                                                                               if( proc.exitValue() == 0 )
                                                                                {
-                                                                                       System.err.println( l ) ;
+                                                                                       System.out.println( "Initial VM archive successfully sent." ) ;
+                                                                               } else {
+                                                                                       System.err.println( "Initial VM archive not sent!" ) ;
+                                                                                       System.err.println( "Error: " + proc.exitValue() ) ;
+                                                                                       BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ;
+                                                       
+                                                                                       String l ;
+                                                                                       try {
+                                                                                               while( (l = b.readLine()) != null )
+                                                                                               {
+                                                                                                       System.err.println( l ) ;
+                                                                                               }
+                                                                                       } catch( IOException e ) {
+                                                                                               e.printStackTrace() ;
+                                                                                       }
+                                                       
+                                                                                       error = true ;
                                                                                }
                                                                        } catch( IOException e ) {
+                                                                               System.err.println( "Error during initial VM archive send command: " ) ;
                                                                                e.printStackTrace() ;
+                                                                               error = true ;
+                                                                       } catch( InterruptedException e ) {
+                                                                               e.printStackTrace() ;
+                                                                               error = true ;
                                                                        }
-                                                       
-                                                                       error = true ;
-                                                                       pb++ ;
                                                                }
-                                                       } catch( IOException e ) {
-                                                               System.err.println( "Error during initial VM archive send command: " ) ;
-                                                               e.printStackTrace() ;
-                                                               error = true ;
-                                                               pb++ ;
-                                                       } catch( InterruptedException e ) {
-                                                               e.printStackTrace() ;
-                                                               error = true ;
-                                                               pb++ ;
-                                                       }
-                                               }
-                               
                                
-                                               if( error )
-                                               {
-                                                       continue ;
-                                               }
+                                                               
+                                                               if( ! error )
+                                                               {                               
+                                                                       // Second try ...
+                                                                       ret = 1 ;
+                                                                       try {
+                                                                               ret = clients.get( indice ).getStub().deployVM( _name, _archive, _directory ) ;
+                                                                       } catch( RemoteException e ) {
+                                                                               System.err.println( "Unable to deploy the VM on " + clients.get( indice ).getName() + "!" ) ;
+                                                                               e.printStackTrace() ;
+                                                                       }
+                                                               }
+                                                       }
                                
-                                               // Second try ...
-                                               ret = 1 ;
-                                               try {
-                                                       ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ;
-                                               } catch( RemoteException e ) {
-                                                       System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ;
-                                                       e.printStackTrace() ;
-                                                       pb++ ;
+                                                       if( ret == 0 )
+                                                       {
+                                                               System.out.println( "Initial VM archive successfully deployed on " + clients.get( indice ).getName() + "." ) ;
+                                                               
+                                                               synchronized( deployingClients )
+                                                               {
+                                                                       deployingClients.inc() ;
+                                                               }
+                                                       }
+                                                       
+                                                       synchronized( limitThread )
+                                                       {
+                                                               limitThread.dec() ;
+                                                               limitThread.notifyAll() ;
+                                                       }
                                                }
-                                       }
-                               
-                                       if( ret == 0 )
-                                       {
-                                               System.out.println( "Initial VM archive successfully deployed on " + clients.get( i ).getName() + "." ) ;
-                                               nb++ ;
-                                       }
+                                       }).start() ;            
+                               }
+                       }
+               }
+               
+               synchronized( limitThread )
+               {
+                       while( limitThread.getNb() > 0  )
+                       {
+                               try {
+                                       limitThread.wait() ;
+                               } catch( InterruptedException e ) {
+                                       e.printStackTrace() ;
                                }
                        }
                }
                
-               if( pb > 0 )
+               if( nb - deployingClients.getNb() > 0 )
                {
                        if( pb == 1 )
                                System.err.println( "** " + pb + " machine is not deployed!" ) ;
@@ -1120,6 +1268,34 @@ public class Server extends UnicastRemoteObject implements ServicesServer
                return working_directory ;
        }
        
+       
+       private class OperatingClients
+       {
+               private int nb ;
+               
+               OperatingClients() { nb = 0 ; }
+               
+               protected void inc() { nb++ ; }
+               
+               protected void dec() { nb-- ; }
+               
+               protected int getNb() { return nb ; }
+       }
+       
+       
+       private class LimitThread
+       {
+               private int nb ;
+               
+               LimitThread() { nb = 0 ; }
+               
+               protected void inc() { nb++ ; }
+               
+               protected void dec() { nb-- ; }
+               
+               protected int getNb() { return nb ; }
+       }
+       
 }
 
 /** La programmation est un art, respectons ceux qui la pratiquent !! **/