+
+ public Integer deployVM( final String _name, final String _archive, final String _directory )
+ {
+ int nb = 0, pb = 0 ;
+
+ if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0
+ && _directory != null && _directory.length() > 0 )
+ {
+ System.out.println( "Deploying the VM " + _name + " (" + _archive + ") ... " ) ;
+
+ File file = new File( working_directory + "/" + _archive ) ;
+ if( ! file.exists() )
+ {
+ System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
+ file = null ;
+ return 1 ;
+ } else if( file.isDirectory() ) {
+ System.err.println( _archive + " is a directory!" ) ;
+ file = null ;
+ return 1 ;
+ }
+
+ file = null ;
+
+ // TODO do a better deployment !!
+// int ret ;
+// boolean error, ok, server ;
+// ArrayList<ConnectedClient> deployed = new ArrayList<ConnectedClient>() ;
+
+ boolean server = true ;
+ boolean ok ;
+
+ for( int i = 0 ; i < clients.size() ; i++ )
+ {
+// ret = 1 ;
+ nb++ ;
+// error = false ;
+// ok = false ;
+ if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
+ {
+ synchronized( limitThread )
+ {
+ while( limitThread.getNb() >= maxThread )
+ {
+ try {
+ limitThread.wait() ;
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ limitThread.inc() ;
+ }
+
+ final int ind = i ;
+ new Thread( new Runnable() {
+
+ @Override
+ public void run() {
+ int ret = -1 ;
+ boolean error = true ;
+
+ try {
+ ret = clients.get( ind ).getStub().deployVM( _name, _archive, _directory ) ;
+ } catch( RemoteException e ) {
+ System.err.println( "Unable to deploy the VM on " + clients.get( ind ).getName() + "!" ) ;
+ e.printStackTrace() ;
+ }
+
+ // 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( ind ).getName() + " ... " ) ;
+
+ String wd = "" ;
+ String snIP = "" ;
+ error = false ;
+
+ try {
+ wd = clients.get( ind ).getStub().getWorkingDirectory() ;
+ snIP = clients.get( ind ).getStub().getIPHost() ;
+ } catch (RemoteException e2) {
+ System.err.println( "Unable to retrieve information on " + clients.get( ind ).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 )
+ {
+ 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 ;
+ }
+ }
+
+
+ if( ! error )
+ {
+ // Second try ...
+ ret = 1 ;
+ try {
+ ret = clients.get( ind ).getStub().deployVM( _name, _archive, _directory ) ;
+ } catch( RemoteException e ) {
+ System.err.println( "Unable to deploy the VM on " + clients.get( ind ).getName() + "!" ) ;
+ e.printStackTrace() ;
+ }
+ }
+ }
+
+ if( ret == 0 )
+ {
+ System.out.println( "Initial VM archive successfully deployed on " + clients.get( ind ).getName() + "." ) ;
+
+ synchronized( deployingClients )
+ {
+ deployingClients.inc() ;
+ }
+ }
+
+ synchronized( limitThread )
+ {
+ limitThread.dec() ;
+ limitThread.notifyAll() ;
+ }
+ }
+ }).start() ;
+ }
+ }
+ }
+
+ synchronized( limitThread )
+ {
+ while( limitThread.getNb() > 0 )
+ {
+ try {
+ limitThread.wait() ;
+ } catch( InterruptedException e ) {
+ e.printStackTrace() ;
+ }
+ }
+ }
+
+ if( nb - deployingClients.getNb() > 0 )
+ {
+ if( pb == 1 )
+ System.err.println( "** " + pb + " machine is not deployed!" ) ;
+ if( pb > 1 )
+ System.err.println( "** " + pb + " machines are not deployed!" ) ;
+ }
+
+ return nb ;
+ }
+
+
+ public String getWorkingDirectory()
+ {
+ 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 ; }
+ }
+