Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction and modification of save mechanisms.
[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( 1 ) ;
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( 1 ) ;
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( 1 ) ;
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( 1 ) ;
265                                                 return 1 ;
266                                         } catch( InterruptedException e ) {
267                                                 e.printStackTrace() ;
268                                                 sema.release() ;
269                                                 stopVM( 1 ) ;
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( 1 ) ;
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( 1 ) ;
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( 1 ) ;
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( 1 ) ;
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( 1 ) ;
371                                         return 1 ;
372                                 } catch( InterruptedException e) {
373                                         e.printStackTrace() ;
374                                         sema.release() ;
375                                         stopVM( 1 ) ;
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( 1 ) ;
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( 1 ) ;
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( 1 ) ;
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( 1 ) ;
453                                         return 1 ;
454                                 } catch( InterruptedException e ) {
455                                         e.printStackTrace() ;
456                                         sema.release() ;
457                                         stopVM( 1 ) ;
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( int _mode ) 
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                         if( _mode == 0 )
509                         try {
510                                 LocalHost.Instance().getServerStub().changeStatus( 
511                                                 LocalHost.Instance().getIP(), "undefined" ) ;
512                         } catch( RemoteException e ) {
513                                 System.err.println( "Unable to inform the server of the VM status!" ) ;
514                                 e.printStackTrace() ;
515                         }
516                         
517                         String[] command = new String[]{VmRunCommand, "-T", "player", "stop",
518                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
519                         
520                         while( ret )
521                         try {
522                                 Process p = Runtime.getRuntime().exec( command ) ;
523                                 p.waitFor() ;
524                                 
525                                 if( p.exitValue() == 0 )
526                                 {
527                                         System.out.println( "Virtual machine successfully stopped." ) ;
528                                         machine.setStatus( "stopped" ) ;
529                                         ret = false ;
530                                 } else {
531                                         System.err.println( "Virtual machine not stopped!" ) ;
532                                         ret = printProcessError( p ) ;
533                                         
534                                         if( ! ret )
535                                         {
536                                                 sema.release() ;
537                                                 return 1 ;
538                                         } else {
539                                                 retry++ ;
540                                                 if( retry >= maxRetryVM )
541                                                 {
542                                                         System.err.println( "Unable to stop VM!" ) ;
543                                                         sema.release() ;
544                                                         return 1 ;
545                                                 }
546                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
547                                                 Thread.sleep( timeRetryVM ) ;
548                                         }
549                                         
550 //                                      return 1 ;
551                                 }
552                         } catch( IOException e ) {
553                                 System.err.println( "Error during execution of stop command: " ) ;
554                                 e.printStackTrace() ;
555                                 sema.release() ;
556                                 return 1 ;
557                         } catch( InterruptedException e ) {
558                                 e.printStackTrace() ;
559                                 sema.release() ;
560                                 return 1 ;
561                         }
562                         
563                         machine.setStatus( "stopped" ) ;
564                         if( _mode == 0 )
565                         try {
566                                 LocalHost.Instance().getServerStub().changeStatus( 
567                                                 LocalHost.Instance().getIP(), "stopped" ) ;
568                         } catch( RemoteException e1 ) {
569                                 System.err.println( "Unable to inform the server of the VM stopped status!" ) ;
570                                 e1.printStackTrace() ;
571                         }
572                         
573
574                         if(     machine.checkVmx() == 0 )
575                         {
576                                 sema.release() ;
577                                 emergencyStop = false ;
578                                 return 0 ;
579                         }
580                 }
581                 
582                 sema.release() ;
583                 
584                 return 1 ;
585         }
586
587         @Override
588         public int suspendVM( int _mode ) 
589         {               
590                 if( machine != null && machine.getStatus().equalsIgnoreCase( "suspended" ) )
591                 {
592                         return 0 ;
593                 }
594                 
595                 if( machine != null && ! machine.getStatus().equalsIgnoreCase( "suspended" ) )
596                 {
597                         try {
598                                 sema.acquire() ;
599                         } catch( InterruptedException e2 ) {
600                                 System.err.println( "Problem with semaphore acquiring!" ) ;
601                                 e2.printStackTrace() ;
602                         }
603                         
604                         System.out.print( "Suspending VM ... " ) ;
605                         
606                         boolean ret = true ;
607                         int retry = 0 ;
608                         
609                         machine.setStatus( "undefined" ) ;
610                         if( _mode == 0 )
611                         {
612                                 try {
613                                         LocalHost.Instance().getServerStub().changeStatus( 
614                                                 LocalHost.Instance().getIP(), "undefined" ) ;
615                                 } catch( RemoteException e ) {
616                                         System.err.println( "Unable to inform the server of the VM status!" ) ;
617                                         e.printStackTrace() ;
618                                 }
619                         }
620                         
621                         String[] command = new String[]{VmRunCommand, "-T", "player", "suspend",
622                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
623                         
624                         while( ret )
625                         try {
626                                 Process p = Runtime.getRuntime().exec( command ) ;
627                                 p.waitFor() ;
628                                 if( p.exitValue() == 0 )
629                                 {
630                                         System.out.println( "Virtual machine successfully suspended." ) ;
631                                         machine.setStatus( "suspended" ) ;
632                                         ret = false ;
633                                 } else {
634                                         System.err.println( "Virtual machine not suspended!" ) ;
635                                         ret = printProcessError( p ) ;
636                                         
637                                         if( ! ret )
638                                         {
639                                                 sema.release() ;
640                                                 
641                                                 return 1 ;
642                                         } else {
643                                                 retry++ ;
644                                                 if( retry >= maxRetryVM )
645                                                 {
646                                                         System.err.println( "Unable to suspend VM!" ) ;
647                                                         
648                                                         sema.release() ;
649                                                         
650                                                         return 1 ;
651                                                 }
652                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
653                                                 Thread.sleep( timeRetryVM ) ;
654                                         }
655                                         
656 //                                      return 1 ;
657                                 }
658                         } catch( IOException e ) {
659                                 System.err.println( "Error during execution of suspend command: " ) ;
660                                 e.printStackTrace() ;
661                         } catch( InterruptedException e ) {
662                                 e.printStackTrace() ;
663                         }
664                         
665                         machine.setStatus( "suspended" ) ;
666                         if( _mode == 0 )
667                         {
668                                 try {
669                                         LocalHost.Instance().getServerStub().changeStatus( 
670                                                 LocalHost.Instance().getIP(), "suspended" ) ;
671                                 } catch( RemoteException e ) {
672                                         System.err.println( "Unable to inform the server of the VM suspended status!" ) ;
673                                         e.printStackTrace() ;
674                                 }
675                         }
676                         
677                         sema.release() ;
678                         
679                         return 0 ;
680                 }
681                 
682                 sema.release() ;
683                 
684                 return 1 ;
685         }
686
687         @Override
688         public int restartVM() 
689         {               
690                 if( machine != null )
691                 {
692                         System.out.print( "Restarting VM ... " ) ;
693                         
694                         boolean ret = true ;
695                         int retry = 0 ;
696                         
697                         try {
698                                 LocalHost.Instance().getServerStub().changeStatus( 
699                                                 LocalHost.Instance().getIP(), "undefined" ) ;
700                         } catch( RemoteException e ) {
701                                 System.err.println( "Unable to inform the server of the VM status!" ) ;
702                                 e.printStackTrace() ;
703                         }
704                         
705                         String[] command = new String[]{VmRunCommand, "-T", "player", "reset",
706                         working_directory + "/" + machine.getDirectory() + "/" + machine.getVmx_name()} ;
707                         
708                         while( ret )
709                         try {
710                                 Process p = Runtime.getRuntime().exec( command ) ;
711                                 p.waitFor() ;
712                                 
713                                 if( p.exitValue() == 0 )
714                                 {
715                                         System.out.println( "Virtual machine successfully restarted." ) ;
716
717                                         if( sendSaveOkVM() == 1 ) { return 1 ; }
718                                         
719                                         ret = false ;
720                                         return 0 ;
721
722                                 } else {
723                                         System.err.println( "Virtual machine not restarted!" ) ;
724                                         ret = printProcessError( p ) ;
725                                         
726                                         if( ! ret )
727                                         {
728                                                 return 1 ;
729                                         } else {
730                                                 retry++ ;
731                                                 if( retry >= maxRetryVM )
732                                                 {
733                                                         System.err.println( "Unable to start VM!" ) ;
734                                                         return 1 ;
735                                                 }
736                                                 System.out.println( "Retrying (" + retry + ") ... " ) ;
737                                                 Thread.sleep( timeRetryVM ) ;
738                                         }
739                                         
740 //                                      return 1 ;
741                                 }
742                         } catch( IOException e ) {
743                                 System.err.println( "Error during execution of restart command: " ) ;
744                                 e.printStackTrace() ;
745                         } catch( InterruptedException e ) {
746                                 e.printStackTrace() ;
747                         }
748                 }
749
750                 return 1 ;
751         }
752
753         @Override
754         public int restartVMAfterCrash() 
755         {
756                 System.out.println( "Restarting VM after a crash ..." ) ;
757                 
758                 try {
759                         LocalHost.Instance().getServerStub().changeStatus( 
760                                         LocalHost.Instance().getIP(), "undefined" ) ;
761                 } catch( RemoteException e ) {
762                         System.err.println( "Unable to inform the server of the VM status!" ) ;
763                         e.printStackTrace() ;
764                 }
765                 
766                 if( stopVM( 1 ) == 0 )
767                 {
768                         if( machine.deployLastSave() == 0 )
769                         {
770                                 if( isRestartedSave )
771                                 {
772                                         // Using the specific vmx file
773                                         machine.setDeployFault( true ) ;
774                                         
775                                         // Writing the restarted save mark
776                                         try {
777                                                 FileWriter fw = new FileWriter( new File( working_directory + "/" + machine.getDirectory() + "/fault.hpcvm" ) ) ;
778                                                 fw.write( "fault!" ) ;
779                                                 fw.flush() ;
780                                                 fw.close() ;
781                                                 fw = null ;
782                                         } catch( IOException e1 ) {
783                                                 e1.printStackTrace() ;
784                                                 System.err.println( "Unable to mark the fault!" ) ;
785                                         }       
786                                         
787                                         if( machine.checkVmx() == 1 )
788                                         {
789                                                 return 1 ;
790                                         }
791                                         
792                                 
793                                         /** Retrieving VM assigned IP **/
794                                         String vmIP = null ;
795                                 
796                                         try {
797                                                 vmIP = LocalHost.Instance().getServerStub().getAssociatedIP( 
798                                                         LocalHost.Instance().getIP() ) ;
799                                         } catch (RemoteException e) {
800                                                 System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
801                                                 e.printStackTrace() ;
802                                                 return 1 ;
803                                         }
804                                 
805                                         machine.setIp( vmIP ) ;
806                                         
807                                         isRestartedSave = false ;
808                                 }
809                                 
810                                 if( startVM( 0 ) == 0 ) 
811                                 {
812                                         if( sendSaveOkVM() == 0 )
813                                         {                                               
814                                                 try {
815                                                         LocalHost.Instance().getServerStub().restartOk( LocalHost.Instance().getIP() ) ;
816                                                 } catch( RemoteException e ) {
817                                                         System.err.println( "Unable to inform server about my successful restart!" ) ;
818                                                         e.printStackTrace() ;
819                                                 }
820                                                 
821                                                 return 0 ;
822                                         }
823                                 } else {
824                                         stopVM( 1 ) ;
825                                 }
826                         }
827                 }
828                 
829                 return 1 ;
830         }
831         
832         
833         private int sendSaveOkVM()
834         {
835                 boolean ret = true ;
836                 int retry = 0 ;
837                 
838                 /** Informing the program that it's ok **/
839                 System.out.print( "Sending OK signal to the program ... " ) ;
840                         
841                 String cmd2 = VmRunCommand + " -T " + " player " + " -gu " + machine.getVmUser() +
842                 " -gp " + machine.getVmUserPasswd() + " runScriptInGuest " +
843                 working_directory + "/" + machine.getDirectory() 
844                 + "/" + machine.getVmx_name() + " " + ushell + 
845                 " \"echo ok > /tmp/vm_save_ok\"" ;// + " -noWait " ;
846                 
847                 try {
848                         FileWriter fw = new FileWriter( new File( working_directory + "/saveOk.sh" ) ) ;
849                         fw.write( cmd2 ) ;
850                         fw.flush() ;
851                         fw.close() ;
852                 } catch( IOException e1 ) {
853                         e1.printStackTrace() ;
854                 }
855                         
856                 String[] command = new String[]{ ushell, working_directory + "/saveOk.sh"} ;
857                 
858                 while( ret )
859                 try {
860                         Process p = Runtime.getRuntime().exec( command ) ;
861                         p.waitFor() ;
862                         
863                         if( p.exitValue() == 0 )
864                         {
865                                 System.out.println( "Signal successfully sent." ) ;
866                                 ret = false ;
867                                 return 0 ;
868                         } else {
869                                 System.err.println( "Signal not sent!" ) ;
870                                 ret = printProcessError( p ) ;
871                                 
872                                 if( ! ret )
873                                 {
874                                         return 1 ;
875                                 } else {
876                                         retry++ ;
877                                         if( retry >= maxRetryVM )
878                                         {
879                                                 System.err.println( "Unable to send ok signal to VM!" ) ;
880                                                 return 1 ;
881                                         }
882                                         System.out.println( "Retrying (" + retry + ") ... " ) ;
883                                         Thread.sleep( timeRetryVM ) ;
884                                 }
885 //                              return 1 ;
886                         }
887                 } catch( IOException e ) {
888                         System.err.println( "Error during ok save signal send command: " ) ;
889                         e.printStackTrace() ;
890                         return 1 ;
891                 } catch( InterruptedException e ) {
892                         e.printStackTrace() ;
893                         return 1 ;
894                 }
895                 
896                 return 1 ;
897         }
898
899         
900         @Override
901         public int saveVM() 
902         {
903                 synchronized( saveProcess ){
904                 while( saveProcess.getStatus() )
905                 {
906                         try {
907                                 saveProcess.wait() ;
908                         } catch( InterruptedException e ) {
909                                 e.printStackTrace() ;
910                         }
911                 }}
912                 
913                 System.out.println( "Saving VM ..." ) ;
914                 saveProcess.setStatus( true ) ;
915                 
916                 long deb = System.currentTimeMillis() ;
917                 
918                 machine.setStatus( "saving" ) ;
919                 try {
920                         LocalHost.Instance().getServerStub().changeStatus( 
921                                 LocalHost.Instance().getIP(), "saving" ) ;
922                 } catch( RemoteException e ) {
923                         System.err.println( "Unable to inform the server of the VM status!" ) ;
924                         e.printStackTrace() ;
925                 }
926                 
927                 String[] command ;
928                 String saveName = "" ;
929                 boolean error = false ;
930                 
931                 
932                 if( suspendVM( 1 ) == 1 ) { return 1 ; }
933                 
934                 
935                 if( ! lastSaveOk )
936                 {
937                         String arch = "" ;
938                         File file = new File( working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ) ;
939                         if( file.exists() )
940                         {
941                                 arch = working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;
942                         }
943                         
944                         file = null ;
945                         
946                         if( arch.length() > 0 )
947                         {                       
948                                 System.out.println( "Deletion of last nok archive ... " ) ;
949                         
950                                 command = new String[]{ "/bin/rm", "-rf", arch } ;
951                                                         
952                                 try {
953                                         procSave = Runtime.getRuntime().exec( command ) ;
954                                         procSave.waitFor() ;
955                                 
956                                         if( procSave.exitValue() == 0 )
957                                         {
958                                                 System.out.println( "Last nok archive successfully deleted." ) ;
959                                         } else {
960                                                 System.err.println( "Last nok archive not deleted!" ) ;
961                                                 printProcessError( procSave ) ;
962                                         
963                                                 error = true ;
964                                         }
965                                 } catch( IOException e ) {
966                                         System.err.println( "Error during nok archive deletion command: " ) ;
967                                         error = true ;
968                                         e.printStackTrace() ;
969                                 } catch( InterruptedException e ) {
970                                         e.printStackTrace() ;
971                                         error = true ;
972                                 }
973                         }
974                 }
975                 
976                 
977                 if( ! error )
978                 {
979                         System.out.print( "Creation of the archive ... " ) ;
980                         
981                         /** Archive creation **/
982                         command = new String[]{ "/bin/tar", "-cz", "-C", working_directory,
983                                         "-f", working_directory + "/" + machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz",
984                                         machine.getDirectory() } ;
985                                 
986                         if( emergencyStop )
987                         {
988                                 return 1 ;
989                         }
990                         
991                         try {
992                                 procSave = Runtime.getRuntime().exec( command ) ;
993                                 procSave.waitFor() ;
994                                 
995                                 if( procSave.exitValue() == 0 )
996                                 {
997                                         System.out.println( "Archive successfully created." ) ;
998                                         
999                                         lastSaveOk = false ;
1000                                         
1001                                 } else {
1002                                         System.err.println( "Archive not created!" ) ;
1003                                         printProcessError( procSave ) ;
1004                                         
1005                                         error = true ;
1006                                 }
1007                         } catch( IOException e ) {
1008                                 System.err.println( "Error during archive creation command: " ) ;
1009                                 error = true ;
1010                                 e.printStackTrace() ;
1011                         } catch( InterruptedException e ) {
1012                                 e.printStackTrace() ;
1013                                 error = true ;
1014                         }
1015                         
1016                         long fin = System.currentTimeMillis() ;
1017                         
1018                         System.out.println( "Time to create the save: " + (fin-deb)/1000 + " seconds." ) ;
1019                         
1020                         
1021                         /** Restarting VM **/
1022                         if( startVM( 0 ) == 1 ) { return 1 ; }
1023                         
1024                         
1025                         /** Sending ok save signal **/
1026                         if( sendSaveOkVM() == 1 ) { return 1 ; }
1027                 
1028                         saveName = machine.getName() + "_new_" + machine.getComputationId() + ".tar.gz" ;       
1029                 
1030                         /** Sending save to neighbor **/
1031                         if( ! error )
1032                         {
1033                                 if( emergencyStop )
1034                                 {
1035                                         return 1 ;
1036                                 }
1037                                 
1038                                 ArrayList<SaveNeighbor> sn = machine.getSaveNeighbors() ;
1039                                 
1040                                 for( int i = 0 ; i < sn.size() ; i++ )
1041                                 {
1042                                         String name = sn.get( i ).getName() ;
1043                                         String wd = sn.get( i ).getWorkingDirectory() ;
1044                                         String snIP = sn.get( i ).getIPHost() ;
1045                                         
1046                                         System.out.print( "Sending save to " + name + " ... " ) ;
1047                                         
1048                                         command = new String[]{ "/usr/bin/scp", working_directory + "/" + saveName,
1049                                                                 snIP + ":" + wd } ;
1050                                 
1051                                         try {
1052                                                 procSave = Runtime.getRuntime().exec( command ) ;
1053                                                 procSave.waitFor() ;
1054                         
1055                                                 if( procSave.exitValue() == 0 )
1056                                                 {
1057                                                         System.out.println( "Archive successfully sent." ) ;
1058                                                 } else {
1059                                                         System.err.println( "Archive not sent!" ) ;
1060                                                         printProcessError( procSave ) ;
1061                                                 
1062                                                         error = true ;
1063                                                 }
1064                                         } catch( IOException e ) {
1065                                                 System.err.println( "Error during archive send command: " ) ;
1066                                                 e.printStackTrace() ;
1067                                                 error = true ;
1068                                         } catch( InterruptedException e ) {
1069                                                 e.printStackTrace() ;
1070                                                 error = true ;
1071                                         }
1072                                 }
1073                         }
1074                 }
1075                 
1076                 /** Informing the server the save is done **/
1077                 if( ! error )
1078                 {
1079                         try {
1080                                 LocalHost.Instance().getServerStub().saveOk( LocalHost.Instance().getIP(), saveName ) ;
1081                         } catch( RemoteException e ) {
1082                                 System.err.println( "Problem while informing the server about the save state!" ) ;
1083                                 e.printStackTrace() ;
1084                         }
1085                         
1086                         synchronized( saveProcess ) {
1087                         saveProcess.setStatus( false ) ;
1088                         try {
1089                                 saveProcess.notifyAll() ;
1090                         } catch( Exception e ) {}}
1091                         
1092                         return 0 ;
1093                 }
1094                 
1095                 synchronized( saveProcess ) {
1096                 saveProcess.setStatus( false ) ;
1097                 try {
1098                         saveProcess.notifyAll() ;
1099                 } catch( Exception e ) {}}
1100                 
1101                 return 1 ;
1102         }
1103
1104         
1105         @Override
1106         public int reloadConfig() 
1107         {
1108                 System.out.println( "Reloading configuration ... " ) ;
1109                 
1110                 // TODO !!!
1111                 return 0 ;
1112         }
1113
1114         public void init( String _server_ip, int _server_port, int _client_port, int _dialog_port ) 
1115         {
1116                 System.out.println( "Initialisation Client ... " ) ;
1117                 System.out.println( "IP " + LocalHost.Instance().getIP() ) ;
1118                 
1119                 server_ip = _server_ip ;
1120                 server_port = _server_port ;
1121                 client_port = _client_port ;
1122                 dialog_port = _server_port + 1 ; // _dialog_port ;
1123                 
1124                 serverStub = null ;
1125                 saveProcess = new SaveProcess() ;
1126                 saveRequest = new SaveRequest() ;
1127                 
1128                 machine = new VirtualMachine() ;
1129                 
1130                 VmRunCommand = "/usr/bin/vmrun" ;
1131 //              VmRunCommandArg = "-T player" ;
1132                 
1133 //              vm_user = "mpi" ;
1134 //              vm_user_passwd = "mpi" ;
1135                 ushell = "/bin/bash" ;
1136                 working_directory = "/localhome/vmware" ;
1137                 
1138                 wait_start = 15000 ;
1139                 max_start_try = 10 ;
1140                 
1141                 sema = new Semaphore( 1 ) ;
1142                 emergencyStop = false ;
1143                 
1144                 maxRetryVM = 10 ;
1145                 timeRetryVM = 10000 ;
1146                 
1147 //              save_interleave = 30 * 60 * 1000 ;
1148 //              date_last_save = 0 ;
1149                 
1150                 isRestartedSave = false ;
1151                 lastSaveOk = false ;
1152                 
1153                 /** Connection to server **/
1154                 try {
1155                         serverStub = (ServicesServer) Naming.lookup( "rmi://"
1156                                         + server_ip + ":" + server_port + "/Server" ) ;
1157                 } catch (MalformedURLException e) {
1158                         e.printStackTrace();
1159                 } catch (RemoteException e) {
1160                         e.printStackTrace();
1161                 } catch (NotBoundException e) {
1162                         e.printStackTrace();
1163                 }
1164                 
1165                 if( serverStub == null )
1166                 {
1167                         System.err.println( "Unable to connect to server!!" ) ;
1168                         System.err.println( "Server IP: " + server_ip + " -- server port: " + server_port ) ;
1169                         
1170                         System.exit( 1 ) ;
1171                 }
1172                 
1173                 System.out.println( "Connected to server " + server_ip + " on port " + server_port + "." ) ;
1174                 
1175                 LocalHost.Instance().setServerStub( serverStub ) ;
1176                 
1177                 
1178                 /** Creating the local server **/
1179                 exportObject() ;
1180
1181                 /** Starting all threads **/
1182                 start() ;
1183         }
1184         
1185         private void exportObject() 
1186         {
1187                 Registry reg = null ;
1188                 
1189                 try 
1190                 {       
1191                         while( true )
1192                         {
1193                                 reg = LocateRegistry.getRegistry( client_port ) ;
1194
1195                                 String tab[] = reg.list() ;
1196                         
1197                                 System.out.println( "There is an existing RMI Registry on port " +
1198                                                 client_port + " with " + tab.length + " entries!" ) ;
1199                                 for( int i = 0 ; i < tab.length ; i++ )
1200                                 {
1201                                         try {
1202                                                 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
1203                                                 {
1204                                                         System.out.println( "Register successfuly deleted!" ) ;
1205                                                 } else {
1206                                                         System.err.println( "Register undeleted !!!" ) ;
1207                                                 }
1208                                         } catch( Exception e ) {
1209                                                 e.printStackTrace() ;
1210                                         }
1211                                 } 
1212                         }
1213                 } catch( RemoteException e ) {
1214                 }                       
1215                 
1216                 try {
1217                         if ( System.getSecurityManager() == null ) 
1218                         {
1219                     System.setSecurityManager( new SecurityManager() ) ;
1220                 }
1221                         
1222                         LocateRegistry.createRegistry( client_port ) ;
1223                         LocateRegistry.getRegistry( client_port ).rebind( "Client", this ) ;
1224                         myStub = (ServicesClient) Naming.lookup( "rmi://"
1225                                         + LocalHost.Instance().getIP() + ":" + client_port
1226                                         + "/Client" ) ;
1227                 } catch( Exception e ) {
1228                         System.err.println( "Error in Client.exportObject() when creating local services:" + e ) ;
1229                         System.err.println( "Exit from Client.exportObject" ) ;
1230                         System.exit( 1 ) ;
1231                 }
1232                 
1233                 LocalHost.Instance().setStub( myStub ) ;
1234         }
1235         
1236         
1237         private boolean printProcessError( Process _p )
1238         {
1239                 boolean ret = false ;
1240                 
1241                 if( _p != null )
1242                 {
1243                         System.err.println( "Error: " + _p.exitValue() ) ;
1244                         BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ;
1245                         String line ;
1246                         try {
1247                                 while( (line = br.readLine()) != null )
1248                                 {
1249                                         System.err.println( line ) ;
1250                                         if( line.contains( "egmentation" ) || _p.exitValue() == 139 ) 
1251                                         {
1252                                                 ret = true ;
1253                                         }
1254                                 }
1255                         } catch( IOException e ) {
1256                                 e.printStackTrace() ;
1257                         }
1258                 }
1259                 
1260                 return ret ;
1261         }
1262         
1263         
1264         @Override
1265         public int echo()
1266         {
1267                 return 0 ;
1268         }
1269         
1270
1271         @Override
1272         public int start() 
1273         {
1274                 /** Registering on server **/
1275                 Integer ret = 0 ;
1276                 try {
1277                         ret = LocalHost.Instance().getServerStub().register( LocalHost.Instance().getStub() );
1278                 } catch (RemoteException e1) {  
1279                         e1.printStackTrace();
1280                         return 1 ;
1281                 }
1282                 
1283                 switch( ret )
1284                 {
1285                 case 0: System.out.println( "Successfully registered on server." ) ; break ;
1286                 case 1: System.err.println( "Problem on server while registreting!" ) ; return 1 ;
1287                 case 2: System.out.println( "Already registered on server!" ) ; break ;                 
1288                 }
1289                 
1290                 /** Retrieving VM assigned IP **/
1291                 String vmIP = null ;
1292                 
1293                 try {
1294                         vmIP = LocalHost.Instance().getServerStub().getAssociatedIP( 
1295                                         LocalHost.Instance().getIP() ) ;
1296                 } catch (RemoteException e) {
1297                         System.err.println( "Problem while retrieving the VM assigned IP!!" ) ;
1298                         e.printStackTrace() ;
1299                         return 1 ;
1300                 }
1301                 
1302                 machine.setIp( vmIP ) ;
1303                 
1304                 System.out.println( "Assigned IP address for the VM: " + vmIP ) ;
1305
1306                 
1307                 /** Starting alive ping to server **/
1308                 pingServer = new PingServer() ;
1309                 pingServer.start() ;
1310
1311                 /** Starting socket server for VM dialog **/
1312                 dialogVmServer = new DialogVMServer() ;
1313                 dialogVmServer.start() ;
1314                 
1315                 return 0 ;
1316         }
1317         
1318         
1319         private class PingServer extends Thread
1320         {
1321                 protected boolean go ;
1322                 
1323                 PingServer()
1324                 {
1325                         go = true ;
1326                 }
1327                 
1328                 protected void stopPing() { go = false ; }
1329                 
1330                 @Override
1331                 public void run() 
1332                 {
1333                         while( go )
1334                         {
1335                                 try {
1336                                         LocalHost.Instance().getServerStub().ping( LocalHost.Instance().getIP() ) ;
1337                                 } catch( RemoteException e1 ) {
1338                                         System.err.println( "Unable to ping the server!" ) ;
1339                                         e1.printStackTrace() ;
1340                                         yield() ;
1341                                 }
1342                                 
1343                                 try {
1344                                         sleep( 2000 ) ;
1345                                 } catch( InterruptedException e ) {
1346                                         e.printStackTrace() ;
1347                                         yield() ;
1348                                 }
1349                         }
1350                 }
1351         }
1352         
1353         
1354         private class DialogVMServer extends Thread
1355         {
1356                 protected boolean go ;
1357                 private Socket socket ;
1358 //              private ArrayList<DialogVM> dialogs = new ArrayList<DialogVM>() ;
1359                 
1360                 DialogVMServer()
1361                 {
1362                         go = true ;
1363                 }
1364                 
1365                 protected void stopDialogVMServer() 
1366                 { 
1367                         go = false ;
1368                         
1369                         if( serverSocket != null )
1370                         {
1371                                 try {
1372                                         serverSocket.close() ;                                  
1373                                 } catch( IOException e ) {
1374                                         e.printStackTrace() ;
1375                                         yield() ;
1376                                 }
1377                         }                       
1378                 }
1379                 
1380                 
1381                 @Override
1382                 public void run() 
1383                 {
1384                         try {
1385                                 serverSocket = new ServerSocket( 0 ) ;
1386                                 dialog_port = serverSocket.getLocalPort() ;
1387                                 
1388                                 System.out.println( "SocketServer listening on port " + dialog_port ) ;
1389                         } catch( IOException e ) {
1390                                 System.err.println( "Unable to launch the SocketServer on port " + dialog_port + "!" ) ;
1391                                 e.printStackTrace() ;
1392                                 System.exit( 1 ) ;
1393                         }       
1394                         
1395                         while( go )
1396                         {                               
1397                                 try {
1398                                         socket = serverSocket.accept() ;
1399                                         
1400                                         new DialogVM( socket ).start() ;
1401
1402                                 } catch( IOException e ) {
1403                                         System.err.println( "Problem with the accept function!" ) ;
1404                                         e.printStackTrace() ;
1405                                         yield() ;
1406                                 }
1407                         }
1408                 }
1409         }
1410
1411         
1412         private class DialogVM extends Thread
1413         {
1414                 private Socket socket ;
1415                 private BufferedReader reader ;
1416                 private String line ;
1417                 
1418                 DialogVM( Socket _socket ) { socket = _socket ; }
1419         
1420                 @Override
1421                 public void run() 
1422                 {
1423                         try {
1424                                 reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ) ;
1425                         } catch( IOException e ) {
1426                                 System.err.println( "Unable to open a dialog socket with the VM!" ) ;
1427                                 e.printStackTrace() ;
1428                                 yield() ;
1429                         }
1430
1431                         try {
1432                                 line = null ;
1433
1434                                 if( reader != null )
1435                                 {
1436                                         line = reader.readLine() ;
1437                                 }
1438
1439                                 /** VM is starting -- retrieving informations **/
1440                                 if( line != null &&  line.equalsIgnoreCase( "infos" ) )
1441                                 {
1442                                         /* Receiving name */
1443                                         machine.setName( reader.readLine() ) ;
1444
1445                                         /* Receiving IP */
1446                                         String ip = reader.readLine() ;
1447                                         if( ! ip.equalsIgnoreCase( machine.getIp() ) )
1448                                         {
1449                                                 System.err.println( "VM IP not well configured!!" ) ;
1450                                         }
1451
1452                                         /* Close streams */
1453                                         reader.close() ; reader = null ;
1454                                         socket.close() ; socket = null ;
1455
1456                                 } else if( line != null &&  line.equalsIgnoreCase( "save" ) ) {
1457                                         /** It's time to do a save **/
1458
1459                                         try {
1460                                                 machine.setComputationId( Integer.parseInt( reader.readLine() ) ) ;
1461                                         } catch( Exception e ) {
1462                                                 System.err.println( "Problem while reading the computation id!" ) ;
1463                                                 e.printStackTrace() ;
1464                                         }
1465
1466                                         synchronized( saveRequest ) 
1467                                         {
1468                                                 try {
1469                                                         LocalHost.Instance().getServerStub().requestSave( LocalHost.Instance().getIP() ) ;
1470                                                 } catch( RemoteException e ) {
1471                                                         System.err.println( "Unable to request save to server!" ) ;
1472                                                         e.printStackTrace() ;
1473                                                 }
1474
1475                                                 try {
1476                                                         saveRequest.wait() ;
1477                                                 } catch( InterruptedException e ) {
1478                                                         e.printStackTrace();
1479                                                 }
1480                                         }
1481
1482                                         if( saveRequest.getStatus() )
1483                                         {
1484                                                 /* Close streams */
1485                                                 reader.close() ; reader = null ;
1486                                                 socket.close() ; socket = null ;
1487
1488                                                 saveRequest.setStatus( false ) ;
1489
1490                                                 /* Requesting the VM save */
1491                                                 if( saveVM() == 1 )
1492                                                 {
1493                                                         System.err.println( "Problem while saving. Exiting ..." ) ;
1494                                                         pingServer.stopPing() ;
1495                                                         emergencyStop() ;
1496                                                         System.exit( 1 ) ;
1497                                                 }
1498                                         } else {
1499                                                 sendSaveOkVM() ;
1500                                         }
1501                                 } else if( line != null &&  line.equalsIgnoreCase( "quit" ) ) {
1502                                         /** Computation is done, we can shutdown the VM **/
1503
1504                                         try {
1505                                                 Thread.sleep( 5000 ) ;
1506                                         } catch( InterruptedException e ) {
1507                                                 e.printStackTrace() ;
1508                                         }
1509
1510                                         /* Close streams */
1511                                         reader.close() ; reader = null ;
1512                                         socket.close() ; socket = null ;
1513
1514                                         stopVM( 0 ) ;
1515
1516                                         machine.setStatus( "connected" ) ;
1517                                         try {
1518                                                 LocalHost.Instance().getServerStub().changeStatus( 
1519                                                                 LocalHost.Instance().getIP(), "connected" ) ;
1520                                         } catch( RemoteException e ) {
1521                                                 System.err.println( "Unable to inform the server of the VM status!" ) ;
1522                                                 e.printStackTrace() ;
1523                                         }
1524                                         
1525                                         try {
1526                                                 LocalHost.Instance().getServerStub().endApplication() ;
1527                                         } catch( RemoteException e ) {
1528                                                 System.err.println( "Unable to inform the server of the end of application!" ) ;
1529                                                 e.printStackTrace() ;
1530                                         }
1531                                 } else if( line != null &&  line.equalsIgnoreCase( "start" ) ) {
1532                                         /** Computation is starting **/
1533
1534                                         /* Close streams */
1535                                         reader.close() ; reader = null ;
1536                                         socket.close() ; socket = null ;
1537                                         System.out.println( "Application is starting." ) ;
1538                                         
1539                                         try {
1540                                                 LocalHost.Instance().getServerStub().goApplication() ;
1541                                         } catch( RemoteException e ) {
1542                                                 System.err.println( "Unable to inform the server of the end of application!" ) ;
1543                                                 e.printStackTrace() ;
1544                                         }
1545                                 }
1546
1547                         } catch( IOException e ) {
1548                                 e.printStackTrace() ;
1549                                 yield() ;
1550                         }
1551                 }
1552         }
1553
1554         @Override
1555         public void stop() 
1556         {
1557                 stopVM( 0 ) ;
1558                 
1559                 pingServer.stopPing() ;
1560                 
1561                 dialogVmServer.stopDialogVMServer() ;
1562                 
1563                 // unexportObject ??
1564                 
1565                 System.exit( 0 ) ;
1566         }
1567
1568         @Override
1569         public String getIPHost() 
1570         {
1571                 return LocalHost.Instance().getIP() ;
1572         }
1573         
1574         @Override
1575         public String getName()
1576         {
1577                 return LocalHost.Instance().getName() ;
1578         }
1579         
1580         
1581         @Override
1582         public void saveOk() 
1583         {
1584                 String save_name = machine.getName() + "_last_" + machine.getComputationId() +
1585                         ".tar.gz" ;
1586                 
1587                 String save_new = machine.getName() + "_new_" 
1588                   + machine.getComputationId() + ".tar.gz" ;
1589                 
1590                 String[] command = new String[]{ "/bin/mv", 
1591                                 working_directory + "/" + save_new, 
1592                                 working_directory + "/" + save_name } ;
1593                 
1594                 try {
1595                         Process p = Runtime.getRuntime().exec( command ) ;
1596                         p.waitFor() ;
1597                 
1598                         if( p.exitValue() == 0 )
1599                         {
1600                                 machine.setSave_last( save_name ) ;
1601                                 System.out.println( "Last save OK" ) ;
1602                         } else {
1603                                 System.err.println( "Last save NOK!" ) ;
1604                                 System.err.println( "Error: " ) ;
1605                                 printProcessError( p ) ;
1606                         }
1607                 } catch( IOException e ) {
1608                         System.err.println( "Error during last archive move:" ) ;
1609                         e.printStackTrace() ;
1610                 } catch( InterruptedException e ) {
1611                         e.printStackTrace() ;
1612                 }
1613                 
1614                 // Changing on save neighbors
1615                 for( int i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1616                 {
1617                         try {
1618                                 machine.getSaveNeighbors().get( i ).getStub().changeSaveName( save_new, save_name, machine.getComputationId() ) ;
1619                         } catch( RemoteException e ) {
1620                                 System.err.println( "Unable to change save name on " + machine.getSaveNeighbors().get( i ).getName() + "!" ) ;
1621                                 e.printStackTrace() ;
1622                         }
1623                 }
1624                 
1625                 // Informing the server
1626                 int ret = 1 ;
1627                 try {
1628                         ret = LocalHost.Instance().getServerStub().changeSaveName( LocalHost.Instance().getIP(), save_name ) ;
1629                 } catch( RemoteException e ) {
1630                         System.err.println( "Unable to inform the server about the new save name!" ) ;
1631                         e.printStackTrace() ;
1632                 }
1633                 
1634                 if( ret == 0 )
1635                 {
1636                         System.out.println( "Successfully informing the server about the new save name." ) ;
1637                 } else {
1638                         System.err.println( "Problem on the server while informing it about the new save name!" ) ;
1639                 }
1640                 
1641                 // Ok here
1642                 lastSaveOk = true ;
1643         }
1644         
1645         
1646         public void changeSaveName( String _n1, String _n2, int _id )
1647         {
1648                 if( _n1 != null && _n1.length() > 0 )
1649                 {
1650                         System.out.println( "Changing save name for processus " + _id + " ... " ) ;
1651                         
1652                         String[] command = new String[]{ "/bin/mv", 
1653                                         working_directory + "/" + _n1, 
1654                                         working_directory + "/" + _n2 } ;
1655                         
1656                         try {
1657                                 Process p = Runtime.getRuntime().exec( command ) ;
1658                                 p.waitFor() ;
1659                         
1660                                 if( p.exitValue() == 0 )
1661                                 {
1662                                         System.out.println( "Change save name OK" ) ;
1663                                 } else {
1664                                         System.err.println( "Change save name NOK!" ) ;
1665                                         System.err.println( "Error: " ) ;
1666                                         printProcessError( p ) ;
1667                                 }
1668                         } catch( IOException e ) {
1669                                 System.err.println( "Error during save renaming:" ) ;
1670                                 e.printStackTrace() ;
1671                         } catch( InterruptedException e ) {
1672                                 e.printStackTrace() ;
1673                         }
1674                 }
1675         }
1676
1677         
1678         @Override
1679         public void setSaveNeighbor( SaveNeighbor _sn )
1680         {
1681                 if( _sn != null )
1682                 {
1683                         ArrayList<SaveNeighbor> as = new ArrayList<SaveNeighbor>() ;
1684                         as.add( _sn ) ;
1685                         
1686                         System.out.println( "Save neighbor: " + _sn.getName() ) ;
1687                         
1688                         machine.setSaveNeighbors( as ) ;
1689                 }
1690         }
1691         
1692         
1693         @Override
1694         public void setSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1695         {
1696                 if( _sn != null && _sn.size() > 0 )
1697                 {
1698                         System.out.print( "Save neighbors: " ) ;
1699                         for( int i = 0 ; i < _sn.size() ; i++ )
1700                         {
1701                                 System.out.print( _sn.get( i ).getName() ) ;
1702                                 
1703                                 if( i != _sn.size() - 1 )
1704                                 {
1705                                         System.out.print( ", " ) ;
1706                                 } else {
1707                                         System.out.println( "." ) ;
1708                                 }
1709                         }
1710                         
1711                         machine.setSaveNeighbors( _sn ) ;
1712                 }
1713         }
1714
1715         
1716         @Override
1717         public void addSaveNeighbor( SaveNeighbor _sn )
1718         {
1719                 if( _sn != null )
1720                 {
1721                         System.out.println( "Adding save neighbor: " + _sn.getName() ) ;
1722                         
1723                         machine.getSaveNeighbors().add( _sn ) ;
1724                 }
1725         }
1726         
1727         
1728         @Override
1729         public void addSaveNeighbors( ArrayList<SaveNeighbor> _sn )
1730         {
1731                 if( _sn != null && _sn.size() > 0 )
1732                 {
1733                         System.out.print( "Adding save neighbors: " ) ;
1734                         for( int i = 0 ; i < _sn.size() ; i++ )
1735                         {
1736                                 System.out.print( _sn.get( i ).getName() ) ;
1737                                                         
1738                                 if( i != _sn.size() - 1 )
1739                                 {
1740                                         System.out.print( ", " ) ;
1741                                 } else {
1742                                         System.out.println( "." ) ;
1743                                 }
1744                                 
1745                                 machine.getSaveNeighbors().add( _sn.get( i ) ) ;
1746                         }
1747                 }
1748         }
1749         
1750         
1751         @Override
1752         public void replaceSaveNeighbor( SaveNeighbor _old, SaveNeighbor _new )
1753         {
1754                 System.out.print( "Replacing a save neihgbor ... " ) ;
1755                 if( _old != null && _new != null )
1756                 {
1757                         int i = 0 ;
1758                         boolean change = false ;
1759                         
1760                         for( i = 0 ; i < machine.getSaveNeighbors().size() ; i++ )
1761                         {
1762                                 if( machine.getSaveNeighbors().get( i ).getIPHost().equalsIgnoreCase( _old.getIPHost() ) )
1763                                 {
1764                                         machine.getSaveNeighbors().set( i, _new ) ;
1765                                         System.out.println( "Save neighbor successfully changed." ) ;
1766                                         change = true ;
1767                                         break ;
1768                                 }
1769                         }
1770                         
1771                         if( ! change )
1772                         {
1773                                 System.out.println( "I am not concerned by the modification." ) ;
1774                         }
1775                 }
1776         }
1777         
1778         
1779         @Override
1780         public int retrieveSave( String _saveName )
1781         {               
1782                 if( _saveName != null )
1783                 {
1784                         if( ! _saveName.equalsIgnoreCase( "none" ) )
1785                         {
1786                                 machine.setSave_last( _saveName ) ;
1787                         } else {
1788                                 System.err.println( "I have no save to retrieve!!" ) ;
1789                                 return 1 ;
1790                         }
1791                         
1792                         // TODO NEIGHBORS !!!!
1793                         boolean ok = false ;
1794                         int i = 0 ;
1795                         
1796                         while( ! ok && i < machine.getSaveNeighbors().size() )
1797                         {
1798                                 System.out.print( "Retrieving a save on " + machine.getSaveNeighbors().get( 0 ).getName() + " ... " ) ;
1799                                 
1800                                 String command[] = {} ;
1801                                 
1802                                 command = new String[]{ "/usr/bin/scp", 
1803                                                 machine.getSaveNeighbors().get( i ).getIPHost() + ":" +
1804                                                 machine.getSaveNeighbors().get( i ).getWorkingDirectory() + "/" +
1805                                                 machine.getSave_last(),
1806                                                 working_directory } ;
1807                                 
1808                                 try {
1809                                         Process p = Runtime.getRuntime().exec( command ) ;
1810                                         p.waitFor() ;
1811                         
1812                                         if( p.exitValue() == 0 )
1813                                         {
1814                                                 System.out.println( "Archive successfully retrieved." ) ;
1815                                                 isRestartedSave = true ;
1816                                                 ok = true ;
1817                                         } else {
1818                                                 System.err.println( "Archive not retrieved!" ) ;
1819                                                 System.err.println( "Error: " ) ; 
1820
1821                                                 printProcessError( p ) ;
1822                                         }
1823                                 } catch( IOException e ) {
1824                                         System.err.println( "Error during archive retrieve command: " ) ;
1825                                         e.printStackTrace() ;
1826                                 } catch( InterruptedException e ) {
1827                                         e.printStackTrace() ;
1828                                 }
1829                                 
1830                                 i++ ;
1831                         }
1832                         
1833                         if( ok )
1834                         {
1835                                 return 0 ;
1836                         } else {
1837                                 System.err.println( "Unable to retrieve a save archive from any neighbor!" ) ;
1838                         }
1839                 }
1840                 
1841                 return 1 ;
1842                 
1843         }
1844
1845         
1846         @Override
1847         public String getIPVM() throws RemoteException 
1848         {
1849                 if( machine != null )
1850                 {
1851                         return machine.getIp() ;
1852                 }
1853                 
1854                 return null ;
1855         }
1856         
1857         
1858         @Override
1859         public void setIPVM( String _ipVM ) throws RemoteException 
1860         {
1861                 if( _ipVM != null && ! _ipVM.isEmpty() )
1862                 {
1863                         System.out.println( "The VM IP is now: " + _ipVM ) ;
1864                         machine.setIp( _ipVM ) ;
1865                 }
1866         }
1867         
1868         
1869         public String getWorkingDirectory()
1870         {
1871                 return working_directory ;
1872         }
1873         
1874         
1875         public Integer deployVM( String _name, String _archive, String _directory )
1876         {
1877                 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 )
1878                 {
1879                         File file = new File( working_directory + "/" + _archive ) ;
1880                         if( ! file.exists() )
1881                         {
1882                                 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
1883                                 file = null ;
1884                                 return 2 ;
1885                         } else if( file.isDirectory() ) {
1886                                 System.err.println( _archive + " is a directory!" ) ;
1887                                 file = null ;
1888                                 return 1 ;
1889                         }
1890                         
1891                         file = null ;
1892                         
1893                         machine.setName( _name ) ;
1894                         machine.setInitial_archive_name( _archive ) ;
1895                         machine.setDirectory( _directory ) ;
1896                         
1897                         if( machine.deployInitialVM() == 1 ) 
1898                         {
1899                                 System.err.println( "Unable to deploy the initial VM archive!" ) ;
1900                         } else {                                
1901                                 return 0 ;
1902                         }
1903                 }
1904                 
1905                 return 1 ;
1906         }
1907         
1908         
1909         @Override
1910         public void responseSave( boolean _b )
1911         {
1912                 synchronized( saveRequest )
1913                 {
1914                         saveRequest.setStatus( _b ) ;
1915                         try {
1916                                 saveRequest.notifyAll() ;
1917                         } catch( Exception e ) {
1918                                 e.printStackTrace() ;
1919                         }
1920                 }
1921         }
1922         
1923         
1924         private class SaveRequest
1925         {
1926                 boolean status ;
1927                 
1928                 SaveRequest()
1929                 {
1930                         status = false ;
1931                 }
1932                 
1933                 protected boolean getStatus() { return status ; }
1934                 
1935                 protected void setStatus( boolean _b ) { status = _b ; }
1936         }
1937         
1938         private class SaveProcess
1939         {
1940                 boolean status ;
1941                 
1942                 SaveProcess()
1943                 {
1944                         status = false ;
1945                 }
1946                 
1947                 protected boolean getStatus() { return status ; }
1948                 
1949                 protected void setStatus( boolean _b ) { status = _b ; }
1950         }
1951         
1952 }
1953
1954
1955 /** La programmation est un art, respectons ceux qui la pratiquent !! **/