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() )
523 cl.getStub().echo() ;
525 } catch( RemoteException e ) {
532 System.out.println( "Disconnection of " + cl.getName() ) ;
533 if( cl.getStatus().equalsIgnoreCase( "running" ) || cl.getStatus().equalsIgnoreCase( "saving" ) )
535 System.out.println( "A VM was running on it!!" ) ;
536 System.out.println( "I will redeploy a save and restart all VM ..." ) ;
538 // for( int i = 0 ; i < computingClients.size() ; i++ )
540 // if( computingClients.get( i ).getClient().getIP().equals( cl.getIP() ) )
542 // computingClients.remove( i ) ;
546 synchronized( counter )
551 new Server.FaultManager( cl ).start() ;
552 nb_disconnections_computing++ ;
554 System.out.println( "There was no VM running on it." ) ;
555 System.out.println( "Maybe it will come back later :)" ) ;
558 synchronized( clients )
562 nb_disconnections++ ;
570 synchronized( clients )
572 if( clients.size() == 0 )
574 System.out.println( "There is no client connected." ) ;
575 } else if( clients.size() == 1 ) {
576 System.out.println( "There is one client connected." ) ;
578 System.out.println( "There are " + clients.size() + " clients connected." ) ;
584 if( nb_disconnections_computing > 0 )
586 System.out.println( "Sending emergency stop signal to all computing nodes ... " ) ;
588 for( int i = 0 ; i < clients.size() ; i++ )
590 if( clients.get( i ).getStatus().equalsIgnoreCase( "running" )
591 || clients.get( i ).getStatus().equalsIgnoreCase( "saving" ) )
594 clients.get( i ).getStub().emergencyStop() ;
595 } catch( RemoteException e ) {
596 System.err.println( "Unable to invoke emergency stop signal on " + clients.get( i ).getName() ) ;
597 e.printStackTrace() ;
603 System.out.println( "I will redeploy save and restart VMs ... " ) ;
605 synchronized( counter )
607 if( counter.getNb() > 0 )
609 System.out.println( "... waiting all redeployments done ..." ) ;
612 while( counter.getNb() != 0 )
616 } catch( InterruptedException e ) {
617 e.printStackTrace() ;
623 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
625 applications.get( ind ).getComputingClients().get( i ).setRestartOk( false ) ;
627 new RestartVM( applications.get( ind ).getComputingClients().get( i ).getClient() ).start() ;
629 // final ServicesClient sc = applications.get( ind ).getComputingClients().get( i ).getClient().getStub() ;
631 // new Thread( new Runnable() {
637 // if( sc.restartVMAfterCrash() != 0 )
639 // System.err.println( "Problem while restarting VM on " +sc.getName() + "!" ) ;
641 // } catch( RemoteException e ) {
643 // System.err.println( "Problem while restarting VM on " + sc.getName() + "!" ) ;
644 // } catch( RemoteException e1 ) {
645 // System.err.println( "Problem while restarting a VM!" ) ;
646 // e1.printStackTrace() ;
648 // e.printStackTrace() ;
658 Thread.sleep( 2000 ) ;
659 } catch( InterruptedException e ) {
660 e.printStackTrace() ;
668 public Integer saveOk( String _ip, String _saveName )
671 for( i = 0 ; i < computingClients.size() ; i ++ )
673 if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
675 computingClients.get( i ).setLastSave( _saveName ) ;
676 computingClients.get( i ).setSaveStatus( true ) ;
682 boolean all_ok = true ;
685 while( all_ok && i < computingClients.size() )
687 all_ok = all_ok & computingClients.get( i ).getSaveStatus() ;
693 for( i = 0 ; i < computingClients.size() ; i++ )
696 computingClients.get( i ).getClient().getStub().saveOk() ;
697 } catch (RemoteException e) {
698 System.err.println( "Unable to invoke the saveOk method on " + computingClients.get( i ).getClient().getName() ) ;
699 e.printStackTrace() ;
701 computingClients.get( i ).setSaveStatus( false ) ;
704 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
711 public Integer changeSaveName( String _ip, String _saveName )
713 if( _ip != null && _ip.length() > 0 && _saveName != null && _saveName.length() > 0 )
715 for( int i = 0 ; i < computingClients.size() ; i ++ )
717 if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
719 computingClients.get( i ).setLastSave( _saveName ) ;
720 System.out.println( "Save name successfully changed on " + computingClients.get( i ).getClient().getName() ) ;
725 System.err.println( "Unable to found computing client with IP " + _ip + "!" ) ;
729 System.err.println( "Unable to change save name. IP or save name empty ! (IP: " + _ip + " ; save name: " + _saveName +")" ) ;
736 public ArrayList<ServicesClient> startApplication( int _nb )
738 int nb = clients.size() - computingClients.size() ;
740 if( nb > _nb && ! running )
744 final ArrayList<ServicesClient> ac = new ArrayList<ServicesClient>() ;
745 final ArrayList<ComputingClient> tmp = new ArrayList<ComputingClient>() ;
747 RunningApplication app = new RunningApplication( "Test" ) ;
752 while( i < clients.size() && ac.size() < _nb )
755 if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
757 synchronized( startingClients )
759 while( ac.size() + startingClients.getNb() >= _nb )
761 if( ac.size() == _nb ) break ;
764 startingClients.wait() ;
765 } catch( InterruptedException e ) {
766 e.printStackTrace() ;
770 if( ac.size() < _nb )
772 startingClients.inc() ;
779 synchronized( limitThread )
781 while( limitThread.getNb() >= maxThread )
785 } catch (InterruptedException e) {
793 final int indice = i ;
794 new Thread( new Runnable()
801 res = clients.get( indice ).getStub().startVM( 0 ) ;
802 } catch( RemoteException e ) {
803 e.printStackTrace() ;
808 ac.add( clients.get( indice ).getStub() ) ;
809 clients.get( indice ).setStatus( "running" ) ;
810 ComputingClient cl = new ComputingClient( clients.get( indice ) ) ;
811 clients.get( indice ).setComputingClient( cl ) ;
812 computingClients.add( cl ) ;
815 System.err.println( "Problem while launching the VM on "
816 + clients.get( indice ).getName() + "!" ) ;
819 synchronized( limitThread )
822 limitThread.notifyAll() ;
825 synchronized( startingClients )
827 startingClients.dec() ;
828 startingClients.notifyAll() ;
838 if( ac.size() == _nb )
840 app.setComputingClients( tmp ) ;
841 app.setRunning( true ) ;
842 // app.setStartTime( System.currentTimeMillis() ) ;
845 /* Choosing save neighbors */
846 for( i = 0 ; i < tmp.size() ; i++ )
848 if( i == tmp.size() - 1 )
850 index = computingClients.indexOf( tmp.get( i ) ) ;
851 index2 = computingClients.indexOf( tmp.get( 0 ) ) ;
853 if( index == -1 || index2 == -1 )
855 System.err.println( "Problem in ComputingClients list!" ) ;
858 computingClients.get( index ).setSaveNeighbor( new SaveNeighbor( computingClients.get( index2 ).getClient().getStub() )) ;
859 } catch( RemoteException e ) {
860 System.err.println( "Unable to create the save neighbor!" ) ;
861 e.printStackTrace() ;
865 index = computingClients.indexOf( tmp.get( i ) ) ;
866 index2 = computingClients.indexOf( tmp.get( i + 1 ) ) ;
868 if( index == -1 || index2 == -1 )
870 System.err.println( "Problem in ComputingClients list!" ) ;
873 computingClients.get( index ).setSaveNeighbor( new SaveNeighbor( computingClients.get( index2 ).getClient().getStub() ) ) ;
874 } catch( RemoteException e ) {
875 System.err.println( "Unable to create the save neighbor!" ) ;
876 e.printStackTrace() ;
883 applications.add( app ) ;
885 ind = applications.indexOf( app ) ;
895 public void requestSave( String _ip )
899 } catch( InterruptedException e ) {
900 System.err.println( "Unable to obtain the semaphore for semaSave!" ) ;
901 e.printStackTrace() ;
904 final String ip = _ip ;
906 new Thread( new Runnable() {
911 treatRequestSave( ip ) ;
917 public void treatRequestSave( String _ip )
919 if( applications.size() > 0 && _ip != null && _ip.length() > 0 )
921 if( (System.currentTimeMillis() - applications.get( ind ).getLastSaveDate()) > save_interleave )
923 // Mark it as a requester
924 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
926 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
928 applications.get( ind ).getComputingClients().get( i ).setSaveRequest( true ) ;
938 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
942 ok = applications.get( ind ).getComputingClients().get( i ).getSaveRequest() ;
944 ok = ok & applications.get( ind ).getComputingClients().get( i ).getSaveRequest() ;
956 // Thread.sleep( 5000 ) ;
957 // } catch( InterruptedException e1 ) {
958 // e1.printStackTrace() ;
961 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
964 applications.get( ind ).getComputingClients().get( i ).getClient().getStub().responseSave( true ) ;
965 applications.get( ind ).getComputingClients().get( i ).setSaveRequest( false ) ;
966 } catch( RemoteException e ) {
967 System.err.println( "Unable to send the save request response to " +
968 applications.get( ind ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
969 e.printStackTrace() ;
973 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
979 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
981 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
984 applications.get( ind ).getComputingClients().get( i ).getClient().getStub().responseSave( false ) ;
985 } catch( RemoteException e ) {
986 System.err.println( "Unable to send the save request response to " +
987 applications.get( ind ).getComputingClients().get( i ).getClient().getName() + "!" ) ;
988 e.printStackTrace() ;
996 System.err.println( "!! Serious problem in treatRequestSave method!!" ) ;
1002 public void restartOk( String _ip )
1004 if( applications.size() > 0 && _ip != null && _ip.length() > 0 )
1006 System.out.println( "Client " + _ip + " has finished to restart ("+applications.get( ind ).getComputingClients().size()+") ... " ) ;
1007 // if( (System.currentTimeMillis() - applications.get( ind ).getLastSaveDate()) > save_interleave )
1009 // Has it already finished?
1010 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1012 if( applications.get( ind ).getComputingClients().get( i ).getClient().getIP().equalsIgnoreCase( _ip ) )
1014 applications.get( ind ).getComputingClients().get( i ).setRestartOk( true ) ;
1021 boolean ok = false ;
1022 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1026 ok = applications.get( ind ).getComputingClients().get( i ).getRestartOk() ;
1028 ok = ok & applications.get( ind ).getComputingClients().get( i ).getRestartOk() ;
1039 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
1041 for( int i = 0 ; i < applications.get( ind ).getComputingClients().size() ; i++ )
1043 applications.get( ind ).getComputingClients().get( i ).setRestartOk( false ) ;
1053 public void goApplication()
1055 synchronized( applications ) {
1056 if( running && applications.get( ind ).getStartTime() != 0 )
1058 applications.get( ind ).setStartTime( System.currentTimeMillis() ) ;
1059 applications.get( ind ).setLastSaveDate( System.currentTimeMillis() ) ;
1065 public void endApplication()
1067 synchronized( applications )
1071 applications.get( ind ).setEndTime( System.currentTimeMillis() ) ;
1072 applications.get( ind ).setRunning( false ) ;
1073 applications.get( ind ).clear() ;
1075 Iterator<ComputingClient> it = computingClients.iterator() ;
1077 while( it.hasNext() )
1079 ComputingClient cl = it.next() ;
1082 cl.getClient().getStub().emergencyStop() ;
1083 } catch (RemoteException e) {
1084 e.printStackTrace();
1087 cl.getClient().setStatus( "connected" ) ;
1088 cl.getClient().setComputingClient( null ) ;
1094 applications.get( ind ).clear() ;
1098 System.out.println( "Application " + applications.get( ind ).getName() + " ends in " +
1099 applications.get( ind ).getExecutionTime() + " seconds." ) ;
1107 public String getAssociatedIP( String _ip )
1111 for( int i = 0 ; i < vmIPs.size() ; i++ )
1113 if( vmIPs.get( i ).getHostIP().equalsIgnoreCase( _ip ) )
1115 ret = vmIPs.get( i ).getVmIP() ;
1124 public Integer deployVM( final String _name, final String _archive, final String _directory )
1126 int nb = 0, pb = 0 ;
1128 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0
1129 && _directory != null && _directory.length() > 0 )
1131 System.out.println( "Deploying the VM " + _name + " (" + _archive + ") ... " ) ;
1133 File file = new File( working_directory + "/" + _archive ) ;
1134 if( ! file.exists() )
1136 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1139 } else if( file.isDirectory() ) {
1140 System.err.println( _archive + " is a directory!" ) ;
1147 // TODO do a better deployment !!
1149 // boolean error, ok, server ;
1150 // ArrayList<ConnectedClient> deployed = new ArrayList<ConnectedClient>() ;
1152 // boolean server = true ;
1155 for( int i = 0 ; i < clients.size() ; i++ )
1161 if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) )
1163 synchronized( limitThread )
1165 while( limitThread.getNb() >= maxThread )
1168 limitThread.wait() ;
1169 } catch (InterruptedException e) {
1170 e.printStackTrace();
1177 final int indice = i ;
1178 new Thread( new Runnable() {
1183 boolean error = true ;
1186 ret = clients.get( indice ).getStub().deployVM( _name, _archive, _directory ) ;
1187 } catch( RemoteException e ) {
1188 System.err.println( "Unable to deploy the VM on " + clients.get( indice ).getName() + "!" ) ;
1189 e.printStackTrace() ;
1192 // The client does not have the archive, we have to send it.
1195 // Attention au multi-envois !!!
1196 System.out.print( "Sending VM archive to " + clients.get( indice ).getName() + " ... " ) ;
1203 wd = clients.get( indice ).getStub().getWorkingDirectory() ;
1204 snIP = clients.get( indice ).getStub().getIPHost() ;
1205 } catch (RemoteException e2) {
1206 System.err.println( "Unable to retrieve information on " + clients.get( indice ).getName() + "!" ) ;
1207 e2.printStackTrace() ;
1211 String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive,
1217 Process proc = Runtime.getRuntime().exec( command ) ;
1220 if( proc.exitValue() == 0 )
1222 System.out.println( "Initial VM archive successfully sent." ) ;
1224 System.err.println( "Initial VM archive not sent!" ) ;
1225 System.err.println( "Error: " + proc.exitValue() ) ;
1226 BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ;
1230 while( (l = b.readLine()) != null )
1232 System.err.println( l ) ;
1234 } catch( IOException e ) {
1235 e.printStackTrace() ;
1240 } catch( IOException e ) {
1241 System.err.println( "Error during initial VM archive send command: " ) ;
1242 e.printStackTrace() ;
1244 } catch( InterruptedException e ) {
1245 e.printStackTrace() ;
1256 ret = clients.get( indice ).getStub().deployVM( _name, _archive, _directory ) ;
1257 } catch( RemoteException e ) {
1258 System.err.println( "Unable to deploy the VM on " + clients.get( indice ).getName() + "!" ) ;
1259 e.printStackTrace() ;
1266 System.out.println( "Initial VM archive successfully deployed on " + clients.get( indice ).getName() + "." ) ;
1268 synchronized( deployingClients )
1270 deployingClients.inc() ;
1274 synchronized( limitThread )
1277 limitThread.notifyAll() ;
1285 synchronized( limitThread )
1287 while( limitThread.getNb() > 0 )
1290 limitThread.wait() ;
1291 } catch( InterruptedException e ) {
1292 e.printStackTrace() ;
1297 if( nb - deployingClients.getNb() > 0 )
1300 System.err.println( "** " + pb + " machine is not deployed!" ) ;
1302 System.err.println( "** " + pb + " machines are not deployed!" ) ;
1309 public String getWorkingDirectory()
1311 return working_directory ;
1315 private class OperatingClients
1319 OperatingClients() { nb = 0 ; }
1321 protected void inc() { nb++ ; }
1323 protected void dec() { nb-- ; }
1325 protected int getNb() { return nb ; }
1329 private class LimitThread
1333 LimitThread() { nb = 0 ; }
1335 protected void inc() { nb++ ; }
1337 protected void dec() { nb-- ; }
1339 protected int getNb() { return nb ; }
1343 private class RestartVM extends Thread
1345 private ConnectedClient cc ;
1347 protected RestartVM( ConnectedClient _cc )
1354 boolean error = false ;
1358 if( cc.getStub().restartVMAfterCrash() != 0 )
1360 System.err.println( "Problem while restarting VM on " + cc.getName() + "!" ) ;
1363 } catch( RemoteException e ) {
1364 e.printStackTrace() ;
1369 System.err.println( "The client to restart is null!" ) ;
1374 cc.setFail( true ) ;
1377 System.out.print( "Trying to stop the client ... " ) ;
1378 cc.getStub().stop() ;
1379 System.out.println( "successful client stop." );
1380 } catch( RemoteException e ) {
1381 System.out.println( "unsuccessful client stop!" ) ;
1382 e.printStackTrace() ;
1390 /** La programmation est un art, respectons ceux qui la pratiquent !! **/