Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Creating the JaceP2P repository.
[jaceP2P.git] / src / jaceP2P / ScanThreadSuperNode.java
1 package jaceP2P;
2
3
4 public class ScanThreadSuperNode extends Thread {
5         public static ScanThreadSuperNode Instance = null;
6         // public boolean scanning=false;
7         public int beat;
8         public int timeBeforeKill;
9         public int myRank;
10         public int neighborRank;
11         private long aliveTime = System.currentTimeMillis();
12         private boolean token = false;
13
14         // public boolean running=false;
15         private ScanThreadSuperNode() {
16                 beat = HeartBeatSNode.Instance().getHeartTime();
17                 timeBeforeKill = beat * 4;
18                 System.out.println("timebfk=" + timeBeforeKill);
19                 // running=true;
20
21         }
22
23         public static ScanThreadSuperNode Instance() {
24                 if (Instance == null) {
25                         System.out.println("creating new ScanThreadSuperNode ");
26                         Instance = new ScanThreadSuperNode();
27                 }
28                 return Instance;
29         }
30
31         public synchronized void setAliveTime() {
32                 aliveTime = System.currentTimeMillis();
33                 // System.out.println("alive Time="+aliveTime);
34         }
35
36         public synchronized void setToken(boolean bool) {
37                 token = bool;
38         }
39
40         // public void setScanning(boolean bool){
41         // System.out.println("in setScanning !!!!!");
42         // scanning=bool;
43         // }
44         // public void kill(){
45         // Instance=null;
46         // running=false;
47         // }
48         @SuppressWarnings("static-access")
49         public void run() {
50                 System.out.println("start ScanThread for SuperNode.......");
51                 while (true) {
52                         // System.out.println("ScanThread alive.......");
53                         /*
54                          * if(scanning==false) try{ this.wait(); }catch(Exception e){} else{
55                          */
56                         // System.out.println("test Neighbor: "+neighborRank);
57                         testNeighbor();
58                         try {
59                                 this.sleep(beat);
60                         } catch (Exception e) {
61                         }
62
63                 }
64         }
65
66         public void testNeighbor() {
67                 //Node host;
68                 //Node tmpNode;
69                 //long workerTime;
70                 long currentTime;
71
72                 //int restempo;
73                 //int nb = 0;
74                 //int nbC = 0;
75                 //boolean changed = false;
76                 try {
77
78                         currentTime = System.currentTimeMillis();
79                         if (currentTime - aliveTime > timeBeforeKill) {
80                                 System.out.println("neighbor maybe Dead, "
81                                                 + (currentTime - aliveTime) + " " + currentTime + " "
82                                                 + aliveTime + " result "
83                                                 + (currentTime - aliveTime > timeBeforeKill)
84                                                 + " timebk=" + timeBeforeKill);
85                                 int index = SuperNodeListe.Instance().existSuperNode(
86                                                 LocalHost.Instance().getIP());
87                                 if (index > 0)
88                                         index--;
89                                 else
90                                         index = SuperNodeListe.Instance().getListe().size() - 1;
91                                 SuperNodeData data;
92                                 synchronized (SuperNodeListe.Instance()) {
93                                         System.out.println("removing "
94                                                         + ((SuperNodeData) SuperNodeListe.Instance()
95                                                                         .getListe().elementAt(index)).getIP());
96                                         data = (SuperNodeData) SuperNodeListe.Instance().getListe()
97                                                         .elementAt(index);
98                                         SuperNodeListe.Instance().removeSuperNode(
99                                                         ((SuperNodeData) SuperNodeListe.Instance()
100                                                                         .getListe().elementAt(index)));
101
102                                 }
103                                 // diffusing the message concerning a dead superNode
104                                 for (int i = 0; i < SuperNodeListe.Instance().getListe().size(); i++)
105                                         try {
106                                                 if (!((SuperNodeData) SuperNodeListe.Instance()
107                                                                 .getListe().elementAt(i)).getIP().equals(
108                                                                 LocalHost.Instance().getIP()))
109                                                         ((SuperNodeData) SuperNodeListe.Instance()
110                                                                         .getListe().elementAt(i)).getStub()
111                                                                         .removeSuperNode(data);
112                                         } catch (Exception e2) {
113                                                 System.out
114                                                                 .println("error diffusing the message concerning a dead superNode: "
115                                                                                 + e2);
116                                         }
117                                 if (token == true) {
118                                         // synchronized(TokenThread.Instance()){
119                                         TokenThread.Instance().setToken();
120                                         // TokenThread.Instance().notify();
121                                         // }
122                                 }
123                                 index = SuperNodeListe.Instance().existSuperNode(
124                                                 LocalHost.Instance().getIP());
125                                 if (index > 0)
126                                         index--;
127                                 else
128                                         index = SuperNodeListe.Instance().getListe().size() - 1;
129                                 ((SuperNodeData) SuperNodeListe.Instance().getListe()
130                                                 .elementAt(index)).getStub().updateHeart(
131                                                 LocalHost.Instance().getSuperNodeStub());
132
133                         }
134                 } catch (Exception e) {
135                         System.out.println("error in testNeighbor: " + e);
136                 }
137         }
138
139 }