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!" ) ;
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" ) ;
509 LocalHost.Instance().getServerStub().changeStatus(
510 LocalHost.Instance().getIP(), "undefined" ) ;
511 } catch( RemoteException e ) {
512 System.err.println( "Unable to inform the server of the VM status!" ) ;
513 e.printStackTrace() ;
516 String[] command = new String[]{VmRunCommand, "-T", "player", "stop",
517 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
521 Process p = Runtime.getRuntime().exec( command ) ;
524 if( p.exitValue() == 0 )
526 System.out.println( "Virtual machine successfully stopped." ) ;
527 machine.setStatus( "stopped" ) ;
530 System.err.println( "Virtual machine not stopped!" ) ;
531 ret = printProcessError( p ) ;
539 if( retry >= maxRetryVM )
541 System.err.println( "Unable to stop VM!" ) ;
545 System.out.println( "Retrying (" + retry + ") ... " ) ;
546 Thread.sleep( timeRetryVM ) ;
551 } catch( IOException e ) {
552 System.err.println( "Error during execution of stop command: " ) ;
553 e.printStackTrace() ;
556 } catch( InterruptedException e ) {
557 e.printStackTrace() ;
562 machine.setStatus( "stopped" ) ;
564 LocalHost.Instance().getServerStub().changeStatus(
565 LocalHost.Instance().getIP(), "stopped" ) ;
566 } catch( RemoteException e1 ) {
567 System.err.println( "Unable to inform the server of the VM stopped status!" ) ;
568 e1.printStackTrace() ;
572 if( machine.checkVmx() == 0 )
575 emergencyStop = false ;
586 public int suspendVM( int _mode )
588 if( machine != null && machine.getStatus().equalsIgnoreCase( "suspended" ) )
593 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "suspended" ) )
597 } catch( InterruptedException e2 ) {
598 System.err.println( "Problem with semaphore acquiring!" ) ;
599 e2.printStackTrace() ;
602 System.out.print( "Suspending VM ... " ) ;
607 machine.setStatus( "undefined" ) ;
611 LocalHost.Instance().getServerStub().changeStatus(
612 LocalHost.Instance().getIP(), "undefined" ) ;
613 } catch( RemoteException e ) {
614 System.err.println( "Unable to inform the server of the VM status!" ) ;
615 e.printStackTrace() ;
619 String[] command = new String[]{VmRunCommand, "-T", "player", "suspend",
620 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
624 Process p = Runtime.getRuntime().exec( command ) ;
626 if( p.exitValue() == 0 )
628 System.out.println( "Virtual machine successfully suspended." ) ;
629 machine.setStatus( "suspended" ) ;
632 System.err.println( "Virtual machine not suspended!" ) ;
633 ret = printProcessError( p ) ;
642 if( retry >= maxRetryVM )
644 System.err.println( "Unable to suspend VM!" ) ;
650 System.out.println( "Retrying (" + retry + ") ... " ) ;
651 Thread.sleep( timeRetryVM ) ;
656 } catch( IOException e ) {
657 System.err.println( "Error during execution of suspend command: " ) ;
658 e.printStackTrace() ;
659 } catch( InterruptedException e ) {
660 e.printStackTrace() ;
663 machine.setStatus( "suspended" ) ;
667 LocalHost.Instance().getServerStub().changeStatus(
668 LocalHost.Instance().getIP(), "suspended" ) ;
669 } catch( RemoteException e ) {
670 System.err.println( "Unable to inform the server of the VM suspended status!" ) ;
671 e.printStackTrace() ;
686 public int restartVM()
688 if( machine != null )
690 System.out.print( "Restarting VM ... " ) ;
696 LocalHost.Instance().getServerStub().changeStatus(
697 LocalHost.Instance().getIP(), "undefined" ) ;
698 } catch( RemoteException e ) {
699 System.err.println( "Unable to inform the server of the VM status!" ) ;
700 e.printStackTrace() ;
703 String[] command = new String[]{VmRunCommand, "-T", "player", "reset",
704 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
708 Process p = Runtime.getRuntime().exec( command ) ;
711 if( p.exitValue() == 0 )
713 System.out.println( "Virtual machine successfully restarted." ) ;
715 if( sendSaveOkVM() == 1 ) { return 1 ; }
721 System.err.println( "Virtual machine not restarted!" ) ;
722 ret = printProcessError( p ) ;
729 if( retry >= maxRetryVM )
731 System.err.println( "Unable to start VM!" ) ;
734 System.out.println( "Retrying (" + retry + ") ... " ) ;
735 Thread.sleep( timeRetryVM ) ;
740 } catch( IOException e ) {
741 System.err.println( "Error during execution of restart command: " ) ;
742 e.printStackTrace() ;
743 } catch( InterruptedException e ) {
744 e.printStackTrace() ;
752 public int restartVMAfterCrash()
754 System.out.println( "Restarting VM after a crash ..." ) ;
757 LocalHost.Instance().getServerStub().changeStatus(
758 LocalHost.Instance().getIP(), "undefined" ) ;
759 } catch( RemoteException e ) {
760 System.err.println( "Unable to inform the server of the VM status!" ) ;
761 e.printStackTrace() ;
766 if( machine.deployLastSave() == 0 )
768 if( isRestartedSave )
770 // Using the specific vmx file
771 machine.setDeployFault( true ) ;
773 // Writing the restarted save mark
775 FileWriter fw = new FileWriter( new File( working_directory + "/" + machine.getDirectory() + "/fault.hpcvm" ) ) ;
776 fw.write( "fault!" ) ;
780 } catch( IOException e1 ) {
781 e1.printStackTrace() ;
782 System.err.println( "Unable to mark the fault!" ) ;
785 if( machine.checkVmx() == 1 )
791 /** Retrieving VM assigned IP **/
795 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
796 LocalHost.Instance().getIP() ) ;
797 } catch (RemoteException e) {
798 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
799 e.printStackTrace() ;
803 machine.setIp( vmIP ) ;
805 isRestartedSave = false ;
808 if( startVM( 0 ) == 0 )
810 if( sendSaveOkVM() == 0 )
813 LocalHost.Instance().getServerStub().restartOk( LocalHost.Instance().getIP() ) ;
814 } catch( RemoteException e ) {
815 System.err.println( "Unable to inform server about my successful restart!" ) ;
816 e.printStackTrace() ;
831 private int sendSaveOkVM()
836 /** Informing the program that it's ok **/
837 System.out.print( "Sending OK signal to the program ... " ) ;
839 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
840 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
841 working_directory + "/" + machine.getDirectory()
842 + "/" + machine.getVmx_name() + " " + ushell +
843 " \"echo ok > /tmp/vm_save_ok\"" ;// + " -noWait " ;
846 FileWriter fw = new FileWriter( new File( working_directory + "/saveOk.sh" ) ) ;
850 } catch( IOException e1 ) {
851 e1.printStackTrace() ;
854 String[] command = new String[]{ ushell, working_directory + "/saveOk.sh"} ;
858 Process p = Runtime.getRuntime().exec( command ) ;
861 if( p.exitValue() == 0 )
863 System.out.println( "Signal successfully sent." ) ;
867 System.err.println( "Signal not sent!" ) ;
868 ret = printProcessError( p ) ;
875 if( retry >= maxRetryVM )
877 System.err.println( "Unable to send ok signal to VM!" ) ;
880 System.out.println( "Retrying (" + retry + ") ... " ) ;
881 Thread.sleep( timeRetryVM ) ;
885 } catch( IOException e ) {
886 System.err.println( "Error during ok save signal send command: " ) ;
887 e.printStackTrace() ;
889 } catch( InterruptedException e ) {
890 e.printStackTrace() ;
901 synchronized( saveProcess ){
902 while( saveProcess.getStatus() )
906 } catch( InterruptedException e ) {
907 e.printStackTrace() ;
911 System.out.println( "Saving VM ..." ) ;
912 saveProcess.setStatus( true ) ;
914 long deb = System.currentTimeMillis() ;
916 machine.setStatus( "saving" ) ;
918 LocalHost.Instance().getServerStub().changeStatus(
919 LocalHost.Instance().getIP(), "saving" ) ;
920 } catch( RemoteException e ) {
921 System.err.println( "Unable to inform the server of the VM status!" ) ;
922 e.printStackTrace() ;
926 String saveName = "" ;
927 boolean error = false ;
930 if( suspendVM( 1 ) == 1 ) { return 1 ; }
935 String arch1 = "", arch2 = "" ;
936 File file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar" ) ;
939 arch1 = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar" ;
944 file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ) ;
947 arch2 = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
952 if( arch1.length() > 0 || arch2.length() > 0 )
954 System.out.println( "Deletion of last nok archive ... " ) ;
956 command = new String[]{ "/bin/rm", "-rf",
960 procSave = Runtime.getRuntime().exec( command ) ;
963 if( procSave.exitValue() == 0 )
965 System.out.println( "Last nok archive successfully deleted." ) ;
967 System.err.println( "Last nok archive not deleted!" ) ;
968 printProcessError( procSave ) ;
972 } catch( IOException e ) {
973 System.err.println( "Error during nok archive deletion command: " ) ;
975 e.printStackTrace() ;
976 } catch( InterruptedException e ) {
977 e.printStackTrace() ;
986 System.out.print( "Creation of the archive ... " ) ;
988 /** Archive creation **/
989 command = new String[]{ "/bin/tar", "-cf",
990 machine.getName() + "_new_" + machine.getComputationId() + ".tar",
991 machine.getDirectory(), "-C", working_directory } ;
999 procSave = Runtime.getRuntime().exec( command ) ;
1000 procSave.waitFor() ;
1002 if( procSave.exitValue() == 0 )
1004 System.out.println( "Archive successfully created." ) ;
1006 lastSaveOk = false ;
1009 System.err.println( "Archive not created!" ) ;
1010 printProcessError( procSave ) ;
1014 } catch( IOException e ) {
1015 System.err.println( "Error during archive creation command: " ) ;
1017 e.printStackTrace() ;
1018 } catch( InterruptedException e ) {
1019 e.printStackTrace() ;
1023 /** Compression of the archive **/
1026 System.out.print( "Compression of the archive ... " ) ;
1027 command = new String[]{ "/bin/gzip",
1028 working_directory + "/" + machine.getName()
1029 + "_new_" + machine.getComputationId() + ".tar" } ;
1037 procSave = Runtime.getRuntime().exec( command ) ;
1038 procSave.waitFor() ;
1040 if( procSave.exitValue() == 0 )
1042 System.out.println( "Archive successfully compressed." ) ;
1044 System.err.println( "Archive not compressed!" ) ;
1045 printProcessError( procSave ) ;
1049 } catch( IOException e ) {
1050 System.err.println( "Error during archive compression command: " ) ;
1051 e.printStackTrace() ;
1053 } catch( InterruptedException e ) {
1054 e.printStackTrace() ;
1059 long fin = System.currentTimeMillis() ;
1061 System.out.println( "Time to create the save: " + (fin-deb)/1000 + " seconds." ) ;
1064 /** Restarting VM **/
1065 if( startVM( 0 ) == 1 ) { return 1 ; }
1068 /** Sending ok save signal **/
1069 if( sendSaveOkVM() == 1 ) { return 1 ; }
1071 saveName = machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
1073 /** Sending save to neighbor **/
1081 ArrayList<SaveNeighbor> sn = machine.getSaveNeighbors() ;
1083 for( int i = 0 ; i < sn.size() ; i++ )
1085 String name = sn.get( i ).getName() ;
1086 String wd = sn.get( i ).getWorkingDirectory() ;
1087 String snIP = sn.get( i ).getIPHost() ;
1089 System.out.print( "Sending save to " + name + " ... " ) ;
1091 command = new String[]{ "/usr/bin/scp", working_directory + "/" + saveName,
1095 procSave = Runtime.getRuntime().exec( command ) ;
1096 procSave.waitFor() ;
1098 if( procSave.exitValue() == 0 )
1100 System.out.println( "Archive successfully sent." ) ;
1102 System.err.println( "Archive not sent!" ) ;
1103 printProcessError( procSave ) ;
1107 } catch( IOException e ) {
1108 System.err.println( "Error during archive send command: " ) ;
1109 e.printStackTrace() ;
1111 } catch( InterruptedException e ) {
1112 e.printStackTrace() ;
1119 /** Informing the server the save is done **/
1123 LocalHost.Instance().getServerStub().saveOk( LocalHost.Instance().getIP(), saveName ) ;
1124 } catch( RemoteException e ) {
1125 System.err.println( "Problem while informing the server about the save state!" ) ;
1126 e.printStackTrace() ;
1129 synchronized( saveProcess ) {
1130 saveProcess.setStatus( false ) ;
1132 saveProcess.notifyAll() ;
1133 } catch( Exception e ) {}}
1138 synchronized( saveProcess ) {
1139 saveProcess.setStatus( false ) ;
1141 saveProcess.notifyAll() ;
1142 } catch( Exception e ) {}}
1149 public int reloadConfig()
1151 System.out.println( "Reloading configuration ... " ) ;
1157 public void init( String _server_ip, int _server_port, int _client_port, int _dialog_port )
1159 System.out.println( "Initialisation Client ... " ) ;
1160 System.out.println( "IP " + LocalHost.Instance().getIP() ) ;
1162 server_ip = _server_ip ;
1163 server_port = _server_port ;
1164 client_port = _client_port ;
1165 dialog_port = _server_port + 1 ; // _dialog_port ;
1168 saveProcess = new SaveProcess() ;
1169 saveRequest = new SaveRequest() ;
1171 machine = new VirtualMachine() ;
1173 VmRunCommand = "/usr/bin/vmrun" ;
1174 // VmRunCommandArg = "-T player" ;
1176 // vm_user = "mpi" ;
1177 // vm_user_passwd = "mpi" ;
1178 ushell = "/bin/bash" ;
1179 working_directory = "/localhome/vmware" ;
1181 wait_start = 15000 ;
1182 max_start_try = 10 ;
1184 sema = new Semaphore( 1 ) ;
1185 emergencyStop = false ;
1188 timeRetryVM = 10000 ;
1190 // save_interleave = 30 * 60 * 1000 ;
1191 // date_last_save = 0 ;
1193 isRestartedSave = false ;
1196 /** Connection to server **/
1198 serverStub = (ServicesServer) Naming.lookup( "rmi://"
1199 + server_ip + ":" + server_port + "/Server" ) ;
1200 } catch (MalformedURLException e) {
1201 e.printStackTrace();
1202 } catch (RemoteException e) {
1203 e.printStackTrace();
1204 } catch (NotBoundException e) {
1205 e.printStackTrace();
1208 if( serverStub == null )
1210 System.err.println( "Unable to connect to server!!" ) ;
1211 System.err.println( "Server IP: " + server_ip + " -- server port: " + server_port ) ;
1216 System.out.println( "Connected to server " + server_ip + " on port " + server_port + "." ) ;
1218 LocalHost.Instance().setServerStub( serverStub ) ;
1221 /** Creating the local server **/
1224 /** Starting all threads **/
1228 private void exportObject()
1230 Registry reg = null ;
1236 reg = LocateRegistry.getRegistry( client_port ) ;
1238 String tab[] = reg.list() ;
1240 System.out.println( "There is an existing RMI Registry on port " +
1241 client_port + " with " + tab.length + " entries!" ) ;
1242 for( int i = 0 ; i < tab.length ; i++ )
1245 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
1247 System.out.println( "Register successfuly deleted!" ) ;
1249 System.err.println( "Register undeleted !!!" ) ;
1251 } catch( Exception e ) {
1252 e.printStackTrace() ;
1256 } catch( RemoteException e ) {
1260 if ( System.getSecurityManager() == null )
1262 System.setSecurityManager( new SecurityManager() ) ;
1265 LocateRegistry.createRegistry( client_port ) ;
1266 LocateRegistry.getRegistry( client_port ).rebind( "Client", this ) ;
1267 myStub = (ServicesClient) Naming.lookup( "rmi://"
1268 + LocalHost.Instance().getIP() + ":" + client_port
1270 } catch( Exception e ) {
1271 System.err.println( "Error in Client.exportObject() when creating local services:" + e ) ;
1272 System.err.println( "Exit from Client.exportObject" ) ;
1276 LocalHost.Instance().setStub( myStub ) ;
1280 private boolean printProcessError( Process _p )
1282 boolean ret = false ;
1286 System.err.println( "Error: " + _p.exitValue() ) ;
1287 BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
1290 while( (line = br.readLine()) != null )
1292 System.err.println( line ) ;
1293 if( line.contains( "egmentation" ) )
1298 } catch( IOException e ) {
1299 e.printStackTrace() ;
1310 /** Registering on server **/
1313 ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1314 } catch (RemoteException e1) {
1315 e1.printStackTrace();
1321 case 0: System.out.println( "Successfully registered on server." ) ; break ;
1322 case 1: System.err.println( "Problem on server while registreting!" ) ; return 1 ;
1323 case 2: System.out.println( "Already registered on server!" ) ; break ;
1326 /** Retrieving VM assigned IP **/
1327 String vmIP = null ;
1330 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
1331 LocalHost.Instance().getIP() ) ;
1332 } catch (RemoteException e) {
1333 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
1334 e.printStackTrace() ;
1338 machine.setIp( vmIP ) ;
1340 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1343 /** Starting alive ping to server **/
1344 pingServer = new PingServer() ;
1345 pingServer.start() ;
1347 /** Starting socket server for VM dialog **/
1348 dialogVmServer = new DialogVMServer() ;
1349 dialogVmServer.start() ;
1355 private class PingServer extends Thread
1357 private boolean run ;
1364 protected void stopPing() { run = false ; }
1372 LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1373 } catch( RemoteException e1 ) {
1374 System.err.println( "Unable to ping the server!" ) ;
1375 e1.printStackTrace() ;
1379 Thread.sleep( 2000 ) ;
1380 } catch( InterruptedException e ) {
1381 e.printStackTrace() ;
1388 private class DialogVMServer extends Thread
1390 private boolean run ;
1391 private Socket socket ;
1392 private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1399 protected void stopDialogVMServer()
1403 if( serverSocket != null )
1406 serverSocket.close() ;
1408 for( int i = 0 ; i < dialogs.size() ; i++ )
1410 dialogs.get( i ).stopDialogVM() ;
1413 } catch( IOException e ) {
1414 e.printStackTrace() ;
1424 serverSocket = new ServerSocket( 0 ) ;
1425 dialog_port = serverSocket.getLocalPort() ;
1427 System.out.println( "SocketServer listening on port " + dialog_port ) ;
1428 } catch( IOException e ) {
1429 System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
1430 e.printStackTrace() ;
1438 socket = serverSocket.accept() ;
1440 dialogs.add( new DialogVM( socket ) ) ;
1441 dialogs.get( dialogs.size() - 1 ).start() ;
1442 } catch( IOException e ) {
1443 System.err.println( "Problem with the accept function!" ) ;
1444 e.printStackTrace() ;
1451 private class DialogVM extends Thread
1453 private boolean run ;
1454 private Socket socket ;
1455 private BufferedReader reader ;
1456 private String line ;
1458 DialogVM( Socket _socket ) { run = true ; socket = _socket ; }
1460 protected void stopDialogVM()
1465 reader.close() ; reader = null ;
1466 socket.close() ; socket = null ;
1467 } catch( IOException e ) {
1468 e.printStackTrace() ;
1476 reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
1477 } catch( IOException e ) {
1478 System.err.println( "Unable to open a dialog socket with the VM!" ) ;
1479 e.printStackTrace();
1488 if( reader != null )
1490 line = reader.readLine() ;
1493 /** VM is starting -- retrieving informations **/
1494 if( run && line != null && line.equalsIgnoreCase( "infos" ) )
1496 /* Receiving name */
1497 machine.setName( reader.readLine() ) ;
1500 String ip = reader.readLine() ;
1501 if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1503 System.err.println( "VM IP not well configured!!" ) ;
1507 reader.close() ; reader = null ;
1508 socket.close() ; socket = null ;
1513 /** It's time to do a save **/
1514 if( run && line != null && line.equalsIgnoreCase( "save" ) )
1517 machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1518 } catch( Exception e ) {
1519 System.err.println( "Problem while reading the computation id!" ) ;
1520 e.printStackTrace() ;
1523 synchronized( saveRequest )
1526 LocalHost.Instance().getServerStub().requestSave( LocalHost.Instance().getIP() ) ;
1527 } catch( RemoteException e ) {
1528 System.err.println( "Unable to request save to server!" ) ;
1529 e.printStackTrace() ;
1533 saveRequest.wait() ;
1534 } catch( InterruptedException e ) {
1535 e.printStackTrace();
1539 if( saveRequest.getStatus() )
1542 reader.close() ; reader = null ;
1543 socket.close() ; socket = null ;
1547 saveRequest.setStatus( false ) ;
1549 /* Requesting the VM save */
1557 /** Computation is done, we can shutdown the VM **/
1558 if( run && line != null && line.equalsIgnoreCase( "quit" ) )
1561 Thread.sleep( 5000 ) ;
1562 } catch( InterruptedException e ) {
1563 e.printStackTrace() ;
1567 reader.close() ; reader = null ;
1568 socket.close() ; socket = null ;
1575 } catch( IOException e ) {
1576 e.printStackTrace() ;
1587 pingServer.stopPing() ;
1589 dialogVmServer.stopDialogVMServer() ;
1591 // unexportObject ??
1597 public String getIPHost()
1599 return LocalHost.Instance().getIP() ;
1603 public String getName()
1605 return LocalHost.Instance().getName() ;
1610 public void saveOk()
1612 String save_name = machine.getName() + "_last_" + machine.getComputationId() +
1615 String save_new = machine.getName() + "_new_"
1616 + machine.getComputationId() + ".tar.gz" ;
1618 String[] command = new String[]{ "/bin/mv",
1619 working_directory + "/" + save_new,
1620 working_directory + "/" + save_name } ;
1623 Process p = Runtime.getRuntime().exec( command ) ;
1626 if( p.exitValue() == 0 )
1628 machine.setSave_last( save_name ) ;
1629 System.out.println( "Last save OK" ) ;
1631 System.err.println( "Last save NOK!" ) ;
1632 System.err.println( "Error: " ) ;
1633 printProcessError( p ) ;
1635 } catch( IOException e ) {
1636 System.err.println( "Error during last archive move:" ) ;
1637 e.printStackTrace() ;
1638 } catch( InterruptedException e ) {
1639 e.printStackTrace() ;
1642 // Changing on save neighbors
1643 for( int i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1646 machine.getSaveNeighbors().get( i ).getStub().changeSaveName( save_new, save_name, machine.getComputationId() ) ;
1647 } catch( RemoteException e ) {
1648 System.err.println( "Unable to change save name on " + machine.getSaveNeighbors().get( i ).getName() + "!" ) ;
1649 e.printStackTrace() ;
1653 // Informing the server
1656 ret = LocalHost.Instance().getServerStub().changeSaveName( LocalHost.Instance().getIP(), save_name ) ;
1657 } catch( RemoteException e ) {
1658 System.err.println( "Unable to inform the server about the new save name!" ) ;
1659 e.printStackTrace() ;
1664 System.out.println( "Successfully informing the server about the new save name." ) ;
1666 System.err.println( "Problem on the server while informing it about the new save name!" ) ;
1674 public void changeSaveName( String _n1, String _n2, int _id )
1676 if( _n1 != null && _n1.length() > 0 )
1678 System.out.println( "Changing save name for processus " + _id + " ... " ) ;
1680 String[] command = new String[]{ "/bin/mv",
1681 working_directory + "/" + _n1,
1682 working_directory + "/" + _n2 } ;
1685 Process p = Runtime.getRuntime().exec( command ) ;
1688 if( p.exitValue() == 0 )
1690 System.out.println( "Change save name OK" ) ;
1692 System.err.println( "Change save name NOK!" ) ;
1693 System.err.println( "Error: " ) ;
1694 printProcessError( p ) ;
1696 } catch( IOException e ) {
1697 System.err.println( "Error during save renaming:" ) ;
1698 e.printStackTrace() ;
1699 } catch( InterruptedException e ) {
1700 e.printStackTrace() ;
1707 public void setSaveNeighbor( SaveNeighbor _sn )
1711 ArrayList<SaveNeighbor> as = new ArrayList<SaveNeighbor>() ;
1714 System.out.println( "Save neighbor: " + _sn.getName() ) ;
1716 machine.setSaveNeighbors( as ) ;
1722 public void setSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1724 if( _sn != null && _sn.size() > 0 )
1726 System.out.print( "Save neighbors: " ) ;
1727 for( int i = 0 ; i < _sn.size() ; i++ )
1729 System.out.print( _sn.get( i ).getName() ) ;
1731 if( i != _sn.size() - 1 )
1733 System.out.print( ", " ) ;
1735 System.out.println( "." ) ;
1739 machine.setSaveNeighbors( _sn ) ;
1745 public void addSaveNeighbor( SaveNeighbor _sn )
1749 System.out.println( "Adding save neighbor: " + _sn.getName() ) ;
1751 machine.getSaveNeighbors().add( _sn ) ;
1757 public void addSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1759 if( _sn != null && _sn.size() > 0 )
1761 System.out.print( "Adding save neighbors: " ) ;
1762 for( int i = 0 ; i < _sn.size() ; i++ )
1764 System.out.print( _sn.get( i ).getName() ) ;
1766 if( i != _sn.size() - 1 )
1768 System.out.print( ", " ) ;
1770 System.out.println( "." ) ;
1773 machine.getSaveNeighbors().add( _sn.get( i ) ) ;
1780 public void replaceSaveNeighbor( SaveNeighbor _old, SaveNeighbor _new )
1782 System.out.print( "Replacing a save neihgbor ... " ) ;
1783 if( _old != null && _new != null )
1786 boolean change = false ;
1788 for( i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1790 if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old.getIPHost() ) )
1792 machine.getSaveNeighbors().set( i, _new ) ;
1793 System.out.println( "Save neighbor successfully changed." ) ;
1801 System.out.println( "I am not concerned by the modification." ) ;
1808 public int retrieveSave( String _saveName )
1810 if( _saveName != null )
1812 if( ! _saveName.equalsIgnoreCase( "none" ) )
1814 machine.setSave_last( _saveName ) ;
1816 System.err.println( "I have no save to retrieve!!" ) ;
1820 // TODO NEIGHBORS !!!!
1821 boolean ok = false ;
1824 while( ! ok && i < machine.getSaveNeighbors().size() )
1826 System.out.print( "Retrieving a save on " + machine.getSaveNeighbors().get( 0 ).getName() + " ... " ) ;
1828 String command[] = {} ;
1830 command = new String[]{ "/usr/bin/scp",
1831 machine.getSaveNeighbors().get( i ).getIPHost() + ":" +
1832 machine.getSaveNeighbors().get( i ).getWorkingDirectory() + "/" +
1833 machine.getSave_last(),
1834 working_directory } ;
1837 Process p = Runtime.getRuntime().exec( command ) ;
1840 if( p.exitValue() == 0 )
1842 System.out.println( "Archive successfully retrieved." ) ;
1843 isRestartedSave = true ;
1846 System.err.println( "Archive not retrieved!" ) ;
1847 System.err.println( "Error: " ) ;
1849 printProcessError( p ) ;
1851 } catch( IOException e ) {
1852 System.err.println( "Error during archive retrieve command: " ) ;
1853 e.printStackTrace() ;
1854 } catch( InterruptedException e ) {
1855 e.printStackTrace() ;
1865 System.err.println( "Unable to retrieve a save archive from any neighbor!" ) ;
1875 public String getIPVM() throws RemoteException
1877 if( machine != null )
1879 return machine.getIp() ;
1887 public void setIPVM( String _ipVM ) throws RemoteException
1889 if( _ipVM != null && ! _ipVM.isEmpty() )
1891 System.out.println( "The VM IP is now: " + _ipVM ) ;
1892 machine.setIp( _ipVM ) ;
1897 public String getWorkingDirectory()
1899 return working_directory ;
1903 public Integer deployVM( String _name, String _archive, String _directory )
1905 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 )
1907 File file = new File( working_directory + "/" + _archive ) ;
1908 if( ! file.exists() )
1910 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1913 } else if( file.isDirectory() ) {
1914 System.err.println( _archive + " is a directory!" ) ;
1921 machine.setName( _name ) ;
1922 machine.setInitial_archive_name( _archive ) ;
1923 machine.setDirectory( _directory ) ;
1925 if( machine.deployInitialVM() == 1 )
1927 System.err.println( "Unable to deploy the initial VM archive!" ) ;
1938 public void responseSave( boolean _b )
1940 synchronized( saveRequest )
1942 saveRequest.setStatus( _b ) ;
1944 saveRequest.notifyAll() ;
1945 } catch( Exception e ) {
1946 e.printStackTrace() ;
1952 private class SaveRequest
1961 protected boolean getStatus() { return status ; }
1963 protected void setStatus( boolean _b ) { status = _b ; }
1966 private class SaveProcess
1975 protected boolean getStatus() { return status ; }
1977 protected void setStatus( boolean _b ) { status = _b ; }
1983 /** La programmation est un art, respectons ceux qui la pratiquent !! **/