Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[jaceP2P.git] / src / jaceP2P / HeartBeatThread.java
1 package jaceP2P;
2
3 public class HeartBeatThread extends Thread {
4
5         // atributes
6         public static HeartBeatThread Instance;
7
8         private Object server = null;
9         private int beat;
10         // private long time;
11         private JaceSuperNodeInterface superNodeStub = null;
12         // private JaceInterface stub = null;
13         private boolean running = true;
14         int count = 0;
15
16         // constructors
17         private HeartBeatThread() {
18         }
19
20         public static HeartBeatThread Instance() {
21                 if (Instance == null) {
22                         Instance = new HeartBeatThread();
23                 }
24                 return Instance;
25         }
26
27         public void setHeartTime(int timeBeat) {
28                 beat = timeBeat;
29         }
30
31         public int getHeartTime() {
32                 return beat;
33         }
34
35         public void setServer(Object serverEntity) {
36                 server = serverEntity;
37                 count = 0;
38                 // JaceBuffer.Instance().purge();
39                 // MsgQueue.Instance().purge();
40
41         }
42
43         public void kill() {
44                 System.out.println("Killing HeartBeatThread ...");
45                 running = false;
46                 Instance = null;
47         }
48
49         public void run() {
50                 // long timeGiven;
51                 // long begin;
52                 // long end;
53                 int count = 0;
54                 while (running) {
55                         try {
56                                 // each "time" milisecondes, get the register if it has changed
57                                 Thread.sleep(beat);
58                                 // time = System.currentTimeMillis();
59                                 if (server instanceof JaceSuperNodeInterface) {
60
61                                         superNodeStub = (JaceSuperNodeInterface) server;
62                                         // begin = System.currentTimeMillis();
63                                         superNodeStub.beating(LocalHost.Instance().getStub());
64
65                                 } // else {
66
67                                 // stub = (JaceInterface) server;
68
69                                 // timeGiven = stub.beating(LocalHost.Instance().getStub());
70                                 // }
71
72                                 yield();
73
74                         } catch (Exception e) {
75                                 try {
76                                         if (server instanceof JaceSuperNodeInterface) {
77                                                 System.out.println("The SuperNode is Dead : " + e);
78                                                 LocalHost.Instance().getStub().reconnectSuperNode();
79                                         } else {
80                                                 // System.out.println("The spawner is Dead : " + e);
81                                                 // LocalHost.Instance().getStub().reconnectSuperNode();
82                                                 System.out
83                                                                 .println("The next node maybe dead!!!! count="
84                                                                                 + count);
85                                                 count++;
86                                                 if (count > 3)
87                                                         try {
88                                                                 int myRank;
89                                                                 TaskId id = Register.Instance()
90                                                                                 .getListeOfTasks().getTaskIdOfHostStub(
91                                                                                                 LocalHost.Instance().getStub());
92                                                                 myRank = id.getRank();
93                                                                 Register newReg = Register.Instance()
94                                                                                 .getSpawnerStub().getRegister(myRank);
95                                                                 if (newReg != null) {
96                                                                         Register.Instance().replaceBy(newReg);
97                                                                         TaskId neighbor = Register
98                                                                                         .Instance()
99                                                                                         .getListeOfTasks()
100                                                                                         .getTaskIdOfRank(
101                                                                                                         (myRank + 1)
102                                                                                                                         % Register
103                                                                                                                                         .Instance()
104                                                                                                                                         .getListeOfTasks()
105                                                                                                                                         .getSize());
106                                                                         server = (Object) neighbor.getHostStub();
107                                                                         count = 0;
108                                                                 } else
109                                                                         System.out
110                                                                                         .println("The server returned a null register !");
111                                                         } catch (Exception e2) {
112                                                                 System.err
113                                                                                 .println("Unable to contact the Spawner :"
114                                                                                                 + e2);
115                                                         }
116                                         }
117
118                                         yield();
119                                 } catch (Exception ex) {
120                                         System.err.println("Cannot reconnect to the SuperNode "
121                                                         + ex);
122                                 }
123                         }
124                         count++;
125                 }
126         }
127 }