3 import java.rmi.Naming;
4 import java.rmi.RemoteException;
6 public class JaceSuperNode {
7 final int NB_HEART_DECONNECT = 3;
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
15 private JaceSuperNodeServer snodeServer ;
17 public JaceSuperNode(String superNodeName, int port, String comProtocol,
20 timeBeforeKill = NB_HEART_DECONNECT * heartTime;
22 protocol = comProtocol;
26 public void initialize() {
27 // if(protocol.equals("rmi")){
29 // create his list of SuperNode
30 // containing the IPs and ports
31 // but not already the stubs
32 SuperNodeListe.Instance().staticInitialization();
34 HeartBeatSNode.Instance().setHeartTime(heartTime);
35 JaceSuperNodeInterface myStub = null;
38 snodeServer = new JaceSuperNodeServer(heartTime);
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
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) {
53 .println("JaceP2P_Error in JaceSuperNode.initialize() when biding the JaceSuperNodeServer : "
55 System.err.println("Exit in JaceSuperNode.initialise()");
59 // get the stubs of the conected SuperNodes
60 SuperNodeListe.Instance().locateSuperNodes(myStub);
64 int index = SuperNodeListe.Instance().existSuperNode(
65 LocalHost.Instance().getIP());
67 if (index == (SuperNodeListe.Instance().getListe().size() - 1))
71 HeartBeatSNode.Instance().setServer(
72 ((SuperNodeData) SuperNodeListe.Instance().getListe()
73 .elementAt(next)).getStub());
75 previous = SuperNodeListe.Instance().getListe().size() - 1;
78 System.out.println(index + " " + next + " " + previous);
80 ((SuperNodeData) SuperNodeListe.Instance().getListe().elementAt(
81 previous)).getStub().updateHeart(
82 ((SuperNodeData) SuperNodeListe.Instance().getListe()
83 .elementAt(index)).getStub());
84 } catch (Exception e) {
87 .println("Unable to modify heartbeat server for previous node"
91 HeartBeatSNode.Instance().start();
93 Thread.sleep(HeartBeatSNode.Instance().getHeartTime());
94 } catch (Exception e) {
96 TokenThread.Instance().start();
97 ScanThreadSuperNode.Instance().start();
101 // mettre des threads pr scanner
102 public void startScanningNodes() {
104 // scan at every "heartTime" milisecondes if nodes registered are
106 scanConnectedHosts();
109 Thread.sleep(heartTime);
110 } catch (Exception e) {
115 // verify if the nodes labeled "alive" in
116 // Register.Instance() of the SuperNode are still alive
117 private synchronized void scanConnectedHosts() {
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();
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
137 // if(protocol.equals("rmi")){
138 host.getStub().reconnectSuperNode();
139 // System.out.println("Daemon reconnected to the super node");
141 } catch (Exception e) {
142 System.out.println("\nDISCONNECTION of " + host.getName()
143 + " size : " + Register.Instance().getSize());
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();
155 snodeServer.delGNodeFromList( host, 0, "" ) ;
156 } catch (RemoteException e) {
157 System.err.println( "Unable to remove the dead node from the list !" ) ;
160 // SuperNodeListe.Instance().removeGNode( deadGNode ) ;
162 // Register.Instance().viewAll();
163 // SuperNodeListe.Instance().viewAll();