Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Creating the JaceP2P repository.
[jaceP2P.git] / src / jaceP2P / ScanThreadSpawner.java
1 package jaceP2P;
2
3 import java.util.Calendar;
4 import java.util.GregorianCalendar;
5
6
7
8 public class ScanThreadSpawner extends Thread {
9         public static ScanThreadSpawner Instance;
10         public boolean scanning = false;
11         public int beat;
12         public int timeBeforeKill;
13         public long aliveTime = System.currentTimeMillis();
14         public int myRank;
15         public int neighborRank;
16         public boolean running = false;
17         public JaceSpawnerInterface previousSpawner = null;
18
19         private ScanThreadSpawner() {
20                 // beat=HeartBeatThread.Instance().getHeartTime();
21                 // timeBeforeKill=beat*3;
22                 running = true;
23
24         }
25
26         public void setHeartTime(int beat) {
27                 this.beat = beat;
28                 timeBeforeKill = beat * 4;
29         }
30
31         public void setServer(JaceSpawnerInterface stub) {
32                 previousSpawner = stub;
33                 aliveTime = System.currentTimeMillis();
34         }
35
36         public static ScanThreadSpawner Instance() {
37                 if (Instance == null) {
38                         System.out.println("creating new ScanThreadSpawner ");
39                         Instance = new ScanThreadSpawner();
40                 }
41                 return Instance;
42         }
43
44         public void kill() {
45                 Instance = null;
46                 running = false;
47         }
48
49         public void setAliveTime() {
50                 aliveTime = System.currentTimeMillis();
51                 // System.out.println("spawner is pinging me at "+aliveTime);
52         }
53
54         @SuppressWarnings("static-access")
55         public void run() {
56                 System.out.println("start ScanThreadSpawner.......");
57                 while (running) {
58                         // System.out.println("ScanThread alive.......");
59                         try {
60                                 this.sleep(beat);
61                         } catch (Exception e) {
62                         }
63                         // System.out.println("test Neighbor: "+neighborRank);
64                         testNeighbor();
65
66                 }
67
68         }
69
70         public void testNeighbor() {
71                 //Node host;
72                 //Node tmpNode;
73                 //long workerTime;
74                 long currentTime;
75
76                 //int restempo;
77                 //int nb = 0;
78                 //int nbC = 0;
79                 //boolean changed = false;
80                 try {
81                         currentTime = System.currentTimeMillis();
82                         if (currentTime - aliveTime > timeBeforeKill
83                                         && Register.Instance().getSpawnerStub().getFinished() == false) {
84                                 Calendar cal = new GregorianCalendar();
85                                 System.out.println("at time=" + cal.get(Calendar.MINUTE) + ":"
86                                                 + cal.get(Calendar.SECOND));
87                                 System.out.println("neighbor maybe Dead, "
88                                                 + (currentTime - aliveTime) + " " + currentTime + " "
89                                                 + aliveTime + " result "
90                                                 + (currentTime - aliveTime > timeBeforeKill)
91                                                 + " timebk=" + timeBeforeKill);
92                                 boolean response = false;
93                                 try {
94                                         response = previousSpawner.ping();
95                                         System.out.println("previous spawner is still alive");
96                                 } catch (Exception e) {
97                                         System.out
98                                                         .println("the previous spawner is officially dead "
99                                                                         + e);
100                                 }
101                                 if (response == false) {
102                                         JaceSpawner.Instance().getNewSpawner(previousSpawner);
103                                         sleep(4 * beat);
104                                 }
105                         }
106                 } catch (Exception e) {
107                         System.out.println("error in test neighbor: " + e);
108                 }
109         }
110 }