Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modification of mapping algorithm identifier mechanisms.
[jaceP2P.git] / src / jaceP2P / JaceSuperNode.java
1 package jaceP2P;
2
3 import java.rmi.Naming;
4 import java.rmi.RemoteException;
5
6 public class JaceSuperNode {
7         final int NB_HEART_DECONNECT = 3;
8         // attribute
9         private int heartTime; // frequency of heartBeat
10         private int port = 1098; // par la suite, donner par fichier de conf
11         private int timeBeforeKill; // wait for 3 non response of heartBeat to kill
12                                                                 // the Daemon
13         String protocol;
14         
15         private JaceSuperNodeServer snodeServer ;
16
17         public JaceSuperNode(String superNodeName, int port, String comProtocol,
18                         int beat) {
19                 heartTime = beat;
20                 timeBeforeKill = NB_HEART_DECONNECT * heartTime;
21                 this.port = port;
22                 protocol = comProtocol;
23                 snodeServer = null ;
24         }
25
26         public void initialize() {
27                 // if(protocol.equals("rmi")){
28
29                 // create his list of SuperNode
30                 // containing the IPs and ports
31                 // but not already the stubs
32                 SuperNodeListe.Instance().staticInitialization();
33
34                 HeartBeatSNode.Instance().setHeartTime(heartTime);
35                 JaceSuperNodeInterface myStub = null;
36
37                 try {
38                         snodeServer = new JaceSuperNodeServer(heartTime);
39
40                         // lauch the rmiregistry
41                         java.rmi.registry.LocateRegistry.createRegistry(port);
42                         java.rmi.registry.LocateRegistry.getRegistry(port).rebind(
43                                         "JaceSuperNode", snodeServer);
44                         myStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
45                                         + LocalHost.Instance().getIP() + ":" + port
46                                         + "/JaceSuperNode");
47                         LocalHost.Instance().setSuperNodeStub(myStub);
48                         LocalHost.Instance().setPort(port);
49                         System.out.println("SuperNode " + LocalHost.Instance().getIP()
50                                         + " launched and waiting for invokations on port " + port);
51                 } catch (Exception e) {
52                         System.err
53                                         .println("JaceP2P_Error in JaceSuperNode.initialize() when biding the JaceSuperNodeServer : "
54                                                         + e);
55                         System.err.println("Exit in JaceSuperNode.initialise()");
56                         System.exit(1);
57                 }
58
59                 // get the stubs of the conected SuperNodes
60                 SuperNodeListe.Instance().locateSuperNodes(myStub);
61
62                 // }
63
64                 int index = SuperNodeListe.Instance().existSuperNode(
65                                 LocalHost.Instance().getIP());
66                 int next, previous;
67                 if (index == (SuperNodeListe.Instance().getListe().size() - 1))
68                         next = 0;
69                 else
70                         next = index + 1;
71                 HeartBeatSNode.Instance().setServer(
72                                 ((SuperNodeData) SuperNodeListe.Instance().getListe()
73                                                 .elementAt(next)).getStub());
74                 if (index == 0)
75                         previous = SuperNodeListe.Instance().getListe().size() - 1;
76                 else
77                         previous = index - 1;
78                 System.out.println(index + " " + next + " " + previous);
79                 try {
80                         ((SuperNodeData) SuperNodeListe.Instance().getListe().elementAt(
81                                         previous)).getStub().updateHeart(
82                                         ((SuperNodeData) SuperNodeListe.Instance().getListe()
83                                                         .elementAt(index)).getStub());
84                 } catch (Exception e) {
85
86                         System.err
87                                         .println("Unable to modify heartbeat server for previous node"
88                                                         + e);
89                 }
90
91                 HeartBeatSNode.Instance().start();
92                 try {
93                         Thread.sleep(HeartBeatSNode.Instance().getHeartTime());
94                 } catch (Exception e) {
95                 }
96                 TokenThread.Instance().start();
97                 ScanThreadSuperNode.Instance().start();
98                 startScanningNodes();
99         }
100
101         // mettre des threads pr scanner
102         public void startScanningNodes() {
103                 while (true) {
104                         // scan at every "heartTime" milisecondes if nodes registered are
105                         // still alive
106                         scanConnectedHosts();
107
108                         try {
109                                 Thread.sleep(heartTime);
110                         } catch (Exception e) {
111                         }
112                 }
113         }
114
115         // verify if the nodes labeled "alive" in
116         // Register.Instance() of the SuperNode are still alive
117         private synchronized void scanConnectedHosts() {
118                 Node host;
119                 long workerTime;
120                 long currentTime;
121
122                 // detects the nodes connected that should have died
123                 for (int i = 0; i < Register.Instance().getSize(); i++) {
124                         host = Register.Instance().getNodeAt(i);
125                         if (host.getAliveFlag() == true && host.getAppliName() == null) {
126                                 workerTime = host.getAliveTime();
127                                 currentTime = System.currentTimeMillis();
128
129                                 // if the worker time has not changed since more than
130                                 // "timeBeforeKill" milisecondes, it is considered down
131                                 if (currentTime - workerTime > timeBeforeKill) {
132                                         // System.out.println(host.getName() +
133                                         // " : difference of time = " + (currentTime - workerTime));
134                                         host.setAliveFlag(false);
135                                         // try to reconnect the daemon to the super node
136                                         try {
137                                                 // if(protocol.equals("rmi")){
138                                                 host.getStub().reconnectSuperNode();
139                                                 // System.out.println("Daemon reconnected to the super node");
140                                                 // }
141                                         } catch (Exception e) {
142                                                 System.out.println("\nDISCONNECTION of " + host.getName()
143                                                                 + " size : " + Register.Instance().getSize());
144                                         }
145
146                                         // System.out.println("I remove the node because it doesnt answer anymore");
147                                         Register.Instance().removeNode(host);
148                                         int index = SuperNodeListe.Instance().existSuperNode(
149                                                         LocalHost.Instance().getIP());
150                                         ((SuperNodeData) SuperNodeListe.Instance().getListe().get(
151                                                         index)).setNbOfNodes(Register.Instance().getSize());
152                                         SuperNodeListe.Instance().forwardCountNode();
153                                         
154                                         try {
155                                                 snodeServer.delGNodeFromList( host, 0, "" ) ;
156                                         } catch (RemoteException e) {
157                                                 System.err.println( "Unable to remove the dead node from the list !" ) ;
158                                                 e.printStackTrace();
159                                         }
160 //                                      SuperNodeListe.Instance().removeGNode( deadGNode ) ;
161
162                                         // Register.Instance().viewAll();
163                                         // SuperNodeListe.Instance().viewAll();
164                                 }
165                         }
166                 }
167         }
168 }