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 ; }
936 File file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ) ;
939 arch = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
944 if( arch.length() > 0 )
946 System.out.println( "Deletion of last nok archive ... " ) ;
948 command = new String[]{ "/bin/rm", "-rf", arch } ;
951 procSave = Runtime.getRuntime().exec( command ) ;
954 if( procSave.exitValue() == 0 )
956 System.out.println( "Last nok archive successfully deleted." ) ;
958 System.err.println( "Last nok archive not deleted!" ) ;
959 printProcessError( procSave ) ;
963 } catch( IOException e ) {
964 System.err.println( "Error during nok archive deletion command: " ) ;
966 e.printStackTrace() ;
967 } catch( InterruptedException e ) {
968 e.printStackTrace() ;
977 System.out.print( "Creation of the archive ... " ) ;
979 /** Archive creation **/
980 command = new String[]{ "/bin/tar", "-cz", "-C", working_directory,
981 "-f", working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz",
982 machine.getDirectory() } ;
990 procSave = Runtime.getRuntime().exec( command ) ;
993 if( procSave.exitValue() == 0 )
995 System.out.println( "Archive successfully created." ) ;
1000 System.err.println( "Archive not created!" ) ;
1001 printProcessError( procSave ) ;
1005 } catch( IOException e ) {
1006 System.err.println( "Error during archive creation command: " ) ;
1008 e.printStackTrace() ;
1009 } catch( InterruptedException e ) {
1010 e.printStackTrace() ;
1014 long fin = System.currentTimeMillis() ;
1016 System.out.println( "Time to create the save: " + (fin-deb)/1000 + " seconds." ) ;
1019 /** Restarting VM **/
1020 if( startVM( 0 ) == 1 ) { return 1 ; }
1023 /** Sending ok save signal **/
1024 if( sendSaveOkVM() == 1 ) { return 1 ; }
1026 saveName = machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
1028 /** Sending save to neighbor **/
1036 ArrayList<SaveNeighbor> sn = machine.getSaveNeighbors() ;
1038 for( int i = 0 ; i < sn.size() ; i++ )
1040 String name = sn.get( i ).getName() ;
1041 String wd = sn.get( i ).getWorkingDirectory() ;
1042 String snIP = sn.get( i ).getIPHost() ;
1044 System.out.print( "Sending save to " + name + " ... " ) ;
1046 command = new String[]{ "/usr/bin/scp", working_directory + "/" + saveName,
1050 procSave = Runtime.getRuntime().exec( command ) ;
1051 procSave.waitFor() ;
1053 if( procSave.exitValue() == 0 )
1055 System.out.println( "Archive successfully sent." ) ;
1057 System.err.println( "Archive not sent!" ) ;
1058 printProcessError( procSave ) ;
1062 } catch( IOException e ) {
1063 System.err.println( "Error during archive send command: " ) ;
1064 e.printStackTrace() ;
1066 } catch( InterruptedException e ) {
1067 e.printStackTrace() ;
1074 /** Informing the server the save is done **/
1078 LocalHost.Instance().getServerStub().saveOk( LocalHost.Instance().getIP(), saveName ) ;
1079 } catch( RemoteException e ) {
1080 System.err.println( "Problem while informing the server about the save state!" ) ;
1081 e.printStackTrace() ;
1084 synchronized( saveProcess ) {
1085 saveProcess.setStatus( false ) ;
1087 saveProcess.notifyAll() ;
1088 } catch( Exception e ) {}}
1093 synchronized( saveProcess ) {
1094 saveProcess.setStatus( false ) ;
1096 saveProcess.notifyAll() ;
1097 } catch( Exception e ) {}}
1104 public int reloadConfig()
1106 System.out.println( "Reloading configuration ... " ) ;
1112 public void init( String _server_ip, int _server_port, int _client_port, int _dialog_port )
1114 System.out.println( "Initialisation Client ... " ) ;
1115 System.out.println( "IP " + LocalHost.Instance().getIP() ) ;
1117 server_ip = _server_ip ;
1118 server_port = _server_port ;
1119 client_port = _client_port ;
1120 dialog_port = _server_port + 1 ; // _dialog_port ;
1123 saveProcess = new SaveProcess() ;
1124 saveRequest = new SaveRequest() ;
1126 machine = new VirtualMachine() ;
1128 VmRunCommand = "/usr/bin/vmrun" ;
1129 // VmRunCommandArg = "-T player" ;
1131 // vm_user = "mpi" ;
1132 // vm_user_passwd = "mpi" ;
1133 ushell = "/bin/bash" ;
1134 working_directory = "/localhome/vmware" ;
1136 wait_start = 15000 ;
1137 max_start_try = 10 ;
1139 sema = new Semaphore( 1 ) ;
1140 emergencyStop = false ;
1143 timeRetryVM = 10000 ;
1145 // save_interleave = 30 * 60 * 1000 ;
1146 // date_last_save = 0 ;
1148 isRestartedSave = false ;
1149 lastSaveOk = false ;
1151 /** Connection to server **/
1153 serverStub = (ServicesServer) Naming.lookup( "rmi://"
1154 + server_ip + ":" + server_port + "/Server" ) ;
1155 } catch (MalformedURLException e) {
1156 e.printStackTrace();
1157 } catch (RemoteException e) {
1158 e.printStackTrace();
1159 } catch (NotBoundException e) {
1160 e.printStackTrace();
1163 if( serverStub == null )
1165 System.err.println( "Unable to connect to server!!" ) ;
1166 System.err.println( "Server IP: " + server_ip + " -- server port: " + server_port ) ;
1171 System.out.println( "Connected to server " + server_ip + " on port " + server_port + "." ) ;
1173 LocalHost.Instance().setServerStub( serverStub ) ;
1176 /** Creating the local server **/
1179 /** Starting all threads **/
1183 private void exportObject()
1185 Registry reg = null ;
1191 reg = LocateRegistry.getRegistry( client_port ) ;
1193 String tab[] = reg.list() ;
1195 System.out.println( "There is an existing RMI Registry on port " +
1196 client_port + " with " + tab.length + " entries!" ) ;
1197 for( int i = 0 ; i < tab.length ; i++ )
1200 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
1202 System.out.println( "Register successfuly deleted!" ) ;
1204 System.err.println( "Register undeleted !!!" ) ;
1206 } catch( Exception e ) {
1207 e.printStackTrace() ;
1211 } catch( RemoteException e ) {
1215 if ( System.getSecurityManager() == null )
1217 System.setSecurityManager( new SecurityManager() ) ;
1220 LocateRegistry.createRegistry( client_port ) ;
1221 LocateRegistry.getRegistry( client_port ).rebind( "Client", this ) ;
1222 myStub = (ServicesClient) Naming.lookup( "rmi://"
1223 + LocalHost.Instance().getIP() + ":" + client_port
1225 } catch( Exception e ) {
1226 System.err.println( "Error in Client.exportObject() when creating local services:" + e ) ;
1227 System.err.println( "Exit from Client.exportObject" ) ;
1231 LocalHost.Instance().setStub( myStub ) ;
1235 private boolean printProcessError( Process _p )
1237 boolean ret = false ;
1241 System.err.println( "Error: " + _p.exitValue() ) ;
1242 BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
1245 while( (line = br.readLine()) != null )
1247 System.err.println( line ) ;
1248 if( line.contains( "egmentation" ) || _p.exitValue() == 139 )
1253 } catch( IOException e ) {
1254 e.printStackTrace() ;
1272 /** Registering on server **/
1275 ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1276 } catch (RemoteException e1) {
1277 e1.printStackTrace();
1283 case 0: System.out.println( "Successfully registered on server." ) ; break ;
1284 case 1: System.err.println( "Problem on server while registreting!" ) ; return 1 ;
1285 case 2: System.out.println( "Already registered on server!" ) ; break ;
1288 /** Retrieving VM assigned IP **/
1289 String vmIP = null ;
1292 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
1293 LocalHost.Instance().getIP() ) ;
1294 } catch (RemoteException e) {
1295 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
1296 e.printStackTrace() ;
1300 machine.setIp( vmIP ) ;
1302 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1305 /** Starting alive ping to server **/
1306 pingServer = new PingServer() ;
1307 pingServer.start() ;
1309 /** Starting socket server for VM dialog **/
1310 dialogVmServer = new DialogVMServer() ;
1311 dialogVmServer.start() ;
1317 private class PingServer extends Thread
1319 protected boolean go ;
1326 protected void stopPing() { go = false ; }
1334 LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1335 } catch( RemoteException e1 ) {
1336 System.err.println( "Unable to ping the server!" ) ;
1337 e1.printStackTrace() ;
1343 } catch( InterruptedException e ) {
1344 e.printStackTrace() ;
1352 private class DialogVMServer extends Thread
1354 protected boolean go ;
1355 private Socket socket ;
1356 // private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1363 protected void stopDialogVMServer()
1367 if( serverSocket != null )
1370 serverSocket.close() ;
1371 } catch( IOException e ) {
1372 e.printStackTrace() ;
1383 serverSocket = new ServerSocket( 0 ) ;
1384 dialog_port = serverSocket.getLocalPort() ;
1386 System.out.println( "SocketServer listening on port " + dialog_port ) ;
1387 } catch( IOException e ) {
1388 System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
1389 e.printStackTrace() ;
1396 socket = serverSocket.accept() ;
1398 new DialogVM( socket ).start() ;
1400 } catch( IOException e ) {
1401 System.err.println( "Problem with the accept function!" ) ;
1402 e.printStackTrace() ;
1410 private class DialogVM extends Thread
1412 private Socket socket ;
1413 private BufferedReader reader ;
1414 private String line ;
1416 DialogVM( Socket _socket ) { socket = _socket ; }
1422 reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
1423 } catch( IOException e ) {
1424 System.err.println( "Unable to open a dialog socket with the VM!" ) ;
1425 e.printStackTrace() ;
1432 if( reader != null )
1434 line = reader.readLine() ;
1437 /** VM is starting -- retrieving informations **/
1438 if( line != null && line.equalsIgnoreCase( "infos" ) )
1440 /* Receiving name */
1441 machine.setName( reader.readLine() ) ;
1444 String ip = reader.readLine() ;
1445 if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1447 System.err.println( "VM IP not well configured!!" ) ;
1451 reader.close() ; reader = null ;
1452 socket.close() ; socket = null ;
1454 } else if( line != null && line.equalsIgnoreCase( "save" ) ) {
1455 /** It's time to do a save **/
1458 machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1459 } catch( Exception e ) {
1460 System.err.println( "Problem while reading the computation id!" ) ;
1461 e.printStackTrace() ;
1464 synchronized( saveRequest )
1467 LocalHost.Instance().getServerStub().requestSave( LocalHost.Instance().getIP() ) ;
1468 } catch( RemoteException e ) {
1469 System.err.println( "Unable to request save to server!" ) ;
1470 e.printStackTrace() ;
1474 saveRequest.wait() ;
1475 } catch( InterruptedException e ) {
1476 e.printStackTrace();
1480 if( saveRequest.getStatus() )
1483 reader.close() ; reader = null ;
1484 socket.close() ; socket = null ;
1486 saveRequest.setStatus( false ) ;
1488 /* Requesting the VM save */
1491 System.err.println( "Problem while saving. Exiting ..." ) ;
1492 pingServer.stopPing() ;
1499 } else if( line != null && line.equalsIgnoreCase( "quit" ) ) {
1500 /** Computation is done, we can shutdown the VM **/
1503 Thread.sleep( 5000 ) ;
1504 } catch( InterruptedException e ) {
1505 e.printStackTrace() ;
1509 reader.close() ; reader = null ;
1510 socket.close() ; socket = null ;
1514 machine.setStatus( "connected" ) ;
1516 LocalHost.Instance().getServerStub().changeStatus(
1517 LocalHost.Instance().getIP(), "connected" ) ;
1518 } catch( RemoteException e ) {
1519 System.err.println( "Unable to inform the server of the VM status!" ) ;
1520 e.printStackTrace() ;
1524 LocalHost.Instance().getServerStub().endApplication() ;
1525 } catch( RemoteException e ) {
1526 System.err.println( "Unable to inform the server of the end of application!" ) ;
1527 e.printStackTrace() ;
1529 } else if( line != null && line.equalsIgnoreCase( "start" ) ) {
1530 /** Computation is starting **/
1533 reader.close() ; reader = null ;
1534 socket.close() ; socket = null ;
1535 System.out.println( "Application is starting." ) ;
1538 LocalHost.Instance().getServerStub().goApplication() ;
1539 } catch( RemoteException e ) {
1540 System.err.println( "Unable to inform the server of the end of application!" ) ;
1541 e.printStackTrace() ;
1545 } catch( IOException e ) {
1546 e.printStackTrace() ;
1557 pingServer.stopPing() ;
1559 dialogVmServer.stopDialogVMServer() ;
1561 // unexportObject ??
1567 public String getIPHost()
1569 return LocalHost.Instance().getIP() ;
1573 public String getName()
1575 return LocalHost.Instance().getName() ;
1580 public void saveOk()
1582 String save_name = machine.getName() + "_last_" + machine.getComputationId() +
1585 String save_new = machine.getName() + "_new_"
1586 + machine.getComputationId() + ".tar.gz" ;
1588 String[] command = new String[]{ "/bin/mv",
1589 working_directory + "/" + save_new,
1590 working_directory + "/" + save_name } ;
1593 Process p = Runtime.getRuntime().exec( command ) ;
1596 if( p.exitValue() == 0 )
1598 machine.setSave_last( save_name ) ;
1599 System.out.println( "Last save OK" ) ;
1601 System.err.println( "Last save NOK!" ) ;
1602 System.err.println( "Error: " ) ;
1603 printProcessError( p ) ;
1605 } catch( IOException e ) {
1606 System.err.println( "Error during last archive move:" ) ;
1607 e.printStackTrace() ;
1608 } catch( InterruptedException e ) {
1609 e.printStackTrace() ;
1612 // Changing on save neighbors
1613 for( int i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1616 machine.getSaveNeighbors().get( i ).getStub().changeSaveName( save_new, save_name, machine.getComputationId() ) ;
1617 } catch( RemoteException e ) {
1618 System.err.println( "Unable to change save name on " + machine.getSaveNeighbors().get( i ).getName() + "!" ) ;
1619 e.printStackTrace() ;
1623 // Informing the server
1626 ret = LocalHost.Instance().getServerStub().changeSaveName( LocalHost.Instance().getIP(), save_name ) ;
1627 } catch( RemoteException e ) {
1628 System.err.println( "Unable to inform the server about the new save name!" ) ;
1629 e.printStackTrace() ;
1634 System.out.println( "Successfully informing the server about the new save name." ) ;
1636 System.err.println( "Problem on the server while informing it about the new save name!" ) ;
1644 public void changeSaveName( String _n1, String _n2, int _id )
1646 if( _n1 != null && _n1.length() > 0 )
1648 System.out.println( "Changing save name for processus " + _id + " ... " ) ;
1650 String[] command = new String[]{ "/bin/mv",
1651 working_directory + "/" + _n1,
1652 working_directory + "/" + _n2 } ;
1655 Process p = Runtime.getRuntime().exec( command ) ;
1658 if( p.exitValue() == 0 )
1660 System.out.println( "Change save name OK" ) ;
1662 System.err.println( "Change save name NOK!" ) ;
1663 System.err.println( "Error: " ) ;
1664 printProcessError( p ) ;
1666 } catch( IOException e ) {
1667 System.err.println( "Error during save renaming:" ) ;
1668 e.printStackTrace() ;
1669 } catch( InterruptedException e ) {
1670 e.printStackTrace() ;
1677 public void setSaveNeighbor( SaveNeighbor _sn )
1681 ArrayList<SaveNeighbor> as = new ArrayList<SaveNeighbor>() ;
1684 System.out.println( "Save neighbor: " + _sn.getName() ) ;
1686 machine.setSaveNeighbors( as ) ;
1692 public void setSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1694 if( _sn != null && _sn.size() > 0 )
1696 System.out.print( "Save neighbors: " ) ;
1697 for( int i = 0 ; i < _sn.size() ; i++ )
1699 System.out.print( _sn.get( i ).getName() ) ;
1701 if( i != _sn.size() - 1 )
1703 System.out.print( ", " ) ;
1705 System.out.println( "." ) ;
1709 machine.setSaveNeighbors( _sn ) ;
1715 public void addSaveNeighbor( SaveNeighbor _sn )
1719 System.out.println( "Adding save neighbor: " + _sn.getName() ) ;
1721 machine.getSaveNeighbors().add( _sn ) ;
1727 public void addSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1729 if( _sn != null && _sn.size() > 0 )
1731 System.out.print( "Adding save neighbors: " ) ;
1732 for( int i = 0 ; i < _sn.size() ; i++ )
1734 System.out.print( _sn.get( i ).getName() ) ;
1736 if( i != _sn.size() - 1 )
1738 System.out.print( ", " ) ;
1740 System.out.println( "." ) ;
1743 machine.getSaveNeighbors().add( _sn.get( i ) ) ;
1750 public void replaceSaveNeighbor( SaveNeighbor _old, SaveNeighbor _new )
1752 System.out.print( "Replacing a save neihgbor ... " ) ;
1753 if( _old != null && _new != null )
1756 boolean change = false ;
1758 for( i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1760 if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old.getIPHost() ) )
1762 machine.getSaveNeighbors().set( i, _new ) ;
1763 System.out.println( "Save neighbor successfully changed." ) ;
1771 System.out.println( "I am not concerned by the modification." ) ;
1778 public int retrieveSave( String _saveName )
1780 if( _saveName != null )
1782 if( ! _saveName.equalsIgnoreCase( "none" ) )
1784 machine.setSave_last( _saveName ) ;
1786 System.err.println( "I have no save to retrieve!!" ) ;
1790 // TODO NEIGHBORS !!!!
1791 boolean ok = false ;
1794 while( ! ok && i < machine.getSaveNeighbors().size() )
1796 System.out.print( "Retrieving a save on " + machine.getSaveNeighbors().get( 0 ).getName() + " ... " ) ;
1798 String command[] = {} ;
1800 command = new String[]{ "/usr/bin/scp",
1801 machine.getSaveNeighbors().get( i ).getIPHost() + ":" +
1802 machine.getSaveNeighbors().get( i ).getWorkingDirectory() + "/" +
1803 machine.getSave_last(),
1804 working_directory } ;
1807 Process p = Runtime.getRuntime().exec( command ) ;
1810 if( p.exitValue() == 0 )
1812 System.out.println( "Archive successfully retrieved." ) ;
1813 isRestartedSave = true ;
1816 System.err.println( "Archive not retrieved!" ) ;
1817 System.err.println( "Error: " ) ;
1819 printProcessError( p ) ;
1821 } catch( IOException e ) {
1822 System.err.println( "Error during archive retrieve command: " ) ;
1823 e.printStackTrace() ;
1824 } catch( InterruptedException e ) {
1825 e.printStackTrace() ;
1835 System.err.println( "Unable to retrieve a save archive from any neighbor!" ) ;
1845 public String getIPVM() throws RemoteException
1847 if( machine != null )
1849 return machine.getIp() ;
1857 public void setIPVM( String _ipVM ) throws RemoteException
1859 if( _ipVM != null && ! _ipVM.isEmpty() )
1861 System.out.println( "The VM IP is now: " + _ipVM ) ;
1862 machine.setIp( _ipVM ) ;
1867 public String getWorkingDirectory()
1869 return working_directory ;
1873 public Integer deployVM( String _name, String _archive, String _directory )
1875 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 )
1877 File file = new File( working_directory + "/" + _archive ) ;
1878 if( ! file.exists() )
1880 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1883 } else if( file.isDirectory() ) {
1884 System.err.println( _archive + " is a directory!" ) ;
1891 machine.setName( _name ) ;
1892 machine.setInitial_archive_name( _archive ) ;
1893 machine.setDirectory( _directory ) ;
1895 if( machine.deployInitialVM() == 1 )
1897 System.err.println( "Unable to deploy the initial VM archive!" ) ;
1908 public void responseSave( boolean _b )
1910 synchronized( saveRequest )
1912 saveRequest.setStatus( _b ) ;
1914 saveRequest.notifyAll() ;
1915 } catch( Exception e ) {
1916 e.printStackTrace() ;
1922 private class SaveRequest
1931 protected boolean getStatus() { return status ; }
1933 protected void setStatus( boolean _b ) { status = _b ; }
1936 private class SaveProcess
1945 protected boolean getStatus() { return status ; }
1947 protected void setStatus( boolean _b ) { status = _b ; }
1953 /** La programmation est un art, respectons ceux qui la pratiquent !! **/