3 import java.rmi.Naming;
5 import and.Mapping.GNode;
7 public class JaceSuperNode {
8 final int NB_HEART_DECONNECT = 3;
10 private int heartTime; // frequency of heartBeat
11 private int port = 1098; // par la suite, donner par fichier de conf
12 private int timeBeforeKill; // wait for 3 non response of heartBeat to kill
16 private JaceSuperNodeServer snodeServer ;
18 public JaceSuperNode(String superNodeName, int port, String comProtocol,
21 timeBeforeKill = NB_HEART_DECONNECT * heartTime;
23 protocol = comProtocol;
27 public void initialize() {
28 // if(protocol.equals("rmi")){
30 // create his list of SuperNode
31 // containing the IPs and ports
32 // but not already the stubs
33 SuperNodeListe.Instance().staticInitialization();
35 HeartBeatSNode.Instance().setHeartTime(heartTime);
36 JaceSuperNodeInterface myStub = null;
39 snodeServer = new JaceSuperNodeServer(heartTime);
41 // lauch the rmiregistry
42 java.rmi.registry.LocateRegistry.createRegistry(port);
43 java.rmi.registry.LocateRegistry.getRegistry(port).rebind(
44 "JaceSuperNode", snodeServer);
45 myStub = (JaceSuperNodeInterface) Naming.lookup("rmi://"
46 + LocalHost.Instance().getIP() + ":" + port
48 LocalHost.Instance().setSuperNodeStub(myStub);
49 LocalHost.Instance().setPort(port);
50 System.out.println("SuperNode " + LocalHost.Instance().getIP()
51 + " launched and waiting for invokations on port " + port);
52 } catch (Exception e) {
54 .println("JaceP2P_Error in JaceSuperNode.initialize() when biding the JaceSuperNodeServer : "
56 System.err.println("Exit in JaceSuperNode.initialise()");
60 // get the stubs of the conected SuperNodes
61 SuperNodeListe.Instance().locateSuperNodes(myStub);
65 int index = SuperNodeListe.Instance().existSuperNode(
66 LocalHost.Instance().getIP());
68 if (index == (SuperNodeListe.Instance().getListe().size() - 1))
72 HeartBeatSNode.Instance().setServer(
73 ((SuperNodeData) SuperNodeListe.Instance().getListe()
74 .elementAt(next)).getStub());
76 previous = SuperNodeListe.Instance().getListe().size() - 1;
79 System.out.println(index + " " + next + " " + previous);
81 ((SuperNodeData) SuperNodeListe.Instance().getListe().elementAt(
82 previous)).getStub().updateHeart(
83 ((SuperNodeData) SuperNodeListe.Instance().getListe()
84 .elementAt(index)).getStub());
85 } catch (Exception e) {
88 .println("Unable to modify heartbeat server for previous node"
92 HeartBeatSNode.Instance().start();
94 Thread.sleep(HeartBeatSNode.Instance().getHeartTime());
95 } catch (Exception e) {
97 TokenThread.Instance().start();
98 ScanThreadSuperNode.Instance().start();
102 // mettre des threads pr scanner
103 public void startScanningNodes() {
105 // scan at every "heartTime" milisecondes if nodes registered are
107 scanConnectedHosts();
110 Thread.sleep(heartTime);
111 } catch (Exception e) {
116 // verify if the nodes labeled "alive" in
117 // Register.Instance() of the SuperNode are still alive
118 private synchronized void scanConnectedHosts() {
123 // detects the nodes connected that should have died
124 for (int i = 0; i < Register.Instance().getSize(); i++) {
125 host = Register.Instance().getNodeAt(i);
126 if (host.getAliveFlag() == true && host.getAppliName() == null) {
127 workerTime = host.getAliveTime();
128 currentTime = System.currentTimeMillis();
130 // if the worker time has not changed since more than
131 // "timeBeforeKill" milisecondes, it is considered down
132 if (currentTime - workerTime > timeBeforeKill) {
133 // System.out.println(host.getName() +
134 // " : difference of time = " + (currentTime - workerTime));
135 host.setAliveFlag(false);
136 // try to reconnect the daemon to the super node
138 // if(protocol.equals("rmi")){
139 host.getStub().reconnectSuperNode();
140 // System.out.println("Daemon reconnected to the super node");
142 } catch (Exception e) {
143 System.out.println("\nDISCONNECTION of " + host.getName()
144 + " size : " + Register.Instance().getSize());
147 // System.out.println("I remove the node because it doesnt answer anymore");
148 Register.Instance().removeNode(host);
149 int index = SuperNodeListe.Instance().existSuperNode(
150 LocalHost.Instance().getIP());
151 ((SuperNodeData) SuperNodeListe.Instance().getListe().get(
152 index)).setNbOfNodes(Register.Instance().getSize());
153 SuperNodeListe.Instance().forwardCountNode();
155 GNode deadGNode = snodeServer.delGNodeFromList( host, 0 ) ;
156 SuperNodeListe.Instance().removeGNode( deadGNode ) ;
158 // Register.Instance().viewAll();
159 // SuperNodeListe.Instance().viewAll();