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;
20 public class Client extends UnicastRemoteObject implements ServicesClient
22 private static final long serialVersionUID = 1L ;
24 private String VmRunCommand ;
25 // private String VmRunCommandArg ;
26 private VirtualMachine machine ;
27 private String server_ip ;
28 private int server_port ;
29 private int client_port ;
30 private int dialog_port ;
31 private ServicesServer serverStub ;
32 private ServicesClient myStub ;
33 private PingServer pingServer ;
34 private DialogVMServer dialogVmServer ;
35 private ServerSocket serverSocket ;
36 private String ushell ;
37 private String working_directory ;
38 private int wait_start ;
39 private int max_start_try ;
40 private boolean isRestartedSave ;
41 private long save_interleave ;
42 private long date_last_save ;
43 private SaveProcess saveProcess;
44 private int maxRetryVM ;
45 private int timeRetryVM ;
48 protected Client() throws RemoteException
55 public int startVM( int _mode )
57 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "running" ) )
63 System.out.print( "Starting VM ... " ) ;
65 machine.setStatus( "undefined" ) ;
69 LocalHost.Instance().getServerStub().changeStatus(
70 LocalHost.Instance().getIP(), "undefined" ) ;
71 } catch( RemoteException e ) {
72 System.err.println( "Unable to inform the server of the VM status!" ) ;
77 String[] command = new String[]{VmRunCommand, "-T", "player", "start",
78 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name(), "nogui"} ;
82 Process p = Runtime.getRuntime().exec( command ) ;
85 if( p.exitValue() == 0 )
87 System.out.println( "Virtual machine successfully started." ) ;
90 System.err.println( "Virtual machine not started!" ) ;
91 ret = printProcessError( p ) ;
98 if( retry >= maxRetryVM )
100 System.err.println( "Unable to start VM!" ) ;
103 System.out.println( "Retrying (" + retry + ") ... " ) ;
104 Thread.sleep( timeRetryVM ) ;
107 } catch( IOException e ) {
108 System.err.println( "Error during execution of start command: " ) ;
109 e.printStackTrace() ;
111 } catch( InterruptedException e ) {
112 e.printStackTrace() ;
117 boolean started = false ;
124 /** Waiting for VM being started **/
126 Thread.sleep( wait_start ) ;
127 } catch( InterruptedException e ) {
128 e.printStackTrace() ;
131 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
132 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
133 working_directory + "/" + machine.getDirectory()
134 + "/" + machine.getVmx_name() + " " + ushell +
135 " \"echo ok\"" ;// + " -noWait " ;
138 FileWriter fw = new FileWriter( new File( working_directory + "/testStarted.sh" ) ) ;
142 } catch( IOException e1 ) {
143 e1.printStackTrace() ;
146 command = new String[]{ ushell, working_directory + "/testStarted.sh"} ;
150 Process p = Runtime.getRuntime().exec( command ) ;
153 if( p.exitValue() == 0 )
156 // machine.setStatus( "running" ) ;
159 // LocalHost.Instance().getServerStub().changeStatus(
160 // LocalHost.Instance().getIP(), "running" ) ;
164 System.err.println( "Error while checking if the VM is started!" ) ;
165 // printProcessError( p.getErrorStream() ) ;
166 ret = printProcessError( p ) ;
173 if( retry >= maxRetryVM )
175 System.err.println( "Unable to check VM!" ) ;
178 System.out.println( "Retrying (" + retry + ") ... " ) ;
179 Thread.sleep( timeRetryVM ) ;
181 // wait_start = wait_start / 2 ;
184 } catch( IOException e ) {
185 e.printStackTrace() ;
186 } catch( InterruptedException e ) {
187 e.printStackTrace() ;
190 if( count == max_start_try && ! started )
192 System.err.println( "Virtual machine not responding!!" ) ;
195 LocalHost.Instance().getServerStub().changeStatus(
196 LocalHost.Instance().getIP(), "undefined" ) ;
197 } catch( RemoteException e ) {
198 e.printStackTrace() ;
206 Thread.sleep( 3000 ) ;
207 } catch( InterruptedException e ) {
208 e.printStackTrace() ;
213 /** Sending the host ip **/
215 System.out.print( "Sending host IP to VM ... " ) ;
217 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
218 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
219 working_directory + "/" + machine.getDirectory()
220 + "/" + machine.getVmx_name() + " " + ushell +
221 " \"echo " + LocalHost.Instance().getIP() + " " + dialog_port
222 + " > /tmp/vm_host_IP\"" ;
225 FileWriter fw = new FileWriter( new File( working_directory + "/sendHostIP.sh" ) ) ;
229 } catch( IOException e1 ) {
230 e1.printStackTrace() ;
233 command = new String[]{ ushell, working_directory + "/sendHostIP.sh"} ;
240 Process p = Runtime.getRuntime().exec( command ) ;
243 if( p.exitValue() == 0 )
245 System.out.println( "VM received the host IP." ) ;
248 System.err.println( "VM did not received the host IP!" ) ;
249 // printProcessError( p.getErrorStream() ) ;
250 ret = printProcessError( p ) ;
257 if( retry >= maxRetryVM )
259 System.err.println( "Unable to send information to VM!" ) ;
265 System.out.println( "Retrying (" + retry + ") ... " ) ;
266 Thread.sleep( timeRetryVM ) ;
269 } catch( IOException e ) {
270 System.err.println( "Error during execution of runScriptInGuest command: " ) ;
271 e.printStackTrace() ;
272 } catch( InterruptedException e) {
273 e.printStackTrace() ;
276 /** Sending the vm ip **/
278 System.out.print( "Sending its IP to VM ... " ) ;
280 cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
281 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
282 working_directory + "/" + machine.getDirectory()
283 + "/" + machine.getVmx_name() + " " + ushell +
284 " \"echo " + machine.getIp()
285 + " > /tmp/vm_IP\"" ;
288 FileWriter fw = new FileWriter( new File( working_directory + "/sendVmIP.sh" ) ) ;
292 } catch( IOException e1 ) {
293 e1.printStackTrace() ;
296 command = new String[]{ ushell, working_directory + "/sendVmIP.sh"} ;
303 Process p = Runtime.getRuntime().exec( command ) ;
306 if( p.exitValue() == 0 )
308 System.out.println( "VM received its assigned IP." ) ;
311 machine.setStatus( "running" ) ;
313 System.err.println( "VM did not received its assigned IP!" ) ;
314 // printProcessError( p.getErrorStream() ) ;
315 ret = printProcessError( p ) ;
322 if( retry >= maxRetryVM )
324 System.err.println( "Unable to send information to VM!" ) ;
330 System.out.println( "Retrying (" + retry + ") ... " ) ;
331 Thread.sleep( timeRetryVM ) ;
334 } catch( IOException e ) {
335 System.err.println( "Error during execution of runScriptInGuest command: " ) ;
336 e.printStackTrace() ;
337 } catch( InterruptedException e ) {
338 e.printStackTrace() ;
344 LocalHost.Instance().getServerStub().changeStatus(
345 LocalHost.Instance().getIP(), "running" ) ;
346 } catch (RemoteException e) {
347 System.err.println( "Unable to inform the server of the VM started status!" ) ;
361 if( machine != null && machine.getStatus().equalsIgnoreCase( "stopped" ) )
366 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "stopped" ) )
368 System.out.print( "Stopping VM ... " ) ;
373 machine.setStatus( "undefined" ) ;
375 LocalHost.Instance().getServerStub().changeStatus(
376 LocalHost.Instance().getIP(), "undefined" ) ;
377 } catch( RemoteException e ) {
378 System.err.println( "Unable to inform the server of the VM status!" ) ;
379 e.printStackTrace() ;
382 String[] command = new String[]{VmRunCommand, "-T", "player", "stop",
383 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
387 Process p = Runtime.getRuntime().exec( command ) ;
390 if( p.exitValue() == 0 )
392 System.out.println( "Virtual machine successfully stopped." ) ;
393 machine.setStatus( "stopped" ) ;
396 System.err.println( "Virtual machine not stopped!" ) ;
397 // printProcessError( p.getErrorStream() ) ;
398 ret = printProcessError( p ) ;
405 if( retry >= maxRetryVM )
407 System.err.println( "Unable to stop VM!" ) ;
410 System.out.println( "Retrying (" + retry + ") ... " ) ;
411 Thread.sleep( timeRetryVM ) ;
416 } catch( IOException e ) {
417 System.err.println( "Error during execution of stop command: " ) ;
418 e.printStackTrace() ;
419 } catch( InterruptedException e ) {
420 e.printStackTrace() ;
423 machine.setStatus( "stopped" ) ;
425 LocalHost.Instance().getServerStub().changeStatus(
426 LocalHost.Instance().getIP(), "stopped" ) ;
427 } catch( RemoteException e1 ) {
428 System.err.println( "Unable to inform the server of the VM stopped status!" ) ;
429 e1.printStackTrace() ;
432 if( ! isRestartedSave )
434 /** Restoring the original vmx file (necessary after a crash) **/
435 command = new String[]{ "/bin/cp",
436 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_normal(),
437 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
440 Process p = Runtime.getRuntime().exec( command ) ;
443 if( p.exitValue() == 0 )
445 System.out.println( "Successfully replaced the VMX file." ) ;
448 System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
449 // printProcessError( p.getErrorStream() ) ;
450 printProcessError( p ) ;
454 } catch( IOException e ) {
455 System.err.println( "Error during VMX file replacement: " ) ;
456 e.printStackTrace() ;
457 } catch( InterruptedException e ) {
458 e.printStackTrace() ;
469 public int suspendVM( int _mode )
471 if( machine != null && machine.getStatus().equalsIgnoreCase( "suspended" ) )
476 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "suspended" ) )
478 System.out.print( "Suspending VM ... " ) ;
483 machine.setStatus( "undefined" ) ;
487 LocalHost.Instance().getServerStub().changeStatus(
488 LocalHost.Instance().getIP(), "undefined" ) ;
489 } catch( RemoteException e ) {
490 System.err.println( "Unable to inform the server of the VM status!" ) ;
491 e.printStackTrace() ;
495 String[] command = new String[]{VmRunCommand, "-T", "player", "suspend",
496 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
500 Process p = Runtime.getRuntime().exec( command ) ;
502 if( p.exitValue() == 0 )
504 System.out.println( "Virtual machine successfully suspended." ) ;
505 machine.setStatus( "suspended" ) ;
508 System.err.println( "Virtual machine not suspended!" ) ;
509 // printProcessError( p.getErrorStream() ) ;
510 ret = printProcessError( p ) ;
517 if( retry >= maxRetryVM )
519 System.err.println( "Unable to suspend VM!" ) ;
522 System.out.println( "Retrying (" + retry + ") ... " ) ;
523 Thread.sleep( timeRetryVM ) ;
528 } catch( IOException e ) {
529 System.err.println( "Error during execution of suspend command: " ) ;
530 e.printStackTrace() ;
531 } catch( InterruptedException e ) {
532 e.printStackTrace() ;
535 machine.setStatus( "suspended" ) ;
539 LocalHost.Instance().getServerStub().changeStatus(
540 LocalHost.Instance().getIP(), "suspended" ) ;
541 } catch( RemoteException e ) {
542 System.err.println( "Unable to inform the server of the VM suspended status!" ) ;
543 e.printStackTrace() ;
554 public int restartVM()
556 if( machine != null )
558 System.out.print( "Restarting VM ... " ) ;
564 LocalHost.Instance().getServerStub().changeStatus(
565 LocalHost.Instance().getIP(), "undefined" ) ;
566 } catch( RemoteException e ) {
567 System.err.println( "Unable to inform the server of the VM status!" ) ;
568 e.printStackTrace() ;
571 String[] command = new String[]{VmRunCommand, "-T", "player", "reset",
572 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
576 Process p = Runtime.getRuntime().exec( command ) ;
579 if( p.exitValue() == 0 )
581 System.out.println( "Virtual machine successfully restarted." ) ;
583 if( sendSaveOkVM() == 1 ) { return 1 ; }
589 System.err.println( "Virtual machine not restarted!" ) ;
590 // printProcessError( p.getErrorStream() ) ;
591 ret = printProcessError( p ) ;
598 if( retry >= maxRetryVM )
600 System.err.println( "Unable to start VM!" ) ;
603 System.out.println( "Retrying (" + retry + ") ... " ) ;
604 Thread.sleep( timeRetryVM ) ;
609 } catch( IOException e ) {
610 System.err.println( "Error during execution of restart command: " ) ;
611 e.printStackTrace() ;
612 } catch( InterruptedException e ) {
613 e.printStackTrace() ;
621 public int restartVMAfterCrash()
623 System.out.println( "Restarting VM after a crash ..." ) ;
626 LocalHost.Instance().getServerStub().changeStatus(
627 LocalHost.Instance().getIP(), "undefined" ) ;
628 } catch( RemoteException e ) {
629 System.err.println( "Unable to inform the server of the VM status!" ) ;
630 e.printStackTrace() ;
635 if( machine.deployLastSave() == 0 )
637 if( isRestartedSave )
639 // Using the specific vmx file
640 System.out.print( "Changing VMX file after crash ... " ) ;
642 String[] command = new String[]{ "/bin/cp",
643 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_crash(),
644 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
647 Process p = Runtime.getRuntime().exec( command ) ;
650 if( p.exitValue() == 0 )
652 System.out.println( "Successfully replaced the VMX file." ) ;
654 System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
655 // printProcessError( p.getErrorStream() ) ;
656 printProcessError( p ) ;
660 } catch( IOException e ) {
661 System.err.println( "Error during VMX file replacement: " ) ;
662 e.printStackTrace() ;
663 } catch( InterruptedException e ) {
664 e.printStackTrace() ;
667 // Removing lock files
668 System.out.print( "Removing lock files ... " ) ;
670 command = new String[]{ "/bin/rm", "-rf",
671 working_directory + "/" + machine.getDirectory()
672 + "/" + machine.getVmx_name() + ".lck" } ;
675 Process p = Runtime.getRuntime().exec( command ) ;
678 if( p.exitValue() == 0 )
680 System.out.println( "Successfully deleted lock files." ) ;
682 System.err.println( "Unsuccessful deletion of lock files!" ) ;
683 // printProcessError( p.getErrorStream() ) ;
684 printProcessError( p ) ;
688 } catch( IOException e ) {
689 System.err.println( "Error during lock files deletion: " ) ;
690 e.printStackTrace() ;
691 } catch( InterruptedException e ) {
692 e.printStackTrace() ;
695 /** Retrieving VM assigned IP **/
699 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
700 LocalHost.Instance().getIP() ) ;
701 } catch (RemoteException e) {
702 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
703 e.printStackTrace() ;
707 machine.setIp( vmIP ) ;
709 isRestartedSave = false ;
712 if( startVM( 0 ) == 0 )
714 if( sendSaveOkVM() == 0 )
728 private int sendSaveOkVM()
733 /** Informing the program that it's ok **/
734 System.out.print( "Sending OK signal to the program ... " ) ;
736 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
737 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
738 working_directory + "/" + machine.getDirectory()
739 + "/" + machine.getVmx_name() + " " + ushell +
740 " \"echo ok > /tmp/vm_save_ok\"" ;// + " -noWait " ;
743 FileWriter fw = new FileWriter( new File( working_directory + "/saveOk.sh" ) ) ;
747 } catch( IOException e1 ) {
748 e1.printStackTrace() ;
751 String[] command = new String[]{ ushell, working_directory + "/saveOk.sh"} ;
755 Process p = Runtime.getRuntime().exec( command ) ;
758 if( p.exitValue() == 0 )
760 System.out.println( "Signal successfully sent." ) ;
764 System.err.println( "Signal not sent!" ) ;
765 // printProcessError( p.getErrorStream() ) ;
766 ret = printProcessError( p ) ;
773 if( retry >= maxRetryVM )
775 System.err.println( "Unable to send ok signal to VM!" ) ;
778 System.out.println( "Retrying (" + retry + ") ... " ) ;
779 Thread.sleep( timeRetryVM ) ;
783 } catch( IOException e ) {
784 System.err.println( "Error during ok save signal send command: " ) ;
785 e.printStackTrace() ;
787 } catch( InterruptedException e ) {
788 e.printStackTrace() ;
799 synchronized( saveProcess ){
800 while( saveProcess.getStatus() )
804 } catch( InterruptedException e ) {
805 e.printStackTrace() ;
809 System.out.println( "Saving VM ..." ) ;
810 saveProcess.setStatus( true ) ;
812 machine.setStatus( "saving" ) ;
814 LocalHost.Instance().getServerStub().changeStatus(
815 LocalHost.Instance().getIP(), "saving" ) ;
816 } catch( RemoteException e ) {
817 System.err.println( "Unable to inform the server of the VM status!" ) ;
818 e.printStackTrace() ;
822 String saveName = "" ;
823 boolean error = false ;
824 boolean errorVM = false ;
826 if( suspendVM( 1 ) == 1 ) { error = true ; errorVM = true ; }
830 System.out.print( "Creation of the archive ... " ) ;
831 /** Archive creation **/
832 command = new String[]{ "/bin/tar", "-cvf",
833 working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar",
834 working_directory + "/" + machine.getDirectory() } ;
837 Process p = Runtime.getRuntime().exec( command ) ;
840 if( p.exitValue() == 0 )
842 System.out.println( "Archive successfully created." ) ;
844 System.err.println( "Archive not created!" ) ;
845 // printProcessError( p.getErrorStream() ) ;
846 printProcessError( p ) ;
850 } catch( IOException e ) {
851 System.err.println( "Error during archive creation command: " ) ;
853 e.printStackTrace() ;
854 } catch( InterruptedException e ) {
855 e.printStackTrace() ;
859 /** Compression of the archive **/
862 System.out.print( "Compression of the archive ... " ) ;
863 command = new String[]{ "/bin/gzip",
864 working_directory + "/" + machine.getName()
865 + "_new_" + machine.getComputationId() + ".tar" } ;
868 Process p = Runtime.getRuntime().exec( command ) ;
871 if( p.exitValue() == 0 )
873 System.out.println( "Archive successfully compressed." ) ;
875 System.err.println( "Archive not compressed!" ) ;
876 // printProcessError( p.getErrorStream() ) ;
877 printProcessError( p ) ;
881 } catch( IOException e ) {
882 System.err.println( "Error during archive compression command: " ) ;
883 e.printStackTrace() ;
885 } catch( InterruptedException e ) {
886 e.printStackTrace() ;
892 /** Restarting VM **/
893 if( errorVM || startVM( 0 ) == 1 ) { error = true ; }
896 /** Sending ok save signal **/
899 if( sendSaveOkVM() == 1 ) { error = true ; }
902 // /** Deletion of the tar archive **/
905 // command = new String[]{ "/bin/rm",
906 // working_directory + "/" + machine.getName()
907 // + "_new_" + machine.getComputationId() + ".tar" } ;
910 // Process p = Runtime.getRuntime().exec( command ) ;
913 // if( p.exitValue() == 0 )
915 // System.out.println( "Archive (not compressed) successfully deleted." ) ;
917 // System.err.println( "Archive (not compressed) not deleted!" ) ;
918 // printProcessError( p.getErrorStream() ) ;
922 // } catch( IOException e ) {
923 // System.err.println( "Error during archive (not compressed) deletion command: " ) ;
924 // e.printStackTrace() ;
926 // } catch( InterruptedException e ) {
927 // e.printStackTrace() ;
932 saveName = working_directory + "/" + machine.getName() + "_new_"
933 + machine.getComputationId() + ".tar.gz" ;
935 /** Sending save to neighbor **/
938 ArrayList<String> sn = machine.getSaveNeighbors() ;
940 for( int i = 0 ; i < sn.size() ; i++ )
942 System.out.print( "Sending save to " + sn.get( i ) + " ... " ) ;
943 command = new String[]{ "/usr/bin/scp", saveName,
945 + working_directory } ;
948 Process p = Runtime.getRuntime().exec( command ) ;
951 if( p.exitValue() == 0 )
953 System.out.println( "Archive successfully sent." ) ;
955 System.err.println( "Archive not sent!" ) ;
956 // printProcessError( p.getErrorStream() ) ;
957 printProcessError( p ) ;
961 } catch( IOException e ) {
962 System.err.println( "Error during archive send command: " ) ;
963 e.printStackTrace() ;
965 } catch( InterruptedException e ) {
966 e.printStackTrace() ;
973 /** Informing the server the save is done **/
977 LocalHost.Instance().getServerStub().saveOk( LocalHost.Instance().getIP(), saveName ) ;
978 } catch( RemoteException e ) {
979 System.err.println( "Problem while informing the server about the save state!" ) ;
980 e.printStackTrace() ;
983 synchronized( saveProcess ) {
984 saveProcess.setStatus( false ) ;
986 saveProcess.notifyAll() ;
987 } catch( Exception e ) {}}
992 synchronized( saveProcess ) {
993 saveProcess.setStatus( false ) ;
995 saveProcess.notifyAll() ;
996 } catch( Exception e ) {}}
1003 public int reloadConfig()
1005 System.out.println( "Reloading configuration ... " ) ;
1011 public void init( String _server_ip, int _server_port, int _client_port, int _dialog_port )
1013 System.out.println( "Initialisation Client ... " ) ;
1014 System.out.println( "IP " + LocalHost.Instance().getIP() ) ;
1016 server_ip = _server_ip ;
1017 server_port = _server_port ;
1018 client_port = _client_port ;
1019 dialog_port = _server_port + 1 ; // _dialog_port ;
1022 saveProcess = new SaveProcess() ;
1024 machine = new VirtualMachine() ;
1026 VmRunCommand = "/usr/bin/vmrun" ;
1027 // VmRunCommandArg = "-T player" ;
1029 // vm_user = "mpi" ;
1030 // vm_user_passwd = "mpi" ;
1031 ushell = "/bin/bash" ;
1032 working_directory = "/localhome/vmware" ;
1034 wait_start = 15000 ;
1035 max_start_try = 10 ;
1038 timeRetryVM = 10000 ;
1040 save_interleave = 30 * 60 * 1000 ;
1041 date_last_save = 0 ;
1043 isRestartedSave = false ;
1045 /** Connection to server **/
1047 serverStub = (ServicesServer) Naming.lookup( "rmi://"
1048 + server_ip + ":" + server_port + "/Server" ) ;
1049 } catch (MalformedURLException e) {
1050 e.printStackTrace();
1051 } catch (RemoteException e) {
1052 e.printStackTrace();
1053 } catch (NotBoundException e) {
1054 e.printStackTrace();
1057 if( serverStub == null )
1059 System.err.println( "Unable to connect to server!!" ) ;
1060 System.err.println( "Server IP: " + server_ip + " -- server port: " + server_port ) ;
1065 System.out.println( "Connected to server " + server_ip + " on port " + server_port + "." ) ;
1067 // LocalHost.Instance().setServerIP( server_ip ) ;
1068 LocalHost.Instance().setServerStub( serverStub ) ;
1071 /** Creating the local server **/
1074 /** Starting all threads **/
1078 private void exportObject()
1080 // ServicesClient ref = null ;
1081 Registry reg = null ;
1087 reg = LocateRegistry.getRegistry( client_port ) ;
1089 String tab[] = reg.list() ;
1091 System.out.println( "There is an existing RMI Registry on port " +
1092 client_port + " with " + tab.length + " entries!" ) ;
1093 for( int i = 0 ; i < tab.length ; i++ )
1096 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
1098 System.out.println( "Register successfuly deleted!" ) ;
1100 System.err.println( "Register undeleted !!!" ) ;
1102 } catch( Exception e ) {
1103 e.printStackTrace() ;
1107 } catch( RemoteException e ) {
1111 if ( System.getSecurityManager() == null )
1113 System.setSecurityManager( new SecurityManager() ) ;
1116 LocateRegistry.createRegistry( client_port ) ;
1117 LocateRegistry.getRegistry( client_port ).rebind( "Client", this ) ;
1118 myStub = (ServicesClient) Naming.lookup( "rmi://"
1119 + LocalHost.Instance().getIP() + ":" + client_port
1121 } catch( Exception e ) {
1122 System.err.println( "Error in Client.exportObject() when creating local services:" + e ) ;
1123 System.err.println( "Exit from Client.exportObject" ) ;
1127 LocalHost.Instance().setStub( myStub ) ;
1131 private boolean printProcessError( Process _p )
1133 boolean ret = false ;
1137 System.err.println( "Error: " + _p.exitValue() ) ;
1138 BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
1141 while( (line = br.readLine()) != null )
1143 System.err.println( line ) ;
1144 if( line.contains( "egmentation" ) )
1149 } catch( IOException e ) {
1150 e.printStackTrace() ;
1161 /** Registering on server **/
1164 ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1165 } catch (RemoteException e1) {
1166 e1.printStackTrace();
1172 case 0: System.out.println( "Successfully registered on server." ) ; break ;
1173 case 1: System.err.println( "Problem on server while registreting!" ) ; return 1 ;
1174 case 2: System.out.println( "Already registered on server!" ) ; break ;
1177 /** Retrieving VM assigned IP **/
1178 String vmIP = null ;
1181 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
1182 LocalHost.Instance().getIP() ) ;
1183 } catch (RemoteException e) {
1184 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
1185 e.printStackTrace() ;
1189 machine.setIp( vmIP ) ;
1191 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1194 /** Starting alive ping to server **/
1195 pingServer = new PingServer() ;
1196 pingServer.start() ;
1198 /** Starting socket server for VM dialog **/
1199 dialogVmServer = new DialogVMServer() ;
1200 dialogVmServer.start() ;
1206 private class PingServer extends Thread
1208 private boolean run ;
1215 protected void stopPing() { run = false ; }
1223 LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1224 } catch (RemoteException e1) {
1225 e1.printStackTrace();
1229 Thread.sleep( 2000 ) ;
1230 } catch( InterruptedException e ) {
1231 e.printStackTrace() ;
1238 private class DialogVMServer extends Thread
1240 private boolean run ;
1241 private Socket socket ;
1242 private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1249 protected void stopDialogVMServer()
1253 if( serverSocket != null )
1256 serverSocket.close() ;
1257 // socket = serverSocket.accept() ;
1259 for( int i = 0 ; i < dialogs.size() ; i++ )
1261 dialogs.get( i ).stopDialogVM() ;
1264 } catch( IOException e ) {
1265 e.printStackTrace() ;
1275 serverSocket = new ServerSocket( 0 ) ;
1276 dialog_port = serverSocket.getLocalPort() ;
1278 System.out.println( "SocketServer listening on port " + dialog_port ) ;
1279 } catch( IOException e ) {
1280 System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
1281 e.printStackTrace() ;
1289 socket = serverSocket.accept() ;
1291 dialogs.add( new DialogVM( socket ) ) ;
1292 dialogs.get( dialogs.size() - 1 ).start() ;
1293 } catch( IOException e ) {
1294 System.err.println( "Problem with the accept function!" ) ;
1295 e.printStackTrace() ;
1302 private class DialogVM extends Thread
1304 private boolean run ;
1305 private Socket socket ;
1306 private BufferedReader reader ;
1307 private String line ;
1309 DialogVM( Socket _socket ) { run = true ; socket = _socket ; }
1311 protected void stopDialogVM()
1316 reader.close() ; reader = null ;
1317 socket.close() ; socket = null ;
1318 } catch( IOException e ) {
1319 e.printStackTrace() ;
1327 reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
1328 } catch( IOException e ) {
1329 System.err.println( "Unable to open a dialog socket with the VM!" ) ;
1330 e.printStackTrace();
1339 if( reader != null )
1341 line = reader.readLine() ;
1344 /** VM is starting -- retrieving informations **/
1345 if( run && line != null && line.equalsIgnoreCase( "infos" ) )
1347 /* Receiving name */
1348 machine.setName( reader.readLine() ) ;
1351 String ip = reader.readLine() ;
1352 if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1354 System.err.println( "VM IP not well configured!!" ) ;
1358 reader.close() ; reader = null ;
1359 socket.close() ; socket = null ;
1364 /** It's time to do a save **/
1365 if( run && line != null && line.equalsIgnoreCase( "save" ) )
1368 machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1369 } catch( Exception e ) {
1370 System.err.println( "Problem while reading the computation id!" ) ;
1371 e.printStackTrace() ;
1374 if( (System.currentTimeMillis() - date_last_save) > save_interleave )
1376 date_last_save = System.currentTimeMillis() ;
1379 reader.close() ; reader = null ;
1380 socket.close() ; socket = null ;
1384 /* Starting the VM save */
1392 /** Computation is done, we can shutdown the VM **/
1393 if( run && line != null && line.equalsIgnoreCase( "quit" ) )
1396 Thread.sleep( 5000 ) ;
1397 } catch( InterruptedException e ) {
1398 e.printStackTrace() ;
1402 reader.close() ; reader = null ;
1403 socket.close() ; socket = null ;
1410 } catch( IOException e ) {
1411 e.printStackTrace() ;
1422 pingServer.stopPing() ;
1424 dialogVmServer.stopDialogVMServer() ;
1426 // unexportObject ??
1432 public String getIPHost()
1434 return LocalHost.Instance().getIP() ;
1438 public String getName()
1440 return LocalHost.Instance().getName() ;
1445 public void saveOk()
1447 String save_name = "VmTest_" + machine.getComputationId() +
1450 String save_new = working_directory + "/" + machine.getName() + "_new_"
1451 + machine.getComputationId() + ".tar.gz" ;
1453 String[] command = new String[]{ "/bin/mv",
1454 save_new, save_name } ;
1457 Process p = Runtime.getRuntime().exec( command ) ;
1460 if( p.exitValue() == 0 )
1462 System.out.println( "Last save OK" ) ;
1463 machine.setSave_last( save_name ) ;
1465 System.err.println( "Last save NOK!" ) ;
1466 System.err.println( "Error: " ) ;
1467 // printProcessError( p.getErrorStream() ) ;
1468 printProcessError( p ) ;
1470 } catch( IOException e ) {
1471 System.err.println( "Error during last archive move:" ) ;
1472 e.printStackTrace() ;
1473 } catch( InterruptedException e ) {
1474 e.printStackTrace() ;
1479 public void setSavingNeighbor( String _sn ) throws RemoteException
1481 if( _sn != null && _sn.length() > 0 )
1483 ArrayList<String> as = new ArrayList<String>() ;
1486 System.out.println( "Save neighbor: " + _sn ) ;
1488 machine.setSaveNeighbors( as ) ;
1493 public int retrieveSave( String _saveName ) throws RemoteException
1495 if( _saveName != null )
1497 if( ! _saveName.equalsIgnoreCase( "none" ) )
1499 machine.setSave_last( _saveName ) ;
1501 System.err.println( "I have no saving neighbor to contact!!" ) ;
1505 // TODO NEIGHBORS !!!!
1506 //System.out.println( "!!!! NEIGHBORS !!!!!" ) ;
1507 System.out.print( "Retrieving a save on " + machine.getSaveNeighbors().get( 0 ) + " ..." ) ;
1508 String[] command = new String[]{ "/usr/bin/scp",
1509 machine.getSaveNeighbors().get( 0 ) + ":"
1510 + working_directory + "/" + machine.getSave_last(),
1511 working_directory } ;
1514 Process p = Runtime.getRuntime().exec( command ) ;
1517 if( p.exitValue() == 0 )
1519 System.out.println( "Archive successfully retrieved." ) ;
1520 isRestartedSave = true ;
1523 System.err.println( "Archive not retrieved!" ) ;
1524 System.err.println( "Error: " ) ;
1525 // printProcessError( p.getErrorStream() ) ;
1526 printProcessError( p ) ;
1529 } catch( IOException e ) {
1530 System.err.println( "Error during archive retrieve command: " ) ;
1531 e.printStackTrace() ;
1533 } catch( InterruptedException e ) {
1534 e.printStackTrace() ;
1544 public String getIPVM() throws RemoteException
1546 if( machine != null )
1548 return machine.getIp() ;
1556 public void setIPVM( String _ipVM ) throws RemoteException
1558 if( _ipVM != null && ! _ipVM.isEmpty() )
1560 System.out.println( "The VM IP is now: " + _ipVM ) ;
1561 machine.setIp( _ipVM ) ;
1566 private class SaveProcess
1575 protected boolean getStatus() { return status ; }
1577 protected void setStatus( boolean _b ) { status = _b ; }
1583 /** La programmation est un art, respectons ceux qui la pratiquent !! **/