Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Changing connection ports and names for logs.
[jaceP2P.git] / src / jaceP2P / JaceDaemon.java
1 package jaceP2P;
2
3 import java.net.ServerSocket;
4 import java.net.Socket;
5 import java.rmi.Naming;
6 import java.rmi.RemoteException;
7 import java.rmi.registry.LocateRegistry;
8 import java.rmi.registry.Registry;
9 import java.rmi.server.UnicastRemoteObject;
10
11 import and.Mapping.GNode;
12 import and.Mapping.Utils;
13
14 public class JaceDaemon {
15         // attributes
16         public static JaceDaemon Instance = null;
17         private String snode_IP = null;
18         private int snode_port = 1098;
19         private int daemonPort = 1098;
20         private int heartTime; // HeartBeat frequency
21         private String protocol;
22         private boolean running = false;
23
24         public JaceDaemon( String superNode, int port, String comProtocol ) {
25                 if( !superNode.equals( "-d" ) ) 
26                 {
27                         // snode_IP = LocalHost.Instance().resolve(superNode); // get IP of
28                         // the super node
29                         snode_IP = superNode ;
30                 }
31
32                 daemonPort = port ;
33                 protocol = comProtocol ;
34                 running = true ;
35                 Instance = this ;
36
37                 // initialize();
38         }
39
40         /**
41      *
42      **/
43         public JaceDaemon() {
44         }
45
46         /**
47      *
48      **/
49         public String getProtocol() {
50                 return protocol;
51         }
52
53         /**
54      *
55      **/
56         public synchronized static JaceDaemon Instance() {
57                 if (Instance == null) {
58                         Instance = new JaceDaemon();
59                 }
60
61                 return Instance;
62         }
63
64         /**
65      *
66      **/
67         public boolean isRunning() {
68                 return running;
69         }
70
71         /**
72      *
73      **/
74         public void initialize() {
75                 LocalHost.Instance().setPort( daemonPort ) ;
76                 exportObject() ; // Instanciate the JaceServer localy
77                 reconnectSuperNode() ; // Connect to one of the SuperNodes
78                 
79                 if( protocol.equals( "socket" ) ) 
80                 {
81                         listenForRequests() ;
82                 }
83         }
84
85         /**
86      *
87      **/
88         public void listenForRequests() {
89                 ServerSocket ss = null;
90
91                 try {
92
93                         ss = new ServerSocket(LocalHost.Instance().getSocketPort());
94
95                 } catch (Exception e) {
96                         System.err.println("Error initializing ServerSocket: " + e);
97                 }
98
99                 while (true) {
100                         Socket s = null;
101                         try {
102                                 // System.out.println( "Nouveau message" ) ;
103                                 s = ss.accept();
104                                 new HandleClient(s).start();
105                         } catch (Exception e) {
106                                 System.err.println("Error handling Client: " + e);
107                         }
108                 }
109         }
110
111         /**
112      *
113      **/
114         private void exportObject() {
115                 JaceInterface ref = null ;
116                 JaceServer myServer = null ;
117
118                 Registry reg ;
119                 
120                 try {
121                         
122                         while( true )
123                         {
124                                 reg = LocateRegistry.getRegistry( daemonPort ) ;
125
126                                 String tab[] = reg.list() ;
127                         
128                                 System.out.println( "There is an existing RMI Registry on port "+daemonPort+" with "+tab.length+" entries!" ) ;
129                                 for( int i = 0 ; i < tab.length ; i++ )
130                                 {
131                                         try {
132                                                 if( UnicastRemoteObject.unexportObject( Naming.lookup(tab[i]), true ) )
133                                                 {
134                                                         System.out.println( "Register successfuly deleted!" ) ;
135                                                 } else {
136                                                         System.err.println( "Register undeleted !!!" ) ;
137                                                 }
138                                         } catch (Exception e) {
139                                                 e.printStackTrace();
140                                         }
141                                 }
142                         
143                                 if( UnicastRemoteObject.unexportObject( myServer, true ) )
144                                 {
145                                         System.out.println( "Register successfuly deleted!" ) ;
146                                 } else {
147                                         System.err.println( "Register undeleted !!!" ) ;
148                                 }
149                         }
150                 } catch( RemoteException e ) {
151                 }       
152                 
153                 
154                 try {
155                         if (System.getSecurityManager() == null) {
156                     System.setSecurityManager(new SecurityManager());
157                 }
158                         
159                         // launch the JaceServer
160                         myServer = new JaceServer( /* this */);
161                         LocateRegistry.createRegistry(daemonPort);
162                         LocateRegistry.getRegistry(daemonPort).rebind(
163                                         "JaceServer", myServer);
164                         ref = (JaceInterface) Naming.lookup("rmi://"
165                                         + LocalHost.Instance().getIP() + ":" + daemonPort
166                                         + "/JaceServer");
167                 } catch (Exception e) {
168                         System.err
169                                         .println("JaceP2P_Error in JaceRuntime.exportObject() when creating the local JaceServer "
170                                                         + e);
171                         System.err.println("Exit from JaceRuntime.exportObject");
172                         System.exit(1);
173                 }
174
175                 LocalHost.Instance().setStub(ref);
176         }
177
178         /**
179      *
180      **/
181         @SuppressWarnings("unused")
182         private void sortir() {
183                 if (protocol.equals("rmi")) {
184                         try {
185                                 java.rmi.registry.LocateRegistry.getRegistry(daemonPort)
186                                                 .unbind("JaceServer");
187                                 //System.out.println("Unbind done !!!!!!!!!!!1");
188                         } catch (Exception e) {
189                                 System.err
190                                                 .println("JaceP2P_Error in JaceRuntime.sortir() when unbind "
191                                                                 + e);
192                         }
193                 }
194
195         }
196         
197         
198         /**
199      *
200      **/
201         public void reconnectSuperNode() {
202
203                 //System.out.println("I'm looking for a JaceP2P Super Node");
204                 //System.out.println(protocol);
205                 // if( protocol.equals( "rmi" ) ) {
206                 JaceSuperNodeInterface snodeStub = null;
207                 boolean connected = false;
208
209                 // while( connected == false ) {
210                 if (snode_IP != null) {
211                         try {
212                                 //System.out.println("Trying to invoke Super Node " + snode_IP);
213                                 snodeStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
214                                                 + snode_IP + ":" + snode_port + "/JaceSuperNode");
215                                 //System.out.println("Succesfully located " + snode_IP);
216
217                                 // Add stub and IP in LocalHost to store it until super node
218                                 // death
219                                 LocalHost.Instance().setSuperNodeStub(snodeStub);
220                                 LocalHost.Instance().setSuperNodeIP(snode_IP);
221                                 connected = true;
222
223                         } catch (Exception e) {
224                                 //System.out
225                                 //              .println("Snode not launched, try another one (1/2s)");
226 //                              try {
227 //                                      Thread.sleep(500);
228 //                              } catch (Exception e1) {
229 //                                      // nothing
230 //                              }
231                         }
232                 }
233
234                 if (connected == false) {
235                         int i = 0;
236
237                         SuperNodeListe.Instance().staticInitialization();
238
239                         while (connected == false
240                                         && i < SuperNodeListe.Instance().getListe().size()) {
241                                 SuperNodeData d = null;
242                                 d = SuperNodeListe.Instance().getSuperNodeData(i);
243                                 snode_IP = LocalHost.Instance().resolve(d.getIP());
244                                 snode_port = d.getPort();
245
246                                 try {
247                                         //System.out.println("Trying to invoke super node "
248                                         //              + snode_IP);
249                                         snodeStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
250                                                         + snode_IP + ":" + snode_port + "/JaceSuperNode");
251                                         // System.out.println( "succesfully located " + snode_IP ) ;
252
253                                         // Add stub and IP in LocalHost to store it until super node
254                                         // death
255                                         LocalHost.Instance().setSuperNodeStub(snodeStub);
256                                         LocalHost.Instance().setSuperNodeIP(snode_IP);
257                                         connected = true;
258
259                                 } catch (Exception e) {
260                                         //System.out.println("Snode " + snode_IP
261                                         //              + " not launched, try another one (1/2s)");
262                                         i++;
263
264                                         try {
265                                                 Thread.sleep(500);
266                                         } catch (Exception e1) {
267                                                 // nothing
268                                         }
269
270                                         // If error, exit the loop and reenter in order to find
271                                         // another Snode
272                                         // continue ;
273                                 }
274                         }
275                 }
276
277                 if (connected == false) {
278                         System.err
279                                         .println("All the SuperNodes in the list are dead, unable to connect to the platform");
280                         System.exit(1);
281                 } else {
282
283                         // Registering to the Super Node
284                         try {
285                                 heartTime = snodeStub.getSuperNodeBeat();
286
287                                 // snodeStub.workerRegistering( LocalHost.Instance().getStub(),
288                                 // LocalHost.Instance().getIP(),
289                                 // LocalHost.Instance().getName(),
290                                 // daemonPort ) ;
291                                 
292                                 GNode me = Utils.createGNode() ;
293                                 
294                                 if( me == null )
295                                 {
296                                         System.err.println( "Problem during the creation of my GNode !!" ) ;
297                                         System.exit( 1 ) ;
298                                 } else {
299                                         if( me.getIP().equals( "" ) || me.getIP() == null
300                                                 || me.getIP().isEmpty() 
301                                                 || ! me.getIP().equals( LocalHost.Instance().getIP()))
302                                         {
303                                                 System.err.println( "Problem with my IP address in my GNode !!" ) ;
304                                                 System.exit( 2 ) ;
305                                         }
306                                 }
307
308                                 snodeStub.workerRegistering(LocalHost.Instance().getStub(),
309                                                 LocalHost.Instance().getIP(), LocalHost.Instance()
310                                                 .getName(), daemonPort, me ) ;
311
312                                 // Launching the heart beat thread
313                                 HeartBeatThread.Instance().setHeartTime(heartTime);
314
315                                 //System.out.println("Starting to ping the Super Node");
316                                 HeartBeatThread.Instance().setServer((Object) snodeStub);
317
318                                 if (HeartBeatThread.Instance().isAlive() == false) {
319                                         // HeartBeatThread.Instance().setPriority(Thread.MAX_PRIORITY);
320                                         HeartBeatThread.Instance().start(); // Starting the beating
321                                 }
322
323                                 //System.out.println("Succesfully registered to Super Node "
324                                 //              + snode_IP);
325
326                         } catch (Exception e) {
327                                 System.err
328                                                 .println("JaceP2P_Error in JaceDamon reconnectSuperNode  when invoking function workerRegistering() : "
329                                                                 + e);
330                                 reconnectSuperNode();
331                         }
332                 }
333                 // }
334                 // }
335         }
336
337         /**
338      *
339      **/
340         public void reinitDaemon() {
341
342                 if (JaceDaemon.Instance().getProtocol().equals("rmi")) {
343                         SenderRmi.Instance().getBuffer().stopGet = true;
344
345                         try {
346                                 SenderRmi.Instance().notify();
347                         } catch (Exception e) {
348                                 // nothing
349                         }
350
351                         SenderRmi.Instance().kill();
352
353                 } else {
354                         SenderSocket.Instance().getBuffer().stopGet = true;
355
356                         try {
357                                 SenderSocket.Instance().notify();
358                         } catch (Exception e) {
359                                 // nothing
360                         }
361
362                         SenderSocket.Instance().kill();
363                 }
364
365                 //System.out.println("$$$$$$$$$$ Killed Sender $$$$$$$$$$");
366                 ScanThread.Instance().kill();
367                 LocalHost.Instance().setStartedThreads(false);
368                 System.out.println("Reinitialization of the Daemon");
369
370                 System.out.println( "I kill the application if any exists" ) ;
371                 // ScanThread.Instance().setScanning( false ) ;
372                 // System.out.println( "Set ScanThread off" ) ;
373                 // Sender.Instance().kill() ;
374
375                 try {
376                         Thread.sleep( 2500 ) ;
377                 } catch (Exception e) {
378                         // nothing
379                 }
380
381                 // Sender.Instance().running = false ;
382                 try {
383                         // Cleaning JaceBuffer, MsgQueue, Register
384                         // and deleting taskObject and taskThread
385                         JaceSession.Instance().kill(); // Also to do when a node has been
386                         // down, in the case he is back
387                         System.out.println("Application killed properly");
388                         BackupsManager.Instance().purge();
389                         Register.Instance().purge();
390
391                 } catch (Exception e) {
392                         System.err.println( "Crashed killing the application : " + e ) ;
393                 }
394
395                 System.out.println( "Daemon reinitialized" ) ;
396                 System.gc() ;
397                 reconnectSuperNode();
398         }
399
400 }
401
402 /** ! **/
403