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 boolean lastSaveOk ;
53 protected Client() throws RemoteException
59 public void emergencyStop()
61 emergencyStop = true ;
63 // Saving processus stop
64 synchronized( saveProcess ) {
65 saveProcess.setStatus( false ) ;
67 saveProcess.notifyAll() ;
68 } catch( Exception e ) {}}
72 } catch( Exception e ) {}
78 public int startVM( int _mode )
80 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "running" ) )
84 } catch( InterruptedException e2 ) {
85 System.err.println( "Problem with semaphore acquiring!" ) ;
86 e2.printStackTrace() ;
93 System.out.print( "Starting VM ... " ) ;
95 machine.setStatus( "undefined" ) ;
99 LocalHost.Instance().getServerStub().changeStatus(
100 LocalHost.Instance().getIP(), "undefined" ) ;
101 } catch( RemoteException e ) {
102 System.err.println( "Unable to inform the server of the VM status!" ) ;
103 e.printStackTrace() ;
107 String[] command = new String[]{VmRunCommand, "-T", "player", "start",
108 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name(), "nogui"} ;
119 Process p = Runtime.getRuntime().exec( command ) ;
122 if( p.exitValue() == 0 )
124 System.out.println( "Virtual machine successfully started." ) ;
127 System.err.println( "Virtual machine not started!" ) ;
128 ret = printProcessError( p ) ;
137 if( retry >= maxRetryVM )
139 System.err.println( "Unable to start VM!" ) ;
145 System.out.println( "Retrying (" + retry + ") ... " ) ;
146 Thread.sleep( timeRetryVM ) ;
149 } catch( IOException e ) {
150 System.err.println( "Error during execution of start command: " ) ;
151 e.printStackTrace() ;
156 } catch( InterruptedException e ) {
157 e.printStackTrace() ;
165 boolean started = false ;
177 /** Waiting for VM being started **/
179 Thread.sleep( wait_start ) ;
180 } catch( InterruptedException e ) {
181 e.printStackTrace() ;
184 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
185 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
186 working_directory + "/" + machine.getDirectory()
187 + "/" + machine.getVmx_name() + " " + ushell +
188 " \"echo ok\"" ;// + " -noWait " ;
191 FileWriter fw = new FileWriter( new File( working_directory + "/testStarted.sh" ) ) ;
195 } catch( IOException e1 ) {
196 e1.printStackTrace() ;
202 command = new String[]{ ushell, working_directory + "/testStarted.sh"} ;
212 Process p = Runtime.getRuntime().exec( command ) ;
215 if( p.exitValue() == 0 )
218 // machine.setStatus( "running" ) ;
221 // LocalHost.Instance().getServerStub().changeStatus(
222 // LocalHost.Instance().getIP(), "running" ) ;
226 System.err.println( "Error while checking if the VM is started!" ) ;
227 // printProcessError( p.getErrorStream() ) ;
228 ret = printProcessError( p ) ;
237 if( retry >= maxRetryVM )
239 System.err.println( "Unable to check VM!" ) ;
245 System.out.println( "Retrying (" + retry + ") ... " ) ;
246 Thread.sleep( timeRetryVM ) ;
248 // wait_start = wait_start / 2 ;
251 } catch( IOException e ) {
252 e.printStackTrace() ;
256 } catch( InterruptedException e ) {
257 e.printStackTrace() ;
263 if( count == max_start_try && ! started )
265 System.err.println( "Virtual machine not responding!!" ) ;
268 LocalHost.Instance().getServerStub().changeStatus(
269 LocalHost.Instance().getIP(), "undefined" ) ;
270 } catch( RemoteException e ) {
271 e.printStackTrace() ;
281 Thread.sleep( 3000 ) ;
282 } catch( InterruptedException e ) {
283 e.printStackTrace() ;
289 /** Sending the host ip **/
291 System.out.print( "Sending host IP to VM ... " ) ;
293 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
294 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
295 working_directory + "/" + machine.getDirectory()
296 + "/" + machine.getVmx_name() + " " + ushell +
297 " \"echo " + LocalHost.Instance().getIP() + " " + dialog_port
298 + " > /tmp/vm_host_IP\"" ;
301 FileWriter fw = new FileWriter( new File( working_directory + "/sendHostIP.sh" ) ) ;
305 } catch( IOException e1 ) {
306 e1.printStackTrace() ;
312 command = new String[]{ ushell, working_directory + "/sendHostIP.sh"} ;
325 Process p = Runtime.getRuntime().exec( command ) ;
328 if( p.exitValue() == 0 )
330 System.out.println( "VM received the host IP." ) ;
333 System.err.println( "VM did not received the host IP!" ) ;
334 // printProcessError( p.getErrorStream() ) ;
335 ret = printProcessError( p ) ;
344 if( retry >= maxRetryVM )
346 System.err.println( "Unable to send information to VM!" ) ;
353 System.out.println( "Retrying (" + retry + ") ... " ) ;
354 Thread.sleep( timeRetryVM ) ;
357 } catch( IOException e ) {
358 System.err.println( "Error during execution of runScriptInGuest command: " ) ;
359 e.printStackTrace() ;
363 } catch( InterruptedException e) {
364 e.printStackTrace() ;
371 /** Sending the vm ip **/
373 System.out.print( "Sending its IP to VM ... " ) ;
375 cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
376 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
377 working_directory + "/" + machine.getDirectory()
378 + "/" + machine.getVmx_name() + " " + ushell +
379 " \"echo " + machine.getIp()
380 + " > /tmp/vm_IP\"" ;
383 FileWriter fw = new FileWriter( new File( working_directory + "/sendVmIP.sh" ) ) ;
387 } catch( IOException e1 ) {
388 e1.printStackTrace() ;
394 command = new String[]{ ushell, working_directory + "/sendVmIP.sh"} ;
407 Process p = Runtime.getRuntime().exec( command ) ;
410 if( p.exitValue() == 0 )
412 System.out.println( "VM received its assigned IP." ) ;
415 machine.setStatus( "running" ) ;
417 System.err.println( "VM did not received its assigned IP!" ) ;
418 // printProcessError( p.getErrorStream() ) ;
419 ret = printProcessError( p ) ;
428 if( retry >= maxRetryVM )
430 System.err.println( "Unable to send information to VM!" ) ;
437 System.out.println( "Retrying (" + retry + ") ... " ) ;
438 Thread.sleep( timeRetryVM ) ;
441 } catch( IOException e ) {
442 System.err.println( "Error during execution of runScriptInGuest command: " ) ;
443 e.printStackTrace() ;
447 } catch( InterruptedException e ) {
448 e.printStackTrace() ;
458 LocalHost.Instance().getServerStub().changeStatus(
459 LocalHost.Instance().getIP(), "running" ) ;
460 } catch (RemoteException e) {
461 System.err.println( "Unable to inform the server of the VM started status!" ) ;
479 if( machine != null && machine.getStatus().equalsIgnoreCase( "stopped" ) )
481 emergencyStop = false ;
485 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "stopped" ) )
489 } catch( InterruptedException e2 ) {
490 System.err.println( "Problem with semaphore acquiring!" ) ;
491 e2.printStackTrace() ;
494 System.out.print( "Stopping VM ... " ) ;
499 machine.setStatus( "undefined" ) ;
501 LocalHost.Instance().getServerStub().changeStatus(
502 LocalHost.Instance().getIP(), "undefined" ) ;
503 } catch( RemoteException e ) {
504 System.err.println( "Unable to inform the server of the VM status!" ) ;
505 e.printStackTrace() ;
508 String[] command = new String[]{VmRunCommand, "-T", "player", "stop",
509 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
513 Process p = Runtime.getRuntime().exec( command ) ;
516 if( p.exitValue() == 0 )
518 System.out.println( "Virtual machine successfully stopped." ) ;
519 machine.setStatus( "stopped" ) ;
522 System.err.println( "Virtual machine not stopped!" ) ;
523 // printProcessError( p.getErrorStream() ) ;
524 ret = printProcessError( p ) ;
532 if( retry >= maxRetryVM )
534 System.err.println( "Unable to stop VM!" ) ;
538 System.out.println( "Retrying (" + retry + ") ... " ) ;
539 Thread.sleep( timeRetryVM ) ;
544 } catch( IOException e ) {
545 System.err.println( "Error during execution of stop command: " ) ;
546 e.printStackTrace() ;
549 } catch( InterruptedException e ) {
550 e.printStackTrace() ;
555 machine.setStatus( "stopped" ) ;
557 LocalHost.Instance().getServerStub().changeStatus(
558 LocalHost.Instance().getIP(), "stopped" ) ;
559 } catch( RemoteException e1 ) {
560 System.err.println( "Unable to inform the server of the VM stopped status!" ) ;
561 e1.printStackTrace() ;
564 if( ! isRestartedSave )
566 /** Restoring the original vmx file (necessary after a crash) **/
567 command = new String[]{ "/bin/cp",
568 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_normal(),
569 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
572 Process p = Runtime.getRuntime().exec( command ) ;
575 if( p.exitValue() == 0 )
577 System.out.println( "Successfully replaced the VMX file." ) ;
579 emergencyStop = false ;
582 System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
583 // printProcessError( p.getErrorStream() ) ;
584 printProcessError( p ) ;
588 } catch( IOException e ) {
589 System.err.println( "Error during VMX file replacement: " ) ;
590 e.printStackTrace() ;
593 } catch( InterruptedException e ) {
594 e.printStackTrace() ;
600 emergencyStop = false ;
611 public int suspendVM( int _mode )
613 if( machine != null && machine.getStatus().equalsIgnoreCase( "suspended" ) )
618 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "suspended" ) )
622 } catch( InterruptedException e2 ) {
623 System.err.println( "Problem with semaphore acquiring!" ) ;
624 e2.printStackTrace() ;
627 System.out.print( "Suspending VM ... " ) ;
632 machine.setStatus( "undefined" ) ;
636 LocalHost.Instance().getServerStub().changeStatus(
637 LocalHost.Instance().getIP(), "undefined" ) ;
638 } catch( RemoteException e ) {
639 System.err.println( "Unable to inform the server of the VM status!" ) ;
640 e.printStackTrace() ;
644 String[] command = new String[]{VmRunCommand, "-T", "player", "suspend",
645 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
649 Process p = Runtime.getRuntime().exec( command ) ;
651 if( p.exitValue() == 0 )
653 System.out.println( "Virtual machine successfully suspended." ) ;
654 machine.setStatus( "suspended" ) ;
657 System.err.println( "Virtual machine not suspended!" ) ;
658 // printProcessError( p.getErrorStream() ) ;
659 ret = printProcessError( p ) ;
668 if( retry >= maxRetryVM )
670 System.err.println( "Unable to suspend VM!" ) ;
676 System.out.println( "Retrying (" + retry + ") ... " ) ;
677 Thread.sleep( timeRetryVM ) ;
682 } catch( IOException e ) {
683 System.err.println( "Error during execution of suspend command: " ) ;
684 e.printStackTrace() ;
685 } catch( InterruptedException e ) {
686 e.printStackTrace() ;
689 machine.setStatus( "suspended" ) ;
693 LocalHost.Instance().getServerStub().changeStatus(
694 LocalHost.Instance().getIP(), "suspended" ) ;
695 } catch( RemoteException e ) {
696 System.err.println( "Unable to inform the server of the VM suspended status!" ) ;
697 e.printStackTrace() ;
712 public int restartVM()
714 if( machine != null )
716 System.out.print( "Restarting VM ... " ) ;
722 LocalHost.Instance().getServerStub().changeStatus(
723 LocalHost.Instance().getIP(), "undefined" ) ;
724 } catch( RemoteException e ) {
725 System.err.println( "Unable to inform the server of the VM status!" ) ;
726 e.printStackTrace() ;
729 String[] command = new String[]{VmRunCommand, "-T", "player", "reset",
730 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
734 Process p = Runtime.getRuntime().exec( command ) ;
737 if( p.exitValue() == 0 )
739 System.out.println( "Virtual machine successfully restarted." ) ;
741 if( sendSaveOkVM() == 1 ) { return 1 ; }
747 System.err.println( "Virtual machine not restarted!" ) ;
748 // printProcessError( p.getErrorStream() ) ;
749 ret = printProcessError( p ) ;
756 if( retry >= maxRetryVM )
758 System.err.println( "Unable to start VM!" ) ;
761 System.out.println( "Retrying (" + retry + ") ... " ) ;
762 Thread.sleep( timeRetryVM ) ;
767 } catch( IOException e ) {
768 System.err.println( "Error during execution of restart command: " ) ;
769 e.printStackTrace() ;
770 } catch( InterruptedException e ) {
771 e.printStackTrace() ;
779 public int restartVMAfterCrash()
781 System.out.println( "Restarting VM after a crash ..." ) ;
784 LocalHost.Instance().getServerStub().changeStatus(
785 LocalHost.Instance().getIP(), "undefined" ) ;
786 } catch( RemoteException e ) {
787 System.err.println( "Unable to inform the server of the VM status!" ) ;
788 e.printStackTrace() ;
793 if( machine.deployLastSave() == 0 )
795 if( isRestartedSave )
797 // Using the specific vmx file
798 System.out.print( "Changing VMX file after crash ... " ) ;
800 String[] command = new String[]{ "/bin/cp",
801 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_crash(),
802 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
805 Process p = Runtime.getRuntime().exec( command ) ;
808 if( p.exitValue() == 0 )
810 System.out.println( "Successfully replaced the VMX file." ) ;
812 System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
813 // printProcessError( p.getErrorStream() ) ;
814 printProcessError( p ) ;
818 } catch( IOException e ) {
819 System.err.println( "Error during VMX file replacement: " ) ;
820 e.printStackTrace() ;
821 } catch( InterruptedException e ) {
822 e.printStackTrace() ;
825 // Removing lock files
826 System.out.print( "Removing lock files ... " ) ;
828 command = new String[]{ "/bin/rm", "-rf",
829 working_directory + "/" + machine.getDirectory()
830 + "/" + machine.getVmx_name() + ".lck" } ;
833 Process p = Runtime.getRuntime().exec( command ) ;
836 if( p.exitValue() == 0 )
838 System.out.println( "Successfully deleted lock files." ) ;
840 System.err.println( "Unsuccessful deletion of lock files!" ) ;
841 // printProcessError( p.getErrorStream() ) ;
842 printProcessError( p ) ;
846 } catch( IOException e ) {
847 System.err.println( "Error during lock files deletion: " ) ;
848 e.printStackTrace() ;
849 } catch( InterruptedException e ) {
850 e.printStackTrace() ;
853 /** Retrieving VM assigned IP **/
857 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
858 LocalHost.Instance().getIP() ) ;
859 } catch (RemoteException e) {
860 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
861 e.printStackTrace() ;
865 machine.setIp( vmIP ) ;
867 isRestartedSave = false ;
870 if( startVM( 0 ) == 0 )
872 if( sendSaveOkVM() == 0 )
886 private int sendSaveOkVM()
891 /** Informing the program that it's ok **/
892 System.out.print( "Sending OK signal to the program ... " ) ;
894 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
895 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
896 working_directory + "/" + machine.getDirectory()
897 + "/" + machine.getVmx_name() + " " + ushell +
898 " \"echo ok > /tmp/vm_save_ok\"" ;// + " -noWait " ;
901 FileWriter fw = new FileWriter( new File( working_directory + "/saveOk.sh" ) ) ;
905 } catch( IOException e1 ) {
906 e1.printStackTrace() ;
909 String[] command = new String[]{ ushell, working_directory + "/saveOk.sh"} ;
913 Process p = Runtime.getRuntime().exec( command ) ;
916 if( p.exitValue() == 0 )
918 System.out.println( "Signal successfully sent." ) ;
922 System.err.println( "Signal not sent!" ) ;
923 // printProcessError( p.getErrorStream() ) ;
924 ret = printProcessError( p ) ;
931 if( retry >= maxRetryVM )
933 System.err.println( "Unable to send ok signal to VM!" ) ;
936 System.out.println( "Retrying (" + retry + ") ... " ) ;
937 Thread.sleep( timeRetryVM ) ;
941 } catch( IOException e ) {
942 System.err.println( "Error during ok save signal send command: " ) ;
943 e.printStackTrace() ;
945 } catch( InterruptedException e ) {
946 e.printStackTrace() ;
957 synchronized( saveProcess ){
958 while( saveProcess.getStatus() )
962 } catch( InterruptedException e ) {
963 e.printStackTrace() ;
967 System.out.println( "Saving VM ..." ) ;
968 saveProcess.setStatus( true ) ;
970 machine.setStatus( "saving" ) ;
972 LocalHost.Instance().getServerStub().changeStatus(
973 LocalHost.Instance().getIP(), "saving" ) ;
974 } catch( RemoteException e ) {
975 System.err.println( "Unable to inform the server of the VM status!" ) ;
976 e.printStackTrace() ;
980 String saveName = "" ;
981 boolean error = false ;
982 boolean errorVM = false ;
984 if( suspendVM( 1 ) == 1 ) { error = true ; errorVM = true ; }
988 System.out.println( "Deletion of last nok archive ... " ) ;
990 command = new String[]{ "/bin/rm", "-rf",
991 working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar",
992 working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz", } ;
995 procSave = Runtime.getRuntime().exec( command ) ;
998 if( procSave.exitValue() == 0 )
1000 System.out.println( "Last nok archive successfully deleted." ) ;
1002 System.err.println( "Last nok archive not deleted!" ) ;
1003 // printProcessError( p.getErrorStream() ) ;
1004 printProcessError( procSave ) ;
1008 } catch( IOException e ) {
1009 System.err.println( "Error during nok archive deletion command: " ) ;
1011 e.printStackTrace() ;
1012 } catch( InterruptedException e ) {
1013 e.printStackTrace() ;
1020 System.out.print( "Creation of the archive ... " ) ;
1021 /** Archive creation **/
1022 // command = new String[]{ "/bin/tar", "-cf",
1023 // working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar",
1024 // working_directory + "/" + machine.getDirectory() } ;
1026 command = new String[]{ "/bin/tar", "-cf",
1027 machine.getName() + "_new_" + machine.getComputationId() + ".tar",
1028 machine.getDirectory(), "-C", working_directory } ;
1036 procSave = Runtime.getRuntime().exec( command ) ;
1037 procSave.waitFor() ;
1039 if( procSave.exitValue() == 0 )
1041 System.out.println( "Archive successfully created." ) ;
1043 lastSaveOk = false ;
1046 System.err.println( "Archive not created!" ) ;
1047 // printProcessError( p.getErrorStream() ) ;
1048 printProcessError( procSave ) ;
1052 } catch( IOException e ) {
1053 System.err.println( "Error during archive creation command: " ) ;
1055 e.printStackTrace() ;
1056 } catch( InterruptedException e ) {
1057 e.printStackTrace() ;
1061 /** Compression of the archive **/
1064 System.out.print( "Compression of the archive ... " ) ;
1065 command = new String[]{ "/bin/gzip",
1066 working_directory + "/" + machine.getName()
1067 + "_new_" + machine.getComputationId() + ".tar" } ;
1075 procSave = Runtime.getRuntime().exec( command ) ;
1076 procSave.waitFor() ;
1078 if( procSave.exitValue() == 0 )
1080 System.out.println( "Archive successfully compressed." ) ;
1082 System.err.println( "Archive not compressed!" ) ;
1083 // printProcessError( p.getErrorStream() ) ;
1084 printProcessError( procSave ) ;
1088 } catch( IOException e ) {
1089 System.err.println( "Error during archive compression command: " ) ;
1090 e.printStackTrace() ;
1092 } catch( InterruptedException e ) {
1093 e.printStackTrace() ;
1099 /** Restarting VM **/
1100 if( errorVM || startVM( 0 ) == 1 ) { error = true ; }
1103 /** Sending ok save signal **/
1106 if( sendSaveOkVM() == 1 ) { error = true ; }
1109 // /** Deletion of the tar archive **/
1112 // command = new String[]{ "/bin/rm",
1113 // working_directory + "/" + machine.getName()
1114 // + "_new_" + machine.getComputationId() + ".tar" } ;
1117 // Process p = Runtime.getRuntime().exec( command ) ;
1120 // if( p.exitValue() == 0 )
1122 // System.out.println( "Archive (not compressed) successfully deleted." ) ;
1124 // System.err.println( "Archive (not compressed) not deleted!" ) ;
1125 // printProcessError( p.getErrorStream() ) ;
1129 // } catch( IOException e ) {
1130 // System.err.println( "Error during archive (not compressed) deletion command: " ) ;
1131 // e.printStackTrace() ;
1133 // } catch( InterruptedException e ) {
1134 // e.printStackTrace() ;
1139 saveName = machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
1141 /** Sending save to neighbor **/
1151 ArrayList<ServicesClient> sn = machine.getSaveNeighbors() ;
1153 for( int i = 0 ; i < sn.size() ; i++ )
1155 System.out.print( "Sending save to " + sn.get( i ) + " ... " ) ;
1157 command = new String[]{ "/usr/bin/scp", working_directory + "/" + saveName,
1158 sn.get( i ).getIPHost() + ":"
1159 + sn.get( i ).getWorkingDirectory() } ;
1160 } catch( RemoteException e1 ) {
1161 System.err.println( "Unable to retrieve save neighbor information!" ) ;
1162 e1.printStackTrace() ;
1168 procSave = Runtime.getRuntime().exec( command ) ;
1169 procSave.waitFor() ;
1171 if( procSave.exitValue() == 0 )
1173 System.out.println( "Archive successfully sent." ) ;
1175 System.err.println( "Archive not sent!" ) ;
1176 // printProcessError( p.getErrorStream() ) ;
1177 printProcessError( procSave ) ;
1181 } catch( IOException e ) {
1182 System.err.println( "Error during archive send command: " ) ;
1183 e.printStackTrace() ;
1185 } catch( InterruptedException e ) {
1186 e.printStackTrace() ;
1193 /** Informing the server the save is done **/
1197 LocalHost.Instance().getServerStub().saveOk( LocalHost.Instance().getIP(), saveName ) ;
1198 } catch( RemoteException e ) {
1199 System.err.println( "Problem while informing the server about the save state!" ) ;
1200 e.printStackTrace() ;
1203 synchronized( saveProcess ) {
1204 saveProcess.setStatus( false ) ;
1206 saveProcess.notifyAll() ;
1207 } catch( Exception e ) {}}
1212 synchronized( saveProcess ) {
1213 saveProcess.setStatus( false ) ;
1215 saveProcess.notifyAll() ;
1216 } catch( Exception e ) {}}
1223 public int reloadConfig()
1225 System.out.println( "Reloading configuration ... " ) ;
1231 public void init( String _server_ip, int _server_port, int _client_port, int _dialog_port )
1233 System.out.println( "Initialisation Client ... " ) ;
1234 System.out.println( "IP " + LocalHost.Instance().getIP() ) ;
1236 server_ip = _server_ip ;
1237 server_port = _server_port ;
1238 client_port = _client_port ;
1239 dialog_port = _server_port + 1 ; // _dialog_port ;
1242 saveProcess = new SaveProcess() ;
1244 machine = new VirtualMachine() ;
1246 VmRunCommand = "/usr/bin/vmrun" ;
1247 // VmRunCommandArg = "-T player" ;
1249 // vm_user = "mpi" ;
1250 // vm_user_passwd = "mpi" ;
1251 ushell = "/bin/bash" ;
1252 working_directory = "/localhome/vmware" ;
1254 wait_start = 15000 ;
1255 max_start_try = 10 ;
1257 sema = new Semaphore( 1 ) ;
1258 emergencyStop = false ;
1261 timeRetryVM = 10000 ;
1263 save_interleave = 30 * 60 * 1000 ;
1264 date_last_save = 0 ;
1266 isRestartedSave = false ;
1269 /** Connection to server **/
1271 serverStub = (ServicesServer) Naming.lookup( "rmi://"
1272 + server_ip + ":" + server_port + "/Server" ) ;
1273 } catch (MalformedURLException e) {
1274 e.printStackTrace();
1275 } catch (RemoteException e) {
1276 e.printStackTrace();
1277 } catch (NotBoundException e) {
1278 e.printStackTrace();
1281 if( serverStub == null )
1283 System.err.println( "Unable to connect to server!!" ) ;
1284 System.err.println( "Server IP: " + server_ip + " -- server port: " + server_port ) ;
1289 System.out.println( "Connected to server " + server_ip + " on port " + server_port + "." ) ;
1291 // LocalHost.Instance().setServerIP( server_ip ) ;
1292 LocalHost.Instance().setServerStub( serverStub ) ;
1295 /** Creating the local server **/
1298 /** Starting all threads **/
1302 private void exportObject()
1304 // ServicesClient ref = null ;
1305 Registry reg = null ;
1311 reg = LocateRegistry.getRegistry( client_port ) ;
1313 String tab[] = reg.list() ;
1315 System.out.println( "There is an existing RMI Registry on port " +
1316 client_port + " with " + tab.length + " entries!" ) ;
1317 for( int i = 0 ; i < tab.length ; i++ )
1320 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
1322 System.out.println( "Register successfuly deleted!" ) ;
1324 System.err.println( "Register undeleted !!!" ) ;
1326 } catch( Exception e ) {
1327 e.printStackTrace() ;
1331 } catch( RemoteException e ) {
1335 if ( System.getSecurityManager() == null )
1337 System.setSecurityManager( new SecurityManager() ) ;
1340 LocateRegistry.createRegistry( client_port ) ;
1341 LocateRegistry.getRegistry( client_port ).rebind( "Client", this ) ;
1342 myStub = (ServicesClient) Naming.lookup( "rmi://"
1343 + LocalHost.Instance().getIP() + ":" + client_port
1345 } catch( Exception e ) {
1346 System.err.println( "Error in Client.exportObject() when creating local services:" + e ) ;
1347 System.err.println( "Exit from Client.exportObject" ) ;
1351 LocalHost.Instance().setStub( myStub ) ;
1355 private boolean printProcessError( Process _p )
1357 boolean ret = false ;
1361 System.err.println( "Error: " + _p.exitValue() ) ;
1362 BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
1365 while( (line = br.readLine()) != null )
1367 System.err.println( line ) ;
1368 if( line.contains( "egmentation" ) )
1373 } catch( IOException e ) {
1374 e.printStackTrace() ;
1385 /** Registering on server **/
1388 ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1389 } catch (RemoteException e1) {
1390 e1.printStackTrace();
1396 case 0: System.out.println( "Successfully registered on server." ) ; break ;
1397 case 1: System.err.println( "Problem on server while registreting!" ) ; return 1 ;
1398 case 2: System.out.println( "Already registered on server!" ) ; break ;
1401 /** Retrieving VM assigned IP **/
1402 String vmIP = null ;
1405 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP(
1406 LocalHost.Instance().getIP() ) ;
1407 } catch (RemoteException e) {
1408 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
1409 e.printStackTrace() ;
1413 machine.setIp( vmIP ) ;
1415 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1418 /** Starting alive ping to server **/
1419 pingServer = new PingServer() ;
1420 pingServer.start() ;
1422 /** Starting socket server for VM dialog **/
1423 dialogVmServer = new DialogVMServer() ;
1424 dialogVmServer.start() ;
1430 private class PingServer extends Thread
1432 private boolean run ;
1439 protected void stopPing() { run = false ; }
1447 LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1448 } catch (RemoteException e1) {
1449 e1.printStackTrace();
1453 Thread.sleep( 2000 ) ;
1454 } catch( InterruptedException e ) {
1455 e.printStackTrace() ;
1462 private class DialogVMServer extends Thread
1464 private boolean run ;
1465 private Socket socket ;
1466 private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1473 protected void stopDialogVMServer()
1477 if( serverSocket != null )
1480 serverSocket.close() ;
1481 // socket = serverSocket.accept() ;
1483 for( int i = 0 ; i < dialogs.size() ; i++ )
1485 dialogs.get( i ).stopDialogVM() ;
1488 } catch( IOException e ) {
1489 e.printStackTrace() ;
1499 serverSocket = new ServerSocket( 0 ) ;
1500 dialog_port = serverSocket.getLocalPort() ;
1502 System.out.println( "SocketServer listening on port " + dialog_port ) ;
1503 } catch( IOException e ) {
1504 System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
1505 e.printStackTrace() ;
1513 socket = serverSocket.accept() ;
1515 dialogs.add( new DialogVM( socket ) ) ;
1516 dialogs.get( dialogs.size() - 1 ).start() ;
1517 } catch( IOException e ) {
1518 System.err.println( "Problem with the accept function!" ) ;
1519 e.printStackTrace() ;
1526 private class DialogVM extends Thread
1528 private boolean run ;
1529 private Socket socket ;
1530 private BufferedReader reader ;
1531 private String line ;
1533 DialogVM( Socket _socket ) { run = true ; socket = _socket ; }
1535 protected void stopDialogVM()
1540 reader.close() ; reader = null ;
1541 socket.close() ; socket = null ;
1542 } catch( IOException e ) {
1543 e.printStackTrace() ;
1551 reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
1552 } catch( IOException e ) {
1553 System.err.println( "Unable to open a dialog socket with the VM!" ) ;
1554 e.printStackTrace();
1563 if( reader != null )
1565 line = reader.readLine() ;
1568 /** VM is starting -- retrieving informations **/
1569 if( run && line != null && line.equalsIgnoreCase( "infos" ) )
1571 /* Receiving name */
1572 machine.setName( reader.readLine() ) ;
1575 String ip = reader.readLine() ;
1576 if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1578 System.err.println( "VM IP not well configured!!" ) ;
1582 reader.close() ; reader = null ;
1583 socket.close() ; socket = null ;
1588 /** It's time to do a save **/
1589 if( run && line != null && line.equalsIgnoreCase( "save" ) )
1592 machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1593 } catch( Exception e ) {
1594 System.err.println( "Problem while reading the computation id!" ) ;
1595 e.printStackTrace() ;
1598 if( (System.currentTimeMillis() - date_last_save) > save_interleave )
1600 date_last_save = System.currentTimeMillis() ;
1603 reader.close() ; reader = null ;
1604 socket.close() ; socket = null ;
1608 /* Starting the VM save */
1616 /** Computation is done, we can shutdown the VM **/
1617 if( run && line != null && line.equalsIgnoreCase( "quit" ) )
1620 Thread.sleep( 5000 ) ;
1621 } catch( InterruptedException e ) {
1622 e.printStackTrace() ;
1626 reader.close() ; reader = null ;
1627 socket.close() ; socket = null ;
1634 } catch( IOException e ) {
1635 e.printStackTrace() ;
1646 pingServer.stopPing() ;
1648 dialogVmServer.stopDialogVMServer() ;
1650 // unexportObject ??
1656 public String getIPHost()
1658 return LocalHost.Instance().getIP() ;
1662 public String getName()
1664 return LocalHost.Instance().getName() ;
1669 public void saveOk()
1671 String save_name = machine.getName() + "_" + machine.getComputationId() +
1674 String save_new = machine.getName() + "_new_"
1675 + machine.getComputationId() + ".tar.gz" ;
1677 String[] command = new String[]{ "/bin/mv",
1678 working_directory + "/" + save_new,
1679 working_directory + "/" + save_name } ;
1682 Process p = Runtime.getRuntime().exec( command ) ;
1685 if( p.exitValue() == 0 )
1687 machine.setSave_last( save_name ) ;
1688 System.out.println( "Last save OK" ) ;
1690 System.err.println( "Last save NOK!" ) ;
1691 System.err.println( "Error: " ) ;
1692 // printProcessError( p.getErrorStream() ) ;
1693 printProcessError( p ) ;
1695 } catch( IOException e ) {
1696 System.err.println( "Error during last archive move:" ) ;
1697 e.printStackTrace() ;
1698 } catch( InterruptedException e ) {
1699 e.printStackTrace() ;
1702 // Changing on save neighbors
1703 for( int i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1706 machine.getSaveNeighbors().get( i ).changeSaveName( save_new, save_name, machine.getComputationId() ) ;
1707 } catch( RemoteException e ) {
1709 System.err.println( "Unable to change save name on " + machine.getSaveNeighbors().get( i ).getName() + "!" ) ;
1710 } catch( RemoteException e1 ) {
1711 System.err.println( "Unable to change save name on an unamed save neighbor!" ) ;
1712 e1.printStackTrace() ;
1714 e.printStackTrace() ;
1723 public void changeSaveName( String _n1, String _n2, int _id )
1725 if( _n1 != null && _n1.length() > 0 )
1727 System.out.println( "Changing save name for processus " + _id + " ... " ) ;
1729 String[] command = new String[]{ "/bin/mv",
1730 working_directory + "/" + _n1,
1731 working_directory + "/" + _n2 } ;
1734 Process p = Runtime.getRuntime().exec( command ) ;
1737 if( p.exitValue() == 0 )
1739 System.out.println( "Change save name OK" ) ;
1741 System.err.println( "Change save name NOK!" ) ;
1742 System.err.println( "Error: " ) ;
1743 // printProcessError( p.getErrorStream() ) ;
1744 printProcessError( p ) ;
1746 } catch( IOException e ) {
1747 System.err.println( "Error during save renaming:" ) ;
1748 e.printStackTrace() ;
1749 } catch( InterruptedException e ) {
1750 e.printStackTrace() ;
1757 public void setSavingNeighbor( ServicesClient _sn )
1761 ArrayList<ServicesClient> as = new ArrayList<ServicesClient>() ;
1765 System.out.println( "Save neighbor: " + _sn.getName() ) ;
1766 } catch( RemoteException e ) {
1767 System.err.println( "Unable to retrieve the name of the save neighbor!" ) ;
1768 e.printStackTrace() ;
1771 machine.setSaveNeighbors( as ) ;
1777 public void setSavingNeighbors( ArrayList<ServicesClient> _sn )
1779 if( _sn != null && _sn.size() > 0 )
1781 System.out.print( "Save neighbors: " ) ;
1782 for( int i = 0 ; i < _sn.size() ; i++ )
1785 System.out.print( _sn.get( i ).getName() ) ;
1786 } catch( RemoteException e ) {
1787 System.err.println( "Unable to retrieve the name of a save neighbor!" ) ;
1788 e.printStackTrace() ;
1791 if( i != _sn.size() - 1 )
1793 System.out.print( ", " ) ;
1795 System.out.println( "." ) ;
1799 machine.setSaveNeighbors( _sn ) ;
1805 public void addSavingNeighbor( ServicesClient _sn )
1810 System.out.println( "Adding save neighbor: " + _sn.getName() ) ;
1811 } catch( RemoteException e ) {
1812 System.err.println( "Unable to retrieve the name of a save neighbor!" ) ;
1813 e.printStackTrace() ;
1816 machine.getSaveNeighbors().add( _sn ) ;
1822 public void addSavingNeighbors( ArrayList<ServicesClient> _sn )
1824 if( _sn != null && _sn.size() > 0 )
1826 System.out.print( "Adding save neighbors: " ) ;
1827 for( int i = 0 ; i < _sn.size() ; i++ )
1830 System.out.print( _sn.get( i ).getName() ) ;
1831 } catch( RemoteException e ) {
1832 System.err.println( "Unable to retrieve the name of a save neighbor!" ) ;
1833 e.printStackTrace() ;
1836 if( i != _sn.size() - 1 )
1838 System.out.print( ", " ) ;
1840 System.out.println( "." ) ;
1843 machine.getSaveNeighbors().add( _sn.get( i ) ) ;
1850 public void replaceSavingNeighbor( ServicesClient _old, ServicesClient _new )
1852 System.out.print( "Replacing a save neihgbor ... " ) ;
1853 if( _old != null && _new != null )
1857 for( i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1860 if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old.getIPVM() ) )
1862 machine.getSaveNeighbors().set( i, _new ) ;
1863 System.out.println( "Save neighbor successfully changed." ) ;
1866 } catch( RemoteException e ) {
1867 System.err.println( "Unable to retrieve the IP address of a save neighbor!" ) ;
1868 e.printStackTrace() ;
1872 if( i == machine.getSaveNeighbors().size() )
1874 System.out.println( "I am not concerned by the modification." ) ;
1881 public int retrieveSave( String _saveName )
1883 if( _saveName != null )
1885 if( ! _saveName.equalsIgnoreCase( "none" ) )
1887 machine.setSave_last( _saveName ) ;
1889 System.err.println( "I have no save to retrieve!!" ) ;
1893 // TODO NEIGHBORS !!!!
1894 //System.out.println( "!!!! NEIGHBORS !!!!!" ) ;
1895 boolean ok = false ;
1899 while( ! ok && i < machine.getSaveNeighbors().size() )
1902 System.out.print( "Retrieving a save on " + machine.getSaveNeighbors().get( 0 ).getName() + " ... " ) ;
1903 } catch( RemoteException e1 ) {
1904 System.err.println( "Unable to retrieve the name of a save neighbor!" ) ;
1905 e1.printStackTrace() ;
1908 String command[] = {} ;
1912 command = new String[]{ "/usr/bin/scp",
1913 machine.getSaveNeighbors().get( i ).getIPHost() + ":" +
1914 machine.getSaveNeighbors().get( i ).getWorkingDirectory() + "/" +
1915 machine.getSave_last(),
1916 working_directory } ;
1917 } catch( RemoteException e1 ) {
1918 System.err.println( "Unable to retrieve the name of a save neighbor!" ) ;
1919 e1.printStackTrace() ;
1925 Process p = Runtime.getRuntime().exec( command ) ;
1928 if( p.exitValue() == 0 )
1930 System.out.println( "Archive successfully retrieved." ) ;
1931 isRestartedSave = true ;
1934 System.err.println( "Archive not retrieved!" ) ;
1935 System.err.println( "Error: " ) ;
1936 // printProcessError( p.getErrorStream() ) ;
1937 printProcessError( p ) ;
1940 } catch( IOException e ) {
1941 System.err.println( "Error during archive retrieve command: " ) ;
1942 e.printStackTrace() ;
1944 } catch( InterruptedException e ) {
1945 e.printStackTrace() ;
1956 System.err.println( "Unable to retrieve a save archive from any neighbor!" ) ;
1965 public String getIPVM() throws RemoteException
1967 if( machine != null )
1969 return machine.getIp() ;
1977 public void setIPVM( String _ipVM ) throws RemoteException
1979 if( _ipVM != null && ! _ipVM.isEmpty() )
1981 System.out.println( "The VM IP is now: " + _ipVM ) ;
1982 machine.setIp( _ipVM ) ;
1987 public String getWorkingDirectory()
1989 return working_directory ;
1993 private class SaveProcess
2002 protected boolean getStatus() { return status ; }
2004 protected void setStatus( boolean _b ) { status = _b ; }
2010 /** La programmation est un art, respectons ceux qui la pratiquent !! **/