3 import java.util.Vector;
5 public class TokenThread extends Thread {
7 public static TokenThread Instance = null;
9 public boolean token = false;
11 public TokenThread() {
14 public boolean getToken() {
18 public static TokenThread Instance() {
21 Instance = new TokenThread();
30 // System.out.println("TokenThread still alive");
32 // System.out.println("distribute!!!!");
35 // System.out.println("waiting !!!! token="+token);
39 } catch (Exception e) {
44 public void setToken() {
46 ScanThreadSuperNode.Instance().setToken(false);
47 System.out.println("the token is put to true in the tokenthread");
49 synchronized (TokenThread.Instance()) {
50 TokenThread.Instance().notify();
52 } catch (Exception e) {
53 System.out.println("unable to notify :" + e);
55 // System.out.println("finished set token");
58 @SuppressWarnings("unchecked")
59 public void distribute() {
60 System.out.println("totalDaemons="
61 + SuperNodeListe.Instance().getTotalDaemons());
62 int nbLocal = Register.Instance().getSize();
64 Vector liste = SuperNodeListe.Instance().getListe();
65 synchronized (SuperNodeListe.Instance()) {
66 synchronized (Register.Instance()) {
68 totalDaemons = SuperNodeListe.Instance().getTotalDaemons();
69 int nbDaemons = totalDaemons / liste.size();
70 // System.out.println("nbDaemons="+nbDaemons+" totalDaemons="+totalDaemons+" nbLocal="+nbLocal);
71 if (nbDaemons < nbLocal)
72 for (int i = 0; i < liste.size(); i++)
73 if (((SuperNodeData) liste.get(i)).getNbOfNodes() < nbDaemons)
75 // System.out.println("Snode="+i+" nodes="+((SuperNodeData)liste.get(i)).getNbOfNodes());
76 Vector nodes = Register.Instance()
78 int amountToSend = nbDaemons
79 - ((SuperNodeData) liste.get(i))
81 Vector newVector = new Vector();
82 for (int j = 0; j < amountToSend; j++) {
83 newVector.add(nodes.elementAt(j));
85 ((SuperNodeData) liste.get(i)).getStub()
86 .sendSurplus(newVector);
87 for (int j = 0; j < amountToSend; j++)
89 int index = SuperNodeListe.Instance()
93 ((SuperNodeData) SuperNodeListe.Instance()
94 .getListe().get(index))
95 .setNbOfNodes(Register.Instance()
97 System.out.println("size reg="
98 + Register.Instance().getSize()
100 + ((SuperNodeData) SuperNodeListe
101 .Instance().getListe().get(
104 new ForwardCount().start();
105 } catch (Exception e) {
108 .println("unable to send surplus to "
109 + ((SuperNodeData) liste
117 boolean sendToken = true;
119 // index of local node
120 int index = SuperNodeListe.Instance().existSuperNode(
121 LocalHost.Instance().getIP());
122 // index of next node
124 // System.out.println("index="+index);
125 // System.out.println(SuperNodeListe.Instance().getListe().size());
126 // System.out.println(((SuperNodeData)SuperNodeListe.Instance().getListe().elementAt(0)).getIP());
127 index = (index + 1) % SuperNodeListe.Instance().getListe().size();
128 // System.out.println("index="+index);
130 ((SuperNodeData) liste.elementAt(index)).getStub().setToken();
131 if (index != SuperNodeListe.Instance().existSuperNode(
132 LocalHost.Instance().getIP()))
135 System.out.println("Passing token to " + index);
136 } catch (Exception e) {
138 System.out.println("Unable to send Token to "
139 + ((SuperNodeData) liste.elementAt(index)).getIP()
142 * SuperNodeListe.Instance().removeSuperNode(((SuperNodeData)
143 * liste.elementAt(index)));
146 * i=0;i<SuperNodeListe.Instance().getListe().size();i++)
148 * ((SuperNodeData)SuperNodeListe.Instance().getListe().
150 * (i)).getStub.removeSuperNode(((SuperNodeData)liste
151 * .elementAt(index)));
153 * }catch(Exception e2){System.out.println(
154 * "diffuse the message concerning a dead superNode: "+e2);
155 * } if(index==SuperNodeListe.Instance().getListe().size())
157 * HeartBeatThread.Instance().setServer(((SuperNodeData
158 * )liste.elementAt(index)).getStub()); try{
161 Thread.sleep(HeartBeatSNode.Instance().getHeartTime());
162 } catch (Exception e2) {
163 System.out.println("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_");