Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[jaceP2P.git] / src / jaceP2P / HeartBeatSpawner.java
1 package jaceP2P;
2
3 public class HeartBeatSpawner extends Thread {
4
5         // atributes
6         public static HeartBeatSpawner Instance;
7
8         private boolean running = true;
9         private int beat;
10         // private long time;
11         private JaceSpawnerInterface spawnerStub = null;
12
13         int count = 0;
14
15         // constructors
16         private HeartBeatSpawner() {
17         }
18
19         public static HeartBeatSpawner Instance() {
20                 if (Instance == null) {
21                         System.out.println("creating new HeartBeatSpawner ");
22                         Instance = new HeartBeatSpawner();
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(JaceSpawnerInterface serverEntity) {
36                 spawnerStub = serverEntity;
37                 count = 0;
38                 // JaceBuffer.Instance().purge();
39                 // MsgQueue.Instance().purge();
40
41         }
42
43         public JaceSpawnerInterface getServer() {
44                 return spawnerStub;
45         }
46
47         public void kill() {
48                 running = false;
49                 Instance = null;
50         }
51
52         public void run() {
53                 // long timeGiven;
54                 // long begin;
55                 // long end;
56                 while (running) {
57                         try {
58                                 // each "time" milisecondes, get the register if it has changed
59                                 Thread.sleep(beat);
60                                 // System.out.println("sleeping for "+beat);
61                                 // time = System.currentTimeMillis();
62
63                                 spawnerStub.beating();
64
65                                 yield();
66
67                         } catch (Exception e) {
68                         }
69
70                 }
71         }
72 }