3 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.rmi.Naming;
8 import java.rmi.RemoteException;
9 import java.rmi.registry.LocateRegistry;
10 import java.rmi.registry.Registry;
11 import java.rmi.server.UnicastRemoteObject;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.concurrent.Semaphore;
17 public class Server extends UnicastRemoteObject implements ServicesServer
19 private class DiscCount
23 DiscCount() { nb = 0 ; }
25 protected void inc() { nb++ ; }
27 protected void dec() {
34 protected int getNb() { return nb ; }
38 private class IPAssociation
41 private String hostIP ;
49 protected void setVmIP( String _vmIP )
54 protected void setHostIP( String _hostIP )
59 protected String getVmIP()
64 protected String getHostIP()
72 private static final long serialVersionUID = 1L ;
74 private ArrayList<ConnectedClient> clients ;
75 private ArrayList<ComputingClient> computingClients ;
76 private ArrayList<RunningApplication> applications ;
77 private int max_timeout ;
78 private ConnectedMonitor monitor ;
79 private DiscCount counter ;
80 private ArrayList<IPAssociation> vmIPs ;
81 private String working_directory ;
82 private long save_interleave ;
83 private Semaphore semaSave ;
84 private OperatingClients startingClients ;
85 private OperatingClients deployingClients ;
86 private LimitThread limitThread ;
87 private int maxThread ;
89 private boolean running ;
92 protected Server() throws RemoteException
100 public Integer register( ServicesClient _stub )
104 synchronized( clients )
108 ip = _stub.getIPHost() ;
109 } catch (RemoteException e) {
110 e.printStackTrace() ;
114 boolean exists = false ;
117 for( i = 0 ; i < clients.size() ; i++ )
119 if( ip.equals( clients.get( i ).getIP() ) )
122 System.out.println( "Client already connected!" ) ;
129 System.out.println( "The client stub will be replaced." ) ;
130 clients.get( i ).setStub( _stub ) ;
131 System.out.println( "(reconnection of " + clients.get( i ).getName() + ")" ) ;
134 System.out.println( "New connection!" ) ;
135 clients.add( new ConnectedClient( _stub ) ) ;
136 System.out.println( "(connection of " + clients.get( clients.size() - 1 ).getName() + ")" ) ;
139 if( clients.size() == 0 )
141 System.out.println( "There is no client connected." ) ;
142 } else if( clients.size() == 1 ) {
143 System.out.println( "There is one client connected." ) ;
145 System.out.println( "There are " + clients.size() + " clients connected." ) ;
157 private void generateVmIP( String _ip )
159 if( _ip != null && ! _ip.equals( "" ) )
161 for( int i = 0 ; i < vmIPs.size() ; i++ )
163 if( vmIPs.get( i ).getHostIP().equalsIgnoreCase( "" ) )
165 vmIPs.get( i ).setHostIP( _ip ) ;
175 public void ping( String _ip )
179 for( int i = 0 ; i < clients.size() ; i++ )
181 if( _ip.equals( clients.get( i ).getIP() ) )
183 clients.get( i ).resetTimeout() ;
192 public void changeStatus( String _ip, String _status )
194 if( _ip != null && _status != null )
196 for( int i = 0 ; i < clients.size() ; i++ )
198 if( _ip.equals( clients.get( i ).getIP() ) )
200 clients.get( i ).setStatus( _status ) ;
201 System.out.println( "Client " + clients.get( i ).getName() + " changed its status to: " + _status ) ;
210 public void init( int _port )
215 clients = new ArrayList<ConnectedClient>() ;
216 computingClients = new ArrayList<ComputingClient>() ;
217 applications = new ArrayList<RunningApplication>() ;
220 startingClients = new OperatingClients() ;
221 deployingClients = new OperatingClients() ;
222 limitThread = new LimitThread() ;
228 working_directory = "/localhome/vmware" ;
230 save_interleave = 30 * 60 * 1000 ;
232 semaSave = new Semaphore( 1 ) ;
236 vmIPs = new ArrayList<IPAssociation>() ;
237 // TODO initialisation of VM IPs
238 for( int i = 2 ; i < 101 ; i++ )
240 vmIPs.add( new IPAssociation() ) ;
241 vmIPs.get( vmIPs.size() - 1 ).setVmIP( "10.11.10." + i ) ;
244 clients = new ArrayList<ConnectedClient>() ;
246 counter = new DiscCount() ;
248 monitor = new ConnectedMonitor() ;
252 // Check if there are running applications ... and restart them :)
258 if( monitor != null ) { monitor.stopMonitor() ; }
260 for( int i = 0 ; i < clients.size() ; i++ )
263 clients.get( i ).getStub().emergencyStop() ;
264 clients.get( i ).getStub().stop() ;
265 } catch (RemoteException e) {
266 System.err.println( "Unable to send stop signal to " + clients.get( i ).getName() ) ;
267 e.printStackTrace() ;
277 private void exportObject()
279 ServicesServer ref = null ;
280 Registry reg = null ;
286 reg = LocateRegistry.getRegistry( port ) ;
288 String tab[] = reg.list() ;
290 System.out.println( "There is an existing RMI Registry on port " +
291 port + " with " + tab.length + " entries!" ) ;
292 for( int i = 0 ; i < tab.length ; i++ )
295 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
297 System.out.println( "Register successfuly deleted!" ) ;
299 System.err.println( "Register undeleted !!!" ) ;
301 } catch( Exception e ) {
302 e.printStackTrace() ;
306 } catch( RemoteException e ) {
310 if ( System.getSecurityManager() == null )
312 System.setSecurityManager( new SecurityManager() ) ;
315 LocateRegistry.createRegistry( port ) ;
316 LocateRegistry.getRegistry( port ).rebind( "Server", this ) ;
317 ref = (ServicesServer) Naming.lookup( "rmi://"
318 + LocalHost.Instance().getIP() + ":" + port
320 } catch ( Exception e ) {
321 System.err.println( "Error in Server.exportObject() when creating local services:" + e ) ;
322 System.err.println( "Exit from Server.exportObject" ) ;
326 LocalHost.Instance().setServerStub( ref ) ;
328 System.out.println( "Server launched on IP " + LocalHost.Instance().getIP() +
329 " on port " + port + "." ) ;
332 /** Fault manager thread **/
333 private class FaultManager extends Thread
337 FaultManager( ConnectedClient _cl )
345 if( cl != null && cl.getStatus().equalsIgnoreCase( "running" ) ||
346 cl.getStatus().equalsIgnoreCase( "saving" ) )
348 ComputingClient cc = cl.getComputingClient() ;
349 // ServicesClient dead = cc.getClient().getStub() ;
350 String ipDead = cc.getClient().getIP() ;
351 SaveNeighbor snDead = null ;
352 for( int i = 0 ; i < computingClients.size() ; i++ )
354 if( computingClients.get( i ).getSaveNeighbor().getIPHost().equalsIgnoreCase( ipDead ) )
356 snDead = computingClients.get( i ).getSaveNeighbor() ;
363 for( int i = 0 ; i < clients.size() ; i++ )
365 if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
369 // res = clients.get( i ).getStub().startVM() ;
370 // } catch( RemoteException e ) {
371 // e.printStackTrace();
376 //clients.get(i).setStatus( "running" ) ;
378 int pos = computingClients.indexOf( cc ) ;
381 System.err.println( "Dead client not found in the computing clients list!" ) ;
383 System.out.println( "Trying to replace " + cc.getClient().getName() + " with " +
384 clients.get(i).getName() + " ... " ) ;
386 String save_name = computingClients.get( pos ).getLastSave() ;
388 ComputingClient ccl = new ComputingClient( clients.get(i) ) ;
389 clients.get( i ).setComputingClient( ccl ) ;
390 SaveNeighbor sn = computingClients.get( pos ).getSaveNeighbor() ;
391 ccl.setSaveNeighbor( sn ) ;
392 computingClients.set( pos, ccl ) ;
397 res = computingClients.get( pos ).getClient().getStub().
398 retrieveSave( save_name ) ;
399 } catch( RemoteException e ) {
400 System.err.println( "Unable to indicate to client to retrieve last save!" ) ;
401 e.printStackTrace() ;
409 boolean ok_new = false, ok_old = false ;
411 // replace dead client in vmIPs
412 for( int j = 0 ; j < vmIPs.size() ; j++ )
414 if( vmIPs.get( j ).getHostIP().equalsIgnoreCase( computingClients.get( pos ).getClient().getIP() ) )
416 vmIPs.get( j ).setHostIP( "" ) ;
419 if( vmIPs.get( j ).getHostIP().equalsIgnoreCase( ipDead ) )
421 String vmIP = vmIPs.get( j ).getVmIP() ;
422 vmIPs.get( j ).setHostIP( computingClients.get( pos ).getClient().getIP() ) ;
426 computingClients.get( pos ).getClient().getStub().setIPVM( vmIP ) ;
427 } catch( RemoteException e ) {
428 System.err.println( "Unable to set the new VM IP on the replacing client!" ) ;
429 e.printStackTrace() ;
433 if( ok_new && ok_old )
440 // Replacing in RunningApplication
441 applications.get( ind ).replaceComputingClient( cc, ccl ) ;
443 for( int l = 0 ; l < applications.get( ind ).getComputingClients().size() ; l++ )
445 applications.get( ind ).getComputingClients().get( l ).setSaveRequest( false ) ;
449 System.out.println( "Successful redeployment of the VM." ) ;
451 System.err.println( "Unable to deploy the save on the new computing client!" ) ;
457 for( int k = 0 ; k < computingClients.size() ; k++ )
460 computingClients.get( k ).getClient().getStub().
461 replaceSaveNeighbor( snDead, new SaveNeighbor( clients.get( i ).getStub() ) ) ;
462 } catch( RemoteException e ) {
463 System.err.println( "Unable to inform " + computingClients.get( k ).getClient().getName() +
464 " of the replacement of a save neighbor!" ) ;
465 e.printStackTrace() ;
469 System.out.println( "Dead client successfully replaced." ) ;
473 System.err.println( "Dead client not replaced!!" ) ;
480 synchronized( counter ) {
482 counter.notifyAll() ;}
483 } catch( Exception e ) {}
488 /** Monitoring thread **/
489 private class ConnectedMonitor extends Thread
498 protected void stopMonitor() { run = false ; }
503 boolean change, dead ;
508 Iterator<ConnectedClient> it = clients.iterator() ;
509 int nb_disconnections = 0 ;
510 int nb_disconnections_computing = 0 ;
512 while( it.hasNext() )
514 ConnectedClient cl = it.next() ;
518 if( cl.getTimeout() > max_timeout || cl.getFail() )
524 cl.getStub().echo() ;
527 } catch( RemoteException e ) {
534 System.out.println( "Disconnection of " + cl.getName() ) ;
535 if( cl.getStatus().equalsIgnoreCase( "running" ) || cl.getStatus().equalsIgnoreCase( "saving" ) )
537 System.out.println( "A VM was running on it!!" ) ;
538 System.out.println( "I will redeploy a save and restart all VM ..." ) ;
540 // for( int i = 0 ; i < computingClients.size() ; i++ )
542 // if( computingClients.get( i ).getClient().getIP().equals( cl.getIP() ) )
544 // computingClients.remove( i ) ;
548 synchronized( counter )
553 new Server.FaultManager( cl ).start() ;
554 nb_disconnections_computing++ ;
556 System.out.println( "There was no VM running on it." ) ;
557 System.out.println( "Maybe it will come back later :)" ) ;
560 synchronized( clients )
564 nb_disconnections++ ;
572 synchronized( clients )
574 if( clients.size() == 0 )
576 System.out.println( "There is no client connected." ) ;
577 } else if( clients.size() == 1 ) {
578 System.out.println( "There is one client connected." ) ;
580 System.out.println( "There are " + clients.size() + " clients connected." ) ;
586 if( nb_disconnections_computing > 0 )
588 System.out.println( "Sending emergency stop signal to all computing nodes ... " ) ;
590 for( int i = 0 ; i < clients.size() ; i++ )
592 if( clients.get( i ).getStatus().equalsIgnoreCase( "running" )
593 || clients.get( i ).getStatus().equalsIgnoreCase( "saving" ) )
596 clients.get( i ).getStub().emergencyStop() ;
597 } catch( RemoteException e ) {
598 System.err.println( "Unable to invoke emergency stop signal on " + clients.get( i ).getName() ) ;
599 e.printStackTrace() ;
605 System.out.println( "I will redeploy save and restart VMs ... " ) ;
607 synchronized( counter )
609 if( counter.getNb() > 0 )
611 System.out.println( "... waiting all redeployments done ..." ) ;
614 while( counter.getNb() != 0 )
618 } catch( InterruptedException e ) {
619 e.printStackTrace() ;
625 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
627 applications.get( ind ).getComputingClients().get( i ).setRestartOk( false ) ;
629 new RestartVM( applications.get( ind ).getComputingClients().get( i ).getClient() ).start() ;
631 // final ServicesClient sc = applications.get( ind ).getComputingClients().get( i ).getClient().getStub() ;
633 // new Thread( new Runnable() {
639 // if( sc.restartVMAfterCrash() != 0 )
641 // System.err.println( "Problem while restarting VM on " +sc.getName() + "!" ) ;
643 // } catch( RemoteException e ) {
645 // System.err.println( "Problem while restarting VM on " + sc.getName() + "!" ) ;
646 // } catch( RemoteException e1 ) {
647 // System.err.println( "Problem while restarting a VM!" ) ;
648 // e1.printStackTrace() ;
650 // e.printStackTrace() ;
660 Thread.sleep( 2000 ) ;
661 } catch( InterruptedException e ) {
662 e.printStackTrace() ;
670 public Integer saveOk( String _ip, String _saveName )
673 for( i = 0 ; i < computingClients.size() ; i ++ )
675 if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
677 computingClients.get( i ).setLastSave( _saveName ) ;
678 computingClients.get( i ).setSaveStatus( true ) ;
684 boolean all_ok = true ;
687 while( all_ok && i < computingClients.size() )
689 all_ok = all_ok & computingClients.get( i ).getSaveStatus() ;
695 for( i = 0 ; i < computingClients.size() ; i++ )
698 computingClients.get( i ).getClient().getStub().saveOk() ;
699 } catch (RemoteException e) {
700 System.err.println( "Unable to invoke the saveOk method on " + computingClients.get( i ).getClient().getName() ) ;
701 e.printStackTrace() ;
703 computingClients.get( i ).setSaveStatus( false ) ;
706 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
713 public Integer changeSaveName( String _ip, String _saveName )
715 if( _ip != null && _ip.length() > 0 && _saveName != null && _saveName.length() > 0 )
717 for( int i = 0 ; i < computingClients.size() ; i ++ )
719 if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
721 computingClients.get( i ).setLastSave( _saveName ) ;
722 System.out.println( "Save name successfully changed on " + computingClients.get( i ).getClient().getName() ) ;
727 System.err.println( "Unable to found computing client with IP " + _ip + "!" ) ;
731 System.err.println( "Unable to change save name. IP or save name empty ! (IP: " + _ip + " ; save name: " + _saveName +")" ) ;
738 public ArrayList<ServicesClient> startApplication( int _nb )
740 int nb = clients.size() - computingClients.size() ;
742 if( nb > _nb && ! running )
746 final ArrayList<ServicesClient> ac = new ArrayList<ServicesClient>() ;
747 final ArrayList<ComputingClient> tmp = new ArrayList<ComputingClient>() ;
749 RunningApplication app = new RunningApplication( "Test" ) ;
754 while( i < clients.size() && ac.size() < _nb )
757 if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
759 synchronized( startingClients )
761 while( ac.size() + startingClients.getNb() >= _nb )
763 if( ac.size() == _nb ) break ;
766 startingClients.wait() ;
767 } catch( InterruptedException e ) {
768 e.printStackTrace() ;
772 if( ac.size() < _nb )
774 startingClients.inc() ;
781 synchronized( limitThread )
783 while( limitThread.getNb() >= maxThread )
787 } catch (InterruptedException e) {
795 final int indice = i ;
796 new Thread( new Runnable()
803 res = clients.get( indice ).getStub().startVM( 0 ) ;
804 } catch( RemoteException e ) {
805 e.printStackTrace() ;
810 ac.add( clients.get( indice ).getStub() ) ;
811 clients.get( indice ).setStatus( "running" ) ;
812 ComputingClient cl = new ComputingClient( clients.get( indice ) ) ;
813 clients.get( indice ).setComputingClient( cl ) ;
814 computingClients.add( cl ) ;
817 System.err.println( "Problem while launching the VM on "
818 + clients.get( indice ).getName() + "!" ) ;
821 synchronized( limitThread )
824 limitThread.notifyAll() ;
827 synchronized( startingClients )
829 startingClients.dec() ;
830 startingClients.notifyAll() ;
840 if( ac.size() == _nb )
842 app.setComputingClients( tmp ) ;
843 app.setRunning( true ) ;
844 // app.setStartTime( System.currentTimeMillis() ) ;
847 /* Choosing save neighbors */
848 for( i = 0 ; i < tmp.size() ; i++ )
850 if( i == tmp.size() - 1 )
852 index = computingClients.indexOf( tmp.get( i ) ) ;
853 index2 = computingClients.indexOf( tmp.get( 0 ) ) ;
855 if( index == -1 || index2 == -1 )
857 System.err.println( "Problem in ComputingClients list!" ) ;
860 computingClients.get( index ).setSaveNeighbor( new SaveNeighbor( computingClients.get( index2 ).getClient().getStub() )) ;
861 } catch( RemoteException e ) {
862 System.err.println( "Unable to create the save neighbor!" ) ;
863 e.printStackTrace() ;
867 index = computingClients.indexOf( tmp.get( i ) ) ;
868 index2 = computingClients.indexOf( tmp.get( i + 1 ) ) ;
870 if( index == -1 || index2 == -1 )
872 System.err.println( "Problem in ComputingClients list!" ) ;
875 computingClients.get( index ).setSaveNeighbor( new SaveNeighbor( computingClients.get( index2 ).getClient().getStub() ) ) ;
876 } catch( RemoteException e ) {
877 System.err.println( "Unable to create the save neighbor!" ) ;
878 e.printStackTrace() ;
885 applications.add( app ) ;
887 ind = applications.indexOf( app ) ;
897 public void requestSave( String _ip )
901 } catch( InterruptedException e ) {
902 System.err.println( "Unable to obtain the semaphore for semaSave!" ) ;
903 e.printStackTrace() ;
906 final String ip = _ip ;
908 new Thread( new Runnable() {
913 treatRequestSave( ip ) ;
919 public void treatRequestSave( String _ip )
921 if( applications.size() > 0 && _ip != null && _ip.length() > 0 )
923 if( (System.currentTimeMillis() - applications.get( ind ).getLastSaveDate()) > save_interleave )
925 // Mark it as a requester
926 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
928 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
930 applications.get( ind ).getComputingClients().get( i ).setSaveRequest( true ) ;
940 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
944 ok = applications.get( ind ).getComputingClients().get( i ).getSaveRequest() ;
946 ok = ok & applications.get( ind ).getComputingClients().get( i ).getSaveRequest() ;
958 // Thread.sleep( 5000 ) ;
959 // } catch( InterruptedException e1 ) {
960 // e1.printStackTrace() ;
963 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
966 applications.get( ind ).getComputingClients().get( i ).getClient().getStub().responseSave( true ) ;
967 applications.get( ind ).getComputingClients().get( i ).setSaveRequest( false ) ;
968 } catch( RemoteException e ) {
969 System.err.println( "Unable to send the save request response to " +
970 applications.get( ind ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
971 e.printStackTrace() ;
975 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
981 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
983 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
986 applications.get( ind ).getComputingClients().get( i ).getClient().getStub().responseSave( false ) ;
987 } catch( RemoteException e ) {
988 System.err.println( "Unable to send the save request response to " +
989 applications.get( ind ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
990 e.printStackTrace() ;
998 System.err.println( "!! Serious problem in treatRequestSave method!!" ) ;
1004 public void restartOk( String _ip )
1006 if( applications.size() > 0 && _ip != null && _ip.length() > 0 )
1008 System.out.println( "Client " + _ip + " has finished to restart ("+applications.get( ind ).getComputingClients().size()+") ... " ) ;
1009 // if( (System.currentTimeMillis() - applications.get( ind ).getLastSaveDate()) > save_interleave )
1011 // Has it already finished?
1012 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1014 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
1016 applications.get( ind ).getComputingClients().get( i ).setRestartOk( true ) ;
1023 boolean ok = false ;
1024 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1028 ok = applications.get( ind ).getComputingClients().get( i ).getRestartOk() ;
1030 ok = ok & applications.get( ind ).getComputingClients().get( i ).getRestartOk() ;
1041 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
1043 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1045 applications.get( ind ).getComputingClients().get( i ).setRestartOk( false ) ;
1055 public void goApplication()
1057 synchronized( applications ) {
1058 if( running && applications.get( ind ).getStartTime() != 0 )
1060 applications.get( ind ).setStartTime( System.currentTimeMillis() ) ;
1061 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
1067 public void endApplication()
1069 synchronized( applications )
1073 applications.get( ind ).setEndTime( System.currentTimeMillis() ) ;
1074 applications.get( ind ).setRunning( false ) ;
1075 applications.get( ind ).clear() ;
1077 Iterator<ComputingClient> it = computingClients.iterator() ;
1079 while( it.hasNext() )
1081 ComputingClient cl = it.next() ;
1084 cl.getClient().getStub().emergencyStop() ;
1085 } catch (RemoteException e) {
1086 e.printStackTrace();
1089 cl.getClient().setStatus( "connected" ) ;
1090 cl.getClient().setComputingClient( null ) ;
1096 applications.get( ind ).clear() ;
1100 System.out.println( "Application " + applications.get( ind ).getName() + " ends in " +
1101 applications.get( ind ).getExecutionTime() + " seconds." ) ;
1109 public String getAssociatedIP( String _ip )
1113 for( int i = 0 ; i < vmIPs.size() ; i++ )
1115 if( vmIPs.get( i ).getHostIP().equalsIgnoreCase( _ip ) )
1117 ret = vmIPs.get( i ).getVmIP() ;
1126 public Integer deployVM( final String _name, final String _archive, final String _directory )
1128 int nb = 0, pb = 0 ;
1130 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0
1131 && _directory != null && _directory.length() > 0 )
1133 System.out.println( "Deploying the VM " + _name + " (" + _archive + ") ... " ) ;
1135 File file = new File( working_directory + "/" + _archive ) ;
1136 if( ! file.exists() )
1138 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1141 } else if( file.isDirectory() ) {
1142 System.err.println( _archive + " is a directory!" ) ;
1149 // TODO do a better deployment !!
1151 // boolean error, ok, server ;
1152 // ArrayList<ConnectedClient> deployed = new ArrayList<ConnectedClient>() ;
1154 // boolean server = true ;
1157 for( int i = 0 ; i < clients.size() ; i++ )
1163 if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
1165 synchronized( limitThread )
1167 while( limitThread.getNb() >= maxThread )
1170 limitThread.wait() ;
1171 } catch (InterruptedException e) {
1172 e.printStackTrace();
1179 final int indice = i ;
1180 new Thread( new Runnable() {
1185 boolean error = true ;
1188 ret = clients.get( indice ).getStub().deployVM( _name, _archive, _directory ) ;
1189 } catch( RemoteException e ) {
1190 System.err.println( "Unable to deploy the VM on " + clients.get( indice ).getName() + "!" ) ;
1191 e.printStackTrace() ;
1194 // The client does not have the archive, we have to send it.
1197 // Attention au multi-envois !!!
1198 System.out.print( "Sending VM archive to " + clients.get( indice ).getName() + " ... " ) ;
1205 wd = clients.get( indice ).getStub().getWorkingDirectory() ;
1206 snIP = clients.get( indice ).getStub().getIPHost() ;
1207 } catch (RemoteException e2) {
1208 System.err.println( "Unable to retrieve information on " + clients.get( indice ).getName() + "!" ) ;
1209 e2.printStackTrace() ;
1213 String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive,
1219 Process proc = Runtime.getRuntime().exec( command ) ;
1222 if( proc.exitValue() == 0 )
1224 System.out.println( "Initial VM archive successfully sent." ) ;
1226 System.err.println( "Initial VM archive not sent!" ) ;
1227 System.err.println( "Error: " + proc.exitValue() ) ;
1228 BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ;
1232 while( (l = b.readLine()) != null )
1234 System.err.println( l ) ;
1236 } catch( IOException e ) {
1237 e.printStackTrace() ;
1242 } catch( IOException e ) {
1243 System.err.println( "Error during initial VM archive send command: " ) ;
1244 e.printStackTrace() ;
1246 } catch( InterruptedException e ) {
1247 e.printStackTrace() ;
1258 ret = clients.get( indice ).getStub().deployVM( _name, _archive, _directory ) ;
1259 } catch( RemoteException e ) {
1260 System.err.println( "Unable to deploy the VM on " + clients.get( indice ).getName() + "!" ) ;
1261 e.printStackTrace() ;
1268 System.out.println( "Initial VM archive successfully deployed on " + clients.get( indice ).getName() + "." ) ;
1270 synchronized( deployingClients )
1272 deployingClients.inc() ;
1276 synchronized( limitThread )
1279 limitThread.notifyAll() ;
1287 synchronized( limitThread )
1289 while( limitThread.getNb() > 0 )
1292 limitThread.wait() ;
1293 } catch( InterruptedException e ) {
1294 e.printStackTrace() ;
1299 if( nb - deployingClients.getNb() > 0 )
1302 System.err.println( "** " + pb + " machine is not deployed!" ) ;
1304 System.err.println( "** " + pb + " machines are not deployed!" ) ;
1311 public String getWorkingDirectory()
1313 return working_directory ;
1317 private class OperatingClients
1321 OperatingClients() { nb = 0 ; }
1323 protected void inc() { nb++ ; }
1325 protected void dec() { nb-- ; }
1327 protected int getNb() { return nb ; }
1331 private class LimitThread
1335 LimitThread() { nb = 0 ; }
1337 protected void inc() { nb++ ; }
1339 protected void dec() { nb-- ; }
1341 protected int getNb() { return nb ; }
1345 private class RestartVM extends Thread
1347 private ConnectedClient cc ;
1349 protected RestartVM( ConnectedClient _cc )
1356 boolean error = false ;
1360 if( cc.getStub().restartVMAfterCrash() != 0 )
1362 System.err.println( "Problem while restarting VM on " + cc.getName() + "!" ) ;
1365 } catch( RemoteException e ) {
1366 e.printStackTrace() ;
1371 System.err.println( "The client to restart is null!" ) ;
1376 cc.setFail( true ) ;
1379 System.out.print( "Trying to stop the client ... " ) ;
1380 cc.getStub().stop() ;
1381 System.out.println( "successful client stop." );
1382 } catch( RemoteException e ) {
1383 System.out.println( "unsuccessful client stop!" ) ;
1384 e.printStackTrace() ;
1392 /** La programmation est un art, respectons ceux qui la pratiquent !! **/