3 import java.rmi.RemoteException;
4 import java.util.ArrayList;
7 public class TokenThread extends Thread {
9 public static TokenThread Instance = null;
11 public boolean token = false;
13 public TokenThread() {
16 public boolean getToken() {
20 public static TokenThread Instance() {
23 Instance = new TokenThread();
32 // System.out.println("TokenThread still alive");
34 // System.out.println("distribute!!!!");
37 // System.out.println("waiting !!!! token="+token);
41 } catch (Exception e) {
46 public void setToken() {
48 ScanThreadSuperNode.Instance().setToken(false);
49 System.out.println("The token is put to true in the tokenthread");
51 synchronized (TokenThread.Instance()) {
52 TokenThread.Instance().notify();
54 } catch (Exception e) {
55 System.err.println("Unable to notify :" + e);
57 // System.out.println("finished set token");
61 public void distribute() {
62 System.out.println("totalDaemons="
63 + SuperNodeListe.Instance().getTotalDaemons());
64 int nbLocal = Register.Instance().getSize();
66 ArrayList<Object> liste = SuperNodeListe.Instance().getListe();
68 /******* Sébastien Miquée ********/
69 ArrayList<GNode> gnodes = null ;
71 gnodes = SuperNodeListe.Instance().getStubOf(LocalHost.Instance().getIP()).getGNodes() ;
72 } catch (RemoteException e1) {
73 System.err.println( "Unable to retrieve local gnodes list !" ) ;
77 synchronized (SuperNodeListe.Instance()) {
78 synchronized (Register.Instance()) {
80 totalDaemons = SuperNodeListe.Instance().getTotalDaemons();
81 int nbDaemons = totalDaemons / liste.size();
82 // System.out.println("nbDaemons="+nbDaemons+" totalDaemons="+totalDaemons+" nbLocal="+nbLocal);
83 if (nbDaemons < nbLocal)
84 for (int i = 0; i < liste.size(); i++)
85 if (((SuperNodeData) liste.get(i)).getNbOfNodes() < nbDaemons)
87 // System.out.println("Snode="+i+" nodes="+((SuperNodeData)liste.get(i)).getNbOfNodes());
88 ArrayList<Node> nodes = Register.Instance()
90 int amountToSend = nbDaemons
91 - ((SuperNodeData) liste.get(i))
93 ArrayList<Node> newVector = new ArrayList<Node>() ;
94 for (int j = 0; j < amountToSend; j++) {
95 newVector.add(nodes.get(j));
97 ((SuperNodeData) liste.get(i)).getStub()
98 .sendSurplus(newVector, gnodes);
99 for (int j = 0; j < amountToSend; j++)
101 int index = SuperNodeListe.Instance()
105 ((SuperNodeData) SuperNodeListe.Instance()
106 .getListe().get(index))
107 .setNbOfNodes(Register.Instance()
109 System.out.println("size reg="
110 + Register.Instance().getSize()
112 + ((SuperNodeData) SuperNodeListe
113 .Instance().getListe().get(
116 new ForwardCount().start();
117 } catch (Exception e) {
120 .println("Unable to send surplus to "
121 + ((SuperNodeData) liste
129 boolean sendToken = true;
131 // index of local node
132 int index = SuperNodeListe.Instance().existSuperNode(
133 LocalHost.Instance().getIP());
134 // index of next node
136 // System.out.println("index="+index);
137 // System.out.println(SuperNodeListe.Instance().getListe().size());
138 // System.out.println(((SuperNodeData)SuperNodeListe.Instance().getListe().elementAt(0)).getIP());
139 index = (index + 1) % SuperNodeListe.Instance().getListe().size();
140 // System.out.println("index="+index);
142 ((SuperNodeData) liste.get(index)).getStub().setToken();
143 if (index != SuperNodeListe.Instance().existSuperNode(
144 LocalHost.Instance().getIP()))
147 System.out.println("Passing token to " + index);
148 } catch (Exception e) {
150 System.err.println("Unable to send Token to "
151 + ((SuperNodeData) liste.get(index)).getIP()
154 * SuperNodeListe.Instance().removeSuperNode(((SuperNodeData)
155 * liste.elementAt(index)));
158 * i=0;i<SuperNodeListe.Instance().getListe().size();i++)
160 * ((SuperNodeData)SuperNodeListe.Instance().getListe().
162 * (i)).getStub.removeSuperNode(((SuperNodeData)liste
163 * .elementAt(index)));
165 * }catch(Exception e2){System.out.println(
166 * "diffuse the message concerning a dead superNode: "+e2);
167 * } if(index==SuperNodeListe.Instance().getListe().size())
169 * HeartBeatThread.Instance().setServer(((SuperNodeData
170 * )liste.elementAt(index)).getStub()); try{
173 Thread.sleep(HeartBeatSNode.Instance().getHeartTime());
174 } catch (Exception e2) {
175 System.out.println("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_");