Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Release of threads, and adding error messages.
[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" ) ) 
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 start() 
1264         {
1265                 /** Registering on server **/
1266                 Integer ret = 0 ;
1267                 try {
1268                         ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1269                 } catch (RemoteException e1) {  
1270                         e1.printStackTrace();
1271                         return 1 ;
1272                 }
1273                 
1274                 switch( ret )
1275                 {
1276                 case 0: System.out.println( "Successfully registered on server." ) ; break ;
1277                 case 1: System.err.println( "Problem on server while registreting!" ) ; return 1 ;
1278                 case 2: System.out.println( "Already registered on server!" ) ; break ;                 
1279                 }
1280                 
1281                 /** Retrieving VM assigned IP **/
1282                 String vmIP = null ;
1283                 
1284                 try {
1285                         vmIP = LocalHost.Instance().getServerStub().getAssociatedIP( 
1286                                         LocalHost.Instance().getIP() ) ;
1287                 } catch (RemoteException e) {
1288                         System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
1289                         e.printStackTrace() ;
1290                         return 1 ;
1291                 }
1292                 
1293                 machine.setIp( vmIP ) ;
1294                 
1295                 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1296
1297                 
1298                 /** Starting alive ping to server **/
1299                 pingServer = new PingServer() ;
1300                 pingServer.start() ;
1301
1302                 /** Starting socket server for VM dialog **/
1303                 dialogVmServer = new DialogVMServer() ;
1304                 dialogVmServer.start() ;
1305                 
1306                 return 0 ;
1307         }
1308         
1309         
1310         private class PingServer extends Thread
1311         {
1312                 protected boolean go ;
1313                 
1314                 PingServer()
1315                 {
1316                         go = true ;
1317                 }
1318                 
1319                 protected void stopPing() { go = false ; }
1320                 
1321                 @Override
1322                 public void run() 
1323                 {
1324                         while( go )
1325                         {
1326                                 try {
1327                                         LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1328                                 } catch( RemoteException e1 ) {
1329                                         System.err.println( "Unable to ping the server!" ) ;
1330                                         e1.printStackTrace() ;
1331                                         yield() ;
1332                                 }
1333                                 
1334                                 try {
1335                                         sleep( 2000 ) ;
1336                                 } catch( InterruptedException e ) {
1337                                         e.printStackTrace() ;
1338                                         yield() ;
1339                                 }
1340                         }
1341                 }
1342         }
1343         
1344         
1345         private class DialogVMServer extends Thread
1346         {
1347                 protected boolean go ;
1348                 private Socket socket ;
1349 //              private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1350                 
1351                 DialogVMServer()
1352                 {
1353                         go = true ;
1354                 }
1355                 
1356                 protected void stopDialogVMServer() 
1357                 { 
1358                         go = false ;
1359                         
1360                         if( serverSocket != null )
1361                         {
1362                                 try {
1363                                         serverSocket.close() ;                                  
1364                                 } catch( IOException e ) {
1365                                         e.printStackTrace() ;
1366                                         yield() ;
1367                                 }
1368                         }                       
1369                 }
1370                 
1371                 
1372                 @Override
1373                 public void run() 
1374                 {
1375                         try {
1376                                 serverSocket = new ServerSocket( 0 ) ;
1377                                 dialog_port = serverSocket.getLocalPort() ;
1378                                 
1379                                 System.out.println( "SocketServer listening on port " + dialog_port ) ;
1380                         } catch( IOException e ) {
1381                                 System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
1382                                 e.printStackTrace() ;
1383                                 System.exit( 1 ) ;
1384                         }       
1385                         
1386                         while( go )
1387                         {                               
1388                                 try {
1389                                         socket = serverSocket.accept() ;
1390                                         
1391                                         new DialogVM( socket ).start() ;
1392
1393                                 } catch( IOException e ) {
1394                                         System.err.println( "Problem with the accept function!" ) ;
1395                                         e.printStackTrace() ;
1396                                         yield() ;
1397                                 }
1398                         }
1399                 }
1400         }
1401
1402         
1403         private class DialogVM extends Thread
1404         {
1405                 private Socket socket ;
1406                 private BufferedReader reader ;
1407                 private String line ;
1408                 
1409                 DialogVM( Socket _socket ) { socket = _socket ; }
1410         
1411                 @Override
1412                 public void run() 
1413                 {
1414                         try {
1415                                 reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
1416                         } catch( IOException e ) {
1417                                 System.err.println( "Unable to open a dialog socket with the VM!" ) ;
1418                                 e.printStackTrace() ;
1419                                 yield() ;
1420                         }
1421
1422                         try {
1423                                 line = null ;
1424
1425                                 if( reader != null )
1426                                 {
1427                                         line = reader.readLine() ;
1428                                 }
1429
1430                                 /** VM is starting -- retrieving informations **/
1431                                 if( line != null &&  line.equalsIgnoreCase( "infos" ) )
1432                                 {
1433                                         /* Receiving name */
1434                                         machine.setName( reader.readLine() ) ;
1435
1436                                         /* Receiving IP */
1437                                         String ip = reader.readLine() ;
1438                                         if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1439                                         {
1440                                                 System.err.println( "VM IP not well configured!!" ) ;
1441                                         }
1442
1443                                         /* Close streams */
1444                                         reader.close() ; reader = null ;
1445                                         socket.close() ; socket = null ;
1446
1447                                 } else if( line != null &&  line.equalsIgnoreCase( "save" ) ) {
1448                                         /** It's time to do a save **/
1449
1450                                         try {
1451                                                 machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1452                                         } catch( Exception e ) {
1453                                                 System.err.println( "Problem while reading the computation id!" ) ;
1454                                                 e.printStackTrace() ;
1455                                         }
1456
1457                                         synchronized( saveRequest ) 
1458                                         {
1459                                                 try {
1460                                                         LocalHost.Instance().getServerStub().requestSave( LocalHost.Instance().getIP() ) ;
1461                                                 } catch( RemoteException e ) {
1462                                                         System.err.println( "Unable to request save to server!" ) ;
1463                                                         e.printStackTrace() ;
1464                                                 }
1465
1466                                                 try {
1467                                                         saveRequest.wait() ;
1468                                                 } catch( InterruptedException e ) {
1469                                                         e.printStackTrace();
1470                                                 }
1471                                         }
1472
1473                                         if( saveRequest.getStatus() )
1474                                         {
1475                                                 /* Close streams */
1476                                                 reader.close() ; reader = null ;
1477                                                 socket.close() ; socket = null ;
1478
1479                                                 saveRequest.setStatus( false ) ;
1480
1481                                                 /* Requesting the VM save */
1482                                                 saveVM() ;
1483                                         } else {
1484                                                 sendSaveOkVM() ;
1485                                         }
1486                                 } else if( line != null &&  line.equalsIgnoreCase( "quit" ) ) {
1487                                         /** Computation is done, we can shutdown the VM **/
1488
1489                                         try {
1490                                                 Thread.sleep( 5000 ) ;
1491                                         } catch( InterruptedException e ) {
1492                                                 e.printStackTrace() ;
1493                                         }
1494
1495                                         /* Close streams */
1496                                         reader.close() ; reader = null ;
1497                                         socket.close() ; socket = null ;
1498
1499                                         stopVM() ;
1500
1501                                         machine.setStatus( "connected" ) ;
1502                                         try {
1503                                                 LocalHost.Instance().getServerStub().changeStatus( 
1504                                                                 LocalHost.Instance().getIP(), "connected" ) ;
1505                                         } catch( RemoteException e ) {
1506                                                 System.err.println( "Unable to inform the server of the VM status!" ) ;
1507                                                 e.printStackTrace() ;
1508                                         }
1509                                         
1510                                         try {
1511                                                 LocalHost.Instance().getServerStub().endApplication() ;
1512                                         } catch( RemoteException e ) {
1513                                                 System.err.println( "Unable to inform the server of the end of application!" ) ;
1514                                                 e.printStackTrace() ;
1515                                         }
1516                                 } else if( line != null &&  line.equalsIgnoreCase( "start" ) ) {
1517                                         /** Computation is starting **/
1518
1519                                         /* Close streams */
1520                                         reader.close() ; reader = null ;
1521                                         socket.close() ; socket = null ;
1522                                         
1523                                         try {
1524                                                 LocalHost.Instance().getServerStub().goApplication() ;
1525                                         } catch( RemoteException e ) {
1526                                                 System.err.println( "Unable to inform the server of the end of application!" ) ;
1527                                                 e.printStackTrace() ;
1528                                         }
1529                                 }
1530
1531                         } catch( IOException e ) {
1532                                 e.printStackTrace() ;
1533                                 yield() ;
1534                         }
1535                 }
1536         }
1537
1538         @Override
1539         public void stop() 
1540         {
1541                 stopVM() ;
1542                 
1543                 pingServer.stopPing() ;
1544                 
1545                 dialogVmServer.stopDialogVMServer() ;
1546                 
1547                 // unexportObject ??
1548                 
1549                 System.exit( 0 ) ;
1550         }
1551
1552         @Override
1553         public String getIPHost() 
1554         {
1555                 return LocalHost.Instance().getIP() ;
1556         }
1557         
1558         @Override
1559         public String getName()
1560         {
1561                 return LocalHost.Instance().getName() ;
1562         }
1563         
1564         
1565         @Override
1566         public void saveOk() 
1567         {
1568                 String save_name = machine.getName() + "_last_" + machine.getComputationId() +
1569                         ".tar.gz" ;
1570                 
1571                 String save_new = machine.getName() + "_new_" 
1572                   + machine.getComputationId() + ".tar.gz" ;
1573                 
1574                 String[] command = new String[]{ "/bin/mv", 
1575                                 working_directory + "/" + save_new, 
1576                                 working_directory + "/" + save_name } ;
1577                 
1578                 try {
1579                         Process p = Runtime.getRuntime().exec( command ) ;
1580                         p.waitFor() ;
1581                 
1582                         if( p.exitValue() == 0 )
1583                         {
1584                                 machine.setSave_last( save_name ) ;
1585                                 System.out.println( "Last save OK" ) ;
1586                         } else {
1587                                 System.err.println( "Last save NOK!" ) ;
1588                                 System.err.println( "Error: " ) ;
1589                                 printProcessError( p ) ;
1590                         }
1591                 } catch( IOException e ) {
1592                         System.err.println( "Error during last archive move:" ) ;
1593                         e.printStackTrace() ;
1594                 } catch( InterruptedException e ) {
1595                         e.printStackTrace() ;
1596                 }
1597                 
1598                 // Changing on save neighbors
1599                 for( int i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1600                 {
1601                         try {
1602                                 machine.getSaveNeighbors().get( i ).getStub().changeSaveName( save_new, save_name, machine.getComputationId() ) ;
1603                         } catch( RemoteException e ) {
1604                                 System.err.println( "Unable to change save name on " + machine.getSaveNeighbors().get( i ).getName() + "!" ) ;
1605                                 e.printStackTrace() ;
1606                         }
1607                 }
1608                 
1609                 // Informing the server
1610                 int ret = 1 ;
1611                 try {
1612                         ret = LocalHost.Instance().getServerStub().changeSaveName( LocalHost.Instance().getIP(), save_name ) ;
1613                 } catch( RemoteException e ) {
1614                         System.err.println( "Unable to inform the server about the new save name!" ) ;
1615                         e.printStackTrace() ;
1616                 }
1617                 
1618                 if( ret == 0 )
1619                 {
1620                         System.out.println( "Successfully informing the server about the new save name." ) ;
1621                 } else {
1622                         System.err.println( "Problem on the server while informing it about the new save name!" ) ;
1623                 }
1624                 
1625                 // Ok here
1626                 lastSaveOk = true ;
1627         }
1628         
1629         
1630         public void changeSaveName( String _n1, String _n2, int _id )
1631         {
1632                 if( _n1 != null && _n1.length() > 0 )
1633                 {
1634                         System.out.println( "Changing save name for processus " + _id + " ... " ) ;
1635                         
1636                         String[] command = new String[]{ "/bin/mv", 
1637                                         working_directory + "/" + _n1, 
1638                                         working_directory + "/" + _n2 } ;
1639                         
1640                         try {
1641                                 Process p = Runtime.getRuntime().exec( command ) ;
1642                                 p.waitFor() ;
1643                         
1644                                 if( p.exitValue() == 0 )
1645                                 {
1646                                         System.out.println( "Change save name OK" ) ;
1647                                 } else {
1648                                         System.err.println( "Change save name NOK!" ) ;
1649                                         System.err.println( "Error: " ) ;
1650                                         printProcessError( p ) ;
1651                                 }
1652                         } catch( IOException e ) {
1653                                 System.err.println( "Error during save renaming:" ) ;
1654                                 e.printStackTrace() ;
1655                         } catch( InterruptedException e ) {
1656                                 e.printStackTrace() ;
1657                         }
1658                 }
1659         }
1660
1661         
1662         @Override
1663         public void setSaveNeighbor( SaveNeighbor _sn )
1664         {
1665                 if( _sn != null )
1666                 {
1667                         ArrayList<SaveNeighbor> as = new ArrayList<SaveNeighbor>() ;
1668                         as.add( _sn ) ;
1669                         
1670                         System.out.println( "Save neighbor: " + _sn.getName() ) ;
1671                         
1672                         machine.setSaveNeighbors( as ) ;
1673                 }
1674         }
1675         
1676         
1677         @Override
1678         public void setSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1679         {
1680                 if( _sn != null && _sn.size() > 0 )
1681                 {
1682                         System.out.print( "Save neighbors: " ) ;
1683                         for( int i = 0 ; i < _sn.size() ; i++ )
1684                         {
1685                                 System.out.print( _sn.get( i ).getName() ) ;
1686                                 
1687                                 if( i != _sn.size() - 1 )
1688                                 {
1689                                         System.out.print( ", " ) ;
1690                                 } else {
1691                                         System.out.println( "." ) ;
1692                                 }
1693                         }
1694                         
1695                         machine.setSaveNeighbors( _sn ) ;
1696                 }
1697         }
1698
1699         
1700         @Override
1701         public void addSaveNeighbor( SaveNeighbor _sn )
1702         {
1703                 if( _sn != null )
1704                 {
1705                         System.out.println( "Adding save neighbor: " + _sn.getName() ) ;
1706                         
1707                         machine.getSaveNeighbors().add( _sn ) ;
1708                 }
1709         }
1710         
1711         
1712         @Override
1713         public void addSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1714         {
1715                 if( _sn != null && _sn.size() > 0 )
1716                 {
1717                         System.out.print( "Adding save neighbors: " ) ;
1718                         for( int i = 0 ; i < _sn.size() ; i++ )
1719                         {
1720                                 System.out.print( _sn.get( i ).getName() ) ;
1721                                                         
1722                                 if( i != _sn.size() - 1 )
1723                                 {
1724                                         System.out.print( ", " ) ;
1725                                 } else {
1726                                         System.out.println( "." ) ;
1727                                 }
1728                                 
1729                                 machine.getSaveNeighbors().add( _sn.get( i ) ) ;
1730                         }
1731                 }
1732         }
1733         
1734         
1735         @Override
1736         public void replaceSaveNeighbor( SaveNeighbor _old, SaveNeighbor _new )
1737         {
1738                 System.out.print( "Replacing a save neihgbor ... " ) ;
1739                 if( _old != null && _new != null )
1740                 {
1741                         int i = 0 ;
1742                         boolean change = false ;
1743                         
1744                         for( i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1745                         {
1746                                 if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old.getIPHost() ) )
1747                                 {
1748                                         machine.getSaveNeighbors().set( i, _new ) ;
1749                                         System.out.println( "Save neighbor successfully changed." ) ;
1750                                         change = true ;
1751                                         break ;
1752                                 }
1753                         }
1754                         
1755                         if( ! change )
1756                         {
1757                                 System.out.println( "I am not concerned by the modification." ) ;
1758                         }
1759                 }
1760         }
1761         
1762         
1763         @Override
1764         public int retrieveSave( String _saveName )
1765         {               
1766                 if( _saveName != null )
1767                 {
1768                         if( ! _saveName.equalsIgnoreCase( "none" ) )
1769                         {
1770                                 machine.setSave_last( _saveName ) ;
1771                         } else {
1772                                 System.err.println( "I have no save to retrieve!!" ) ;
1773                                 return 1 ;
1774                         }
1775                         
1776                         // TODO NEIGHBORS !!!!
1777                         boolean ok = false ;
1778                         int i = 0 ;
1779                         
1780                         while( ! ok && i < machine.getSaveNeighbors().size() )
1781                         {
1782                                 System.out.print( "Retrieving a save on " + machine.getSaveNeighbors().get( 0 ).getName() + " ... " ) ;
1783                                 
1784                                 String command[] = {} ;
1785                                 
1786                                 command = new String[]{ "/usr/bin/scp", 
1787                                                 machine.getSaveNeighbors().get( i ).getIPHost() + ":" +
1788                                                 machine.getSaveNeighbors().get( i ).getWorkingDirectory() + "/" +
1789                                                 machine.getSave_last(),
1790                                                 working_directory } ;
1791                                 
1792                                 try {
1793                                         Process p = Runtime.getRuntime().exec( command ) ;
1794                                         p.waitFor() ;
1795                         
1796                                         if( p.exitValue() == 0 )
1797                                         {
1798                                                 System.out.println( "Archive successfully retrieved." ) ;
1799                                                 isRestartedSave = true ;
1800                                                 ok = true ;
1801                                         } else {
1802                                                 System.err.println( "Archive not retrieved!" ) ;
1803                                                 System.err.println( "Error: " ) ; 
1804
1805                                                 printProcessError( p ) ;
1806                                         }
1807                                 } catch( IOException e ) {
1808                                         System.err.println( "Error during archive retrieve command: " ) ;
1809                                         e.printStackTrace() ;
1810                                 } catch( InterruptedException e ) {
1811                                         e.printStackTrace() ;
1812                                 }
1813                                 
1814                                 i++ ;
1815                         }
1816                         
1817                         if( ok )
1818                         {
1819                                 return 0 ;
1820                         } else {
1821                                 System.err.println( "Unable to retrieve a save archive from any neighbor!" ) ;
1822                         }
1823                 }
1824                 
1825                 return 1 ;
1826                 
1827         }
1828
1829         
1830         @Override
1831         public String getIPVM() throws RemoteException 
1832         {
1833                 if( machine != null )
1834                 {
1835                         return machine.getIp() ;
1836                 }
1837                 
1838                 return null ;
1839         }
1840         
1841         
1842         @Override
1843         public void setIPVM( String _ipVM ) throws RemoteException 
1844         {
1845                 if( _ipVM != null && ! _ipVM.isEmpty() )
1846                 {
1847                         System.out.println( "The VM IP is now: " + _ipVM ) ;
1848                         machine.setIp( _ipVM ) ;
1849                 }
1850         }
1851         
1852         
1853         public String getWorkingDirectory()
1854         {
1855                 return working_directory ;
1856         }
1857         
1858         
1859         public Integer deployVM( String _name, String _archive, String _directory )
1860         {
1861                 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 )
1862                 {
1863                         File file = new File( working_directory + "/" + _archive ) ;
1864                         if( ! file.exists() )
1865                         {
1866                                 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1867                                 file = null ;
1868                                 return 2 ;
1869                         } else if( file.isDirectory() ) {
1870                                 System.err.println( _archive + " is a directory!" ) ;
1871                                 file = null ;
1872                                 return 1 ;
1873                         }
1874                         
1875                         file = null ;
1876                         
1877                         machine.setName( _name ) ;
1878                         machine.setInitial_archive_name( _archive ) ;
1879                         machine.setDirectory( _directory ) ;
1880                         
1881                         if( machine.deployInitialVM() == 1 ) 
1882                         {
1883                                 System.err.println( "Unable to deploy the initial VM archive!" ) ;
1884                         } else {                                
1885                                 return 0 ;
1886                         }
1887                 }
1888                 
1889                 return 1 ;
1890         }
1891         
1892         
1893         @Override
1894         public void responseSave( boolean _b )
1895         {
1896                 synchronized( saveRequest )
1897                 {
1898                         saveRequest.setStatus( _b ) ;
1899                         try {
1900                                 saveRequest.notifyAll() ;
1901                         } catch( Exception e ) {
1902                                 e.printStackTrace() ;
1903                         }
1904                 }
1905         }
1906         
1907         
1908         private class SaveRequest
1909         {
1910                 boolean status ;
1911                 
1912                 SaveRequest()
1913                 {
1914                         status = false ;
1915                 }
1916                 
1917                 protected boolean getStatus() { return status ; }
1918                 
1919                 protected void setStatus( boolean _b ) { status = _b ; }
1920         }
1921         
1922         private class SaveProcess
1923         {
1924                 boolean status ;
1925                 
1926                 SaveProcess()
1927                 {
1928                         status = false ;
1929                 }
1930                 
1931                 protected boolean getStatus() { return status ; }
1932                 
1933                 protected void setStatus( boolean _b ) { status = _b ; }
1934         }
1935         
1936 }
1937
1938
1939 /** La programmation est un art, respectons ceux qui la pratiquent !! **/