Logo AND Algorithmique Numérique Distribuée

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