Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding missing handler when a restart crash.
[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 import java.util.concurrent.Semaphore;
19
20
21 public class Client extends UnicastRemoteObject implements ServicesClient
22 {
23         private static final long serialVersionUID = 1L ;
24         
25         private String VmRunCommand ;
26 //      private String VmRunCommandArg ;
27         private VirtualMachine machine ;
28         private String server_ip ;
29         private int server_port ;
30         private int client_port ;
31         private int dialog_port ;
32         private ServicesServer serverStub ;
33         private ServicesClient myStub ;
34         private PingServer pingServer ;
35         private DialogVMServer dialogVmServer ;
36         private ServerSocket serverSocket ;
37         private String ushell ;
38         private String working_directory ;
39         private int wait_start ;
40         private int max_start_try ;
41         private boolean isRestartedSave ;
42 //      private long save_interleave ;
43 //      private long date_last_save ;
44         private SaveProcess saveProcess;
45         private int maxRetryVM ;
46         private int timeRetryVM ;
47         private Semaphore sema ;
48         private boolean emergencyStop ;
49         private Process procSave ;
50         private SaveRequest saveRequest ;
51         private boolean lastSaveOk ;
52         
53                 
54         protected Client() throws RemoteException 
55         {
56                 super() ;
57         }
58
59         @Override
60         public void emergencyStop()
61         {
62                 emergencyStop = true ;
63                 
64                 // Saving processus stop
65                 synchronized( saveProcess ) {
66                         saveProcess.setStatus( false ) ;
67                         try {
68                                 saveProcess.notifyAll() ;
69                         } catch( Exception e ) {}}
70                 
71                 try {
72                         procSave.destroy() ;
73                 } catch( Exception e ) {}
74                 
75                 // Stopping the save request
76                 synchronized( saveRequest )
77                 {
78                         saveRequest.setStatus( false ) ;
79                         try {
80                                 saveRequest.notifyAll() ;
81                         } catch( Exception e ) {}
82                 }
83                 
84                 stopVM() ;
85         }
86         
87         
88         @Override
89         public int startVM( int _mode ) 
90         {               
91                 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "running" ) )
92                 {
93                         try {
94                                 sema.acquire() ;
95                         } catch( InterruptedException e2 ) {
96                                 System.err.println( "Problem with semaphore acquiring!" ) ;
97                                 e2.printStackTrace() ;
98                         }
99
100                         
101                         // Checking first start
102                         if( machine.checkVmx() == 1 )
103                         {
104                                 return 1 ;
105                         }
106                         
107                         boolean ret = true ;
108                         int retry = 0 ;
109                         
110                         /** Starting VM **/
111                         System.out.print( "Starting VM ... " ) ;
112                         
113                         machine.setStatus( "undefined" ) ;
114                         if( _mode == 0 )
115                         {
116                                 try {
117                                         LocalHost.Instance().getServerStub().changeStatus( 
118                                                 LocalHost.Instance().getIP(), "undefined" ) ;
119                                 } catch( RemoteException e ) {
120                                         System.err.println( "Unable to inform the server of the VM status!" ) ;
121                                         e.printStackTrace() ;
122                                 }
123                         }
124                         
125                         String[] command = new String[]{VmRunCommand, "-T", "player", "start",
126                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name(), "nogui"} ;
127                         
128                         while( ret )
129                         {
130                                 
131                                 if( emergencyStop )
132                                 {
133                                         return 1 ;
134                                 }
135                 
136                                 try {
137                                         Process p = Runtime.getRuntime().exec( command ) ;
138                                         p.waitFor() ;
139                                 
140                                         if( p.exitValue() == 0 )
141                                         {
142                                                 System.out.println( "Virtual machine successfully started." ) ;
143                                                 ret = false ;
144                                         } else {
145                                                 System.err.println( "Virtual machine not started!" ) ;
146                                                 ret = printProcessError( p ) ;
147                                         
148                                                 if( ! ret )
149                                                 {
150                                                         sema.release() ;
151                                                 
152                                                         return 1 ;
153                                                 } else {
154                                                         retry++ ;
155                                                         if( retry >= maxRetryVM )
156                                                         {
157                                                                 System.err.println( "Unable to start VM!" ) ;
158                                                         
159                                                                 sema.release() ;
160                                                         
161                                                                 return 1 ;
162                                                         }
163                                                         System.out.println( "Retrying (" + retry + ") ... " ) ;
164                                                         Thread.sleep( timeRetryVM ) ;
165                                                 }
166                                         }
167                                 } catch( IOException e ) {
168                                         System.err.println( "Error during execution of start command: " ) ;
169                                         e.printStackTrace() ;
170                                 
171                                         sema.release() ;
172                                 
173                                         return 1 ;
174                                 } catch( InterruptedException e ) {
175                                         e.printStackTrace() ;
176                                 
177                                         sema.release() ;
178                                         return 1 ;
179                                 }
180                         }
181                         
182                         
183                         boolean started = false ;
184                         int count = 1 ;
185                         ret = true ;
186                         retry = 0 ;
187                         
188                         while( ! started )
189                         {
190                                 if( emergencyStop )
191                                 {
192                                         return 1 ;
193                                 }
194                                 
195                                 /** Waiting for VM being started **/
196                                 try {
197                                         Thread.sleep( wait_start ) ;
198                                 } catch( InterruptedException e ) {
199                                         e.printStackTrace() ;
200                                 }
201                                                         
202                                 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
203                                 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
204                                 working_directory + "/" + machine.getDirectory() 
205                                 + "/" + machine.getVmx_name() + " " + ushell + 
206                                 " \"echo ok\"" ;
207                                 
208                                 try {
209                                         FileWriter fw = new FileWriter( new File( working_directory + "/testStarted.sh" ) ) ;
210                                         fw.write( cmd2 ) ;
211                                         fw.flush() ;
212                                         fw.close() ;
213                                 } catch( IOException e1 ) {
214                                         e1.printStackTrace() ;
215                                         
216                                         sema.release() ;
217                                         return 1 ;
218                                 }
219                                 
220                                 command = new String[]{ ushell, working_directory + "/testStarted.sh"} ;
221
222                                 while( ret )
223                                 {
224                                         if( emergencyStop )
225                                         {
226                                                 return 1 ;
227                                         }
228                                 
229                                         try {
230                                                 Process p = Runtime.getRuntime().exec( command ) ;
231                                                 p.waitFor() ;
232                                         
233                                                 if( p.exitValue() == 0 )
234                                                 {
235                                                         started = true ;
236                                                         ret = false ;
237                                                 } else {
238                                                         System.err.println( "Error while checking if the VM is started!" ) ;
239                                                         ret = printProcessError( p ) ;
240                                                 
241                                                         if( ! ret )
242                                                         {
243                                                                 sema.release() ;
244                                                                 stopVM() ;
245                                                                 return 1 ;
246                                                         } else {
247                                                                 retry++ ;
248                                                                 if( retry >= maxRetryVM )
249                                                                 {
250                                                                         System.err.println( "Unable to check VM!" ) ;
251                                                                 
252                                                                         sema.release() ;
253                                                                         stopVM() ;
254                                                                         return 1 ;
255                                                                 }
256                                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
257                                                                 Thread.sleep( timeRetryVM ) ;
258                                                         }
259                                                         count++ ;
260                                                 }
261                                         } catch( IOException e ) {
262                                                 e.printStackTrace() ;
263                                                 sema.release() ;
264                                                 stopVM() ;
265                                                 return 1 ;
266                                         } catch( InterruptedException e ) {
267                                                 e.printStackTrace() ;
268                                                 sema.release() ;
269                                                 stopVM() ;
270                                                 return 1 ;
271                                         }
272                                 
273                                         if( count == max_start_try && ! started )
274                                         {
275                                                 System.err.println( "Virtual machine not responding!!" ) ;
276                                         
277                                                 try {
278                                                         LocalHost.Instance().getServerStub().changeStatus( 
279                                                                         LocalHost.Instance().getIP(), "undefined" ) ;
280                                                 } catch( RemoteException e ) {
281                                                         e.printStackTrace() ;
282                                                 }
283                                                 
284                                                 sema.release() ;
285                                         
286                                                 stopVM() ;
287                                         
288                                                 return 1 ;
289                                         } else {
290                                                 try {
291                                                         Thread.sleep( 3000 ) ;
292                                                 } catch( InterruptedException e ) {
293                                                         e.printStackTrace() ;
294                                                 }
295                                         }
296                                 }
297                         }
298                         
299                         /** Sending the host ip **/
300                         
301                         System.out.print( "Sending host IP to VM ... " ) ;
302                         
303                         String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
304                         " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
305                         working_directory + "/" + machine.getDirectory() 
306                         + "/" + machine.getVmx_name() + " " +  ushell + 
307                         " \"echo " + LocalHost.Instance().getIP() + " " + dialog_port 
308                         + " > /tmp/vm_host_IP\"" ;
309                         
310                         try {
311                                 FileWriter fw = new FileWriter( new File( working_directory + "/sendHostIP.sh" ) ) ;
312                                 fw.write( cmd2 ) ;
313                                 fw.flush() ;
314                                 fw.close() ;
315                         } catch( IOException e1 ) {
316                                 e1.printStackTrace() ;
317                                 sema.release() ;
318                                 stopVM() ;
319                                 return 1 ;
320                         }
321                         
322                         command = new String[]{ ushell, working_directory + "/sendHostIP.sh"} ;
323                         
324                         ret = true ;
325                         retry = 0 ;
326                         
327                         while( ret )
328                         {
329                                 if( emergencyStop )
330                                 {
331                                         return 1 ;
332                                 }
333                         
334                                 try {
335                                         Process p = Runtime.getRuntime().exec( command ) ;
336                                         p.waitFor() ;
337                                 
338                                         if( p.exitValue() == 0 )
339                                         {
340                                                 System.out.println( "VM received the host IP." ) ;
341                                                 ret = false ;
342                                         } else {
343                                                 System.err.println( "VM did not received the host IP!" ) ;
344                                                 ret = printProcessError( p ) ;
345                                         
346                                                 if( ! ret )
347                                                 {
348                                                         sema.release() ;
349                                                         stopVM() ;
350                                                         return 1 ;
351                                                 } else {
352                                                         retry++ ;
353                                                         if( retry >= maxRetryVM )
354                                                         {
355                                                                 System.err.println( "Unable to send information to VM!" ) ;
356                                         
357                                                                 sema.release() ;
358                                                                 stopVM() ;
359                                                         
360                                                                 return 1 ;
361                                                         }
362                                                         System.out.println( "Retrying (" + retry + ") ... " ) ;
363                                                         Thread.sleep( timeRetryVM ) ;
364                                                 }
365                                         }
366                                 } catch( IOException e ) {
367                                         System.err.println( "Error during execution of runScriptInGuest command: " ) ;
368                                         e.printStackTrace() ;
369                                         sema.release() ;
370                                         stopVM() ;
371                                         return 1 ;
372                                 } catch( InterruptedException e) {
373                                         e.printStackTrace() ;
374                                         sema.release() ;
375                                         stopVM() ;
376                                         return 1 ;
377                                 }
378                         }
379                                         
380                         /** Sending the vm ip **/
381                         
382                         System.out.print( "Sending its IP to VM ... " ) ;
383                         
384                         cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
385                         " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
386                         working_directory + "/" + machine.getDirectory() 
387                         + "/" + machine.getVmx_name() + " " + ushell +
388                         " \"echo " + machine.getIp() 
389                         + " > /tmp/vm_IP\"" ;
390                         
391                         try {
392                                 FileWriter fw = new FileWriter( new File( working_directory + "/sendVmIP.sh" ) ) ;
393                                 fw.write( cmd2 ) ;
394                                 fw.flush() ;
395                                 fw.close() ;
396                         } catch( IOException e1 ) {
397                                 e1.printStackTrace() ;
398                                 sema.release() ;
399                                 stopVM() ;
400                                 return 1 ;
401                         }
402                         
403                         command = new String[]{ ushell, working_directory + "/sendVmIP.sh"} ;
404                         
405                         ret = true ;
406                         retry = 0 ;
407                         
408                         while( ret )
409                         {
410                                 if( emergencyStop )
411                                 {
412                                         return 1 ;
413                                 }
414                         
415                                 try {
416                                         Process p = Runtime.getRuntime().exec( command ) ;
417                                         p.waitFor() ;
418                                 
419                                         if( p.exitValue() == 0 )
420                                         {
421                                                 System.out.println( "VM received its assigned IP." ) ;
422                                                 ret = false ;
423                                                 machine.setStatus( "running" ) ;
424                                         } else {
425                                                 System.err.println( "VM did not received its assigned IP!" ) ;
426                                                 ret = printProcessError( p ) ;
427                                         
428                                                 if( ! ret )
429                                                 {
430                                                         sema.release() ;
431                                                         stopVM() ;
432                                                         return 1 ;
433                                                 } else {
434                                                         retry++ ;
435                                                         if( retry >= maxRetryVM )
436                                                         {
437                                                                 System.err.println( "Unable to send information to VM!" ) ;
438                                                         
439                                                                 sema.release() ;
440                                                                 stopVM() ;
441                                                         
442                                                                 return 1 ;
443                                                         }
444                                                         System.out.println( "Retrying (" + retry + ") ... " ) ;
445                                                         Thread.sleep( timeRetryVM ) ;
446                                                 }
447                                         }
448                                 } catch( IOException e ) {
449                                         System.err.println( "Error during execution of runScriptInGuest command: " ) ;
450                                         e.printStackTrace() ;
451                                         sema.release() ; 
452                                         stopVM() ;
453                                         return 1 ;
454                                 } catch( InterruptedException e ) {
455                                         e.printStackTrace() ;
456                                         sema.release() ;
457                                         stopVM() ;
458                                         return 1 ;
459                                 }
460                         }
461                         
462                         if( _mode == 0 )
463                         {
464                                 try {
465                                         LocalHost.Instance().getServerStub().changeStatus( 
466                                                         LocalHost.Instance().getIP(), "running" ) ;
467                                 } catch (RemoteException e) {
468                                         System.err.println( "Unable to inform the server of the VM started status!" ) ;
469                                         e.printStackTrace();
470                                 }
471                         }
472                         
473                         sema.release() ;
474                         
475                         return 0 ;
476                 }
477                 
478                 sema.release() ;
479                 
480                 return 1 ;
481         }
482
483         
484         @Override
485         public int stopVM() 
486         {               
487                 if( machine != null && machine.getStatus().equalsIgnoreCase( "stopped" ) )
488                 {
489                         emergencyStop = false ;
490                         return 0 ;
491                 }
492                 
493                 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "stopped" ) )
494                 {
495                         try {
496                                 sema.acquire() ;
497                         } catch( InterruptedException e2 ) {
498                                 System.err.println( "Problem with semaphore acquiring!" ) ;
499                                 e2.printStackTrace() ;
500                         }
501                         
502                         System.out.print( "Stopping VM ... " ) ;
503                         
504                         boolean ret = true ;
505                         int retry = 0 ;
506                         
507                         machine.setStatus( "undefined" ) ;
508                         try {
509                                 LocalHost.Instance().getServerStub().changeStatus( 
510                                                 LocalHost.Instance().getIP(), "undefined" ) ;
511                         } catch( RemoteException e ) {
512                                 System.err.println( "Unable to inform the server of the VM status!" ) ;
513                                 e.printStackTrace() ;
514                         }
515                         
516                         String[] command = new String[]{VmRunCommand, "-T", "player", "stop",
517                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
518                         
519                         while( ret )
520                         try {
521                                 Process p = Runtime.getRuntime().exec( command ) ;
522                                 p.waitFor() ;
523                                 
524                                 if( p.exitValue() == 0 )
525                                 {
526                                         System.out.println( "Virtual machine successfully stopped." ) ;
527                                         machine.setStatus( "stopped" ) ;
528                                         ret = false ;
529                                 } else {
530                                         System.err.println( "Virtual machine not stopped!" ) ;
531                                         ret = printProcessError( p ) ;
532                                         
533                                         if( ! ret )
534                                         {
535                                                 sema.release() ;
536                                                 return 1 ;
537                                         } else {
538                                                 retry++ ;
539                                                 if( retry >= maxRetryVM )
540                                                 {
541                                                         System.err.println( "Unable to stop VM!" ) ;
542                                                         sema.release() ;
543                                                         return 1 ;
544                                                 }
545                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
546                                                 Thread.sleep( timeRetryVM ) ;
547                                         }
548                                         
549 //                                      return 1 ;
550                                 }
551                         } catch( IOException e ) {
552                                 System.err.println( "Error during execution of stop command: " ) ;
553                                 e.printStackTrace() ;
554                                 sema.release() ;
555                                 return 1 ;
556                         } catch( InterruptedException e ) {
557                                 e.printStackTrace() ;
558                                 sema.release() ;
559                                 return 1 ;
560                         }
561                         
562                         machine.setStatus( "stopped" ) ;
563                         try {
564                                 LocalHost.Instance().getServerStub().changeStatus( 
565                                                 LocalHost.Instance().getIP(), "stopped" ) ;
566                         } catch( RemoteException e1 ) {
567                                 System.err.println( "Unable to inform the server of the VM stopped status!" ) ;
568                                 e1.printStackTrace() ;
569                         }
570                         
571
572                         if(     machine.checkVmx() == 0 )
573                         {
574                                 sema.release() ;
575                                 emergencyStop = false ;
576                                 return 0 ;
577                         }
578                 }
579                 
580                 sema.release() ;
581                 
582                 return 1 ;
583         }
584
585         @Override
586         public int suspendVM( int _mode ) 
587         {               
588                 if( machine != null && machine.getStatus().equalsIgnoreCase( "suspended" ) )
589                 {
590                         return 0 ;
591                 }
592                 
593                 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "suspended" ) )
594                 {
595                         try {
596                                 sema.acquire() ;
597                         } catch( InterruptedException e2 ) {
598                                 System.err.println( "Problem with semaphore acquiring!" ) ;
599                                 e2.printStackTrace() ;
600                         }
601                         
602                         System.out.print( "Suspending VM ... " ) ;
603                         
604                         boolean ret = true ;
605                         int retry = 0 ;
606                         
607                         machine.setStatus( "undefined" ) ;
608                         if( _mode == 0 )
609                         {
610                                 try {
611                                         LocalHost.Instance().getServerStub().changeStatus( 
612                                                 LocalHost.Instance().getIP(), "undefined" ) ;
613                                 } catch( RemoteException e ) {
614                                         System.err.println( "Unable to inform the server of the VM status!" ) ;
615                                         e.printStackTrace() ;
616                                 }
617                         }
618                         
619                         String[] command = new String[]{VmRunCommand, "-T", "player", "suspend",
620                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
621                         
622                         while( ret )
623                         try {
624                                 Process p = Runtime.getRuntime().exec( command ) ;
625                                 p.waitFor() ;
626                                 if( p.exitValue() == 0 )
627                                 {
628                                         System.out.println( "Virtual machine successfully suspended." ) ;
629                                         machine.setStatus( "suspended" ) ;
630                                         ret = false ;
631                                 } else {
632                                         System.err.println( "Virtual machine not suspended!" ) ;
633                                         ret = printProcessError( p ) ;
634                                         
635                                         if( ! ret )
636                                         {
637                                                 sema.release() ;
638                                                 
639                                                 return 1 ;
640                                         } else {
641                                                 retry++ ;
642                                                 if( retry >= maxRetryVM )
643                                                 {
644                                                         System.err.println( "Unable to suspend VM!" ) ;
645                                                         
646                                                         sema.release() ;
647                                                         
648                                                         return 1 ;
649                                                 }
650                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
651                                                 Thread.sleep( timeRetryVM ) ;
652                                         }
653                                         
654 //                                      return 1 ;
655                                 }
656                         } catch( IOException e ) {
657                                 System.err.println( "Error during execution of suspend command: " ) ;
658                                 e.printStackTrace() ;
659                         } catch( InterruptedException e ) {
660                                 e.printStackTrace() ;
661                         }
662                         
663                         machine.setStatus( "suspended" ) ;
664                         if( _mode == 0 )
665                         {
666                                 try {
667                                         LocalHost.Instance().getServerStub().changeStatus( 
668                                                 LocalHost.Instance().getIP(), "suspended" ) ;
669                                 } catch( RemoteException e ) {
670                                         System.err.println( "Unable to inform the server of the VM suspended status!" ) ;
671                                         e.printStackTrace() ;
672                                 }
673                         }
674                         
675                         sema.release() ;
676                         
677                         return 0 ;
678                 }
679                 
680                 sema.release() ;
681                 
682                 return 1 ;
683         }
684
685         @Override
686         public int restartVM() 
687         {               
688                 if( machine != null )
689                 {
690                         System.out.print( "Restarting VM ... " ) ;
691                         
692                         boolean ret = true ;
693                         int retry = 0 ;
694                         
695                         try {
696                                 LocalHost.Instance().getServerStub().changeStatus( 
697                                                 LocalHost.Instance().getIP(), "undefined" ) ;
698                         } catch( RemoteException e ) {
699                                 System.err.println( "Unable to inform the server of the VM status!" ) ;
700                                 e.printStackTrace() ;
701                         }
702                         
703                         String[] command = new String[]{VmRunCommand, "-T", "player", "reset",
704                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
705                         
706                         while( ret )
707                         try {
708                                 Process p = Runtime.getRuntime().exec( command ) ;
709                                 p.waitFor() ;
710                                 
711                                 if( p.exitValue() == 0 )
712                                 {
713                                         System.out.println( "Virtual machine successfully restarted." ) ;
714
715                                         if( sendSaveOkVM() == 1 ) { return 1 ; }
716                                         
717                                         ret = false ;
718                                         return 0 ;
719
720                                 } else {
721                                         System.err.println( "Virtual machine not restarted!" ) ;
722                                         ret = printProcessError( p ) ;
723                                         
724                                         if( ! ret )
725                                         {
726                                                 return 1 ;
727                                         } else {
728                                                 retry++ ;
729                                                 if( retry >= maxRetryVM )
730                                                 {
731                                                         System.err.println( "Unable to start VM!" ) ;
732                                                         return 1 ;
733                                                 }
734                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
735                                                 Thread.sleep( timeRetryVM ) ;
736                                         }
737                                         
738 //                                      return 1 ;
739                                 }
740                         } catch( IOException e ) {
741                                 System.err.println( "Error during execution of restart command: " ) ;
742                                 e.printStackTrace() ;
743                         } catch( InterruptedException e ) {
744                                 e.printStackTrace() ;
745                         }
746                 }
747
748                 return 1 ;
749         }
750
751         @Override
752         public int restartVMAfterCrash() 
753         {
754                 System.out.println( "Restarting VM after a crash ..." ) ;
755                 
756                 try {
757                         LocalHost.Instance().getServerStub().changeStatus( 
758                                         LocalHost.Instance().getIP(), "undefined" ) ;
759                 } catch( RemoteException e ) {
760                         System.err.println( "Unable to inform the server of the VM status!" ) ;
761                         e.printStackTrace() ;
762                 }
763                 
764                 if( stopVM() == 0 )
765                 {
766                         if( machine.deployLastSave() == 0 )
767                         {
768                                 if( isRestartedSave )
769                                 {
770                                         // Using the specific vmx file
771                                         machine.setDeployFault( true ) ;
772                                         
773                                         // Writing the restarted save mark
774                                         try {
775                                                 FileWriter fw = new FileWriter( new File( working_directory + "/" + machine.getDirectory() + "/fault.hpcvm" ) ) ;
776                                                 fw.write( "fault!" ) ;
777                                                 fw.flush() ;
778                                                 fw.close() ;
779                                                 fw = null ;
780                                         } catch( IOException e1 ) {
781                                                 e1.printStackTrace() ;
782                                                 System.err.println( "Unable to mark the fault!" ) ;
783                                         }       
784                                         
785                                         if( machine.checkVmx() == 1 )
786                                         {
787                                                 return 1 ;
788                                         }
789                                         
790                                 
791                                         /** Retrieving VM assigned IP **/
792                                         String vmIP = null ;
793                                 
794                                         try {
795                                                 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP( 
796                                                         LocalHost.Instance().getIP() ) ;
797                                         } catch (RemoteException e) {
798                                                 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
799                                                 e.printStackTrace() ;
800                                                 return 1 ;
801                                         }
802                                 
803                                         machine.setIp( vmIP ) ;
804                                         
805                                         isRestartedSave = false ;
806                                 }
807                                 
808                                 if( startVM( 0 ) == 0 ) 
809                                 {
810                                         if( sendSaveOkVM() == 0 )
811                                         {                                               
812                                                 try {
813                                                         LocalHost.Instance().getServerStub().restartOk( LocalHost.Instance().getIP() ) ;
814                                                 } catch( RemoteException e ) {
815                                                         System.err.println( "Unable to inform server about my successful restart!" ) ;
816                                                         e.printStackTrace() ;
817                                                 }
818                                                 
819                                                 return 0 ;
820                                         }
821                                 } else {
822                                         stopVM() ;
823                                 }
824                         }
825                 }
826                 
827                 return 1 ;
828         }
829         
830         
831         private int sendSaveOkVM()
832         {
833                 boolean ret = true ;
834                 int retry = 0 ;
835                 
836                 /** Informing the program that it's ok **/
837                 System.out.print( "Sending OK signal to the program ... " ) ;
838                         
839                 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
840                 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
841                 working_directory + "/" + machine.getDirectory() 
842                 + "/" + machine.getVmx_name() + " " + ushell + 
843                 " \"echo ok > /tmp/vm_save_ok\"" ;// + " -noWait " ;
844                 
845                 try {
846                         FileWriter fw = new FileWriter( new File( working_directory + "/saveOk.sh" ) ) ;
847                         fw.write( cmd2 ) ;
848                         fw.flush() ;
849                         fw.close() ;
850                 } catch( IOException e1 ) {
851                         e1.printStackTrace() ;
852                 }
853                         
854                 String[] command = new String[]{ ushell, working_directory + "/saveOk.sh"} ;
855                 
856                 while( ret )
857                 try {
858                         Process p = Runtime.getRuntime().exec( command ) ;
859                         p.waitFor() ;
860                         
861                         if( p.exitValue() == 0 )
862                         {
863                                 System.out.println( "Signal successfully sent." ) ;
864                                 ret = false ;
865                                 return 0 ;
866                         } else {
867                                 System.err.println( "Signal not sent!" ) ;
868                                 ret = printProcessError( p ) ;
869                                 
870                                 if( ! ret )
871                                 {
872                                         return 1 ;
873                                 } else {
874                                         retry++ ;
875                                         if( retry >= maxRetryVM )
876                                         {
877                                                 System.err.println( "Unable to send ok signal to VM!" ) ;
878                                                 return 1 ;
879                                         }
880                                         System.out.println( "Retrying (" + retry + ") ... " ) ;
881                                         Thread.sleep( timeRetryVM ) ;
882                                 }
883 //                              return 1 ;
884                         }
885                 } catch( IOException e ) {
886                         System.err.println( "Error during ok save signal send command: " ) ;
887                         e.printStackTrace() ;
888                         return 1 ;
889                 } catch( InterruptedException e ) {
890                         e.printStackTrace() ;
891                         return 1 ;
892                 }
893                 
894                 return 1 ;
895         }
896
897         
898         @Override
899         public int saveVM() 
900         {
901                 synchronized( saveProcess ){
902                 while( saveProcess.getStatus() )
903                 {
904                         try {
905                                 saveProcess.wait() ;
906                         } catch( InterruptedException e ) {
907                                 e.printStackTrace() ;
908                         }
909                 }}
910                 
911                 System.out.println( "Saving VM ..." ) ;
912                 saveProcess.setStatus( true ) ;
913                 
914                 long deb = System.currentTimeMillis() ;
915                 
916                 machine.setStatus( "saving" ) ;
917                 try {
918                         LocalHost.Instance().getServerStub().changeStatus( 
919                                 LocalHost.Instance().getIP(), "saving" ) ;
920                 } catch( RemoteException e ) {
921                         System.err.println( "Unable to inform the server of the VM status!" ) ;
922                         e.printStackTrace() ;
923                 }
924                 
925                 String[] command ;
926                 String saveName = "" ;
927                 boolean error = false ;
928                 
929                 
930                 if( suspendVM( 1 ) == 1 ) { return 1 ; }
931                 
932                 
933                 if( ! lastSaveOk )
934                 {
935                         String arch = "" ;
936                         File file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ) ;
937                         if( file.exists() )
938                         {
939                                 arch = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
940                         }
941                         
942                         file = null ;
943                         
944                         if( arch.length() > 0 )
945                         {                       
946                                 System.out.println( "Deletion of last nok archive ... " ) ;
947                         
948                                 command = new String[]{ "/bin/rm", "-rf", arch } ;
949                                                         
950                                 try {
951                                         procSave = Runtime.getRuntime().exec( command ) ;
952                                         procSave.waitFor() ;
953                                 
954                                         if( procSave.exitValue() == 0 )
955                                         {
956                                                 System.out.println( "Last nok archive successfully deleted." ) ;
957                                         } else {
958                                                 System.err.println( "Last nok archive not deleted!" ) ;
959                                                 printProcessError( procSave ) ;
960                                         
961                                                 error = true ;
962                                         }
963                                 } catch( IOException e ) {
964                                         System.err.println( "Error during nok archive deletion command: " ) ;
965                                         error = true ;
966                                         e.printStackTrace() ;
967                                 } catch( InterruptedException e ) {
968                                         e.printStackTrace() ;
969                                         error = true ;
970                                 }
971                         }
972                 }
973                 
974                 
975                 if( ! error )
976                 {
977                         System.out.print( "Creation of the archive ... " ) ;
978                         
979                         /** Archive creation **/
980                         command = new String[]{ "/bin/tar", "-cz", "-C", working_directory,
981                                         "-f", working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz",
982                                         machine.getDirectory() } ;
983                                 
984                         if( emergencyStop )
985                         {
986                                 return 1 ;
987                         }
988                         
989                         try {
990                                 procSave = Runtime.getRuntime().exec( command ) ;
991                                 procSave.waitFor() ;
992                                 
993                                 if( procSave.exitValue() == 0 )
994                                 {
995                                         System.out.println( "Archive successfully created." ) ;
996                                         
997                                         lastSaveOk = false ;
998                                         
999                                 } else {
1000                                         System.err.println( "Archive not created!" ) ;
1001                                         printProcessError( procSave ) ;
1002                                         
1003                                         error = true ;
1004                                 }
1005                         } catch( IOException e ) {
1006                                 System.err.println( "Error during archive creation command: " ) ;
1007                                 error = true ;
1008                                 e.printStackTrace() ;
1009                         } catch( InterruptedException e ) {
1010                                 e.printStackTrace() ;
1011                                 error = true ;
1012                         }
1013                         
1014                         long fin = System.currentTimeMillis() ;
1015                         
1016                         System.out.println( "Time to create the save: " + (fin-deb)/1000 + " seconds." ) ;
1017                         
1018                         
1019                         /** Restarting VM **/
1020                         if( startVM( 0 ) == 1 ) { return 1 ; }
1021                         
1022                         
1023                         /** Sending ok save signal **/
1024                         if( sendSaveOkVM() == 1 ) { return 1 ; }
1025                 
1026                         saveName = machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;       
1027                 
1028                         /** Sending save to neighbor **/
1029                         if( ! error )
1030                         {
1031                                 if( emergencyStop )
1032                                 {
1033                                         return 1 ;
1034                                 }
1035                                 
1036                                 ArrayList<SaveNeighbor> sn = machine.getSaveNeighbors() ;
1037                                 
1038                                 for( int i = 0 ; i < sn.size() ; i++ )
1039                                 {
1040                                         String name = sn.get( i ).getName() ;
1041                                         String wd = sn.get( i ).getWorkingDirectory() ;
1042                                         String snIP = sn.get( i ).getIPHost() ;
1043                                         
1044                                         System.out.print( "Sending save to " + name + " ... " ) ;
1045                                         
1046                                         command = new String[]{ "/usr/bin/scp", working_directory + "/" + saveName,
1047                                                                 snIP + ":" + wd } ;
1048                                 
1049                                         try {
1050                                                 procSave = Runtime.getRuntime().exec( command ) ;
1051                                                 procSave.waitFor() ;
1052                         
1053                                                 if( procSave.exitValue() == 0 )
1054                                                 {
1055                                                         System.out.println( "Archive successfully sent." ) ;
1056                                                 } else {
1057                                                         System.err.println( "Archive not sent!" ) ;
1058                                                         printProcessError( procSave ) ;
1059                                                 
1060                                                         error = true ;
1061                                                 }
1062                                         } catch( IOException e ) {
1063                                                 System.err.println( "Error during archive send command: " ) ;
1064                                                 e.printStackTrace() ;
1065                                                 error = true ;
1066                                         } catch( InterruptedException e ) {
1067                                                 e.printStackTrace() ;
1068                                                 error = true ;
1069                                         }
1070                                 }
1071                         }
1072                 }
1073                 
1074                 /** Informing the server the save is done **/
1075                 if( ! error )
1076                 {
1077                         try {
1078                                 LocalHost.Instance().getServerStub().saveOk( LocalHost.Instance().getIP(), saveName ) ;
1079                         } catch( RemoteException e ) {
1080                                 System.err.println( "Problem while informing the server about the save state!" ) ;
1081                                 e.printStackTrace() ;
1082                         }
1083                         
1084                         synchronized( saveProcess ) {
1085                         saveProcess.setStatus( false ) ;
1086                         try {
1087                                 saveProcess.notifyAll() ;
1088                         } catch( Exception e ) {}}
1089                         
1090                         return 0 ;
1091                 }
1092                 
1093                 synchronized( saveProcess ) {
1094                 saveProcess.setStatus( false ) ;
1095                 try {
1096                         saveProcess.notifyAll() ;
1097                 } catch( Exception e ) {}}
1098                 
1099                 return 1 ;
1100         }
1101
1102         
1103         @Override
1104         public int reloadConfig() 
1105         {
1106                 System.out.println( "Reloading configuration ... " ) ;
1107                 
1108                 // TODO !!!
1109                 return 0 ;
1110         }
1111
1112         public void init( String _server_ip, int _server_port, int _client_port, int _dialog_port ) 
1113         {
1114                 System.out.println( "Initialisation Client ... " ) ;
1115                 System.out.println( "IP " + LocalHost.Instance().getIP() ) ;
1116                 
1117                 server_ip = _server_ip ;
1118                 server_port = _server_port ;
1119                 client_port = _client_port ;
1120                 dialog_port = _server_port + 1 ; // _dialog_port ;
1121                 
1122                 serverStub = null ;
1123                 saveProcess = new SaveProcess() ;
1124                 saveRequest = new SaveRequest() ;
1125                 
1126                 machine = new VirtualMachine() ;
1127                 
1128                 VmRunCommand = "/usr/bin/vmrun" ;
1129 //              VmRunCommandArg = "-T player" ;
1130                 
1131 //              vm_user = "mpi" ;
1132 //              vm_user_passwd = "mpi" ;
1133                 ushell = "/bin/bash" ;
1134                 working_directory = "/localhome/vmware" ;
1135                 
1136                 wait_start = 15000 ;
1137                 max_start_try = 10 ;
1138                 
1139                 sema = new Semaphore( 1 ) ;
1140                 emergencyStop = false ;
1141                 
1142                 maxRetryVM = 10 ;
1143                 timeRetryVM = 10000 ;
1144                 
1145 //              save_interleave = 30 * 60 * 1000 ;
1146 //              date_last_save = 0 ;
1147                 
1148                 isRestartedSave = false ;
1149                 lastSaveOk = false ;
1150                 
1151                 /** Connection to server **/
1152                 try {
1153                         serverStub = (ServicesServer) Naming.lookup( "rmi://"
1154                                         + server_ip + ":" + server_port + "/Server" ) ;
1155                 } catch (MalformedURLException e) {
1156                         e.printStackTrace();
1157                 } catch (RemoteException e) {
1158                         e.printStackTrace();
1159                 } catch (NotBoundException e) {
1160                         e.printStackTrace();
1161                 }
1162                 
1163                 if( serverStub == null )
1164                 {
1165                         System.err.println( "Unable to connect to server!!" ) ;
1166                         System.err.println( "Server IP: " + server_ip + " -- server port: " + server_port ) ;
1167                         
1168                         System.exit( 1 ) ;
1169                 }
1170                 
1171                 System.out.println( "Connected to server " + server_ip + " on port " + server_port + "." ) ;
1172                 
1173                 LocalHost.Instance().setServerStub( serverStub ) ;
1174                 
1175                 
1176                 /** Creating the local server **/
1177                 exportObject() ;
1178
1179                 /** Starting all threads **/
1180                 start() ;
1181         }
1182         
1183         private void exportObject() 
1184         {
1185                 Registry reg = null ;
1186                 
1187                 try 
1188                 {       
1189                         while( true )
1190                         {
1191                                 reg = LocateRegistry.getRegistry( client_port ) ;
1192
1193                                 String tab[] = reg.list() ;
1194                         
1195                                 System.out.println( "There is an existing RMI Registry on port " +
1196                                                 client_port + " with " + tab.length + " entries!" ) ;
1197                                 for( int i = 0 ; i < tab.length ; i++ )
1198                                 {
1199                                         try {
1200                                                 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
1201                                                 {
1202                                                         System.out.println( "Register successfuly deleted!" ) ;
1203                                                 } else {
1204                                                         System.err.println( "Register undeleted !!!" ) ;
1205                                                 }
1206                                         } catch( Exception e ) {
1207                                                 e.printStackTrace() ;
1208                                         }
1209                                 } 
1210                         }
1211                 } catch( RemoteException e ) {
1212                 }                       
1213                 
1214                 try {
1215                         if ( System.getSecurityManager() == null ) 
1216                         {
1217                     System.setSecurityManager( new SecurityManager() ) ;
1218                 }
1219                         
1220                         LocateRegistry.createRegistry( client_port ) ;
1221                         LocateRegistry.getRegistry( client_port ).rebind( "Client", this ) ;
1222                         myStub = (ServicesClient) Naming.lookup( "rmi://"
1223                                         + LocalHost.Instance().getIP() + ":" + client_port
1224                                         + "/Client" ) ;
1225                 } catch( Exception e ) {
1226                         System.err.println( "Error in Client.exportObject() when creating local services:" + e ) ;
1227                         System.err.println( "Exit from Client.exportObject" ) ;
1228                         System.exit( 1 ) ;
1229                 }
1230                 
1231                 LocalHost.Instance().setStub( myStub ) ;
1232         }
1233         
1234         
1235         private boolean printProcessError( Process _p )
1236         {
1237                 boolean ret = false ;
1238                 
1239                 if( _p != null )
1240                 {
1241                         System.err.println( "Error: " + _p.exitValue() ) ;
1242                         BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
1243                         String line ;
1244                         try {
1245                                 while( (line = br.readLine()) != null )
1246                                 {
1247                                         System.err.println( line ) ;
1248                                         if( line.contains( "egmentation" ) || _p.exitValue() == 139 ) 
1249                                         {
1250                                                 ret = true ;
1251                                         }
1252                                 }
1253                         } catch( IOException e ) {
1254                                 e.printStackTrace() ;
1255                         }
1256                 }
1257                 
1258                 return ret ;
1259         }
1260         
1261         
1262         @Override
1263         public int echo()
1264         {
1265                 return 0 ;
1266         }
1267         
1268
1269         @Override
1270         public int start() 
1271         {
1272                 /** Registering on server **/
1273                 Integer ret = 0 ;
1274                 try {
1275                         ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1276                 } catch (RemoteException e1) {  
1277                         e1.printStackTrace();
1278                         return 1 ;
1279                 }
1280                 
1281                 switch( ret )
1282                 {
1283                 case 0: System.out.println( "Successfully registered on server." ) ; break ;
1284                 case 1: System.err.println( "Problem on server while registreting!" ) ; return 1 ;
1285                 case 2: System.out.println( "Already registered on server!" ) ; break ;                 
1286                 }
1287                 
1288                 /** Retrieving VM assigned IP **/
1289                 String vmIP = null ;
1290                 
1291                 try {
1292                         vmIP = LocalHost.Instance().getServerStub().getAssociatedIP( 
1293                                         LocalHost.Instance().getIP() ) ;
1294                 } catch (RemoteException e) {
1295                         System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
1296                         e.printStackTrace() ;
1297                         return 1 ;
1298                 }
1299                 
1300                 machine.setIp( vmIP ) ;
1301                 
1302                 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1303
1304                 
1305                 /** Starting alive ping to server **/
1306                 pingServer = new PingServer() ;
1307                 pingServer.start() ;
1308
1309                 /** Starting socket server for VM dialog **/
1310                 dialogVmServer = new DialogVMServer() ;
1311                 dialogVmServer.start() ;
1312                 
1313                 return 0 ;
1314         }
1315         
1316         
1317         private class PingServer extends Thread
1318         {
1319                 protected boolean go ;
1320                 
1321                 PingServer()
1322                 {
1323                         go = true ;
1324                 }
1325                 
1326                 protected void stopPing() { go = false ; }
1327                 
1328                 @Override
1329                 public void run() 
1330                 {
1331                         while( go )
1332                         {
1333                                 try {
1334                                         LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1335                                 } catch( RemoteException e1 ) {
1336                                         System.err.println( "Unable to ping the server!" ) ;
1337                                         e1.printStackTrace() ;
1338                                         yield() ;
1339                                 }
1340                                 
1341                                 try {
1342                                         sleep( 2000 ) ;
1343                                 } catch( InterruptedException e ) {
1344                                         e.printStackTrace() ;
1345                                         yield() ;
1346                                 }
1347                         }
1348                 }
1349         }
1350         
1351         
1352         private class DialogVMServer extends Thread
1353         {
1354                 protected boolean go ;
1355                 private Socket socket ;
1356 //              private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1357                 
1358                 DialogVMServer()
1359                 {
1360                         go = true ;
1361                 }
1362                 
1363                 protected void stopDialogVMServer() 
1364                 { 
1365                         go = false ;
1366                         
1367                         if( serverSocket != null )
1368                         {
1369                                 try {
1370                                         serverSocket.close() ;                                  
1371                                 } catch( IOException e ) {
1372                                         e.printStackTrace() ;
1373                                         yield() ;
1374                                 }
1375                         }                       
1376                 }
1377                 
1378                 
1379                 @Override
1380                 public void run() 
1381                 {
1382                         try {
1383                                 serverSocket = new ServerSocket( 0 ) ;
1384                                 dialog_port = serverSocket.getLocalPort() ;
1385                                 
1386                                 System.out.println( "SocketServer listening on port " + dialog_port ) ;
1387                         } catch( IOException e ) {
1388                                 System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
1389                                 e.printStackTrace() ;
1390                                 System.exit( 1 ) ;
1391                         }       
1392                         
1393                         while( go )
1394                         {                               
1395                                 try {
1396                                         socket = serverSocket.accept() ;
1397                                         
1398                                         new DialogVM( socket ).start() ;
1399
1400                                 } catch( IOException e ) {
1401                                         System.err.println( "Problem with the accept function!" ) ;
1402                                         e.printStackTrace() ;
1403                                         yield() ;
1404                                 }
1405                         }
1406                 }
1407         }
1408
1409         
1410         private class DialogVM extends Thread
1411         {
1412                 private Socket socket ;
1413                 private BufferedReader reader ;
1414                 private String line ;
1415                 
1416                 DialogVM( Socket _socket ) { socket = _socket ; }
1417         
1418                 @Override
1419                 public void run() 
1420                 {
1421                         try {
1422                                 reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
1423                         } catch( IOException e ) {
1424                                 System.err.println( "Unable to open a dialog socket with the VM!" ) ;
1425                                 e.printStackTrace() ;
1426                                 yield() ;
1427                         }
1428
1429                         try {
1430                                 line = null ;
1431
1432                                 if( reader != null )
1433                                 {
1434                                         line = reader.readLine() ;
1435                                 }
1436
1437                                 /** VM is starting -- retrieving informations **/
1438                                 if( line != null &&  line.equalsIgnoreCase( "infos" ) )
1439                                 {
1440                                         /* Receiving name */
1441                                         machine.setName( reader.readLine() ) ;
1442
1443                                         /* Receiving IP */
1444                                         String ip = reader.readLine() ;
1445                                         if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1446                                         {
1447                                                 System.err.println( "VM IP not well configured!!" ) ;
1448                                         }
1449
1450                                         /* Close streams */
1451                                         reader.close() ; reader = null ;
1452                                         socket.close() ; socket = null ;
1453
1454                                 } else if( line != null &&  line.equalsIgnoreCase( "save" ) ) {
1455                                         /** It's time to do a save **/
1456
1457                                         try {
1458                                                 machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1459                                         } catch( Exception e ) {
1460                                                 System.err.println( "Problem while reading the computation id!" ) ;
1461                                                 e.printStackTrace() ;
1462                                         }
1463
1464                                         synchronized( saveRequest ) 
1465                                         {
1466                                                 try {
1467                                                         LocalHost.Instance().getServerStub().requestSave( LocalHost.Instance().getIP() ) ;
1468                                                 } catch( RemoteException e ) {
1469                                                         System.err.println( "Unable to request save to server!" ) ;
1470                                                         e.printStackTrace() ;
1471                                                 }
1472
1473                                                 try {
1474                                                         saveRequest.wait() ;
1475                                                 } catch( InterruptedException e ) {
1476                                                         e.printStackTrace();
1477                                                 }
1478                                         }
1479
1480                                         if( saveRequest.getStatus() )
1481                                         {
1482                                                 /* Close streams */
1483                                                 reader.close() ; reader = null ;
1484                                                 socket.close() ; socket = null ;
1485
1486                                                 saveRequest.setStatus( false ) ;
1487
1488                                                 /* Requesting the VM save */
1489                                                 if( saveVM() == 1 )
1490                                                 {
1491                                                         System.err.println( "Problem while saving. Exiting ..." ) ;
1492                                                         pingServer.stopPing() ;
1493                                                         emergencyStop() ;
1494                                                         System.exit( 1 ) ;
1495                                                 }
1496                                         } else {
1497                                                 sendSaveOkVM() ;
1498                                         }
1499                                 } else if( line != null &&  line.equalsIgnoreCase( "quit" ) ) {
1500                                         /** Computation is done, we can shutdown the VM **/
1501
1502                                         try {
1503                                                 Thread.sleep( 5000 ) ;
1504                                         } catch( InterruptedException e ) {
1505                                                 e.printStackTrace() ;
1506                                         }
1507
1508                                         /* Close streams */
1509                                         reader.close() ; reader = null ;
1510                                         socket.close() ; socket = null ;
1511
1512                                         stopVM() ;
1513
1514                                         machine.setStatus( "connected" ) ;
1515                                         try {
1516                                                 LocalHost.Instance().getServerStub().changeStatus( 
1517                                                                 LocalHost.Instance().getIP(), "connected" ) ;
1518                                         } catch( RemoteException e ) {
1519                                                 System.err.println( "Unable to inform the server of the VM status!" ) ;
1520                                                 e.printStackTrace() ;
1521                                         }
1522                                         
1523                                         try {
1524                                                 LocalHost.Instance().getServerStub().endApplication() ;
1525                                         } catch( RemoteException e ) {
1526                                                 System.err.println( "Unable to inform the server of the end of application!" ) ;
1527                                                 e.printStackTrace() ;
1528                                         }
1529                                 } else if( line != null &&  line.equalsIgnoreCase( "start" ) ) {
1530                                         /** Computation is starting **/
1531
1532                                         /* Close streams */
1533                                         reader.close() ; reader = null ;
1534                                         socket.close() ; socket = null ;
1535                                         
1536                                         try {
1537                                                 LocalHost.Instance().getServerStub().goApplication() ;
1538                                         } catch( RemoteException e ) {
1539                                                 System.err.println( "Unable to inform the server of the end of application!" ) ;
1540                                                 e.printStackTrace() ;
1541                                         }
1542                                 }
1543
1544                         } catch( IOException e ) {
1545                                 e.printStackTrace() ;
1546                                 yield() ;
1547                         }
1548                 }
1549         }
1550
1551         @Override
1552         public void stop() 
1553         {
1554                 stopVM() ;
1555                 
1556                 pingServer.stopPing() ;
1557                 
1558                 dialogVmServer.stopDialogVMServer() ;
1559                 
1560                 // unexportObject ??
1561                 
1562                 System.exit( 0 ) ;
1563         }
1564
1565         @Override
1566         public String getIPHost() 
1567         {
1568                 return LocalHost.Instance().getIP() ;
1569         }
1570         
1571         @Override
1572         public String getName()
1573         {
1574                 return LocalHost.Instance().getName() ;
1575         }
1576         
1577         
1578         @Override
1579         public void saveOk() 
1580         {
1581                 String save_name = machine.getName() + "_last_" + machine.getComputationId() +
1582                         ".tar.gz" ;
1583                 
1584                 String save_new = machine.getName() + "_new_" 
1585                   + machine.getComputationId() + ".tar.gz" ;
1586                 
1587                 String[] command = new String[]{ "/bin/mv", 
1588                                 working_directory + "/" + save_new, 
1589                                 working_directory + "/" + save_name } ;
1590                 
1591                 try {
1592                         Process p = Runtime.getRuntime().exec( command ) ;
1593                         p.waitFor() ;
1594                 
1595                         if( p.exitValue() == 0 )
1596                         {
1597                                 machine.setSave_last( save_name ) ;
1598                                 System.out.println( "Last save OK" ) ;
1599                         } else {
1600                                 System.err.println( "Last save NOK!" ) ;
1601                                 System.err.println( "Error: " ) ;
1602                                 printProcessError( p ) ;
1603                         }
1604                 } catch( IOException e ) {
1605                         System.err.println( "Error during last archive move:" ) ;
1606                         e.printStackTrace() ;
1607                 } catch( InterruptedException e ) {
1608                         e.printStackTrace() ;
1609                 }
1610                 
1611                 // Changing on save neighbors
1612                 for( int i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1613                 {
1614                         try {
1615                                 machine.getSaveNeighbors().get( i ).getStub().changeSaveName( save_new, save_name, machine.getComputationId() ) ;
1616                         } catch( RemoteException e ) {
1617                                 System.err.println( "Unable to change save name on " + machine.getSaveNeighbors().get( i ).getName() + "!" ) ;
1618                                 e.printStackTrace() ;
1619                         }
1620                 }
1621                 
1622                 // Informing the server
1623                 int ret = 1 ;
1624                 try {
1625                         ret = LocalHost.Instance().getServerStub().changeSaveName( LocalHost.Instance().getIP(), save_name ) ;
1626                 } catch( RemoteException e ) {
1627                         System.err.println( "Unable to inform the server about the new save name!" ) ;
1628                         e.printStackTrace() ;
1629                 }
1630                 
1631                 if( ret == 0 )
1632                 {
1633                         System.out.println( "Successfully informing the server about the new save name." ) ;
1634                 } else {
1635                         System.err.println( "Problem on the server while informing it about the new save name!" ) ;
1636                 }
1637                 
1638                 // Ok here
1639                 lastSaveOk = true ;
1640         }
1641         
1642         
1643         public void changeSaveName( String _n1, String _n2, int _id )
1644         {
1645                 if( _n1 != null && _n1.length() > 0 )
1646                 {
1647                         System.out.println( "Changing save name for processus " + _id + " ... " ) ;
1648                         
1649                         String[] command = new String[]{ "/bin/mv", 
1650                                         working_directory + "/" + _n1, 
1651                                         working_directory + "/" + _n2 } ;
1652                         
1653                         try {
1654                                 Process p = Runtime.getRuntime().exec( command ) ;
1655                                 p.waitFor() ;
1656                         
1657                                 if( p.exitValue() == 0 )
1658                                 {
1659                                         System.out.println( "Change save name OK" ) ;
1660                                 } else {
1661                                         System.err.println( "Change save name NOK!" ) ;
1662                                         System.err.println( "Error: " ) ;
1663                                         printProcessError( p ) ;
1664                                 }
1665                         } catch( IOException e ) {
1666                                 System.err.println( "Error during save renaming:" ) ;
1667                                 e.printStackTrace() ;
1668                         } catch( InterruptedException e ) {
1669                                 e.printStackTrace() ;
1670                         }
1671                 }
1672         }
1673
1674         
1675         @Override
1676         public void setSaveNeighbor( SaveNeighbor _sn )
1677         {
1678                 if( _sn != null )
1679                 {
1680                         ArrayList<SaveNeighbor> as = new ArrayList<SaveNeighbor>() ;
1681                         as.add( _sn ) ;
1682                         
1683                         System.out.println( "Save neighbor: " + _sn.getName() ) ;
1684                         
1685                         machine.setSaveNeighbors( as ) ;
1686                 }
1687         }
1688         
1689         
1690         @Override
1691         public void setSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1692         {
1693                 if( _sn != null && _sn.size() > 0 )
1694                 {
1695                         System.out.print( "Save neighbors: " ) ;
1696                         for( int i = 0 ; i < _sn.size() ; i++ )
1697                         {
1698                                 System.out.print( _sn.get( i ).getName() ) ;
1699                                 
1700                                 if( i != _sn.size() - 1 )
1701                                 {
1702                                         System.out.print( ", " ) ;
1703                                 } else {
1704                                         System.out.println( "." ) ;
1705                                 }
1706                         }
1707                         
1708                         machine.setSaveNeighbors( _sn ) ;
1709                 }
1710         }
1711
1712         
1713         @Override
1714         public void addSaveNeighbor( SaveNeighbor _sn )
1715         {
1716                 if( _sn != null )
1717                 {
1718                         System.out.println( "Adding save neighbor: " + _sn.getName() ) ;
1719                         
1720                         machine.getSaveNeighbors().add( _sn ) ;
1721                 }
1722         }
1723         
1724         
1725         @Override
1726         public void addSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1727         {
1728                 if( _sn != null && _sn.size() > 0 )
1729                 {
1730                         System.out.print( "Adding save neighbors: " ) ;
1731                         for( int i = 0 ; i < _sn.size() ; i++ )
1732                         {
1733                                 System.out.print( _sn.get( i ).getName() ) ;
1734                                                         
1735                                 if( i != _sn.size() - 1 )
1736                                 {
1737                                         System.out.print( ", " ) ;
1738                                 } else {
1739                                         System.out.println( "." ) ;
1740                                 }
1741                                 
1742                                 machine.getSaveNeighbors().add( _sn.get( i ) ) ;
1743                         }
1744                 }
1745         }
1746         
1747         
1748         @Override
1749         public void replaceSaveNeighbor( SaveNeighbor _old, SaveNeighbor _new )
1750         {
1751                 System.out.print( "Replacing a save neihgbor ... " ) ;
1752                 if( _old != null && _new != null )
1753                 {
1754                         int i = 0 ;
1755                         boolean change = false ;
1756                         
1757                         for( i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1758                         {
1759                                 if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old.getIPHost() ) )
1760                                 {
1761                                         machine.getSaveNeighbors().set( i, _new ) ;
1762                                         System.out.println( "Save neighbor successfully changed." ) ;
1763                                         change = true ;
1764                                         break ;
1765                                 }
1766                         }
1767                         
1768                         if( ! change )
1769                         {
1770                                 System.out.println( "I am not concerned by the modification." ) ;
1771                         }
1772                 }
1773         }
1774         
1775         
1776         @Override
1777         public int retrieveSave( String _saveName )
1778         {               
1779                 if( _saveName != null )
1780                 {
1781                         if( ! _saveName.equalsIgnoreCase( "none" ) )
1782                         {
1783                                 machine.setSave_last( _saveName ) ;
1784                         } else {
1785                                 System.err.println( "I have no save to retrieve!!" ) ;
1786                                 return 1 ;
1787                         }
1788                         
1789                         // TODO NEIGHBORS !!!!
1790                         boolean ok = false ;
1791                         int i = 0 ;
1792                         
1793                         while( ! ok && i < machine.getSaveNeighbors().size() )
1794                         {
1795                                 System.out.print( "Retrieving a save on " + machine.getSaveNeighbors().get( 0 ).getName() + " ... " ) ;
1796                                 
1797                                 String command[] = {} ;
1798                                 
1799                                 command = new String[]{ "/usr/bin/scp", 
1800                                                 machine.getSaveNeighbors().get( i ).getIPHost() + ":" +
1801                                                 machine.getSaveNeighbors().get( i ).getWorkingDirectory() + "/" +
1802                                                 machine.getSave_last(),
1803                                                 working_directory } ;
1804                                 
1805                                 try {
1806                                         Process p = Runtime.getRuntime().exec( command ) ;
1807                                         p.waitFor() ;
1808                         
1809                                         if( p.exitValue() == 0 )
1810                                         {
1811                                                 System.out.println( "Archive successfully retrieved." ) ;
1812                                                 isRestartedSave = true ;
1813                                                 ok = true ;
1814                                         } else {
1815                                                 System.err.println( "Archive not retrieved!" ) ;
1816                                                 System.err.println( "Error: " ) ; 
1817
1818                                                 printProcessError( p ) ;
1819                                         }
1820                                 } catch( IOException e ) {
1821                                         System.err.println( "Error during archive retrieve command: " ) ;
1822                                         e.printStackTrace() ;
1823                                 } catch( InterruptedException e ) {
1824                                         e.printStackTrace() ;
1825                                 }
1826                                 
1827                                 i++ ;
1828                         }
1829                         
1830                         if( ok )
1831                         {
1832                                 return 0 ;
1833                         } else {
1834                                 System.err.println( "Unable to retrieve a save archive from any neighbor!" ) ;
1835                         }
1836                 }
1837                 
1838                 return 1 ;
1839                 
1840         }
1841
1842         
1843         @Override
1844         public String getIPVM() throws RemoteException 
1845         {
1846                 if( machine != null )
1847                 {
1848                         return machine.getIp() ;
1849                 }
1850                 
1851                 return null ;
1852         }
1853         
1854         
1855         @Override
1856         public void setIPVM( String _ipVM ) throws RemoteException 
1857         {
1858                 if( _ipVM != null && ! _ipVM.isEmpty() )
1859                 {
1860                         System.out.println( "The VM IP is now: " + _ipVM ) ;
1861                         machine.setIp( _ipVM ) ;
1862                 }
1863         }
1864         
1865         
1866         public String getWorkingDirectory()
1867         {
1868                 return working_directory ;
1869         }
1870         
1871         
1872         public Integer deployVM( String _name, String _archive, String _directory )
1873         {
1874                 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 )
1875                 {
1876                         File file = new File( working_directory + "/" + _archive ) ;
1877                         if( ! file.exists() )
1878                         {
1879                                 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1880                                 file = null ;
1881                                 return 2 ;
1882                         } else if( file.isDirectory() ) {
1883                                 System.err.println( _archive + " is a directory!" ) ;
1884                                 file = null ;
1885                                 return 1 ;
1886                         }
1887                         
1888                         file = null ;
1889                         
1890                         machine.setName( _name ) ;
1891                         machine.setInitial_archive_name( _archive ) ;
1892                         machine.setDirectory( _directory ) ;
1893                         
1894                         if( machine.deployInitialVM() == 1 ) 
1895                         {
1896                                 System.err.println( "Unable to deploy the initial VM archive!" ) ;
1897                         } else {                                
1898                                 return 0 ;
1899                         }
1900                 }
1901                 
1902                 return 1 ;
1903         }
1904         
1905         
1906         @Override
1907         public void responseSave( boolean _b )
1908         {
1909                 synchronized( saveRequest )
1910                 {
1911                         saveRequest.setStatus( _b ) ;
1912                         try {
1913                                 saveRequest.notifyAll() ;
1914                         } catch( Exception e ) {
1915                                 e.printStackTrace() ;
1916                         }
1917                 }
1918         }
1919         
1920         
1921         private class SaveRequest
1922         {
1923                 boolean status ;
1924                 
1925                 SaveRequest()
1926                 {
1927                         status = false ;
1928                 }
1929                 
1930                 protected boolean getStatus() { return status ; }
1931                 
1932                 protected void setStatus( boolean _b ) { status = _b ; }
1933         }
1934         
1935         private class SaveProcess
1936         {
1937                 boolean status ;
1938                 
1939                 SaveProcess()
1940                 {
1941                         status = false ;
1942                 }
1943                 
1944                 protected boolean getStatus() { return status ; }
1945                 
1946                 protected void setStatus( boolean _b ) { status = _b ; }
1947         }
1948         
1949 }
1950
1951
1952 /** La programmation est un art, respectons ceux qui la pratiquent !! **/