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
98 public void setSaveTime( int _saveTime )
100 save_interleave = _saveTime * 60 * 1000 ;
105 public Integer register( ServicesClient _stub )
109 synchronized( clients )
113 ip = _stub.getIPHost() ;
114 } catch (RemoteException e) {
115 e.printStackTrace() ;
119 boolean exists = false ;
122 for( i = 0 ; i < clients.size() ; i++ )
124 if( ip.equals( clients.get( i ).getIP() ) )
127 System.out.println( "Client already connected!" ) ;
134 System.out.println( "The client stub will be replaced." ) ;
135 clients.get( i ).setStub( _stub ) ;
136 System.out.println( "(reconnection of " + clients.get( i ).getName() + ")" ) ;
139 System.out.println( "New connection!" ) ;
140 clients.add( new ConnectedClient( _stub ) ) ;
141 System.out.println( "(connection of " + clients.get( clients.size() - 1 ).getName() + ")" ) ;
144 if( clients.size() == 0 )
146 System.out.println( "There is no client connected." ) ;
147 } else if( clients.size() == 1 ) {
148 System.out.println( "There is one client connected." ) ;
150 System.out.println( "There are " + clients.size() + " clients connected." ) ;
162 private void generateVmIP( String _ip )
164 if( _ip != null && ! _ip.equals( "" ) )
166 for( int i = 0 ; i < vmIPs.size() ; i++ )
168 if( vmIPs.get( i ).getHostIP().equalsIgnoreCase( "" ) )
170 vmIPs.get( i ).setHostIP( _ip ) ;
180 public void ping( String _ip )
184 for( int i = 0 ; i < clients.size() ; i++ )
186 if( _ip.equals( clients.get( i ).getIP() ) )
188 clients.get( i ).resetTimeout() ;
197 public void changeStatus( String _ip, String _status )
199 if( _ip != null && _status != null )
201 for( int i = 0 ; i < clients.size() ; i++ )
203 if( _ip.equals( clients.get( i ).getIP() ) )
205 clients.get( i ).setStatus( _status ) ;
206 System.out.println( "Client " + clients.get( i ).getName() + " changed its status to: " + _status ) ;
215 public void init( int _port )
220 clients = new ArrayList<ConnectedClient>() ;
221 computingClients = new ArrayList<ComputingClient>() ;
222 applications = new ArrayList<RunningApplication>() ;
225 startingClients = new OperatingClients() ;
226 deployingClients = new OperatingClients() ;
227 limitThread = new LimitThread() ;
233 working_directory = "/localhome/vmware" ;
235 save_interleave = 30 * 60 * 1000 ;
237 semaSave = new Semaphore( 1 ) ;
241 vmIPs = new ArrayList<IPAssociation>() ;
242 // TODO initialisation of VM IPs
243 for( int i = 2 ; i < 101 ; i++ )
245 vmIPs.add( new IPAssociation() ) ;
246 vmIPs.get( vmIPs.size() - 1 ).setVmIP( "10.11.10." + i ) ;
249 clients = new ArrayList<ConnectedClient>() ;
251 counter = new DiscCount() ;
253 monitor = new ConnectedMonitor() ;
257 // Check if there are running applications ... and restart them :)
263 if( monitor != null ) { monitor.stopMonitor() ; }
265 for( int i = 0 ; i < clients.size() ; i++ )
268 clients.get( i ).getStub().emergencyStop() ;
269 clients.get( i ).getStub().stop() ;
270 } catch (RemoteException e) {
271 System.err.println( "Unable to send stop signal to " + clients.get( i ).getName() ) ;
272 e.printStackTrace() ;
282 private void exportObject()
284 ServicesServer ref = null ;
285 Registry reg = null ;
291 reg = LocateRegistry.getRegistry( port ) ;
293 String tab[] = reg.list() ;
295 System.out.println( "There is an existing RMI Registry on port " +
296 port + " with " + tab.length + " entries!" ) ;
297 for( int i = 0 ; i < tab.length ; i++ )
300 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
302 System.out.println( "Register successfuly deleted!" ) ;
304 System.err.println( "Register undeleted !!!" ) ;
306 } catch( Exception e ) {
307 e.printStackTrace() ;
311 } catch( RemoteException e ) {
315 if ( System.getSecurityManager() == null )
317 System.setSecurityManager( new SecurityManager() ) ;
320 LocateRegistry.createRegistry( port ) ;
321 LocateRegistry.getRegistry( port ).rebind( "Server", this ) ;
322 ref = (ServicesServer) Naming.lookup( "rmi://"
323 + LocalHost.Instance().getIP() + ":" + port
325 } catch ( Exception e ) {
326 System.err.println( "Error in Server.exportObject() when creating local services:" + e ) ;
327 System.err.println( "Exit from Server.exportObject" ) ;
331 LocalHost.Instance().setServerStub( ref ) ;
333 System.out.println( "Server launched on IP " + LocalHost.Instance().getIP() +
334 " on port " + port + "." ) ;
337 /** Fault manager thread **/
338 private class FaultManager extends Thread
342 FaultManager( ConnectedClient _cl )
350 if( cl != null && cl.getStatus().equalsIgnoreCase( "running" ) ||
351 cl.getStatus().equalsIgnoreCase( "saving" ) )
353 ComputingClient cc = cl.getComputingClient() ;
354 // ServicesClient dead = cc.getClient().getStub() ;
355 String ipDead = cc.getClient().getIP() ;
356 SaveNeighbor snDead = null ;
357 for( int i = 0 ; i < computingClients.size() ; i++ )
359 if( computingClients.get( i ).getSaveNeighbor().getIPHost().equalsIgnoreCase( ipDead ) )
361 snDead = computingClients.get( i ).getSaveNeighbor() ;
368 for( int i = 0 ; i < clients.size() ; i++ )
370 if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
374 // res = clients.get( i ).getStub().startVM() ;
375 // } catch( RemoteException e ) {
376 // e.printStackTrace();
381 //clients.get(i).setStatus( "running" ) ;
383 int pos = computingClients.indexOf( cc ) ;
386 System.err.println( "Dead client not found in the computing clients list!" ) ;
388 System.out.println( "Trying to replace " + cc.getClient().getName() + " with " +
389 clients.get(i).getName() + " ... " ) ;
391 String save_name = computingClients.get( pos ).getLastSave() ;
393 ComputingClient ccl = new ComputingClient( clients.get(i) ) ;
394 clients.get( i ).setComputingClient( ccl ) ;
395 SaveNeighbor sn = computingClients.get( pos ).getSaveNeighbor() ;
396 ccl.setSaveNeighbor( sn ) ;
397 computingClients.set( pos, ccl ) ;
402 res = computingClients.get( pos ).getClient().getStub().
403 retrieveSave( save_name ) ;
404 } catch( RemoteException e ) {
405 System.err.println( "Unable to indicate to client to retrieve last save!" ) ;
406 e.printStackTrace() ;
414 boolean ok_new = false, ok_old = false ;
416 // replace dead client in vmIPs
417 for( int j = 0 ; j < vmIPs.size() ; j++ )
419 if( vmIPs.get( j ).getHostIP().equalsIgnoreCase( computingClients.get( pos ).getClient().getIP() ) )
421 vmIPs.get( j ).setHostIP( "" ) ;
424 if( vmIPs.get( j ).getHostIP().equalsIgnoreCase( ipDead ) )
426 String vmIP = vmIPs.get( j ).getVmIP() ;
427 vmIPs.get( j ).setHostIP( computingClients.get( pos ).getClient().getIP() ) ;
431 computingClients.get( pos ).getClient().getStub().setIPVM( vmIP ) ;
432 } catch( RemoteException e ) {
433 System.err.println( "Unable to set the new VM IP on the replacing client!" ) ;
434 e.printStackTrace() ;
438 if( ok_new && ok_old )
445 // Replacing in RunningApplication
446 applications.get( ind ).replaceComputingClient( cc, ccl ) ;
448 for( int l = 0 ; l < applications.get( ind ).getComputingClients().size() ; l++ )
450 applications.get( ind ).getComputingClients().get( l ).setSaveRequest( false ) ;
454 System.out.println( "Successful redeployment of the VM." ) ;
456 System.err.println( "Unable to deploy the save on the new computing client!" ) ;
462 for( int k = 0 ; k < computingClients.size() ; k++ )
465 computingClients.get( k ).getClient().getStub().
466 replaceSaveNeighbor( snDead, new SaveNeighbor( clients.get( i ).getStub() ) ) ;
467 } catch( RemoteException e ) {
468 System.err.println( "Unable to inform " + computingClients.get( k ).getClient().getName() +
469 " of the replacement of a save neighbor!" ) ;
470 e.printStackTrace() ;
474 System.out.println( "Dead client successfully replaced." ) ;
478 System.err.println( "Dead client not replaced!!" ) ;
485 synchronized( counter ) {
487 counter.notifyAll() ;}
488 } catch( Exception e ) {}
493 /** Monitoring thread **/
494 private class ConnectedMonitor extends Thread
503 protected void stopMonitor() { run = false ; }
508 boolean change, dead ;
513 Iterator<ConnectedClient> it = clients.iterator() ;
514 int nb_disconnections = 0 ;
515 int nb_disconnections_computing = 0 ;
517 while( it.hasNext() )
519 ConnectedClient cl = it.next() ;
523 if( cl.getTimeout() > max_timeout || cl.getFail() )
529 cl.getStub().echo() ;
532 } catch( RemoteException e ) {
539 System.out.println( "Disconnection of " + cl.getName() ) ;
540 if( cl.getStatus().equalsIgnoreCase( "running" ) || cl.getStatus().equalsIgnoreCase( "saving" ) )
542 System.out.println( "A VM was running on it!!" ) ;
543 System.out.println( "I will redeploy a save and restart all VM ..." ) ;
545 // for( int i = 0 ; i < computingClients.size() ; i++ )
547 // if( computingClients.get( i ).getClient().getIP().equals( cl.getIP() ) )
549 // computingClients.remove( i ) ;
553 synchronized( counter )
558 new Server.FaultManager( cl ).start() ;
559 nb_disconnections_computing++ ;
561 System.out.println( "There was no VM running on it." ) ;
562 System.out.println( "Maybe it will come back later :)" ) ;
565 synchronized( clients )
569 nb_disconnections++ ;
577 synchronized( clients )
579 if( clients.size() == 0 )
581 System.out.println( "There is no client connected." ) ;
582 } else if( clients.size() == 1 ) {
583 System.out.println( "There is one client connected." ) ;
585 System.out.println( "There are " + clients.size() + " clients connected." ) ;
591 if( nb_disconnections_computing > 0 )
593 System.out.println( "Sending emergency stop signal to all computing nodes ... " ) ;
595 for( int i = 0 ; i < clients.size() ; i++ )
597 if( clients.get( i ).getStatus().equalsIgnoreCase( "running" )
598 || clients.get( i ).getStatus().equalsIgnoreCase( "saving" ) )
601 clients.get( i ).getStub().emergencyStop() ;
602 } catch( RemoteException e ) {
603 System.err.println( "Unable to invoke emergency stop signal on " + clients.get( i ).getName() ) ;
604 e.printStackTrace() ;
610 System.out.println( "I will redeploy save and restart VMs ... " ) ;
612 synchronized( counter )
614 if( counter.getNb() > 0 )
616 System.out.println( "... waiting all redeployments done ..." ) ;
619 while( counter.getNb() != 0 )
623 } catch( InterruptedException e ) {
624 e.printStackTrace() ;
630 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
632 applications.get( ind ).getComputingClients().get( i ).setRestartOk( false ) ;
634 new RestartVM( applications.get( ind ).getComputingClients().get( i ).getClient() ).start() ;
636 // final ServicesClient sc = applications.get( ind ).getComputingClients().get( i ).getClient().getStub() ;
638 // new Thread( new Runnable() {
644 // if( sc.restartVMAfterCrash() != 0 )
646 // System.err.println( "Problem while restarting VM on " +sc.getName() + "!" ) ;
648 // } catch( RemoteException e ) {
650 // System.err.println( "Problem while restarting VM on " + sc.getName() + "!" ) ;
651 // } catch( RemoteException e1 ) {
652 // System.err.println( "Problem while restarting a VM!" ) ;
653 // e1.printStackTrace() ;
655 // e.printStackTrace() ;
665 Thread.sleep( 2000 ) ;
666 } catch( InterruptedException e ) {
667 e.printStackTrace() ;
675 public Integer saveOk( String _ip, String _saveName )
678 for( i = 0 ; i < computingClients.size() ; i ++ )
680 if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
682 computingClients.get( i ).setLastSave( _saveName ) ;
683 computingClients.get( i ).setSaveStatus( true ) ;
689 boolean all_ok = true ;
692 while( all_ok && i < computingClients.size() )
694 all_ok = all_ok & computingClients.get( i ).getSaveStatus() ;
700 for( i = 0 ; i < computingClients.size() ; i++ )
703 computingClients.get( i ).getClient().getStub().saveOk() ;
704 } catch (RemoteException e) {
705 System.err.println( "Unable to invoke the saveOk method on " + computingClients.get( i ).getClient().getName() ) ;
706 e.printStackTrace() ;
708 computingClients.get( i ).setSaveStatus( false ) ;
711 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
718 public Integer changeSaveName( String _ip, String _saveName )
720 if( _ip != null && _ip.length() > 0 && _saveName != null && _saveName.length() > 0 )
722 for( int i = 0 ; i < computingClients.size() ; i ++ )
724 if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
726 computingClients.get( i ).setLastSave( _saveName ) ;
727 System.out.println( "Save name successfully changed on " + computingClients.get( i ).getClient().getName() ) ;
732 System.err.println( "Unable to found computing client with IP " + _ip + "!" ) ;
736 System.err.println( "Unable to change save name. IP or save name empty ! (IP: " + _ip + " ; save name: " + _saveName +")" ) ;
743 public ArrayList<ServicesClient> startApplication( int _nb )
745 int nb = clients.size() - computingClients.size() ;
747 if( nb > _nb && ! running )
751 final ArrayList<ServicesClient> ac = new ArrayList<ServicesClient>() ;
752 final ArrayList<ComputingClient> tmp = new ArrayList<ComputingClient>() ;
754 RunningApplication app = new RunningApplication( "Test" ) ;
759 while( i < clients.size() && ac.size() < _nb )
762 if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
764 synchronized( startingClients )
766 while( ac.size() + startingClients.getNb() >= _nb )
768 if( ac.size() == _nb ) break ;
771 startingClients.wait() ;
772 } catch( InterruptedException e ) {
773 e.printStackTrace() ;
777 if( ac.size() < _nb )
779 startingClients.inc() ;
786 synchronized( limitThread )
788 while( limitThread.getNb() >= maxThread )
792 } catch (InterruptedException e) {
800 final int indice = i ;
801 new Thread( new Runnable()
808 res = clients.get( indice ).getStub().startVM( 0 ) ;
809 } catch( RemoteException e ) {
810 e.printStackTrace() ;
815 ac.add( clients.get( indice ).getStub() ) ;
816 clients.get( indice ).setStatus( "running" ) ;
817 ComputingClient cl = new ComputingClient( clients.get( indice ) ) ;
818 clients.get( indice ).setComputingClient( cl ) ;
819 computingClients.add( cl ) ;
822 System.err.println( "Problem while launching the VM on "
823 + clients.get( indice ).getName() + "!" ) ;
826 synchronized( limitThread )
829 limitThread.notifyAll() ;
832 synchronized( startingClients )
834 startingClients.dec() ;
835 startingClients.notifyAll() ;
845 if( ac.size() == _nb )
847 app.setComputingClients( tmp ) ;
848 app.setRunning( true ) ;
849 app.setStartTime( System.currentTimeMillis() ) ;
852 /* Choosing save neighbors */
853 for( i = 0 ; i < tmp.size() ; i++ )
855 if( i == tmp.size() - 1 )
857 index = computingClients.indexOf( tmp.get( i ) ) ;
858 index2 = computingClients.indexOf( tmp.get( 0 ) ) ;
860 if( index == -1 || index2 == -1 )
862 System.err.println( "Problem in ComputingClients list!" ) ;
865 computingClients.get( index ).setSaveNeighbor( new SaveNeighbor( computingClients.get( index2 ).getClient().getStub() )) ;
866 } catch( RemoteException e ) {
867 System.err.println( "Unable to create the save neighbor!" ) ;
868 e.printStackTrace() ;
872 index = computingClients.indexOf( tmp.get( i ) ) ;
873 index2 = computingClients.indexOf( tmp.get( i + 1 ) ) ;
875 if( index == -1 || index2 == -1 )
877 System.err.println( "Problem in ComputingClients list!" ) ;
880 computingClients.get( index ).setSaveNeighbor( new SaveNeighbor( computingClients.get( index2 ).getClient().getStub() ) ) ;
881 } catch( RemoteException e ) {
882 System.err.println( "Unable to create the save neighbor!" ) ;
883 e.printStackTrace() ;
890 applications.add( app ) ;
892 ind = applications.indexOf( app ) ;
902 public void requestSave( String _ip )
906 } catch( InterruptedException e ) {
907 System.err.println( "Unable to obtain the semaphore for semaSave!" ) ;
908 e.printStackTrace() ;
911 final String ip = _ip ;
913 new Thread( new Runnable() {
918 treatRequestSave( ip ) ;
924 public void treatRequestSave( String _ip )
926 if( applications.size() > 0 && _ip != null && _ip.length() > 0 )
928 if( (System.currentTimeMillis() - applications.get( ind ).getLastSaveDate()) > save_interleave )
930 // Mark it as a requester
931 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
933 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
935 applications.get( ind ).getComputingClients().get( i ).setSaveRequest( true ) ;
945 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
949 ok = applications.get( ind ).getComputingClients().get( i ).getSaveRequest() ;
951 ok = ok & applications.get( ind ).getComputingClients().get( i ).getSaveRequest() ;
963 // Thread.sleep( 5000 ) ;
964 // } catch( InterruptedException e1 ) {
965 // e1.printStackTrace() ;
968 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
971 applications.get( ind ).getComputingClients().get( i ).getClient().getStub().responseSave( true ) ;
972 applications.get( ind ).getComputingClients().get( i ).setSaveRequest( false ) ;
973 } catch( RemoteException e ) {
974 System.err.println( "Unable to send the save request response to " +
975 applications.get( ind ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
976 e.printStackTrace() ;
980 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
986 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
988 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
991 applications.get( ind ).getComputingClients().get( i ).getClient().getStub().responseSave( false ) ;
992 } catch( RemoteException e ) {
993 System.err.println( "Unable to send the save request response to " +
994 applications.get( ind ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
995 e.printStackTrace() ;
1002 semaSave.release() ;
1003 System.err.println( "!! Serious problem in treatRequestSave method!!" ) ;
1009 public void restartOk( String _ip )
1011 if( applications.size() > 0 && _ip != null && _ip.length() > 0 )
1013 System.out.println( "Client " + _ip + " has finished to restart ("+applications.get( ind ).getComputingClients().size()+") ... " ) ;
1014 // if( (System.currentTimeMillis() - applications.get( ind ).getLastSaveDate()) > save_interleave )
1016 // Has it already finished?
1017 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1019 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
1021 applications.get( ind ).getComputingClients().get( i ).setRestartOk( true ) ;
1028 boolean ok = false ;
1029 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1033 ok = applications.get( ind ).getComputingClients().get( i ).getRestartOk() ;
1035 ok = ok & applications.get( ind ).getComputingClients().get( i ).getRestartOk() ;
1046 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
1048 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1050 applications.get( ind ).getComputingClients().get( i ).setRestartOk( false ) ;
1060 public void goApplication()
1062 synchronized( applications ) {
1063 if( running && ! applications.get( ind ).getStartMark() )
1065 System.out.println( "Application is starting." ) ;
1066 applications.get( ind ).setStartMark() ;
1067 applications.get( ind ).setStartTime( System.currentTimeMillis() ) ;
1068 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
1075 public void endApplication()
1077 synchronized( applications )
1081 applications.get( ind ).setEndTime( System.currentTimeMillis() ) ;
1082 applications.get( ind ).setRunning( false ) ;
1083 applications.get( ind ).clear() ;
1085 Iterator<ComputingClient> it = computingClients.iterator() ;
1087 while( it.hasNext() )
1089 ComputingClient cl = it.next() ;
1092 cl.getClient().getStub().emergencyStop() ;
1093 } catch (RemoteException e) {
1094 e.printStackTrace();
1097 cl.getClient().setStatus( "connected" ) ;
1098 cl.getClient().setComputingClient( null ) ;
1104 applications.get( ind ).clear() ;
1108 System.out.println( "Application " + applications.get( ind ).getName() + " ends in " +
1109 applications.get( ind ).getExecutionTime() + " seconds." ) ;
1117 public String getAssociatedIP( String _ip )
1121 for( int i = 0 ; i < vmIPs.size() ; i++ )
1123 if( vmIPs.get( i ).getHostIP().equalsIgnoreCase( _ip ) )
1125 ret = vmIPs.get( i ).getVmIP() ;
1134 public Integer deployVM( final String _name, final String _archive, final String _directory )
1136 int nb = 0, pb = 0 ;
1138 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0
1139 && _directory != null && _directory.length() > 0 )
1141 System.out.println( "Deploying the VM " + _name + " (" + _archive + ") ... " ) ;
1143 File file = new File( working_directory + "/" + _archive ) ;
1144 if( ! file.exists() )
1146 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1149 } else if( file.isDirectory() ) {
1150 System.err.println( _archive + " is a directory!" ) ;
1157 // TODO do a better deployment !!
1159 // boolean error, ok, server ;
1160 // ArrayList<ConnectedClient> deployed = new ArrayList<ConnectedClient>() ;
1162 // boolean server = true ;
1165 for( int i = 0 ; i < clients.size() ; i++ )
1171 if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
1173 synchronized( limitThread )
1175 while( limitThread.getNb() >= maxThread )
1178 limitThread.wait() ;
1179 } catch (InterruptedException e) {
1180 e.printStackTrace();
1187 final int indice = i ;
1188 new Thread( new Runnable() {
1193 boolean error = true ;
1196 ret = clients.get( indice ).getStub().deployVM( _name, _archive, _directory ) ;
1197 } catch( RemoteException e ) {
1198 System.err.println( "Unable to deploy the VM on " + clients.get( indice ).getName() + "!" ) ;
1199 e.printStackTrace() ;
1202 // The client does not have the archive, we have to send it.
1205 // Attention au multi-envois !!!
1206 System.out.print( "Sending VM archive to " + clients.get( indice ).getName() + " ... " ) ;
1213 wd = clients.get( indice ).getStub().getWorkingDirectory() ;
1214 snIP = clients.get( indice ).getStub().getIPHost() ;
1215 } catch (RemoteException e2) {
1216 System.err.println( "Unable to retrieve information on " + clients.get( indice ).getName() + "!" ) ;
1217 e2.printStackTrace() ;
1221 String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive,
1227 Process proc = Runtime.getRuntime().exec( command ) ;
1230 if( proc.exitValue() == 0 )
1232 System.out.println( "Initial VM archive successfully sent." ) ;
1234 System.err.println( "Initial VM archive not sent!" ) ;
1235 System.err.println( "Error: " + proc.exitValue() ) ;
1236 BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ;
1240 while( (l = b.readLine()) != null )
1242 System.err.println( l ) ;
1244 } catch( IOException e ) {
1245 e.printStackTrace() ;
1250 } catch( IOException e ) {
1251 System.err.println( "Error during initial VM archive send command: " ) ;
1252 e.printStackTrace() ;
1254 } catch( InterruptedException e ) {
1255 e.printStackTrace() ;
1266 ret = clients.get( indice ).getStub().deployVM( _name, _archive, _directory ) ;
1267 } catch( RemoteException e ) {
1268 System.err.println( "Unable to deploy the VM on " + clients.get( indice ).getName() + "!" ) ;
1269 e.printStackTrace() ;
1276 System.out.println( "Initial VM archive successfully deployed on " + clients.get( indice ).getName() + "." ) ;
1278 synchronized( deployingClients )
1280 deployingClients.inc() ;
1284 synchronized( limitThread )
1287 limitThread.notifyAll() ;
1295 synchronized( limitThread )
1297 while( limitThread.getNb() > 0 )
1300 limitThread.wait() ;
1301 } catch( InterruptedException e ) {
1302 e.printStackTrace() ;
1307 if( nb - deployingClients.getNb() > 0 )
1310 System.err.println( "** " + pb + " machine is not deployed!" ) ;
1312 System.err.println( "** " + pb + " machines are not deployed!" ) ;
1319 public String getWorkingDirectory()
1321 return working_directory ;
1325 private class OperatingClients
1329 OperatingClients() { nb = 0 ; }
1331 protected void inc() { nb++ ; }
1333 protected void dec() { nb-- ; }
1335 protected int getNb() { return nb ; }
1339 private class LimitThread
1343 LimitThread() { nb = 0 ; }
1345 protected void inc() { nb++ ; }
1347 protected void dec() { nb-- ; }
1349 protected int getNb() { return nb ; }
1353 private class RestartVM extends Thread
1355 private ConnectedClient cc ;
1357 protected RestartVM( ConnectedClient _cc )
1364 boolean error = false ;
1368 if( cc.getStub().restartVMAfterCrash() != 0 )
1370 System.err.println( "Problem while restarting VM on " + cc.getName() + "!" ) ;
1373 } catch( RemoteException e ) {
1374 e.printStackTrace() ;
1379 System.err.println( "The client to restart is null!" ) ;
1384 cc.setFail( true ) ;
1387 System.out.print( "Trying to stop the client ... " ) ;
1388 cc.getStub().stop() ;
1389 System.out.println( "successful client stop." );
1390 } catch( RemoteException e ) {
1391 System.out.println( "unsuccessful client stop!" ) ;
1392 e.printStackTrace() ;
1400 /** La programmation est un art, respectons ceux qui la pratiquent !! **/