3 import java.io.BufferedReader;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.net.MalformedURLException;
9 import java.net.ServerSocket;
10 import java.net.Socket;
11 import java.rmi.Naming;
12 import java.rmi.NotBoundException;
13 import java.rmi.RemoteException;
14 import java.rmi.registry.LocateRegistry;
15 import java.rmi.registry.Registry;
16 import java.rmi.server.UnicastRemoteObject;
17 import java.util.ArrayList;
18 import java.util.concurrent.Semaphore;
21 public class Client extends UnicastRemoteObject implements ServicesClient
23 private static final long serialVersionUID = 1L ;
25 private String VmRunCommand ;
26 // private String VmRunCommandArg ;
27 private VirtualMachine machine ;
28 private String server_ip ;
29 private int server_port ;
30 private int client_port ;
31 private int dialog_port ;
32 private ServicesServer serverStub ;
33 private ServicesClient myStub ;
34 private PingServer pingServer ;
35 private DialogVMServer dialogVmServer ;
36 private ServerSocket serverSocket ;
37 private String ushell ;
38 private String working_directory ;
39 private int wait_start ;
40 private int max_start_try ;
41 private boolean isRestartedSave ;
42 // private long save_interleave ;
43 // private long date_last_save ;
44 private SaveProcess saveProcess;
45 private int maxRetryVM ;
46 private int timeRetryVM ;
47 private Semaphore sema ;
48 private boolean emergencyStop ;
49 private Process procSave ;
50 private SaveRequest saveRequest ;
51 private boolean lastSaveOk ;
54 protected Client() throws RemoteException
60 public void emergencyStop()
62 emergencyStop = true ;
64 // Saving processus stop
65 synchronized( saveProcess ) {
66 saveProcess.setStatus( false ) ;
68 saveProcess.notifyAll() ;
69 } catch( Exception e ) {}}
73 } catch( Exception e ) {}
75 // Stopping the save request
76 synchronized( saveRequest )
78 saveRequest.setStatus( false ) ;
80 saveRequest.notifyAll() ;
81 } catch( Exception e ) {}
89 public int startVM( int _mode )
91 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "running" ) )
95 } catch( InterruptedException e2 ) {
96 System.err.println( "Problem with semaphore acquiring!" ) ;
97 e2.printStackTrace() ;
101 // Checking first start
102 if( machine.checkVmx() == 1 )
111 System.out.print( "Starting VM ... " ) ;
113 machine.setStatus( "undefined" ) ;
117 LocalHost.Instance().getServerStub().changeStatus(
118 LocalHost.Instance().getIP(), "undefined" ) ;
119 } catch( RemoteException e ) {
120 System.err.println( "Unable to inform the server of the VM status!" ) ;
121 e.printStackTrace() ;
125 String[] command = new String[]{VmRunCommand, "-T", "player", "start",
126 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name(), "nogui"} ;
137 Process p = Runtime.getRuntime().exec( command ) ;
140 if( p.exitValue() == 0 )
142 System.out.println( "Virtual machine successfully started." ) ;
145 System.err.println( "Virtual machine not started!" ) ;
146 ret = printProcessError( p ) ;
155 if( retry >= maxRetryVM )
157 System.err.println( "Unable to start VM!" ) ;
163 System.out.println( "Retrying (" + retry + ") ... " ) ;
164 Thread.sleep( timeRetryVM ) ;
167 } catch( IOException e ) {
168 System.err.println( "Error during execution of start command: " ) ;
169 e.printStackTrace() ;
174 } catch( InterruptedException e ) {
175 e.printStackTrace() ;
183 boolean started = false ;
195 /** Waiting for VM being started **/
197 Thread.sleep( wait_start ) ;
198 } catch( InterruptedException e ) {
199 e.printStackTrace() ;
202 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
203 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
204 working_directory + "/" + machine.getDirectory()
205 + "/" + machine.getVmx_name() + " " + ushell +
209 FileWriter fw = new FileWriter( new File( working_directory + "/testStarted.sh" ) ) ;
213 } catch( IOException e1 ) {
214 e1.printStackTrace() ;
220 command = new String[]{ ushell, working_directory + "/testStarted.sh"} ;
230 Process p = Runtime.getRuntime().exec( command ) ;
233 if( p.exitValue() == 0 )
238 System.err.println( "Error while checking if the VM is started!" ) ;
239 ret = printProcessError( p ) ;
248 if( retry >= maxRetryVM )
250 System.err.println( "Unable to check VM!" ) ;
256 System.out.println( "Retrying (" + retry + ") ... " ) ;
257 Thread.sleep( timeRetryVM ) ;
261 } catch( IOException e ) {
262 e.printStackTrace() ;
266 } catch( InterruptedException e ) {
267 e.printStackTrace() ;
273 if( count == max_start_try && ! started )
275 System.err.println( "Virtual machine not responding!!" ) ;
278 LocalHost.Instance().getServerStub().changeStatus(
279 LocalHost.Instance().getIP(), "undefined" ) ;
280 } catch( RemoteException e ) {
281 e.printStackTrace() ;
291 Thread.sleep( 3000 ) ;
292 } catch( InterruptedException e ) {
293 e.printStackTrace() ;
299 /** Sending the host ip **/
301 System.out.print( "Sending host IP to VM ... " ) ;
303 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
304 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
305 working_directory + "/" + machine.getDirectory()
306 + "/" + machine.getVmx_name() + " " + ushell +
307 " \"echo " + LocalHost.Instance().getIP() + " " + dialog_port
308 + " > /tmp/vm_host_IP\"" ;
311 FileWriter fw = new FileWriter( new File( working_directory + "/sendHostIP.sh" ) ) ;
315 } catch( IOException e1 ) {
316 e1.printStackTrace() ;
322 command = new String[]{ ushell, working_directory + "/sendHostIP.sh"} ;
335 Process p = Runtime.getRuntime().exec( command ) ;
338 if( p.exitValue() == 0 )
340 System.out.println( "VM received the host IP." ) ;
343 System.err.println( "VM did not received the host IP!" ) ;
344 ret = printProcessError( p ) ;
353 if( retry >= maxRetryVM )
355 System.err.println( "Unable to send information to VM!" ) ;
362 System.out.println( "Retrying (" + retry + ") ... " ) ;
363 Thread.sleep( timeRetryVM ) ;
366 } catch( IOException e ) {
367 System.err.println( "Error during execution of runScriptInGuest command: " ) ;
368 e.printStackTrace() ;
372 } catch( InterruptedException e) {
373 e.printStackTrace() ;
380 /** Sending the vm ip **/
382 System.out.print( "Sending its IP to VM ... " ) ;
384 cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
385 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
386 working_directory + "/" + machine.getDirectory()
387 + "/" + machine.getVmx_name() + " " + ushell +
388 " \"echo " + machine.getIp()
389 + " > /tmp/vm_IP\"" ;
392 FileWriter fw = new FileWriter( new File( working_directory + "/sendVmIP.sh" ) ) ;
396 } catch( IOException e1 ) {
397 e1.printStackTrace() ;
403 command = new String[]{ ushell, working_directory + "/sendVmIP.sh"} ;
416 Process p = Runtime.getRuntime().exec( command ) ;
419 if( p.exitValue() == 0 )
421 System.out.println( "VM received its assigned IP." ) ;
423 machine.setStatus( "running" ) ;
425 System.err.println( "VM did not received its assigned IP!" ) ;
426 ret = printProcessError( p ) ;
435 if( retry >= maxRetryVM )
437 System.err.println( "Unable to send information to VM!" ) ;
444 System.out.println( "Retrying (" + retry + ") ... " ) ;
445 Thread.sleep( timeRetryVM ) ;
448 } catch( IOException e ) {
449 System.err.println( "Error during execution of runScriptInGuest command: " ) ;
450 e.printStackTrace() ;
454 } catch( InterruptedException e ) {
455 e.printStackTrace() ;
465 LocalHost.Instance().getServerStub().changeStatus(
466 LocalHost.Instance().getIP(), "running" ) ;
467 } catch (RemoteException e) {
468 System.err.println( "Unable to inform the server of the VM started status!" ) ;
485 public int stopVM( int _mode )
487 if( machine != null && machine.getStatus().equalsIgnoreCase( "stopped" ) )
489 emergencyStop = false ;
493 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "stopped" ) )
497 } catch( InterruptedException e2 ) {
498 System.err.println( "Problem with semaphore acquiring!" ) ;
499 e2.printStackTrace() ;
502 System.out.print( "Stopping VM ... " ) ;
507 machine.setStatus( "undefined" ) ;
510 LocalHost.Instance().getServerStub().changeStatus(
511 LocalHost.Instance().getIP(), "undefined" ) ;
512 } catch( RemoteException e ) {
513 System.err.println( "Unable to inform the server of the VM status!" ) ;
514 e.printStackTrace() ;
517 String[] command = new String[]{VmRunCommand, "-T", "player", "stop",
518 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
522 Process p = Runtime.getRuntime().exec( command ) ;
525 if( p.exitValue() == 0 )
527 System.out.println( "Virtual machine successfully stopped." ) ;
528 machine.setStatus( "stopped" ) ;
531 System.err.println( "Virtual machine not stopped!" ) ;
532 ret = printProcessError( p ) ;
540 if( retry >= maxRetryVM )
542 System.err.println( "Unable to stop VM!" ) ;
546 System.out.println( "Retrying (" + retry + ") ... " ) ;
547 Thread.sleep( timeRetryVM ) ;
552 } catch( IOException e ) {
553 System.err.println( "Error during execution of stop command: " ) ;
554 e.printStackTrace() ;
557 } catch( InterruptedException e ) {
558 e.printStackTrace() ;
563 machine.setStatus( "stopped" ) ;
566 LocalHost.Instance().getServerStub().changeStatus(
567 LocalHost.Instance().getIP(), "stopped" ) ;
568 } catch( RemoteException e1 ) {
569 System.err.println( "Unable to inform the server of the VM stopped status!" ) ;
570 e1.printStackTrace() ;
574 if( machine.checkVmx() == 0 )
577 emergencyStop = false ;
588 public int suspendVM( int _mode )
590 if( machine != null && machine.getStatus().equalsIgnoreCase( "suspended" ) )
595 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "suspended" ) )
599 } catch( InterruptedException e2 ) {
600 System.err.println( "Problem with semaphore acquiring!" ) ;
601 e2.printStackTrace() ;
604 System.out.print( "Suspending VM ... " ) ;
609 machine.setStatus( "undefined" ) ;
613 LocalHost.Instance().getServerStub().changeStatus(
614 LocalHost.Instance().getIP(), "undefined" ) ;
615 } catch( RemoteException e ) {
616 System.err.println( "Unable to inform the server of the VM status!" ) ;
617 e.printStackTrace() ;
621 String[] command = new String[]{VmRunCommand, "-T", "player", "suspend",
622 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
626 Process p = Runtime.getRuntime().exec( command ) ;
628 if( p.exitValue() == 0 )
630 System.out.println( "Virtual machine successfully suspended." ) ;
631 machine.setStatus( "suspended" ) ;
634 System.err.println( "Virtual machine not suspended!" ) ;
635 ret = printProcessError( p ) ;
644 if( retry >= maxRetryVM )
646 System.err.println( "Unable to suspend VM!" ) ;
652 System.out.println( "Retrying (" + retry + ") ... " ) ;
653 Thread.sleep( timeRetryVM ) ;
658 } catch( IOException e ) {
659 System.err.println( "Error during execution of suspend command: " ) ;
660 e.printStackTrace() ;
661 } catch( InterruptedException e ) {
662 e.printStackTrace() ;
665 machine.setStatus( "suspended" ) ;
669 LocalHost.Instance().getServerStub().changeStatus(
670 LocalHost.Instance().getIP(), "suspended" ) ;
671 } catch( RemoteException e ) {
672 System.err.println( "Unable to inform the server of the VM suspended status!" ) ;
673 e.printStackTrace() ;
688 public int restartVM()
690 if( machine != null )
692 System.out.print( "Restarting VM ... " ) ;
698 LocalHost.Instance().getServerStub().changeStatus(
699 LocalHost.Instance().getIP(), "undefined" ) ;
700 } catch( RemoteException e ) {
701 System.err.println( "Unable to inform the server of the VM status!" ) ;
702 e.printStackTrace() ;
705 String[] command = new String[]{VmRunCommand, "-T", "player", "reset",
706 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
710 Process p = Runtime.getRuntime().exec( command ) ;
713 if( p.exitValue() == 0 )
715 System.out.println( "Virtual machine successfully restarted." ) ;
717 if( sendSaveOkVM() == 1 ) { return 1 ; }
723 System.err.println( "Virtual machine not restarted!" ) ;
724 ret = printProcessError( p ) ;
731 if( retry >= maxRetryVM )
733 System.err.println( "Unable to start VM!" ) ;
736 System.out.println( "Retrying (" + retry + ") ... " ) ;
737 Thread.sleep( timeRetryVM ) ;
742 } catch( IOException e ) {
743 System.err.println( "Error during execution of restart command: " ) ;
744 e.printStackTrace() ;
745 } catch( InterruptedException e ) {
746 e.printStackTrace() ;
754 public int restartVMAfterCrash()
756 System.out.println( "Restarting VM after a crash ..." ) ;
759 LocalHost.Instance().getServerStub().changeStatus(
760 LocalHost.Instance().getIP(), "undefined" ) ;
761 } catch( RemoteException e ) {
762 System.err.println( "Unable to inform the server of the VM status!" ) ;
763 e.printStackTrace() ;
766 if( stopVM( 1 ) == 0 )
768 if( machine.deployLastSave() == 0 )
770 if( isRestartedSave )
772 // Using the specific vmx file
773 machine.setDeployFault( true ) ;
775 // Writing the restarted save mark
777 FileWriter fw = new FileWriter( new File( working_directory + "/" + machine.getDirectory() + "/fault.hpcvm" ) ) ;
778 fw.write( "fault!" ) ;
782 } catch( IOException e1 ) {
783 e1.printStackTrace() ;
784 System.err.println( "Unable to mark the fault!" ) ;
787 if( machine.checkVmx() == 1 )
793 /** Retrieving VM assigned IP **/
797 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
798 LocalHost.Instance().getIP() ) ;
799 } catch (RemoteException e) {
800 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
801 e.printStackTrace() ;
805 machine.setIp( vmIP ) ;
807 isRestartedSave = false ;
810 if( startVM( 0 ) == 0 )
812 if( sendSaveOkVM() == 0 )
815 LocalHost.Instance().getServerStub().restartOk( LocalHost.Instance().getIP() ) ;
816 } catch( RemoteException e ) {
817 System.err.println( "Unable to inform server about my successful restart!" ) ;
818 e.printStackTrace() ;
833 private int sendSaveOkVM()
838 /** Informing the program that it's ok **/
839 System.out.print( "Sending OK signal to the program ... " ) ;
841 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
842 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
843 working_directory + "/" + machine.getDirectory()
844 + "/" + machine.getVmx_name() + " " + ushell +
845 " \"echo ok > /tmp/vm_save_ok\"" ;// + " -noWait " ;
848 FileWriter fw = new FileWriter( new File( working_directory + "/saveOk.sh" ) ) ;
852 } catch( IOException e1 ) {
853 e1.printStackTrace() ;
856 String[] command = new String[]{ ushell, working_directory + "/saveOk.sh"} ;
860 Process p = Runtime.getRuntime().exec( command ) ;
863 if( p.exitValue() == 0 )
865 System.out.println( "Signal successfully sent." ) ;
869 System.err.println( "Signal not sent!" ) ;
870 ret = printProcessError( p ) ;
877 if( retry >= maxRetryVM )
879 System.err.println( "Unable to send ok signal to VM!" ) ;
882 System.out.println( "Retrying (" + retry + ") ... " ) ;
883 Thread.sleep( timeRetryVM ) ;
887 } catch( IOException e ) {
888 System.err.println( "Error during ok save signal send command: " ) ;
889 e.printStackTrace() ;
891 } catch( InterruptedException e ) {
892 e.printStackTrace() ;
903 synchronized( saveProcess ){
904 while( saveProcess.getStatus() )
908 } catch( InterruptedException e ) {
909 e.printStackTrace() ;
913 System.out.println( "Saving VM ..." ) ;
914 saveProcess.setStatus( true ) ;
916 long deb = System.currentTimeMillis() ;
918 machine.setStatus( "saving" ) ;
920 LocalHost.Instance().getServerStub().changeStatus(
921 LocalHost.Instance().getIP(), "saving" ) ;
922 } catch( RemoteException e ) {
923 System.err.println( "Unable to inform the server of the VM status!" ) ;
924 e.printStackTrace() ;
928 String saveName = "" ;
929 boolean error = false ;
932 if( suspendVM( 1 ) == 1 ) { return 1 ; }
938 File file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ) ;
941 arch = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
946 if( arch.length() > 0 )
948 System.out.println( "Deletion of last nok archive ... " ) ;
950 command = new String[]{ "/bin/rm", "-rf", arch } ;
953 procSave = Runtime.getRuntime().exec( command ) ;
956 if( procSave.exitValue() == 0 )
958 System.out.println( "Last nok archive successfully deleted." ) ;
960 System.err.println( "Last nok archive not deleted!" ) ;
961 printProcessError( procSave ) ;
965 } catch( IOException e ) {
966 System.err.println( "Error during nok archive deletion command: " ) ;
968 e.printStackTrace() ;
969 } catch( InterruptedException e ) {
970 e.printStackTrace() ;
979 System.out.print( "Creation of the archive ... " ) ;
981 /** Archive creation **/
982 command = new String[]{ "/bin/tar", "-cz", "-C", working_directory,
983 "-f", working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz",
984 machine.getDirectory() } ;
992 procSave = Runtime.getRuntime().exec( command ) ;
995 if( procSave.exitValue() == 0 )
997 System.out.println( "Archive successfully created." ) ;
1002 System.err.println( "Archive not created!" ) ;
1003 printProcessError( procSave ) ;
1007 } catch( IOException e ) {
1008 System.err.println( "Error during archive creation command: " ) ;
1010 e.printStackTrace() ;
1011 } catch( InterruptedException e ) {
1012 e.printStackTrace() ;
1016 long fin = System.currentTimeMillis() ;
1018 System.out.println( "Time to create the save: " + (fin-deb)/1000 + " seconds." ) ;
1021 /** Restarting VM **/
1022 if( startVM( 0 ) == 1 ) { return 1 ; }
1025 /** Sending ok save signal **/
1026 if( sendSaveOkVM() == 1 ) { return 1 ; }
1028 saveName = machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
1030 /** Sending save to neighbor **/
1038 ArrayList<SaveNeighbor> sn = machine.getSaveNeighbors() ;
1040 for( int i = 0 ; i < sn.size() ; i++ )
1042 String name = sn.get( i ).getName() ;
1043 String wd = sn.get( i ).getWorkingDirectory() ;
1044 String snIP = sn.get( i ).getIPHost() ;
1046 System.out.print( "Sending save to " + name + " ... " ) ;
1048 command = new String[]{ "/usr/bin/scp", working_directory + "/" + saveName,
1052 procSave = Runtime.getRuntime().exec( command ) ;
1053 procSave.waitFor() ;
1055 if( procSave.exitValue() == 0 )
1057 System.out.println( "Archive successfully sent." ) ;
1059 System.err.println( "Archive not sent!" ) ;
1060 printProcessError( procSave ) ;
1064 } catch( IOException e ) {
1065 System.err.println( "Error during archive send command: " ) ;
1066 e.printStackTrace() ;
1068 } catch( InterruptedException e ) {
1069 e.printStackTrace() ;
1076 /** Informing the server the save is done **/
1080 LocalHost.Instance().getServerStub().saveOk( LocalHost.Instance().getIP(), saveName ) ;
1081 } catch( RemoteException e ) {
1082 System.err.println( "Problem while informing the server about the save state!" ) ;
1083 e.printStackTrace() ;
1086 synchronized( saveProcess ) {
1087 saveProcess.setStatus( false ) ;
1089 saveProcess.notifyAll() ;
1090 } catch( Exception e ) {}}
1095 synchronized( saveProcess ) {
1096 saveProcess.setStatus( false ) ;
1098 saveProcess.notifyAll() ;
1099 } catch( Exception e ) {}}
1106 public int reloadConfig()
1108 System.out.println( "Reloading configuration ... " ) ;
1114 public void init( String _server_ip, int _server_port, int _client_port, int _dialog_port )
1116 System.out.println( "Initialisation Client ... " ) ;
1117 System.out.println( "IP " + LocalHost.Instance().getIP() ) ;
1119 server_ip = _server_ip ;
1120 server_port = _server_port ;
1121 client_port = _client_port ;
1122 dialog_port = _server_port + 1 ; // _dialog_port ;
1125 saveProcess = new SaveProcess() ;
1126 saveRequest = new SaveRequest() ;
1128 machine = new VirtualMachine() ;
1130 VmRunCommand = "/usr/bin/vmrun" ;
1131 // VmRunCommandArg = "-T player" ;
1133 // vm_user = "mpi" ;
1134 // vm_user_passwd = "mpi" ;
1135 ushell = "/bin/bash" ;
1136 working_directory = "/localhome/vmware" ;
1138 wait_start = 15000 ;
1139 max_start_try = 10 ;
1141 sema = new Semaphore( 1 ) ;
1142 emergencyStop = false ;
1145 timeRetryVM = 10000 ;
1147 // save_interleave = 30 * 60 * 1000 ;
1148 // date_last_save = 0 ;
1150 isRestartedSave = false ;
1151 lastSaveOk = false ;
1153 /** Connection to server **/
1155 serverStub = (ServicesServer) Naming.lookup( "rmi://"
1156 + server_ip + ":" + server_port + "/Server" ) ;
1157 } catch (MalformedURLException e) {
1158 e.printStackTrace();
1159 } catch (RemoteException e) {
1160 e.printStackTrace();
1161 } catch (NotBoundException e) {
1162 e.printStackTrace();
1165 if( serverStub == null )
1167 System.err.println( "Unable to connect to server!!" ) ;
1168 System.err.println( "Server IP: " + server_ip + " -- server port: " + server_port ) ;
1173 System.out.println( "Connected to server " + server_ip + " on port " + server_port + "." ) ;
1175 LocalHost.Instance().setServerStub( serverStub ) ;
1178 /** Creating the local server **/
1181 /** Starting all threads **/
1185 private void exportObject()
1187 Registry reg = null ;
1193 reg = LocateRegistry.getRegistry( client_port ) ;
1195 String tab[] = reg.list() ;
1197 System.out.println( "There is an existing RMI Registry on port " +
1198 client_port + " with " + tab.length + " entries!" ) ;
1199 for( int i = 0 ; i < tab.length ; i++ )
1202 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
1204 System.out.println( "Register successfuly deleted!" ) ;
1206 System.err.println( "Register undeleted !!!" ) ;
1208 } catch( Exception e ) {
1209 e.printStackTrace() ;
1213 } catch( RemoteException e ) {
1217 if ( System.getSecurityManager() == null )
1219 System.setSecurityManager( new SecurityManager() ) ;
1222 LocateRegistry.createRegistry( client_port ) ;
1223 LocateRegistry.getRegistry( client_port ).rebind( "Client", this ) ;
1224 myStub = (ServicesClient) Naming.lookup( "rmi://"
1225 + LocalHost.Instance().getIP() + ":" + client_port
1227 } catch( Exception e ) {
1228 System.err.println( "Error in Client.exportObject() when creating local services:" + e ) ;
1229 System.err.println( "Exit from Client.exportObject" ) ;
1233 LocalHost.Instance().setStub( myStub ) ;
1237 private boolean printProcessError( Process _p )
1239 boolean ret = false ;
1243 System.err.println( "Error: " + _p.exitValue() ) ;
1244 BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
1247 while( (line = br.readLine()) != null )
1249 System.err.println( line ) ;
1250 if( line.contains( "egmentation" ) || _p.exitValue() == 139 )
1255 } catch( IOException e ) {
1256 e.printStackTrace() ;
1274 /** Registering on server **/
1277 ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1278 } catch (RemoteException e1) {
1279 e1.printStackTrace();
1285 case 0: System.out.println( "Successfully registered on server." ) ; break ;
1286 case 1: System.err.println( "Problem on server while registreting!" ) ; return 1 ;
1287 case 2: System.out.println( "Already registered on server!" ) ; break ;
1290 /** Retrieving VM assigned IP **/
1291 String vmIP = null ;
1294 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
1295 LocalHost.Instance().getIP() ) ;
1296 } catch (RemoteException e) {
1297 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
1298 e.printStackTrace() ;
1302 machine.setIp( vmIP ) ;
1304 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1307 /** Starting alive ping to server **/
1308 pingServer = new PingServer() ;
1309 pingServer.start() ;
1311 /** Starting socket server for VM dialog **/
1312 dialogVmServer = new DialogVMServer() ;
1313 dialogVmServer.start() ;
1319 private class PingServer extends Thread
1321 protected boolean go ;
1328 protected void stopPing() { go = false ; }
1336 LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1337 } catch( RemoteException e1 ) {
1338 System.err.println( "Unable to ping the server!" ) ;
1339 e1.printStackTrace() ;
1345 } catch( InterruptedException e ) {
1346 e.printStackTrace() ;
1354 private class DialogVMServer extends Thread
1356 protected boolean go ;
1357 private Socket socket ;
1358 // private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1365 protected void stopDialogVMServer()
1369 if( serverSocket != null )
1372 serverSocket.close() ;
1373 } catch( IOException e ) {
1374 e.printStackTrace() ;
1385 serverSocket = new ServerSocket( 0 ) ;
1386 dialog_port = serverSocket.getLocalPort() ;
1388 System.out.println( "SocketServer listening on port " + dialog_port ) ;
1389 } catch( IOException e ) {
1390 System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
1391 e.printStackTrace() ;
1398 socket = serverSocket.accept() ;
1400 new DialogVM( socket ).start() ;
1402 } catch( IOException e ) {
1403 System.err.println( "Problem with the accept function!" ) ;
1404 e.printStackTrace() ;
1412 private class DialogVM extends Thread
1414 private Socket socket ;
1415 private BufferedReader reader ;
1416 private String line ;
1418 DialogVM( Socket _socket ) { socket = _socket ; }
1424 reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
1425 } catch( IOException e ) {
1426 System.err.println( "Unable to open a dialog socket with the VM!" ) ;
1427 e.printStackTrace() ;
1434 if( reader != null )
1436 line = reader.readLine() ;
1439 /** VM is starting -- retrieving informations **/
1440 if( line != null && line.equalsIgnoreCase( "infos" ) )
1442 /* Receiving name */
1443 machine.setName( reader.readLine() ) ;
1446 String ip = reader.readLine() ;
1447 if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1449 System.err.println( "VM IP not well configured!!" ) ;
1453 reader.close() ; reader = null ;
1454 socket.close() ; socket = null ;
1456 } else if( line != null && line.equalsIgnoreCase( "save" ) ) {
1457 /** It's time to do a save **/
1460 machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1461 } catch( Exception e ) {
1462 System.err.println( "Problem while reading the computation id!" ) ;
1463 e.printStackTrace() ;
1466 synchronized( saveRequest )
1469 LocalHost.Instance().getServerStub().requestSave( LocalHost.Instance().getIP() ) ;
1470 } catch( RemoteException e ) {
1471 System.err.println( "Unable to request save to server!" ) ;
1472 e.printStackTrace() ;
1476 saveRequest.wait() ;
1477 } catch( InterruptedException e ) {
1478 e.printStackTrace();
1482 if( saveRequest.getStatus() )
1485 reader.close() ; reader = null ;
1486 socket.close() ; socket = null ;
1488 saveRequest.setStatus( false ) ;
1490 /* Requesting the VM save */
1493 System.err.println( "Problem while saving. Exiting ..." ) ;
1494 pingServer.stopPing() ;
1501 } else if( line != null && line.equalsIgnoreCase( "quit" ) ) {
1502 /** Computation is done, we can shutdown the VM **/
1505 Thread.sleep( 5000 ) ;
1506 } catch( InterruptedException e ) {
1507 e.printStackTrace() ;
1511 reader.close() ; reader = null ;
1512 socket.close() ; socket = null ;
1516 machine.setStatus( "connected" ) ;
1518 LocalHost.Instance().getServerStub().changeStatus(
1519 LocalHost.Instance().getIP(), "connected" ) ;
1520 } catch( RemoteException e ) {
1521 System.err.println( "Unable to inform the server of the VM status!" ) ;
1522 e.printStackTrace() ;
1526 LocalHost.Instance().getServerStub().endApplication() ;
1527 } catch( RemoteException e ) {
1528 System.err.println( "Unable to inform the server of the end of application!" ) ;
1529 e.printStackTrace() ;
1531 } else if( line != null && line.equalsIgnoreCase( "start" ) ) {
1532 /** Computation is starting **/
1535 reader.close() ; reader = null ;
1536 socket.close() ; socket = null ;
1537 System.out.println( "Application is starting." ) ;
1540 LocalHost.Instance().getServerStub().goApplication() ;
1541 } catch( RemoteException e ) {
1542 System.err.println( "Unable to inform the server of the end of application!" ) ;
1543 e.printStackTrace() ;
1547 } catch( IOException e ) {
1548 e.printStackTrace() ;
1559 pingServer.stopPing() ;
1561 dialogVmServer.stopDialogVMServer() ;
1563 // unexportObject ??
1569 public String getIPHost()
1571 return LocalHost.Instance().getIP() ;
1575 public String getName()
1577 return LocalHost.Instance().getName() ;
1582 public void saveOk()
1584 String save_name = machine.getName() + "_last_" + machine.getComputationId() +
1587 String save_new = machine.getName() + "_new_"
1588 + machine.getComputationId() + ".tar.gz" ;
1590 String[] command = new String[]{ "/bin/mv",
1591 working_directory + "/" + save_new,
1592 working_directory + "/" + save_name } ;
1595 Process p = Runtime.getRuntime().exec( command ) ;
1598 if( p.exitValue() == 0 )
1600 machine.setSave_last( save_name ) ;
1601 System.out.println( "Last save OK" ) ;
1603 System.err.println( "Last save NOK!" ) ;
1604 System.err.println( "Error: " ) ;
1605 printProcessError( p ) ;
1607 } catch( IOException e ) {
1608 System.err.println( "Error during last archive move:" ) ;
1609 e.printStackTrace() ;
1610 } catch( InterruptedException e ) {
1611 e.printStackTrace() ;
1614 // Changing on save neighbors
1615 for( int i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1618 machine.getSaveNeighbors().get( i ).getStub().changeSaveName( save_new, save_name, machine.getComputationId() ) ;
1619 } catch( RemoteException e ) {
1620 System.err.println( "Unable to change save name on " + machine.getSaveNeighbors().get( i ).getName() + "!" ) ;
1621 e.printStackTrace() ;
1625 // Informing the server
1628 ret = LocalHost.Instance().getServerStub().changeSaveName( LocalHost.Instance().getIP(), save_name ) ;
1629 } catch( RemoteException e ) {
1630 System.err.println( "Unable to inform the server about the new save name!" ) ;
1631 e.printStackTrace() ;
1636 System.out.println( "Successfully informing the server about the new save name." ) ;
1638 System.err.println( "Problem on the server while informing it about the new save name!" ) ;
1646 public void changeSaveName( String _n1, String _n2, int _id )
1648 if( _n1 != null && _n1.length() > 0 )
1650 System.out.println( "Changing save name for processus " + _id + " ... " ) ;
1652 String[] command = new String[]{ "/bin/mv",
1653 working_directory + "/" + _n1,
1654 working_directory + "/" + _n2 } ;
1657 Process p = Runtime.getRuntime().exec( command ) ;
1660 if( p.exitValue() == 0 )
1662 System.out.println( "Change save name OK" ) ;
1664 System.err.println( "Change save name NOK!" ) ;
1665 System.err.println( "Error: " ) ;
1666 printProcessError( p ) ;
1668 } catch( IOException e ) {
1669 System.err.println( "Error during save renaming:" ) ;
1670 e.printStackTrace() ;
1671 } catch( InterruptedException e ) {
1672 e.printStackTrace() ;
1679 public void setSaveNeighbor( SaveNeighbor _sn )
1683 ArrayList<SaveNeighbor> as = new ArrayList<SaveNeighbor>() ;
1686 System.out.println( "Save neighbor: " + _sn.getName() ) ;
1688 machine.setSaveNeighbors( as ) ;
1694 public void setSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1696 if( _sn != null && _sn.size() > 0 )
1698 System.out.print( "Save neighbors: " ) ;
1699 for( int i = 0 ; i < _sn.size() ; i++ )
1701 System.out.print( _sn.get( i ).getName() ) ;
1703 if( i != _sn.size() - 1 )
1705 System.out.print( ", " ) ;
1707 System.out.println( "." ) ;
1711 machine.setSaveNeighbors( _sn ) ;
1717 public void addSaveNeighbor( SaveNeighbor _sn )
1721 System.out.println( "Adding save neighbor: " + _sn.getName() ) ;
1723 machine.getSaveNeighbors().add( _sn ) ;
1729 public void addSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1731 if( _sn != null && _sn.size() > 0 )
1733 System.out.print( "Adding save neighbors: " ) ;
1734 for( int i = 0 ; i < _sn.size() ; i++ )
1736 System.out.print( _sn.get( i ).getName() ) ;
1738 if( i != _sn.size() - 1 )
1740 System.out.print( ", " ) ;
1742 System.out.println( "." ) ;
1745 machine.getSaveNeighbors().add( _sn.get( i ) ) ;
1752 public void replaceSaveNeighbor( SaveNeighbor _old, SaveNeighbor _new )
1754 System.out.print( "Replacing a save neihgbor ... " ) ;
1755 if( _old != null && _new != null )
1758 boolean change = false ;
1760 for( i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1762 if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old.getIPHost() ) )
1764 machine.getSaveNeighbors().set( i, _new ) ;
1765 System.out.println( "Save neighbor successfully changed." ) ;
1773 System.out.println( "I am not concerned by the modification." ) ;
1780 public int retrieveSave( String _saveName )
1782 if( _saveName != null )
1784 if( ! _saveName.equalsIgnoreCase( "none" ) )
1786 machine.setSave_last( _saveName ) ;
1788 System.err.println( "I have no save to retrieve!!" ) ;
1792 // TODO NEIGHBORS !!!!
1793 boolean ok = false ;
1796 while( ! ok && i < machine.getSaveNeighbors().size() )
1798 System.out.print( "Retrieving a save on " + machine.getSaveNeighbors().get( 0 ).getName() + " ... " ) ;
1800 String command[] = {} ;
1802 command = new String[]{ "/usr/bin/scp",
1803 machine.getSaveNeighbors().get( i ).getIPHost() + ":" +
1804 machine.getSaveNeighbors().get( i ).getWorkingDirectory() + "/" +
1805 machine.getSave_last(),
1806 working_directory } ;
1809 Process p = Runtime.getRuntime().exec( command ) ;
1812 if( p.exitValue() == 0 )
1814 System.out.println( "Archive successfully retrieved." ) ;
1815 isRestartedSave = true ;
1818 System.err.println( "Archive not retrieved!" ) ;
1819 System.err.println( "Error: " ) ;
1821 printProcessError( p ) ;
1823 } catch( IOException e ) {
1824 System.err.println( "Error during archive retrieve command: " ) ;
1825 e.printStackTrace() ;
1826 } catch( InterruptedException e ) {
1827 e.printStackTrace() ;
1837 System.err.println( "Unable to retrieve a save archive from any neighbor!" ) ;
1847 public String getIPVM() throws RemoteException
1849 if( machine != null )
1851 return machine.getIp() ;
1859 public void setIPVM( String _ipVM ) throws RemoteException
1861 if( _ipVM != null && ! _ipVM.isEmpty() )
1863 System.out.println( "The VM IP is now: " + _ipVM ) ;
1864 machine.setIp( _ipVM ) ;
1869 public String getWorkingDirectory()
1871 return working_directory ;
1875 public Integer deployVM( String _name, String _archive, String _directory )
1877 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 )
1879 File file = new File( working_directory + "/" + _archive ) ;
1880 if( ! file.exists() )
1882 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1885 } else if( file.isDirectory() ) {
1886 System.err.println( _archive + " is a directory!" ) ;
1893 machine.setName( _name ) ;
1894 machine.setInitial_archive_name( _archive ) ;
1895 machine.setDirectory( _directory ) ;
1897 if( machine.deployInitialVM() == 1 )
1899 System.err.println( "Unable to deploy the initial VM archive!" ) ;
1910 public void responseSave( boolean _b )
1912 synchronized( saveRequest )
1914 saveRequest.setStatus( _b ) ;
1916 saveRequest.notifyAll() ;
1917 } catch( Exception e ) {
1918 e.printStackTrace() ;
1924 private class SaveRequest
1933 protected boolean getStatus() { return status ; }
1935 protected void setStatus( boolean _b ) { status = _b ; }
1938 private class SaveProcess
1947 protected boolean getStatus() { return status ; }
1949 protected void setStatus( boolean _b ) { status = _b ; }
1955 /** La programmation est un art, respectons ceux qui la pratiquent !! **/