Logo AND Algorithmique Numérique Distribuée

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