Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Refunding the restart/redeployment mechanisms.
[hpcvm.git] / src / and / hpcvm / Server.java
1 package and.hpcvm ;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.rmi.Naming;
8 import java.rmi.RemoteException;
9 import java.rmi.registry.LocateRegistry;
10 import java.rmi.registry.Registry;
11 import java.rmi.server.UnicastRemoteObject;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14
15
16 public class Server extends UnicastRemoteObject implements ServicesServer
17 {
18         private class DiscCount
19         {
20                 private int nb ;
21                 
22                 DiscCount() { nb = 0 ; }
23                 
24                 protected void inc() { nb++ ; }
25                 
26                 protected void dec() { 
27                         if( nb > 0 )
28                         {
29                                 nb-- ;
30                         }
31                 }
32                 
33                 protected int getNb() { return nb ; }
34         }
35         
36         
37         private class ConnectedClient
38         {
39                 private ServicesClient stub ;
40                 private int timeout ;
41                 private Status state ;
42                 private String ip ;
43                 private String name ;
44                 private ComputingClient cl ;
45                 
46                 ConnectedClient( ServicesClient _stub )
47                 {
48                         stub = _stub ;
49                         timeout = 0 ;
50                         state = new Status() ;
51                         state.setStatus( "connected" ) ;
52                         try {
53                                 ip = stub.getIPHost() ;
54                                 name = stub.getName() ;
55                         } catch (RemoteException e) {
56                                 e.printStackTrace();
57                         }
58                         cl = null ;
59                 }
60                 
61                 protected ServicesClient getStub() { return stub ; }
62                 
63                 protected void setStub( ServicesClient _stub ) { stub = _stub ; }
64                 
65                 protected int getTimeout() { return timeout ; }
66                 
67                 protected void incTimeout() { timeout++ ; }
68                 
69                 protected void resetTimeout() { timeout = 0 ; }
70                 
71                 protected String getStatus() { return state.getStatus() ; }
72                 
73                 protected void setStatus( String _state ) { state.setStatus( _state ) ; }
74                 
75                 protected String getIP() { return ip ; }
76                 
77                 protected String getName() { return name ; } ;
78                 
79                 protected void setComputingClient( ComputingClient _cl ) { cl = _cl ; }
80                 
81                 protected ComputingClient getComputingClient() { return cl ; } 
82         }
83         
84         
85         private class ComputingClient
86         {
87                 private ConnectedClient client ;
88                 private boolean save_status ;
89                 private ArrayList<ServicesClient> save_neighbor ;
90                 private String lastSaveName ;
91                 
92                 ComputingClient( ConnectedClient cl )
93                 {
94                         client = cl ;
95                         save_status = false ;
96                         save_neighbor = new ArrayList<ServicesClient>() ;
97                         lastSaveName = "none" ;
98                 }
99                 
100                 protected ConnectedClient getClient() { return client ; }
101                 
102                 protected boolean getSaveStatus(){ return save_status ; }
103                 
104                 protected void setSaveStatus( boolean _status ) { save_status = _status ; }
105                 
106                 protected void setSaveNeighbor( ServicesClient _sn ) 
107                 { 
108                         if( _sn != null )
109                         {
110                                 if( save_neighbor.size() == 0 )
111                                 {
112                                         save_neighbor.add( _sn ) ;
113                                 } else {
114                                         save_neighbor.set( 0, _sn ) ;
115                                 }
116                                 
117 //                              System.out.println( "My save neighbor is " + _sn ) ;
118                         
119                                 try {
120                                         client.getStub().setSavingNeighbor( _sn ) ;
121                                 } catch( RemoteException e ) {
122                                         System.err.println( "Error while setting save neighbor on " + 
123                                                         client.getName() + "(" + client.getIP() + ")!" ) ;
124                                         e.printStackTrace() ;
125                                 }
126                         }
127                 }
128                 
129                 protected ServicesClient getSaveNeighbor() 
130                 {
131                         if( save_neighbor.isEmpty() )
132                         {
133                                 return null ;
134                         } else {
135                                 return save_neighbor.get( 0 ) ;
136                         }
137                 }
138
139                 public void setLastSave( String _saveName ) 
140                 {
141                         lastSaveName = _saveName ;                      
142                 }
143                 
144                 public String getLastSave() { return lastSaveName ; }
145                 
146         }
147         
148         
149         private class IPAssociation
150         {
151                 private String vmIP ;
152                 private String hostIP ;
153                 
154                 IPAssociation()
155                 {
156                         vmIP = null ;
157                         hostIP = null ;
158                 }
159                 
160                 protected void setVmIP( String _vmIP )
161                 {
162                         vmIP = _vmIP ;
163                 }
164                 
165                 protected void setHostIP( String _hostIP )
166                 {
167                         hostIP = _hostIP ;
168                 }
169                 
170                 protected String getVmIP()
171                 {
172                         return vmIP ;
173                 }
174                 
175                 protected String getHostIP()
176                 {
177                         return hostIP ;
178                 }
179         }
180         
181         
182         
183         private static final long serialVersionUID = 1L ;
184         private int port ;
185         private ArrayList<ConnectedClient> clients ;
186         private ArrayList<ComputingClient> computingClients ;
187         private int max_timeout ;
188         private ConnectedMonitor monitor ;
189         private DiscCount counter ;
190         private ArrayList<IPAssociation> vmIPs ;
191         private String working_directory ;
192         
193         
194         protected Server() throws RemoteException 
195         {
196                 super() ;
197         }
198
199
200
201         @Override
202         public Integer register( ServicesClient _stub ) 
203         {
204                 if( _stub != null )
205                 {
206                         String ip = "" ;
207                         try {
208                                 ip = _stub.getIPHost() ;
209                         } catch (RemoteException e) {
210                                 e.printStackTrace() ;
211                                 return 1 ;
212                         }
213                         
214                         boolean exists = false ;
215                         int i ;
216                         
217                         for( i = 0 ; i < clients.size() ; i++ )
218                         {
219                                 if( ip.equals( clients.get( i ).getIP() ) )
220                                 {
221                                         exists = true ;
222                                         System.out.println( "Client already connected!" ) ;
223                                         break ;
224                                 }
225                         }
226                         
227                         if( exists )
228                         {
229                                 System.out.println( "The client stub will be replaced." ) ;
230                                 clients.get( i ).setStub( _stub ) ;
231                                 System.out.println( "(reconnection of " + clients.get( i ).getName() + ")" ) ;
232                                 return 2 ;
233                         } else {
234                                 System.out.println( "New connection!" ) ;
235                                 clients.add( new ConnectedClient( _stub ) ) ;
236                                 System.out.println( "(connection of " + clients.get( clients.size() - 1 ).getName() + ")" ) ;
237                                 generateVmIP( ip ) ;
238                                 
239                                 if( clients.size() == 0 )
240                                 {
241                                         System.out.println( "There is no client connected." ) ;
242                                 } else if( clients.size() == 1 ) {
243                                         System.out.println( "There is one client connected." ) ;
244                                 } else {
245                                         System.out.println( "There are " + clients.size() + " clients connected." ) ;
246                                 }
247
248                                 return 0 ;
249                         }
250                 }
251                 
252                 return 1 ;
253         }
254
255         
256         private void generateVmIP( String _ip ) 
257         {
258                 if( _ip != null && ! _ip.equals( "" ) ) 
259                 {       
260                         for( int i = 0 ; i < vmIPs.size() ; i++ )
261                         {
262                                 if( vmIPs.get( i ).getHostIP() == null )
263                                 {
264                                         vmIPs.get( i ).setHostIP( _ip ) ;
265                                         
266                                         break ;
267                                 }
268                         }
269                 }
270         }
271         
272
273         @Override
274         public void ping( String _ip ) 
275         {
276                 if( _ip != null )
277                 {
278                         for( int i = 0 ; i < clients.size() ; i++ )
279                         {
280                                 if( _ip.equals( clients.get( i ).getIP() ) ) 
281                                 {
282                                         clients.get( i ).resetTimeout() ;
283                                         break ;
284                                 }
285                         }
286                 }               
287         }
288
289         
290         @Override
291         public void changeStatus( String _ip, String _status ) 
292         {
293                 if( _ip != null && _status != null )
294                 {
295                         for( int i = 0 ; i < clients.size() ; i++ )
296                         {
297                                 if( _ip.equals( clients.get( i ).getIP() ) ) 
298                                 {
299                                         clients.get( i ).setStatus( _status ) ;
300                                         System.out.println( "Client " + clients.get( i ).getName() + " changed its status to: " + _status ) ;
301                                         break ;
302                                 }
303                         }
304                 }
305         }
306         
307
308         public void init( int _port ) 
309         {       
310                 port = _port ;
311                 max_timeout = 4 ;
312                 
313                 clients = new ArrayList<Server.ConnectedClient>() ;
314                 computingClients = new ArrayList<Server.ComputingClient>() ;
315                 monitor = null ;
316                 
317                 working_directory = "/localhome/vmware" ;
318                 
319                 exportObject() ;
320
321                 vmIPs = new ArrayList<IPAssociation>() ;
322                 // TODO initialisation of VM IPs
323                 for( int i = 2 ; i < 101 ; i++ )
324                 {
325                         vmIPs.add( new IPAssociation() ) ;
326                         vmIPs.get( vmIPs.size() - 1 ).setVmIP( "10.11.10." + i ) ;
327                 }
328                 
329                 clients = new ArrayList<Server.ConnectedClient>() ;
330                 
331                 counter = new DiscCount() ;
332                 
333                 monitor = new ConnectedMonitor() ;
334                 monitor.start() ;
335         }
336         
337         
338         public void stop()
339         {
340                 if( monitor != null ) { monitor.stopMonitor() ; }
341                 
342                 for( int i = 0 ; i < clients.size() ; i++ )
343                 {
344                         try {
345                                 clients.get( i ).getStub().stop() ;
346                         } catch (RemoteException e) {
347                                 e.printStackTrace();
348                         }
349                 }
350                 
351                 // unexportObject ?
352                 
353                 System.exit( 0 ) ;
354         }
355
356         
357         private void exportObject() 
358         {
359                 ServicesServer ref = null ;
360                 Registry reg = null ;
361                 
362                 try 
363                 {       
364                         while( true )
365                         {
366                                 reg = LocateRegistry.getRegistry( port ) ;
367
368                                 String tab[] = reg.list() ;
369                         
370                                 System.out.println( "There is an existing RMI Registry on port " +
371                                                 port + " with " + tab.length + " entries!" ) ;
372                                 for( int i = 0 ; i < tab.length ; i++ )
373                                 {
374                                         try {
375                                                 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
376                                                 {
377                                                         System.out.println( "Register successfuly deleted!" ) ;
378                                                 } else {
379                                                         System.err.println( "Register undeleted !!!" ) ;
380                                                 }
381                                         } catch( Exception e ) {
382                                                 e.printStackTrace() ;
383                                         }
384                                 } 
385                         }
386                 } catch( RemoteException e ) {
387                 }       
388                                 
389                 try {
390                         if ( System.getSecurityManager() == null ) 
391                         {
392                     System.setSecurityManager( new SecurityManager() ) ;
393                 }
394                         
395                         LocateRegistry.createRegistry( port ) ;
396                         LocateRegistry.getRegistry( port ).rebind( "Server", this ) ;
397                         ref = (ServicesServer) Naming.lookup( "rmi://"
398                                         + LocalHost.Instance().getIP() + ":" + port
399                                         + "/Server" ) ;
400                 } catch ( Exception e ) {
401                         System.err.println( "Error in Server.exportObject() when creating local services:" + e ) ;
402                         System.err.println( "Exit from Server.exportObject" ) ;
403                         System.exit( 1 ) ;
404                 }
405
406                 LocalHost.Instance().setServerStub( ref ) ;
407                 
408                 System.out.println( "Server launched on IP " + LocalHost.Instance().getIP() + 
409                                 " on port " + port + "." ) ;
410         }
411         
412         /** Fault manager thread **/
413         private class FaultManager extends Thread
414         {
415                 ConnectedClient cl ;
416                 
417                 FaultManager( ConnectedClient _cl )
418                 {
419                         cl = _cl ;
420                 }
421                 
422                 @Override
423                 public void run() 
424                 {
425                         if( cl != null && cl.getStatus().equalsIgnoreCase( "running" ) ||
426                                         cl.getStatus().equalsIgnoreCase( "saving" ) )
427                         {
428                                 ComputingClient cc = cl.getComputingClient() ;
429 //                              ServicesClient dead = cc.getClient().getStub() ;
430                                 String ipDead = cc.getClient().getIP() ;
431                                                                 
432                                 boolean ok = false ;
433                                 
434                                 for( int i = 0 ; i < clients.size() ; i++ )
435                                 {
436                                         if( clients.get( i ).getStatus().equalsIgnoreCase( "connected" ) ) 
437                                         {
438 //                                              int res = 1 ;
439 //                                              try {
440 //                                                      res = clients.get( i ).getStub().startVM() ;
441 //                                              } catch( RemoteException e ) {
442 //                                                      e.printStackTrace();
443 //                                              }
444 //                                              
445 //                                              if( res == 0 )
446 //                                              {
447                                                         //clients.get(i).setStatus( "running" ) ;
448                                                 
449                                                 int pos = computingClients.indexOf( cc )  ;
450                                                 if( pos == -1 )
451                                                 {
452                                                         System.err.println( "Dead client not found in the computing clients list!" ) ;
453                                                 } else {
454                                                         System.out.println( "Trying to replace " + cc.getClient().getName() + " with " +
455                                                                         clients.get(i).getName() + " ... " ) ;
456                                                         
457                                                         String save_name = computingClients.get( pos ).getLastSave() ;
458                                                         
459                                                         ComputingClient ccl = new ComputingClient( clients.get(i) ) ;
460                                                         clients.get( i ).setComputingClient( ccl ) ;
461                                                         ServicesClient sn = computingClients.get( pos ).getSaveNeighbor() ;
462                                                         ccl.setSaveNeighbor( sn ) ;
463                                                         computingClients.set( pos, ccl ) ;
464
465                                                                 
466                                                         int res = 1 ;
467                                                         try {
468                                                                 res = computingClients.get( pos ).getClient().getStub().
469                                                                       retrieveSave( save_name ) ;
470                                                         } catch( RemoteException e ) {
471                                                                 System.err.println( "Unable to indicate to client to retrieve last save!" ) ;
472                                                                 e.printStackTrace() ;
473                                                         }
474                                                                 
475                                                         if( res == 0 )
476                                                         {
477                                                                 ok = true ;
478                                                                         
479                                                                         // replace dead client in vmIPs
480                                                                 for( int j = 0 ; j < vmIPs.size() ; j++ )
481                                                                 {
482                                                                         if( vmIPs.get( j ).getHostIP().equalsIgnoreCase( ipDead ) )
483                                                                         {
484                                                                                 String vmIP = vmIPs.get( j ).getVmIP() ;
485                                                                                 vmIPs.get( j ).setHostIP( computingClients.get( pos ).getClient().getIP() ) ;
486                                                                                         
487                                                                                 try {
488                                                                                         computingClients.get( pos ).getClient().getStub().setIPVM( vmIP ) ;
489                                                                                 } catch( RemoteException e ) {
490                                                                                         System.err.println( "Unable to set the new VM IP on the replacing client!" ) ;
491                                                                                         e.printStackTrace() ;
492                                                                                 }
493                                                                                 break ;
494                                                                         }
495                                                                 }
496                                                                         
497                                                                 System.out.println( "Successful redeployment of the VM." ) ;
498                                                         } else {
499                                                                 System.err.println( "Unable to deploy the save on the new computing client!" ) ;
500                                                         }
501                                                 }
502 //                                              } else {
503 //                                                      System.err.println( "Problem while launching the VM on " 
504 //                                                                      + clients.get(i).getName() + "!" ) ;
505 //                                              }
506                                                         
507                                                 if( ok )
508                                                 {
509                                                         for( int k = 0 ; k < computingClients.size() ; k++ )
510                                                         {
511                                                                 try {
512                                                                         computingClients.get( k ).getClient().getStub().
513                                                                                 replaceSavingNeighbor( ipDead, clients.get( i ).getStub() ) ;
514                                                                 } catch( RemoteException e ) {
515                                                                         System.err.println( "Unable to inform " + computingClients.get( k ).getClient().getName() +
516                                                                                         " of the replacement of a save neighbor!" ) ;
517                                                                         e.printStackTrace() ;
518                                                                 }
519                                                         }
520                                                         
521                                                         System.out.println( "Dead client successfully replaced." ) ;
522
523                                                         break ;
524                                                 } else {
525                                                         System.err.println( "Dead client not replaced!!" ) ;
526                                                 }
527                                         }
528                                 }
529                         }
530                         
531                         try {
532                                 synchronized( counter ) {
533                                         counter.dec() ;
534                                         counter.notifyAll() ;}
535                         } catch( Exception e ) {}
536                 }
537         }
538         
539         
540         /** Monitoring thread **/
541         private class ConnectedMonitor extends Thread
542         {
543                 boolean run ;
544                 
545                 ConnectedMonitor()
546                 {
547                         run = true ;
548                 }
549                 
550                 protected void stopMonitor() { run = false ; }
551                 
552                 @Override
553                 public void run() 
554                 {
555                         boolean change ;
556                         
557                         while( run )
558                         {
559                                 change = false ;
560                                 Iterator<ConnectedClient> it = clients.iterator() ;
561                                 int nb_disconnections = 0 ;
562                                 int nb_disconnections_computing = 0 ;
563                                 
564                                 while( it.hasNext() )
565                                 {
566                                         ConnectedClient cl = it.next() ;
567                                         cl.incTimeout() ;
568                                         
569                                         if( cl.getTimeout() > max_timeout )
570                                         {
571                                                 System.out.println( "Disconnection of " + cl.getName() ) ;
572                                                 if( cl.getStatus().equalsIgnoreCase( "running" ) || cl.getStatus().equalsIgnoreCase( "saving" ) )
573                                                 {
574                                                         System.out.println( "A VM was running on it!!" ) ;
575                                                         System.out.println( "I will redeploy a save and restart all VM ..." ) ;
576                                 
577 //                                                      for( int i = 0 ; i < computingClients.size() ; i++ )
578 //                                                      {
579 //                                                              if( computingClients.get( i ).getClient().getIP().equals( cl.getIP() ) )
580 //                                                              {
581 //                                                                      computingClients.remove( i ) ;
582 //                                                                      break ;
583 //                                                              }
584 //                                                      }
585                                                         synchronized( counter ){
586                                                                 counter.inc() ;}
587                                                                 
588                                                         
589                                                         new Server.FaultManager( cl ).start() ;
590                                                         nb_disconnections_computing++ ;
591                                                 } else {
592                                                         System.out.println( "There was no VM running on it." ) ;
593                                                         System.out.println( "Maybe it will come back later :)" ) ;
594                                                 }
595                                                 
596                                                 it.remove() ;
597                                                 nb_disconnections++ ;
598                                                 change = true ;
599                                         }
600                                 }
601                                 
602                                 if( change )
603                                 {
604                                         if( clients.size() == 0 )
605                                         {
606                                                 System.out.println( "There is no client connected." ) ;
607                                         } else if( clients.size() == 1 ) {
608                                                 System.out.println( "There is one client connected." ) ;
609                                         } else {
610                                                 System.out.println( "There are " + clients.size() + " clients connected." ) ;
611                                         }
612                                 }
613                                 
614                                 
615                                 if( nb_disconnections_computing > 0 )
616                                 {
617                                         System.out.println( "Sending emergency stop signal to all computing nodes ... " ) ;
618                                         
619                                         for( int i = 0 ; i < clients.size() ; i++ )
620                                         {
621                                                 if( clients.get( i ).getStatus().equalsIgnoreCase( "running" ) 
622                                                         || clients.get( i ).getStatus().equalsIgnoreCase( "saving" ) )
623                                                 {
624                                                         try {
625                                                                 clients.get( i ).getStub().emergencyStop() ;
626                                                         } catch( RemoteException e ) {
627                                                                 System.err.println( "Unable to invoke emergency stop signal on " + clients.get( i ).getName() ) ;
628                                                                 e.printStackTrace() ;
629                                                         }
630                                                 }
631                                         }
632                                         
633                                         System.out.println( "I will redeploy save and restart VMs ... " ) ;
634                                         
635                                         synchronized( counter ) 
636                                         {
637                                                 if( counter.getNb() > 0 )
638                                                 {
639                                                         System.out.println( "... waiting all redeployments done ..." ) ;
640                                                 }
641                                         
642                                                 while( counter.getNb() != 0 )
643                                                 {
644                                                         try {
645                                                                 counter.wait() ;
646                                                         } catch( InterruptedException e ) {
647                                                                 e.printStackTrace() ;
648                                                         }
649                                                 }
650                                         }
651                                         
652                                         for( int  i = 0 ; i < computingClients.size() ; i++ )
653                                         {
654                                                 final ServicesClient sc = computingClients.get( i ).getClient().getStub() ;
655                                                 
656                                                 new Thread( new Runnable() {
657                                                         
658                                                         @Override
659                                                         public void run() 
660                                                         {
661                                                                 try {
662                                                                         sc.restartVMAfterCrash() ;
663                                                                 } catch( RemoteException e ) {
664                                                                         e.printStackTrace() ;
665                                                                 }
666                                                         }
667                                                 } ).start() ;
668                                         }
669                                 }
670                                 
671                                 try 
672                                 {
673                                         Thread.sleep( 2000 ) ;
674                                 } catch( InterruptedException e ) {
675                                         e.printStackTrace() ;
676                                 }
677                         }
678                 }
679         }
680
681         @Override
682         public Integer saveOk( String _ip, String _saveName ) 
683         {
684                 int i ;
685                 for( i = 0 ; i < computingClients.size() ; i ++ )
686                 {
687                         if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) ) 
688                         {
689                                 computingClients.get( i ).setLastSave( _saveName ) ;
690                                 computingClients.get( i ).setSaveStatus( true ) ;
691                                 break ;
692                         }
693                 }
694                 
695                 
696                 boolean all_ok = true ;
697                 i = 0 ;
698                 
699                 while( all_ok && i < computingClients.size() )
700                 {
701                         all_ok = all_ok & computingClients.get( i ).getSaveStatus() ;
702                         i++ ;
703                 }
704                 
705                 if( all_ok )
706                 {
707                         for( i = 0 ; i < computingClients.size() ; i++ )
708                         {
709                                 try {
710                                         computingClients.get( i ).getClient().getStub().saveOk() ;
711                                 } catch (RemoteException e) {
712                                         e.printStackTrace();
713                                 }
714                                 computingClients.get( i ).setSaveStatus( false ) ;
715                         }
716                 }
717                 
718                 return 0 ;
719         }
720         
721         
722         public Integer changeSaveName( String _ip, String _saveName )
723         {
724                 if( _ip != null && _ip.length() > 0 && _saveName != null && _saveName.length() > 0 )
725                 {
726                         for( int i = 0 ; i < computingClients.size() ; i ++ )
727                         {
728                                 if( computingClients.get( i ).getClient().getIP().equalsIgnoreCase( _ip ) ) 
729                                 {
730                                         computingClients.get( i ).setLastSave( _saveName ) ;
731                                         System.out.println( "Save name successfully change for " + _ip ) ;
732                                         return 0 ;
733                                 }
734                         }
735                 }
736                 
737                 return 1 ;
738         }
739
740         @Override
741         public ArrayList<ServicesClient> startApplication( int _nb ) 
742         {
743                 int nb = clients.size() - computingClients.size() ;
744                 
745                 if( nb > _nb )
746                 {
747                         ArrayList<ServicesClient> ac = new ArrayList<ServicesClient>() ;
748                         ArrayList<ComputingClient> tmp = new ArrayList<Server.ComputingClient>() ;
749                         
750                         int i = 0 ;
751                         
752                         while( i < clients.size() && ac.size() < _nb )
753                         {
754                                 if( clients.get(i).getStatus().equalsIgnoreCase( "connected" ) ) 
755                                 {
756                                         int res = 1 ;
757                                         try {
758                                                 res = clients.get( i ).getStub().startVM( 0 ) ;
759                                         } catch( RemoteException e ) {
760                                                 e.printStackTrace();
761                                         }
762                                         
763                                         if( res == 0 )
764                                         {
765                                                 ac.add( clients.get( i ).getStub() ) ;
766                                                 clients.get( i ).setStatus( "running" ) ;
767                                                 ComputingClient cl = new ComputingClient( clients.get( i ) ) ;
768                                                 clients.get( i ).setComputingClient( cl ) ;
769                                                 computingClients.add( cl ) ;
770                                                 tmp.add( cl ) ;
771                                         } else {
772                                                 System.err.println( "Problem while launching the VM on " 
773                                                                 + clients.get(i).getName() + "!" ) ;
774                                         }
775                                 }
776                                 
777                                 i++ ;
778                         }
779                         
780                         if( ac.size() == _nb )
781                         {
782                                 int index, index2 ;
783                                 /* Choosing save neighbors */
784                                 for( i = 0 ; i < tmp.size() ; i++ )
785                                 {
786                                         if( i == tmp.size() - 1 )
787                                         {
788                                                 index = computingClients.indexOf( tmp.get( i ) ) ;
789                                                 index2 = computingClients.indexOf( tmp.get( 0 ) ) ;
790                                                 
791                                                 if( index == -1 || index2 == -1 )
792                                                 {
793                                                         System.err.println( "Problem in ComputingClients list!" ) ;
794                                                 } else {
795                                                         computingClients.get( index ).setSaveNeighbor( computingClients.get( index2 ).getClient().getStub() ) ;
796                                                 }
797                                         } else {
798                                                 index = computingClients.indexOf( tmp.get( i ) ) ;
799                                                 index2 = computingClients.indexOf( tmp.get( i + 1 ) ) ;
800                                                 
801                                                 if( index == -1 || index2 == -1 )
802                                                 {
803                                                         System.err.println( "Problem in ComputingClients list!" ) ;
804                                                 } else {
805                                                         computingClients.get( index ).setSaveNeighbor( computingClients.get( index2 ).getClient().getStub() ) ;
806                                                 }
807                                         }
808                                 }
809                         }
810                         
811                         /* Cleaning */
812                         tmp.clear() ;
813                         tmp = null ;
814                         
815                         return ac ;
816                 }
817                 
818                 return null ;
819         }
820
821
822
823         @Override
824         public void endApplication() 
825         {
826                 Iterator<ComputingClient> it = computingClients.iterator() ;
827                 
828                 while( it.hasNext() )
829                 {
830                         ComputingClient cl = it.next() ;
831
832                         try {
833                                 cl.getClient().getStub().stopVM() ;
834                         } catch (RemoteException e) {
835                                 e.printStackTrace();
836                         }
837                         
838                         cl.getClient().setStatus( "connected" ) ;
839                         cl.getClient().setComputingClient( null ) ;
840                         it.remove() ;
841                         cl = null ;
842                 }
843                 
844         }
845
846
847
848         @Override
849         public String getAssociatedIP( String _ip )
850         {
851                 String ret = null ;
852                 
853                 for( int i = 0 ; i < vmIPs.size() ; i++ )
854                 {
855                         if( vmIPs.get( i ).getHostIP().equalsIgnoreCase( _ip ) )
856                         {
857                                 ret = vmIPs.get( i ).getVmIP() ;
858                                 break ;
859                         }
860                 }
861                 
862                 return ret ;
863         }
864         
865         
866         public Integer deployVM( String _name, String _archive, String _directory )
867         {
868                 int pb = -1 ;
869                 int nb = 0 ;
870                 
871                 if( _name != null && _name.length() > 0 && _archive != null && _name.length() > 0 
872                                 && _directory != null && _directory.length() > 0 )
873                 {
874                         System.out.println( "Deploying the VM " + _name + " (" + _archive + ") ... " ) ;
875                         
876                         File file = new File( working_directory + "/" + _archive ) ;
877                         if( ! file.exists() )
878                         {
879                                 System.err.println( "There is no archive named " + _archive + " in my working directory!" ) ;
880                                 file = null ;
881                                 return 1 ;
882                         } else if( file.isDirectory() ) {
883                                 System.err.println( _archive + " is a directory!" ) ;
884                                 file = null ;
885                                 return 1 ;
886                         }
887                         
888                         file = null ;
889                         
890                         // TODO do a better deployment !!
891                         int ret ;
892                         boolean error ;
893                         
894                         pb = 0 ;
895                         
896                         for( int i = 0 ; i < clients.size() ; i++ )
897                         {
898                                 ret = 1 ;
899                                 error = false ;
900                                 try {
901                                         ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ;
902                                 } catch( RemoteException e ) {
903                                         System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ;
904                                         e.printStackTrace() ;
905                                 }
906                                 
907                                 // The client does not have the archive, we have to send it.
908                                 if( ret == 2 )
909                                 {
910                                         System.out.print( "Sending VM archive to " + clients.get( i ).getName() + " ... " ) ;
911                                         
912                                         String wd = "" ;
913                                         String snIP = "" ;
914                                         error = false ;
915                                         
916                                         try {
917                                                 wd = clients.get( i ).getStub().getWorkingDirectory() ;
918                                                 snIP = clients.get( i ).getStub().getIPHost() ;
919                                         } catch (RemoteException e2) {
920                                                 System.err.println( "Unable to retrieve information on " + clients.get( i ).getName() + "!" ) ;
921                                                 e2.printStackTrace() ;
922                                                 error = true ;
923                                                 pb++ ;
924                                         }
925                                         
926                                         String[] command = new String[]{ "/usr/bin/scp", working_directory + "/" + _archive,
927                                                                 snIP + ":" + wd } ;
928                                 
929                                         if( ! error )
930                                         try {
931                                                 Process proc = Runtime.getRuntime().exec( command ) ;
932                                                 proc.waitFor() ;
933                         
934                                                 if( proc.exitValue() == 0 )
935                                                 {
936                                                         System.out.println( "Initial VM archive successfully sent." ) ;
937                                                 } else {
938                                                         System.err.println( "Initial VM archive not sent!" ) ;
939 //                                                      printProcessError( p.getErrorStream() ) ;
940                                                         System.err.println( "Error: " + proc.exitValue() ) ;
941                                                         BufferedReader b = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) ) ;
942                                                         
943                                                         String l ;
944                                                         try {
945                                                                 while( (l = b.readLine()) != null )
946                                                                 {
947                                                                         System.err.println( l ) ;
948                                                                 }
949                                                         } catch( IOException e ) {
950                                                                         e.printStackTrace() ;
951                                                         }
952                                                         
953                                                         error = true ;
954                                                         pb++ ;
955                                                 }
956                                         } catch( IOException e ) {
957                                                 System.err.println( "Error during initial VM archive send command: " ) ;
958                                                 e.printStackTrace() ;
959                                                 error = true ;
960                                                 pb++ ;
961                                         } catch( InterruptedException e ) {
962                                                 e.printStackTrace() ;
963                                                 error = true ;
964                                                 pb++ ;
965                                         }
966                                 
967                                 
968                                         if( error )
969                                         {
970                                                 continue ;
971                                         }
972                                 
973                                         // Second try ...
974                                         ret = 1 ;
975                                         try {
976                                                 ret = clients.get( i ).getStub().deployVM( _name, _archive, _directory ) ;
977                                         } catch( RemoteException e ) {
978                                                 System.err.println( "Unable to deploy the VM on " + clients.get( i ).getName() + "!" ) ;
979                                                 e.printStackTrace() ;
980                                                 pb++ ;
981                                         }
982                                 }
983                                 
984                                 if( ret == 0 )
985                                 {
986                                         System.out.println( "Initial VM archive successfully deployed on " + clients.get( i ).getName() + "." ) ;
987                                         nb++ ;
988                                 }
989                         }
990                 }
991                 
992                 if( pb > 0 )
993                 {
994                         if( pb == 1 )
995                                 System.err.println( "** " + pb + " machine is not deployed!" ) ;
996                         if( pb > 1 )
997                                 System.err.println( "** " + pb + " machine(s) are not deployed!" ) ;
998                 }
999                 
1000                 return nb ;
1001         }
1002         
1003         
1004         public String getWorkingDirectory()
1005         {
1006                 return working_directory ;
1007         }
1008         
1009 }
1010
1011 /** La programmation est un art, respectons ceux qui la pratiquent !! **/
1012