3 import java.rmi.RemoteException;
4 import java.util.ArrayList;
5 import java.util.Vector;
8 public class TokenThread extends Thread {
10 public static TokenThread Instance = null;
12 public boolean token = false;
14 public TokenThread() {
17 public boolean getToken() {
21 public static TokenThread Instance() {
24 Instance = new TokenThread();
33 // System.out.println("TokenThread still alive");
35 // System.out.println("distribute!!!!");
38 // System.out.println("waiting !!!! token="+token);
42 } catch (Exception e) {
47 public void setToken() {
49 ScanThreadSuperNode.Instance().setToken(false);
50 System.out.println("The token is put to true in the tokenthread");
52 synchronized (TokenThread.Instance()) {
53 TokenThread.Instance().notify();
55 } catch (Exception e) {
56 System.err.println("Unable to notify :" + e);
58 // System.out.println("finished set token");
61 @SuppressWarnings("unchecked")
62 public void distribute() {
63 System.out.println("totalDaemons="
64 + SuperNodeListe.Instance().getTotalDaemons());
65 int nbLocal = Register.Instance().getSize();
67 Vector liste = SuperNodeListe.Instance().getListe();
69 /******* Sébastien Miquée ********/
70 ArrayList<GNode> gnodes = null ;
72 gnodes = SuperNodeListe.Instance().getStubOf(LocalHost.Instance().getIP()).getGNodes() ;
73 } catch (RemoteException e1) {
74 System.err.println( "Unable to retrieve local gnodes list !" ) ;
78 synchronized (SuperNodeListe.Instance()) {
79 synchronized (Register.Instance()) {
81 totalDaemons = SuperNodeListe.Instance().getTotalDaemons();
82 int nbDaemons = totalDaemons / liste.size();
83 // System.out.println("nbDaemons="+nbDaemons+" totalDaemons="+totalDaemons+" nbLocal="+nbLocal);
84 if (nbDaemons < nbLocal)
85 for (int i = 0; i < liste.size(); i++)
86 if (((SuperNodeData) liste.get(i)).getNbOfNodes() < nbDaemons)
88 // System.out.println("Snode="+i+" nodes="+((SuperNodeData)liste.get(i)).getNbOfNodes());
89 Vector nodes = Register.Instance()
91 int amountToSend = nbDaemons
92 - ((SuperNodeData) liste.get(i))
94 Vector newVector = new Vector();
95 for (int j = 0; j < amountToSend; j++) {
96 newVector.add(nodes.elementAt(j));
98 ((SuperNodeData) liste.get(i)).getStub()
99 .sendSurplus(newVector, gnodes);
100 for (int j = 0; j < amountToSend; j++)
102 int index = SuperNodeListe.Instance()
106 ((SuperNodeData) SuperNodeListe.Instance()
107 .getListe().get(index))
108 .setNbOfNodes(Register.Instance()
110 System.out.println("size reg="
111 + Register.Instance().getSize()
113 + ((SuperNodeData) SuperNodeListe
114 .Instance().getListe().get(
117 new ForwardCount().start();
118 } catch (Exception e) {
121 .println("Unable to send surplus to "
122 + ((SuperNodeData) liste
130 boolean sendToken = true;
132 // index of local node
133 int index = SuperNodeListe.Instance().existSuperNode(
134 LocalHost.Instance().getIP());
135 // index of next node
137 // System.out.println("index="+index);
138 // System.out.println(SuperNodeListe.Instance().getListe().size());
139 // System.out.println(((SuperNodeData)SuperNodeListe.Instance().getListe().elementAt(0)).getIP());
140 index = (index + 1) % SuperNodeListe.Instance().getListe().size();
141 // System.out.println("index="+index);
143 ((SuperNodeData) liste.elementAt(index)).getStub().setToken();
144 if (index != SuperNodeListe.Instance().existSuperNode(
145 LocalHost.Instance().getIP()))
148 System.out.println("Passing token to " + index);
149 } catch (Exception e) {
151 System.err.println("Unable to send Token to "
152 + ((SuperNodeData) liste.elementAt(index)).getIP()
155 * SuperNodeListe.Instance().removeSuperNode(((SuperNodeData)
156 * liste.elementAt(index)));
159 * i=0;i<SuperNodeListe.Instance().getListe().size();i++)
161 * ((SuperNodeData)SuperNodeListe.Instance().getListe().
163 * (i)).getStub.removeSuperNode(((SuperNodeData)liste
164 * .elementAt(index)));
166 * }catch(Exception e2){System.out.println(
167 * "diffuse the message concerning a dead superNode: "+e2);
168 * } if(index==SuperNodeListe.Instance().getListe().size())
170 * HeartBeatThread.Instance().setServer(((SuperNodeData
171 * )liste.elementAt(index)).getStub()); try{
174 Thread.sleep(HeartBeatSNode.Instance().getHeartTime());
175 } catch (Exception e2) {
176 System.out.println("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_");