Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First ~stable~ version.
[hpcvm.git] / src / and / hpcvm / Client.java
1 package and.hpcvm ;
2
3 import java.io.BufferedReader;
4 import java.io.File;
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
19
20 public class Client extends UnicastRemoteObject implements ServicesClient
21 {
22         private static final long serialVersionUID = 1L ;
23         
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 ;
46         
47                 
48         protected Client() throws RemoteException 
49         {
50                 super() ;
51         }
52
53         
54         @Override
55         public int startVM( int _mode ) 
56         {               
57                 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "running" ) )
58                 {
59                         boolean ret = true ;
60                         int retry = 0 ;
61                         
62                         /** Starting VM **/
63                         System.out.print( "Starting VM ... " ) ;
64                         
65                         machine.setStatus( "undefined" ) ;
66                         if( _mode == 0 )
67                         {
68                                 try {
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!" ) ;
73                                         e.printStackTrace() ;
74                                 }
75                         }
76                         
77                         String[] command = new String[]{VmRunCommand, "-T", "player", "start",
78                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name(), "nogui"} ;
79                         
80                         while( ret )
81                         try {
82                                 Process p = Runtime.getRuntime().exec( command ) ;
83                                 p.waitFor() ;
84                                 
85                                 if( p.exitValue() == 0 )
86                                 {
87                                         System.out.println( "Virtual machine successfully started." ) ;
88                                         ret = false ;
89                                 } else {
90                                         System.err.println( "Virtual machine not started!" ) ;
91                                         ret = printProcessError( p ) ;
92                                         
93                                         if( ! ret )
94                                         {
95                                                 return 1 ;
96                                         } else {
97                                                 retry++ ;
98                                                 if( retry >= maxRetryVM )
99                                                 {
100                                                         System.err.println( "Unable to start VM!" ) ;
101                                                         return 1 ;
102                                                 }
103                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
104                                                 Thread.sleep( timeRetryVM ) ;
105                                         }
106                                 }
107                         } catch( IOException e ) {
108                                 System.err.println( "Error during execution of start command: " ) ;
109                                 e.printStackTrace() ;
110                                 return 1 ;
111                         } catch( InterruptedException e ) {
112                                 e.printStackTrace() ;
113                                 return 1 ;
114                         }
115                         
116                         
117                         boolean started = false ;
118                         int count = 1 ;
119                         ret = true ;
120                         retry = 0 ;
121                         
122                         while( ! started )
123                         {
124                                 /** Waiting for VM being started **/
125                                 try {
126                                         Thread.sleep( wait_start ) ;
127                                 } catch( InterruptedException e ) {
128                                         e.printStackTrace() ;
129                                 }
130                                                         
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 " ;
136                                 
137                                 try {
138                                         FileWriter fw = new FileWriter( new File( working_directory + "/testStarted.sh" ) ) ;
139                                         fw.write( cmd2 ) ;
140                                         fw.flush() ;
141                                         fw.close() ;
142                                 } catch( IOException e1 ) {
143                                         e1.printStackTrace() ;
144                                 }
145                                 
146                                 command = new String[]{ ushell, working_directory + "/testStarted.sh"} ;
147
148                                 while( ret )
149                                 try {
150                                         Process p = Runtime.getRuntime().exec( command ) ;
151                                         p.waitFor() ;
152                                         
153                                         if( p.exitValue() == 0 )
154                                         {
155                                                 started = true ;
156 //                                              machine.setStatus( "running" ) ;
157 //                                              if( _mode == 0 )
158 //                                              {
159 //                                                      LocalHost.Instance().getServerStub().changeStatus( 
160 //                                                              LocalHost.Instance().getIP(), "running" ) ;
161 //                                              } 
162                                                 ret = false ;
163                                         } else {
164                                                 System.err.println( "Error while checking if the VM is started!" ) ;
165 //                                              printProcessError( p.getErrorStream() ) ;
166                                                 ret = printProcessError( p ) ;
167                                                 
168                                                 if( ! ret )
169                                                 {
170                                                         return 1 ;
171                                                 } else {
172                                                         retry++ ;
173                                                         if( retry >= maxRetryVM )
174                                                         {
175                                                                 System.err.println( "Unable to check VM!" ) ;
176                                                                 return 1 ;
177                                                         }
178                                                         System.out.println( "Retrying (" + retry + ") ... " ) ;
179                                                         Thread.sleep( timeRetryVM ) ;
180                                                 }
181 //                                              wait_start = wait_start / 2 ;
182                                                 count++ ;
183                                         }
184                                 } catch( IOException e ) {
185                                         e.printStackTrace() ;
186                                 } catch( InterruptedException e ) {
187                                         e.printStackTrace() ;
188                                 }
189                                 
190                                 if( count == max_start_try && ! started )
191                                 {
192                                         System.err.println( "Virtual machine not responding!!" ) ;
193                                         
194                                         try {
195                                                 LocalHost.Instance().getServerStub().changeStatus( 
196                                                                 LocalHost.Instance().getIP(), "undefined" ) ;
197                                         } catch( RemoteException e ) {
198                                                 e.printStackTrace() ;
199                                         }
200                                                                                 
201                                         stopVM() ;
202                                         
203                                         return 1 ;
204                                 } else {
205                                         try {
206                                                 Thread.sleep( 3000 ) ;
207                                         } catch( InterruptedException e ) {
208                                                 e.printStackTrace() ;
209                                         }
210                                 }
211                         }
212                         
213                         /** Sending the host ip **/
214                         
215                         System.out.print( "Sending host IP to VM ... " ) ;
216                         
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\"" ;
223                         
224                         try {
225                                 FileWriter fw = new FileWriter( new File( working_directory + "/sendHostIP.sh" ) ) ;
226                                 fw.write( cmd2 ) ;
227                                 fw.flush() ;
228                                 fw.close() ;
229                         } catch( IOException e1 ) {
230                                 e1.printStackTrace() ;
231                         }
232                         
233                         command = new String[]{ ushell, working_directory + "/sendHostIP.sh"} ;
234                         
235                         ret = true ;
236                         retry = 0 ;
237                         
238                         while( ret )
239                         try {
240                                 Process p = Runtime.getRuntime().exec( command ) ;
241                                 p.waitFor() ;
242                                 
243                                 if( p.exitValue() == 0 )
244                                 {
245                                         System.out.println( "VM received the host IP." ) ;
246                                         ret = false ;
247                                 } else {
248                                         System.err.println( "VM did not received the host IP!" ) ;
249 //                                      printProcessError( p.getErrorStream() ) ;
250                                         ret = printProcessError( p ) ;
251                                         
252                                         if( ! ret )
253                                         {
254                                                 return 1 ;
255                                         } else {
256                                                 retry++ ;
257                                                 if( retry >= maxRetryVM )
258                                                 {
259                                                         System.err.println( "Unable to send information to VM!" ) ;
260                                         
261                                                         stopVM() ;
262                                                         
263                                                         return 1 ;
264                                                 }
265                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
266                                                 Thread.sleep( timeRetryVM ) ;
267                                         }
268                                 }
269                         } catch( IOException e ) {
270                                 System.err.println( "Error during execution of runScriptInGuest command: " ) ;
271                                 e.printStackTrace() ;
272                         } catch( InterruptedException e) {
273                                 e.printStackTrace() ;
274                         }
275                                         
276                         /** Sending the vm ip **/
277                         
278                         System.out.print( "Sending its IP to VM ... " ) ;
279                         
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\"" ;
286                         
287                         try {
288                                 FileWriter fw = new FileWriter( new File( working_directory + "/sendVmIP.sh" ) ) ;
289                                 fw.write( cmd2 ) ;
290                                 fw.flush() ;
291                                 fw.close() ;
292                         } catch( IOException e1 ) {
293                                 e1.printStackTrace() ;
294                         }
295                         
296                         command = new String[]{ ushell, working_directory + "/sendVmIP.sh"} ;
297                         
298                         ret = true ;
299                         retry = 0 ;
300                         
301                         while( ret )
302                         try {
303                                 Process p = Runtime.getRuntime().exec( command ) ;
304                                 p.waitFor() ;
305                                 
306                                 if( p.exitValue() == 0 )
307                                 {
308                                         System.out.println( "VM received its assigned IP." ) ;
309 //                                      return 0 ;
310                                         ret = false ;
311                                         machine.setStatus( "running" ) ;
312                                 } else {
313                                         System.err.println( "VM did not received its assigned IP!" ) ;
314 //                                      printProcessError( p.getErrorStream() ) ;
315                                         ret = printProcessError( p ) ;
316                                         
317                                         if( ! ret )
318                                         {
319                                                 return 1 ;
320                                         } else {
321                                                 retry++ ;
322                                                 if( retry >= maxRetryVM )
323                                                 {
324                                                         System.err.println( "Unable to send information to VM!" ) ;
325                                                         
326                                                         stopVM() ;
327                                                         
328                                                         return 1 ;
329                                                 }
330                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
331                                                 Thread.sleep( timeRetryVM ) ;
332                                         }
333                                 }
334                         } catch( IOException e ) {
335                                 System.err.println( "Error during execution of runScriptInGuest command: " ) ;
336                                 e.printStackTrace() ;
337                         } catch( InterruptedException e ) {
338                                 e.printStackTrace() ;
339                         }
340                         
341                         if( _mode == 0 )
342                         {
343                                 try {
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!" ) ;
348                                         e.printStackTrace();
349                                 }
350                         }
351                         
352                         return 0 ;
353                 }
354                 
355                 return 1 ;
356         }
357
358         @Override
359         public int stopVM() 
360         {               
361                 if( machine != null && machine.getStatus().equalsIgnoreCase( "stopped" ) )
362                 {
363                         return 0 ;
364                 }
365                 
366                 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "stopped" ) )
367                 {
368                         System.out.print( "Stopping VM ... " ) ;
369                         
370                         boolean ret = true ;
371                         int retry = 0 ;
372                         
373                         machine.setStatus( "undefined" ) ;
374                         try {
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() ;
380                         }
381                         
382                         String[] command = new String[]{VmRunCommand, "-T", "player", "stop",
383                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
384                         
385                         while( ret )
386                         try {
387                                 Process p = Runtime.getRuntime().exec( command ) ;
388                                 p.waitFor() ;
389                                 
390                                 if( p.exitValue() == 0 )
391                                 {
392                                         System.out.println( "Virtual machine successfully stopped." ) ;
393                                         machine.setStatus( "stopped" ) ;
394                                         ret = false ;
395                                 } else {
396                                         System.err.println( "Virtual machine not stopped!" ) ;
397 //                                      printProcessError( p.getErrorStream() ) ;
398                                         ret = printProcessError( p ) ;
399                                         
400                                         if( ! ret )
401                                         {
402                                                 return 1 ;
403                                         } else {
404                                                 retry++ ;
405                                                 if( retry >= maxRetryVM )
406                                                 {
407                                                         System.err.println( "Unable to stop VM!" ) ;
408                                                         return 1 ;
409                                                 }
410                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
411                                                 Thread.sleep( timeRetryVM ) ;
412                                         }
413                                         
414 //                                      return 1 ;
415                                 }
416                         } catch( IOException e ) {
417                                 System.err.println( "Error during execution of stop command: " ) ;
418                                 e.printStackTrace() ;
419                         } catch( InterruptedException e ) {
420                                 e.printStackTrace() ;
421                         }
422                         
423                         machine.setStatus( "stopped" ) ;
424                         try {
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() ;
430                         }
431                         
432                         if( ! isRestartedSave )
433                         {
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() } ;
438                                 
439                                         try {
440                                                 Process p = Runtime.getRuntime().exec( command ) ;
441                                                 p.waitFor() ;
442                         
443                                                 if( p.exitValue() == 0 )
444                                                 {
445                                                         System.out.println( "Successfully replaced the VMX file." ) ;
446                                                         return 0 ;
447                                                 } else {
448                                                         System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
449 //                                                      printProcessError( p.getErrorStream() ) ;
450                                                         printProcessError( p ) ;
451                                                 
452                                                         return 1 ;
453                                                 }
454                                         } catch( IOException e ) {
455                                                 System.err.println( "Error during VMX file replacement: " ) ;
456                                                 e.printStackTrace() ;
457                                         } catch( InterruptedException e ) {
458                                                 e.printStackTrace() ;
459                                         }
460                         } else {
461                                 return 0 ;
462                         }
463                 }
464                 
465                 return 1 ;
466         }
467
468         @Override
469         public int suspendVM( int _mode ) 
470         {               
471                 if( machine != null && machine.getStatus().equalsIgnoreCase( "suspended" ) )
472                 {
473                         return 0 ;
474                 }
475                 
476                 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "suspended" ) )
477                 {
478                         System.out.print( "Suspending VM ... " ) ;
479                         
480                         boolean ret = true ;
481                         int retry = 0 ;
482                         
483                         machine.setStatus( "undefined" ) ;
484                         if( _mode == 0 )
485                         {
486                                 try {
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() ;
492                                 }
493                         }
494                         
495                         String[] command = new String[]{VmRunCommand, "-T", "player", "suspend",
496                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
497                         
498                         while( ret )
499                         try {
500                                 Process p = Runtime.getRuntime().exec( command ) ;
501                                 p.waitFor() ;
502                                 if( p.exitValue() == 0 )
503                                 {
504                                         System.out.println( "Virtual machine successfully suspended." ) ;
505                                         machine.setStatus( "suspended" ) ;
506                                         ret = false ;
507                                 } else {
508                                         System.err.println( "Virtual machine not suspended!" ) ;
509 //                                      printProcessError( p.getErrorStream() ) ;
510                                         ret = printProcessError( p ) ;
511                                         
512                                         if( ! ret )
513                                         {
514                                                 return 1 ;
515                                         } else {
516                                                 retry++ ;
517                                                 if( retry >= maxRetryVM )
518                                                 {
519                                                         System.err.println( "Unable to suspend VM!" ) ;
520                                                         return 1 ;
521                                                 }
522                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
523                                                 Thread.sleep( timeRetryVM ) ;
524                                         }
525                                         
526 //                                      return 1 ;
527                                 }
528                         } catch( IOException e ) {
529                                 System.err.println( "Error during execution of suspend command: " ) ;
530                                 e.printStackTrace() ;
531                         } catch( InterruptedException e ) {
532                                 e.printStackTrace() ;
533                         }
534                         
535                         machine.setStatus( "suspended" ) ;
536                         if( _mode == 0 )
537                         {
538                                 try {
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() ;
544                                 }
545                         }
546                         
547                         return 0 ;
548                 }
549
550                 return 1 ;
551         }
552
553         @Override
554         public int restartVM() 
555         {               
556                 if( machine != null )
557                 {
558                         System.out.print( "Restarting VM ... " ) ;
559                         
560                         boolean ret = true ;
561                         int retry = 0 ;
562                         
563                         try {
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() ;
569                         }
570                         
571                         String[] command = new String[]{VmRunCommand, "-T", "player", "reset",
572                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
573                         
574                         while( ret )
575                         try {
576                                 Process p = Runtime.getRuntime().exec( command ) ;
577                                 p.waitFor() ;
578                                 
579                                 if( p.exitValue() == 0 )
580                                 {
581                                         System.out.println( "Virtual machine successfully restarted." ) ;
582
583                                         if( sendSaveOkVM() == 1 ) { return 1 ; }
584                                         
585                                         ret = false ;
586                                         return 0 ;
587
588                                 } else {
589                                         System.err.println( "Virtual machine not restarted!" ) ;
590 //                                      printProcessError( p.getErrorStream() ) ;
591                                         ret = printProcessError( p ) ;
592                                         
593                                         if( ! ret )
594                                         {
595                                                 return 1 ;
596                                         } else {
597                                                 retry++ ;
598                                                 if( retry >= maxRetryVM )
599                                                 {
600                                                         System.err.println( "Unable to start VM!" ) ;
601                                                         return 1 ;
602                                                 }
603                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
604                                                 Thread.sleep( timeRetryVM ) ;
605                                         }
606                                         
607 //                                      return 1 ;
608                                 }
609                         } catch( IOException e ) {
610                                 System.err.println( "Error during execution of restart command: " ) ;
611                                 e.printStackTrace() ;
612                         } catch( InterruptedException e ) {
613                                 e.printStackTrace() ;
614                         }
615                 }
616
617                 return 1 ;
618         }
619
620         @Override
621         public int restartVMAfterCrash() 
622         {
623                 System.out.println( "Restarting VM after a crash ..." ) ;
624                 
625                 try {
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() ;
631                 }
632                 
633                 if( stopVM() == 0 )
634                 {
635                         if( machine.deployLastSave() == 0 )
636                         {
637                                 if( isRestartedSave )
638                                 {
639                                         // Using the specific vmx file
640                                         System.out.print( "Changing VMX file after crash ... " ) ;
641                                 
642                                         String[] command = new String[]{ "/bin/cp", 
643                                                 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name_crash(),
644                                                 working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name() } ;
645                                         
646                                         try {
647                                                 Process p = Runtime.getRuntime().exec( command ) ;
648                                                 p.waitFor() ;
649                                 
650                                                 if( p.exitValue() == 0 )
651                                                 {
652                                                         System.out.println( "Successfully replaced the VMX file." ) ;
653                                                 } else {
654                                                         System.err.println( "Unsuccessful replacement of the VMX file!" ) ;
655 //                                                      printProcessError( p.getErrorStream() ) ;
656                                                         printProcessError( p ) ;
657                                                         
658                                                         return 1 ;
659                                                 }
660                                         } catch( IOException e ) {
661                                                 System.err.println( "Error during VMX file replacement: " ) ;
662                                                 e.printStackTrace() ;
663                                         } catch( InterruptedException e ) {
664                                                 e.printStackTrace() ;
665                                         }
666                                         
667                                         // Removing lock files
668                                         System.out.print( "Removing lock files ... " ) ;
669                                 
670                                         command = new String[]{ "/bin/rm", "-rf", 
671                                                 working_directory + "/" + machine.getDirectory() 
672                                                 + "/" + machine.getVmx_name() + ".lck" } ;
673                                         
674                                         try {
675                                                 Process p = Runtime.getRuntime().exec( command ) ;
676                                                 p.waitFor() ;
677                                 
678                                                 if( p.exitValue() == 0 )
679                                                 {
680                                                         System.out.println( "Successfully deleted lock files." ) ;
681                                                 } else {
682                                                         System.err.println( "Unsuccessful deletion of lock files!" ) ;
683 //                                                      printProcessError( p.getErrorStream() ) ;
684                                                         printProcessError( p ) ;
685                                                         
686                                                         return 1 ;
687                                                 }
688                                         } catch( IOException e ) {
689                                                 System.err.println( "Error during lock files deletion: " ) ;
690                                                 e.printStackTrace() ;
691                                         } catch( InterruptedException e ) {
692                                                 e.printStackTrace() ;
693                                         }
694                                 
695                                         /** Retrieving VM assigned IP **/
696                                         String vmIP = null ;
697                                 
698                                         try {
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() ;
704                                                 return 1 ;
705                                         }
706                                 
707                                         machine.setIp( vmIP ) ;
708                                         
709                                         isRestartedSave = false ;
710                                 }
711                                 
712                                 if( startVM( 0 ) == 0 ) 
713                                 {
714                                         if( sendSaveOkVM() == 0 )
715                                         {
716                                                 return 0 ;
717                                         }
718                                 } else {
719                                         stopVM() ;
720                                 }
721                         }
722                 }
723                 
724                 return 1 ;
725         }
726         
727         
728         private int sendSaveOkVM()
729         {
730                 boolean ret = true ;
731                 int retry = 0 ;
732                 
733                 /** Informing the program that it's ok **/
734                 System.out.print( "Sending OK signal to the program ... " ) ;
735                         
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 " ;
741                 
742                 try {
743                         FileWriter fw = new FileWriter( new File( working_directory + "/saveOk.sh" ) ) ;
744                         fw.write( cmd2 ) ;
745                         fw.flush() ;
746                         fw.close() ;
747                 } catch( IOException e1 ) {
748                         e1.printStackTrace() ;
749                 }
750                         
751                 String[] command = new String[]{ ushell, working_directory + "/saveOk.sh"} ;
752                 
753                 while( ret )
754                 try {
755                         Process p = Runtime.getRuntime().exec( command ) ;
756                         p.waitFor() ;
757                         
758                         if( p.exitValue() == 0 )
759                         {
760                                 System.out.println( "Signal successfully sent." ) ;
761                                 ret = false ;
762                                 return 0 ;
763                         } else {
764                                 System.err.println( "Signal not sent!" ) ;
765 //                              printProcessError( p.getErrorStream() ) ;
766                                 ret = printProcessError( p ) ;
767                                 
768                                 if( ! ret )
769                                 {
770                                         return 1 ;
771                                 } else {
772                                         retry++ ;
773                                         if( retry >= maxRetryVM )
774                                         {
775                                                 System.err.println( "Unable to send ok signal to VM!" ) ;
776                                                 return 1 ;
777                                         }
778                                         System.out.println( "Retrying (" + retry + ") ... " ) ;
779                                         Thread.sleep( timeRetryVM ) ;
780                                 }
781 //                              return 1 ;
782                         }
783                 } catch( IOException e ) {
784                         System.err.println( "Error during ok save signal send command: " ) ;
785                         e.printStackTrace() ;
786                         return 1 ;
787                 } catch( InterruptedException e ) {
788                         e.printStackTrace() ;
789                         return 1 ;
790                 }
791                 
792                 return 1 ;
793         }
794
795         
796         @Override
797         public int saveVM() 
798         {
799                 synchronized( saveProcess ){
800                 while( saveProcess.getStatus() )
801                 {
802                         try {
803                                 saveProcess.wait() ;
804                         } catch( InterruptedException e ) {
805                                 e.printStackTrace() ;
806                         }
807                 }}
808                 
809                 System.out.println( "Saving VM ..." ) ;
810                 saveProcess.setStatus( true ) ;
811                 
812                 machine.setStatus( "saving" ) ;
813                 try {
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() ;
819                 }
820                 
821                 String[] command ;
822                 String saveName = "" ;
823                 boolean error = false ;
824                 boolean errorVM = false ;
825                 
826                 if( suspendVM( 1 ) == 1 ) { error = true ; errorVM = true ; }
827                 
828                 if( ! error )
829                 {
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() } ;
835                                         
836                         try {
837                                 Process p = Runtime.getRuntime().exec( command ) ;
838                                 p.waitFor() ;
839                                 
840                                 if( p.exitValue() == 0 )
841                                 {
842                                         System.out.println( "Archive successfully created." ) ;
843                                 } else {
844                                         System.err.println( "Archive not created!" ) ;
845 //                                      printProcessError( p.getErrorStream() ) ;
846                                         printProcessError( p ) ;
847                                         
848                                         error = true ;
849                                 }
850                         } catch( IOException e ) {
851                                 System.err.println( "Error during archive creation command: " ) ;
852                                 error = true ;
853                                 e.printStackTrace() ;
854                         } catch( InterruptedException e ) {
855                                 e.printStackTrace() ;
856                                 error = true ;
857                         }
858                 
859                         /** Compression of the archive **/
860                         if( ! error )
861                         {
862                                 System.out.print( "Compression of the archive ... " ) ;
863                                 command = new String[]{ "/bin/gzip", 
864                                                 working_directory + "/" + machine.getName() 
865                                                 + "_new_" + machine.getComputationId() + ".tar" } ;
866                                 
867                                 try {
868                                         Process p = Runtime.getRuntime().exec( command ) ;
869                                         p.waitFor() ;
870                         
871                                         if( p.exitValue() == 0 )
872                                         {
873                                                 System.out.println( "Archive successfully compressed." ) ;
874                                         } else {
875                                                 System.err.println( "Archive not compressed!" ) ;
876 //                                              printProcessError( p.getErrorStream() ) ;
877                                                 printProcessError( p ) ;
878                                                 
879                                                 error = true ;
880                                         }
881                                 } catch( IOException e ) {
882                                         System.err.println( "Error during archive compression command: " ) ;
883                                         e.printStackTrace() ;
884                                         error = true ;
885                                 } catch( InterruptedException e ) {
886                                         e.printStackTrace() ;
887                                         error = true ;
888                                 }
889                         }
890                         
891                         
892                         /** Restarting VM **/
893                         if( errorVM || startVM( 0 ) == 1 ) { error = true ; }
894                         
895                         
896                         /** Sending ok save signal **/
897                         if( ! error )
898                         {
899                                 if( sendSaveOkVM() == 1 ) { error = true ; }
900                         }               
901                         
902 //                      /** Deletion of the tar archive **/
903 //                      if( ! error )
904 //                      {
905 //                              command = new String[]{ "/bin/rm", 
906 //                                              working_directory + "/" + machine.getName() 
907 //                                              + "_new_" + machine.getComputationId() + ".tar" } ;
908 //                              
909 //                              try {
910 //                                      Process p = Runtime.getRuntime().exec( command ) ;
911 //                                      p.waitFor() ;
912 //                      
913 //                                      if( p.exitValue() == 0 )
914 //                                      {
915 //                                              System.out.println( "Archive (not compressed) successfully deleted." ) ;
916 //                                      } else {
917 //                                              System.err.println( "Archive (not compressed) not deleted!" ) ;
918 //                                              printProcessError( p.getErrorStream() ) ;
919 //                                              
920 //                                              error = true ;
921 //                                      }
922 //                              } catch( IOException e ) {
923 //                                      System.err.println( "Error during archive (not compressed) deletion command: " ) ;
924 //                                      e.printStackTrace() ;
925 //                                      error = true ;
926 //                              } catch( InterruptedException e ) {
927 //                                      e.printStackTrace() ;
928 //                                      error = true ;
929 //                              }
930 //                      }       
931                 
932                         saveName = working_directory + "/" + machine.getName() + "_new_" 
933                                                   + machine.getComputationId() + ".tar.gz" ;    
934                 
935                         /** Sending save to neighbor **/
936                         if( ! error )
937                         {
938                                 ArrayList<String> sn = machine.getSaveNeighbors() ;
939                                 
940                                 for( int i = 0 ; i < sn.size() ; i++ )
941                                 {
942                                         System.out.print( "Sending save to " + sn.get( i ) + " ... " ) ;
943                                         command = new String[]{ "/usr/bin/scp", saveName,
944                                                         sn.get( i ) + ":" 
945                                                         + working_directory } ;
946                                 
947                                         try {
948                                                 Process p = Runtime.getRuntime().exec( command ) ;
949                                                 p.waitFor() ;
950                         
951                                                 if( p.exitValue() == 0 )
952                                                 {
953                                                         System.out.println( "Archive successfully sent." ) ;
954                                                 } else {
955                                                         System.err.println( "Archive not sent!" ) ;
956 //                                                      printProcessError( p.getErrorStream() ) ;
957                                                         printProcessError( p ) ;
958                                                 
959                                                         error = true ;
960                                                 }
961                                         } catch( IOException e ) {
962                                                 System.err.println( "Error during archive send command: " ) ;
963                                                 e.printStackTrace() ;
964                                                 error = true ;
965                                         } catch( InterruptedException e ) {
966                                                 e.printStackTrace() ;
967                                                 error = true ;
968                                         }
969                                 }
970                         }
971                 }
972                 
973                 /** Informing the server the save is done **/
974                 if( ! error )
975                 {
976                         try {
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() ;
981                         }
982                         
983                         synchronized( saveProcess ) {
984                         saveProcess.setStatus( false ) ;
985                         try {
986                                 saveProcess.notifyAll() ;
987                         } catch( Exception e ) {}}
988                         
989                         return 0 ;
990                 }
991                 
992                 synchronized( saveProcess ) {
993                 saveProcess.setStatus( false ) ;
994                 try {
995                         saveProcess.notifyAll() ;
996                 } catch( Exception e ) {}}
997                 
998                 return 1 ;
999         }
1000
1001         
1002         @Override
1003         public int reloadConfig() 
1004         {
1005                 System.out.println( "Reloading configuration ... " ) ;
1006                 
1007                 // TODO !!!
1008                 return 0 ;
1009         }
1010
1011         public void init( String _server_ip, int _server_port, int _client_port, int _dialog_port ) 
1012         {
1013                 System.out.println( "Initialisation Client ... " ) ;
1014                 System.out.println( "IP " + LocalHost.Instance().getIP() ) ;
1015                 
1016                 server_ip = _server_ip ;
1017                 server_port = _server_port ;
1018                 client_port = _client_port ;
1019                 dialog_port = _server_port + 1 ; // _dialog_port ;
1020                 
1021                 serverStub = null ;
1022                 saveProcess = new SaveProcess() ;
1023                 
1024                 machine = new VirtualMachine() ;
1025                 
1026                 VmRunCommand = "/usr/bin/vmrun" ;
1027 //              VmRunCommandArg = "-T player" ;
1028                 
1029 //              vm_user = "mpi" ;
1030 //              vm_user_passwd = "mpi" ;
1031                 ushell = "/bin/bash" ;
1032                 working_directory = "/localhome/vmware" ;
1033                 
1034                 wait_start = 15000 ;
1035                 max_start_try = 10 ;
1036                 
1037                 maxRetryVM = 10 ;
1038                 timeRetryVM = 10000 ;
1039                 
1040                 save_interleave = 30 * 60 * 1000 ;
1041                 date_last_save = 0 ;
1042                 
1043                 isRestartedSave = false ;
1044                 
1045                 /** Connection to server **/
1046                 try {
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();
1055                 }
1056                 
1057                 if( serverStub == null )
1058                 {
1059                         System.err.println( "Unable to connect to server!!" ) ;
1060                         System.err.println( "Server IP: " + server_ip + " -- server port: " + server_port ) ;
1061                         
1062                         System.exit( 1 ) ;
1063                 }
1064                 
1065                 System.out.println( "Connected to server " + server_ip + " on port " + server_port + "." ) ;
1066                 
1067 //              LocalHost.Instance().setServerIP( server_ip ) ;
1068                 LocalHost.Instance().setServerStub( serverStub ) ;
1069                 
1070                 
1071                 /** Creating the local server **/
1072                 exportObject() ;
1073
1074                 /** Starting all threads **/
1075                 start() ;
1076         }
1077         
1078         private void exportObject() 
1079         {
1080 //              ServicesClient ref = null ;
1081                 Registry reg = null ;
1082                 
1083                 try 
1084                 {       
1085                         while( true )
1086                         {
1087                                 reg = LocateRegistry.getRegistry( client_port ) ;
1088
1089                                 String tab[] = reg.list() ;
1090                         
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++ )
1094                                 {
1095                                         try {
1096                                                 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
1097                                                 {
1098                                                         System.out.println( "Register successfuly deleted!" ) ;
1099                                                 } else {
1100                                                         System.err.println( "Register undeleted !!!" ) ;
1101                                                 }
1102                                         } catch( Exception e ) {
1103                                                 e.printStackTrace() ;
1104                                         }
1105                                 } 
1106                         }
1107                 } catch( RemoteException e ) {
1108                 }                       
1109                 
1110                 try {
1111                         if ( System.getSecurityManager() == null ) 
1112                         {
1113                     System.setSecurityManager( new SecurityManager() ) ;
1114                 }
1115                         
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
1120                                         + "/Client" ) ;
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" ) ;
1124                         System.exit( 1 ) ;
1125                 }
1126                 
1127                 LocalHost.Instance().setStub( myStub ) ;
1128         }
1129         
1130         
1131         private boolean printProcessError( Process _p )
1132         {
1133                 boolean ret = false ;
1134                 
1135                 if( _p != null )
1136                 {
1137                         System.err.println( "Error: " + _p.exitValue() ) ;
1138                         BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
1139                         String line ;
1140                         try {
1141                                 while( (line = br.readLine()) != null )
1142                                 {
1143                                         System.err.println( line ) ;
1144                                         if( line.contains( "egmentation" ) ) 
1145                                         {
1146                                                 ret = true ;
1147                                         }
1148                                 }
1149                         } catch( IOException e ) {
1150                                 e.printStackTrace() ;
1151                         }
1152                 }
1153                 
1154                 return ret ;
1155         }
1156         
1157
1158         @Override
1159         public int start() 
1160         {
1161                 /** Registering on server **/
1162                 Integer ret = 0 ;
1163                 try {
1164                         ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1165                 } catch (RemoteException e1) {  
1166                         e1.printStackTrace();
1167                         return 1 ;
1168                 }
1169                 
1170                 switch( ret )
1171                 {
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 ;                 
1175                 }
1176                 
1177                 /** Retrieving VM assigned IP **/
1178                 String vmIP = null ;
1179                 
1180                 try {
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() ;
1186                         return 1 ;
1187                 }
1188                 
1189                 machine.setIp( vmIP ) ;
1190                 
1191                 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1192
1193                 
1194                 /** Starting alive ping to server **/
1195                 pingServer = new PingServer() ;
1196                 pingServer.start() ;
1197
1198                 /** Starting socket server for VM dialog **/
1199                 dialogVmServer = new DialogVMServer() ;
1200                 dialogVmServer.start() ;
1201                 
1202                 return 0 ;
1203         }
1204         
1205         
1206         private class PingServer extends Thread
1207         {
1208                 private boolean run ;
1209                 
1210                 PingServer()
1211                 {
1212                         run = true ;
1213                 }
1214                 
1215                 protected void stopPing() { run = false ; }
1216                 
1217                 @Override
1218                 public void run() 
1219                 {
1220                         while( run )
1221                         {
1222                                 try {
1223                                         LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1224                                 } catch (RemoteException e1) {
1225                                         e1.printStackTrace();
1226                                 }
1227                                 
1228                                 try {
1229                                         Thread.sleep( 2000 ) ;
1230                                 } catch( InterruptedException e ) {
1231                                         e.printStackTrace() ;
1232                                 }
1233                         }
1234                 }
1235         }
1236         
1237         
1238         private class DialogVMServer extends Thread
1239         {
1240                 private boolean run ;
1241                 private Socket socket ;
1242                 private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1243                 
1244                 DialogVMServer()
1245                 {
1246                         run = true ;
1247                 }
1248                 
1249                 protected void stopDialogVMServer() 
1250                 { 
1251                         run = false ;
1252                         
1253                         if( serverSocket != null )
1254                         {
1255                                 try {
1256                                         serverSocket.close() ;
1257 //                                      socket = serverSocket.accept() ;
1258                                         
1259                                         for( int i = 0 ; i < dialogs.size() ; i++ )
1260                                         {
1261                                                 dialogs.get( i ).stopDialogVM() ;
1262                                         }
1263                                         
1264                                 } catch( IOException e ) {
1265                                         e.printStackTrace() ;
1266                                 }
1267                         }                       
1268                 }
1269                 
1270                 
1271                 @Override
1272                 public void run() 
1273                 {
1274                         try {
1275                                 serverSocket = new ServerSocket( 0 ) ;
1276                                 dialog_port = serverSocket.getLocalPort() ;
1277                                 
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() ;
1282                                 
1283                                 run = false ;
1284                         }       
1285                         
1286                         while( run )
1287                         {                               
1288                                 try {
1289                                         socket = serverSocket.accept() ;
1290                                         
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() ;
1296                                 }
1297                         }
1298                 }
1299         }
1300
1301         
1302         private class DialogVM extends Thread
1303         {
1304                 private boolean run ;
1305                 private Socket socket ;
1306                 private BufferedReader reader ;
1307                 private String line ;
1308                 
1309                 DialogVM( Socket _socket ) { run = true ; socket = _socket ; }
1310         
1311                 protected void stopDialogVM()
1312                 {
1313                         run = false ;
1314
1315                         try {
1316                                 reader.close() ; reader = null ;
1317                                 socket.close() ; socket = null ;
1318                         } catch( IOException e ) {
1319                                 e.printStackTrace() ;
1320                         }
1321                 }
1322         
1323                 @Override
1324                 public void run() 
1325                 {
1326                         try {
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();
1331                                 stopDialogVM() ;
1332                         }
1333                         
1334                         while( run )
1335                         {
1336                                 try {
1337                                         line = null ;
1338                                         
1339                                         if( reader != null )
1340                                         {
1341                                                 line = reader.readLine() ;
1342                                         }
1343                                         
1344                                         /** VM is starting -- retrieving informations **/
1345                                         if( run && line != null &&  line.equalsIgnoreCase( "infos" ) )
1346                                         {
1347                                                 /* Receiving name */
1348                                                 machine.setName( reader.readLine() ) ;
1349                                                 
1350                                                 /* Receiving IP */
1351                                                 String ip = reader.readLine() ;
1352                                                 if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1353                                                 {
1354                                                         System.err.println( "VM IP not well configured!!" ) ;
1355                                                 }
1356                                                 
1357                                                 /* Close streams */
1358                                                 reader.close() ; reader = null ;
1359                                                 socket.close() ; socket = null ;
1360                                                 
1361                                                 run = false ;
1362                                         }
1363                                         
1364                                         /** It's time to do a save **/
1365                                         if( run && line != null &&  line.equalsIgnoreCase( "save" ) )
1366                                         {
1367                                                 try {
1368                                                         machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1369                                                 } catch( Exception e ) {
1370                                                         System.err.println( "Problem while reading the computation id!" ) ;
1371                                                         e.printStackTrace() ;
1372                                                 }
1373                                                 
1374                                                 if( (System.currentTimeMillis() - date_last_save) > save_interleave )
1375                                                 {
1376                                                         date_last_save = System.currentTimeMillis() ; 
1377                                                         
1378                                                         /* Close streams */
1379                                                         reader.close() ; reader = null ;
1380                                                         socket.close() ; socket = null ;
1381                                                         
1382                                                         run = false ;
1383                                                 
1384                                                         /* Starting the VM save */
1385                                                         saveVM() ;
1386                                                 } else {
1387                                                         sendSaveOkVM() ;
1388                                                 }
1389                                         }
1390                                         
1391                                         
1392                                         /** Computation is done, we can shutdown the VM **/
1393                                         if( run && line != null &&  line.equalsIgnoreCase( "quit" ) )
1394                                         {
1395                                                 try {
1396                                                         Thread.sleep( 5000 ) ;
1397                                                 } catch( InterruptedException e ) {
1398                                                         e.printStackTrace() ;
1399                                                 }
1400                                                 
1401                                                 /* Close streams */
1402                                                 reader.close() ; reader = null ;
1403                                                 socket.close() ; socket = null ;
1404                                                 
1405                                                 run = false ;
1406                                                 
1407                                                 stopVM() ;
1408                                         }
1409                                         
1410                                 } catch( IOException e ) {
1411                                         e.printStackTrace() ;
1412                                 }
1413                         }
1414                 }
1415         }
1416
1417         @Override
1418         public void stop() 
1419         {
1420                 stopVM() ;
1421                 
1422                 pingServer.stopPing() ;
1423                 
1424                 dialogVmServer.stopDialogVMServer() ;
1425                 
1426                 // unexportObject ??
1427                 
1428                 System.exit( 0 ) ;
1429         }
1430
1431         @Override
1432         public String getIPHost() 
1433         {
1434                 return LocalHost.Instance().getIP() ;
1435         }
1436         
1437         @Override
1438         public String getName()
1439         {
1440                 return LocalHost.Instance().getName() ;
1441         }
1442         
1443         
1444         @Override
1445         public void saveOk() 
1446         {
1447                 String save_name = "VmTest_" + machine.getComputationId() +
1448                         "_last.tar.gz" ;
1449                 
1450                 String save_new = working_directory + "/" + machine.getName() + "_new_" 
1451                   + machine.getComputationId() + ".tar.gz" ;
1452                 
1453                 String[] command = new String[]{ "/bin/mv", 
1454                                 save_new, save_name } ;
1455                 
1456                 try {
1457                         Process p = Runtime.getRuntime().exec( command ) ;
1458                         p.waitFor() ;
1459                 
1460                         if( p.exitValue() == 0 )
1461                         {
1462                                 System.out.println( "Last save OK" ) ;
1463                                 machine.setSave_last( save_name ) ;
1464                         } else {
1465                                 System.err.println( "Last save NOK!" ) ;
1466                                 System.err.println( "Error: " ) ;
1467 //                              printProcessError( p.getErrorStream() ) ;
1468                                 printProcessError( p ) ;
1469                         }
1470                 } catch( IOException e ) {
1471                         System.err.println( "Error during last archive move:" ) ;
1472                         e.printStackTrace() ;
1473                 } catch( InterruptedException e ) {
1474                         e.printStackTrace() ;
1475                 }
1476         }
1477
1478         @Override
1479         public void setSavingNeighbor( String _sn ) throws RemoteException 
1480         {
1481                 if( _sn != null && _sn.length() > 0 )
1482                 {
1483                         ArrayList<String> as = new ArrayList<String>() ;
1484                         as.add( _sn ) ;
1485                         
1486                         System.out.println( "Save neighbor: " + _sn ) ;
1487                         
1488                         machine.setSaveNeighbors( as ) ;
1489                 }
1490         }
1491
1492         @Override
1493         public int retrieveSave( String _saveName ) throws RemoteException 
1494         {               
1495                 if( _saveName != null )
1496                 {
1497                         if( ! _saveName.equalsIgnoreCase( "none" ) )
1498                         {
1499                                 machine.setSave_last( _saveName ) ;
1500                         } else {
1501                                 System.err.println( "I have no saving neighbor to contact!!" ) ;
1502                                 return 1 ;
1503                         }
1504                         
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 } ;
1512                                 
1513                         try {
1514                                 Process p = Runtime.getRuntime().exec( command ) ;
1515                                 p.waitFor() ;
1516                         
1517                                 if( p.exitValue() == 0 )
1518                                 {
1519                                         System.out.println( "Archive successfully retrieved." ) ;
1520                                         isRestartedSave = true ;
1521                                         return 0 ;
1522                                 } else {
1523                                         System.err.println( "Archive not retrieved!" ) ;
1524                                         System.err.println( "Error: " ) ; 
1525 //                                      printProcessError( p.getErrorStream() ) ;
1526                                         printProcessError( p ) ;
1527 //                                      error = true ;
1528                                 }
1529                         } catch( IOException e ) {
1530                                 System.err.println( "Error during archive retrieve command: " ) ;
1531                                 e.printStackTrace() ;
1532 //                              error = true ;
1533                         } catch( InterruptedException e ) {
1534                                 e.printStackTrace() ;
1535 //                              error = true ;
1536                         }
1537                 }
1538                 
1539                 return 1 ;
1540                 
1541         }
1542
1543         @Override
1544         public String getIPVM() throws RemoteException 
1545         {
1546                 if( machine != null )
1547                 {
1548                         return machine.getIp() ;
1549                 }
1550                 
1551                 return null ;
1552         }
1553         
1554         
1555         @Override
1556         public void setIPVM( String _ipVM ) throws RemoteException 
1557         {
1558                 if( _ipVM != null && ! _ipVM.isEmpty() )
1559                 {
1560                         System.out.println( "The VM IP is now: " + _ipVM ) ;
1561                         machine.setIp( _ipVM ) ;
1562                 }
1563         }
1564         
1565         
1566         private class SaveProcess
1567         {
1568                 boolean status ;
1569                 
1570                 SaveProcess()
1571                 {
1572                         status = false ;
1573                 }
1574                 
1575                 protected boolean getStatus() { return status ; }
1576                 
1577                 protected void setStatus( boolean _b ) { status = _b ; }
1578         }
1579         
1580 }
1581
1582
1583 /** La programmation est un art, respectons ceux qui la pratiquent !! **/