Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f7aa36cbaf76fc9b5bec9a8f12f174847183227a
[jaceP2P.git] / src / jaceP2P / ScanThread.java
1 package jaceP2P;
2
3 import java.rmi.RemoteException;
4
5
6 public class ScanThread extends Thread {
7         public static ScanThread Instance;
8         public boolean scanning = false;
9         public int beat;
10         public int timeBeforeKill;
11         public int myRank;
12         public int neighborRank;
13         public boolean running = false;
14
15         private ScanThread() {
16                 beat = HeartBeatThread.Instance().getHeartTime();
17                 timeBeforeKill = beat * 3;
18                 running = true;
19
20         }
21
22         public static ScanThread Instance() {
23                 if (Instance == null) {
24                         System.out.println("Creating new ScanThread ");
25                         Instance = new ScanThread();
26                 }
27                 return Instance;
28         }
29
30         public void setScanning(boolean bool) {
31 //              System.out.println("in setScanning !!!!!");
32                 scanning = bool;
33         }
34
35         public void kill() {
36                 Instance = null;
37                 running = false;
38         }
39
40         @SuppressWarnings("static-access")
41         public void run() {
42                 System.out.println("Start ScanThread.......");
43                 while (running) {
44                         // System.out.println("ScanThread alive.......");
45                         if (scanning == false)
46                                 try {
47                                         this.wait();
48                                 } catch (Exception e) {
49                                 }
50                         else {
51                                 // System.out.println("test Neighbor: "+neighborRank);
52                                 testNeighbor();
53                                 try {
54                                         this.sleep(beat);
55                                 } catch (Exception e) {
56                                 }
57                         }
58                 }
59         }
60
61         @SuppressWarnings("static-access")
62         public void testNeighbor() {
63                 Node host;
64                 //Node tmpNode;
65                 long workerTime;
66                 long currentTime;
67
68                 //int restempo;
69                 //int nb = 0;
70                 //int nbC = 0;
71                 //boolean changed = false;
72                 try {
73                         TaskId id = Register.Instance().getListeOfTasks()
74                                         .getTaskIdOfHostStub(LocalHost.Instance().getStub());
75                         myRank = id.getRank();
76                         if (myRank == 0)
77                                 neighborRank = Register.Instance().getNbOfTasks() - 1;
78                         else
79                                 neighborRank = myRank - 1;
80                         try {
81                                 // TaskId myTaskId = null;
82                                 TaskId neighborTask = Register.Instance().getListeOfTasks()
83                                                 .getTaskIdOfRank(neighborRank);
84                                 JaceInterface jaceStub = neighborTask.getHostStub();
85                                 host = Register.Instance().getNodeOfStub(jaceStub);
86                                 // if (host.getAliveFlag() == true) {
87                                 workerTime = host.getAliveTime();
88                                 currentTime = System.currentTimeMillis();
89                                 if (currentTime - workerTime > timeBeforeKill) {
90                                         // Calendar cal = new GregorianCalendar();
91                                         // System.out.println("at time="+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));
92                                         // System.out.println("currentTime="+currentTime+" workerTime="+workerTime+" timeBeforeKill="+timeBeforeKill);
93
94                                         host.setAliveFlag(false);
95                                         host.setAppliName(null);
96
97                                         // String ip = LocalHost.Instance().resolve(host.getName());
98                                         boolean reponse;
99                                         boolean dead = false;
100                                         try {
101                                                 Node noeud = Register.Instance().getNodeOfStub(
102                                                                 host.getStub());
103
104                                                 reponse = host.getStub().ping();
105                                                 if (reponse == true) {
106                                                         noeud.setAliveFlag(true);
107                                                         noeud.setAliveTime();
108                                                         noeud.getAliveTime();
109                                                         // System.out.println("the previous node is still alive");
110                                                 } else {
111                                                         dead = true;
112                                                         //changed = true;
113                                                         host.getStub().suicide("Not responding");
114                                                         System.out.println("\n\nNot responding node "
115                                                                         + host.getName() + " (" + host.getIP()
116                                                                         + ")      size : "
117                                                                         + Register.Instance().getNbOfTasks());
118
119                                                 }
120                                         } catch (Exception e) {
121                                                 dead = true;
122                                                 //changed = true;
123
124                                                 System.out.println("\n\nDisconnection of "
125                                                                 + host.getName() + " (" + host.getIP()
126                                                                 + ")       size : "
127                                                                 + Register.Instance().getNbOfTasks());
128                                         }
129
130                                         if (dead == true) {
131                                                 JaceSpawnerInterface spawnerStub = Register.Instance()
132                                                                 .getSpawnerStub();
133                                                 if (spawnerStub == null)
134                                                         System.err.println("SpawnerStub is null");
135                                                 try {
136                                                         spawnerStub.signalDeadNode(jaceStub, neighborRank);
137
138                                                         this.sleep(10 * beat);
139
140                                                 } catch (RemoteException e1) {
141                                                         System.err.println("Couldn't reach the spawner: "
142                                                                         + e1);
143                                                 }
144                                         }
145                                 }
146                                 // }
147                         } catch (Exception e) {
148                                 System.err.println("Error in testNeighbor: " + e);
149                         }
150                 } catch (Exception e) {
151                         if (Register.Instance().getListeOfTasks() == null)
152                                 System.err.println("Task list is null: " + e);
153                 }
154         }
155 }