3 import java.io.BufferedReader;
4 import java.io.FileInputStream;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.rmi.Naming;
8 import java.util.Random;
9 import java.util.Vector;
11 import and.Mapping.Algo;
12 import and.Mapping.GNode;
14 public class SuperNodeListe implements Cloneable {
17 public static SuperNodeListe Instance;
19 @SuppressWarnings("unchecked")
20 private Vector liste = null;
23 @SuppressWarnings("unchecked")
24 public SuperNodeListe() {
29 public synchronized static SuperNodeListe Instance() {
30 if (Instance == null) {
31 Instance = new SuperNodeListe();
36 @SuppressWarnings("unchecked")
37 public synchronized SuperNodeListe clone() {
38 SuperNodeListe d = new SuperNodeListe();
39 // d.r = (Random) r.clone();
40 d.liste = (Vector) liste.clone();
46 // TODO change to have non-static list
47 // using a config file
48 // in order to discover new superNodes dynamicaly
50 /* Sébastien Miquée -- ajout de la lecture du fichier de config **/
52 @SuppressWarnings("unchecked")
53 public void staticInitialization() {
55 String home = System.getProperty("user.home");
56 String fichier = home + "/supernode.conf";
58 // lecture du fichier texte
60 InputStream ips = new FileInputStream(fichier);
61 InputStreamReader ipsr = new InputStreamReader(ips);
62 BufferedReader br = new BufferedReader(ipsr);
64 while ((adr = br.readLine()) != null) {
65 liste.add(new SuperNodeData(adr, 1098));
69 } catch (Exception e) {
70 System.err.println(e.toString());
74 // LocalHost.Instance().resolve("azur-38.sophia.grid5000.fr");
76 // LocalHost.Instance().resolve("cluster1.iut-bm.univ-fcomte.fr");
78 // LocalHost.Instance().resolve("smiths.iut-bm.univ-fcomte.fr");
79 // String adr1="193.52.61.142";
80 // String adr2="172.20.96.21";
82 // String adr3="193.52.61.140";
83 // System.out.println("adr3 = " + adr3);
84 // liste.add(new SuperNodeData(adr1, 1098));
85 // liste.add(new SuperNodeData(adr2, 1098));
86 // liste.add(new SuperNodeData(adr3, 1098));
89 public void fileInitialization(String fileName) {
90 // TODO modif JaceParser pr lire
91 // donnees ds un fichier de conf
94 @SuppressWarnings("unchecked")
95 public Vector getListe() {
99 public int getSize() {
103 // Randomly returns a super Node of the liste
104 public SuperNodeData getASuperNodeData() {
105 int index = r.nextInt() % liste.size();
109 return (SuperNodeData) liste.elementAt(index);
112 public SuperNodeData getSuperNodeData(int i) {
113 return (SuperNodeData) liste.elementAt(i);
116 // if returns 0, there are no nodes at all
117 public SuperNodeData getBestSuperNodeData() {
119 SuperNodeData d = null;
120 SuperNodeData return_d = null;
122 for (int i = 0; i < liste.size(); i++) {
123 d = (SuperNodeData) liste.elementAt(i);
124 if (d.getNbOfNodes() > max) {
125 max = d.getNbOfNodes();
126 return_d = (SuperNodeData) liste.elementAt(i);
132 public void addStub(int index, JaceSuperNodeInterface stub) {
133 ((SuperNodeData) liste.elementAt(index)).setStub(stub);
136 @SuppressWarnings("unchecked")
137 public void addStubOf(String snode, int port, JaceSuperNodeInterface stub) {
139 is = existSuperNode(snode);
141 ((SuperNodeData) liste.elementAt(is)).setStub(stub);
143 System.err.println("This superNode does not exist : " + snode);
144 System.out.println("Add the new SuperNode");
145 SuperNodeData data = new SuperNodeData(snode, port);
147 data.setNbOfNodes(0);
153 public int getTotalDaemons() {
156 for (int i = 0; i < liste.size(); i++) {
157 s = s + ((SuperNodeData) liste.get(i)).getNbOfNodes();
158 System.out.println("s=" + s + " " + Register.Instance().getSize());
163 public synchronized JaceSuperNodeInterface getStubOf(String superNode) {
165 if (liste.isEmpty()) {
168 if (superNode == null) {
170 .println("In Register.getNode : host == null !!!!!!!!!!!!!!!");
172 is = existSuperNode(superNode);
174 return ((SuperNodeData) liste.get(is)).getStub();
176 System.err.println("This superNode does not exist");
182 public synchronized int existSuperNode(String snode) {
185 .println("HostIP in existNode = "
190 while ((existe == -1) && (index < liste.size())) {
191 if (snode.equals(((SuperNodeData) liste.get(index)).getIP())) {
199 public void forwardCountNode() {
200 SuperNodeData d = null;
201 JaceSuperNodeInterface remoteStub = null;
203 for (int i = 0; i < liste.size(); i++) {
204 d = (SuperNodeData) liste.elementAt(i);
205 if (!d.getIP().equals(LocalHost.Instance().getIP())) {
206 // if not me, I inform the other super nodes
207 remoteStub = d.getStub();
209 remoteStub.updateCountNode(LocalHost.Instance().getIP(),
210 Register.Instance().getSize());
211 // System.out.println("envoye : " +
212 // Register.Instance().getSize() + " a " + d.getIP());
213 } catch (Exception e) {
214 // System.out.println(d.getIP() + " is probably dead");
222 // compare with stub rather than IP adresses
223 public void modifCountNode(String IP, int nb) {
225 is = existSuperNode(IP);
227 SuperNodeData d = (SuperNodeData) liste.elementAt(is);
230 System.err.println("This SuperNode does not exists in my list: " + IP);
234 @SuppressWarnings("unchecked")
235 public void locateSuperNodes(JaceSuperNodeInterface stub) {
239 boolean connected = false;
240 boolean inStaticList = false;
241 while (i < liste.size() && !connected) {
242 remoteIP = LocalHost.Instance().resolve(
243 ((SuperNodeData) liste.elementAt(i)).getIP());
244 remotePort = ((SuperNodeData) liste.elementAt(i)).getPort();
246 // If I'm not this superNode, I locate the others
247 if (!remoteIP.equals(LocalHost.Instance().getIP())) {
248 JaceSuperNodeInterface remoteStub;
251 // get the stub of remote SuperNodes
252 remoteStub = (JaceSuperNodeInterface) Naming
253 .lookup("rmi://" + remoteIP + ":" + remotePort
255 System.out.println("trying to contact " + remoteIP);
256 // add this stub in the local SuperNodeListe
257 // SuperNodeListe.Instance().addStub(i, remoteStub);
259 // Send my stub to this corresponding remote SuperNode
260 liste = (Vector<?>) remoteStub.sendStub(LocalHost.Instance()
261 .getIP(), LocalHost.Instance().getPort(), stub);
262 System.out.println("Recieved List");
264 // tell this remote SuperNode that I have registered no
266 // remoteStub.updateCountNode(LocalHost.Instance().getIP(),
269 // get the nb of Daemons the remote SuperNode has already
271 // nb = remoteStub.getNbOfNodes();
272 // ((SuperNodeData)liste.elementAt(i)).setNbOfNodes(nb);
274 // System.out.println("bien envoye mon stub a " + remoteIP);
275 System.out.println("size:" + liste.size());
277 } catch (Exception e) {
278 System.err.println("SuperNode " + remoteIP
279 + " is down or not already launched: " + e);
288 SuperNodeData data = new SuperNodeData(LocalHost.Instance()
289 .getIP(), LocalHost.Instance().getPort());
291 data.setNbOfNodes(0);
293 TokenThread.Instance().setToken();
296 .println("No superNode alive in the static List, closing application");
302 @SuppressWarnings("unchecked")
303 public void addSuperNode(SuperNodeData d) {
305 String name = d.getIP();
306 is = existSuperNode(name);
308 liste.setElementAt(d, is);
314 public void removeSuperNode(SuperNodeData d) {
315 String name = d.getIP();
316 int i = existSuperNode(name);
318 // System.out.println("je remove le noeud de rang " + i);
321 System.err.println("The SuperNode does not exists!");
325 public void viewAll() {
326 System.out.println("\nConfig of the superNodes :");
328 for (int i = 0; i < liste.size(); i++) {
329 SuperNodeData d = (SuperNodeData) liste.elementAt(i);
330 if (d.getIP().equals(LocalHost.Instance().getIP())) {
331 msg = "me : " + Register.Instance().getSize();
333 System.out.println(d.getIP() + " : "
334 + ((SuperNodeData) liste.elementAt(i)).getNbOfNodes());
337 System.out.println(msg);
338 // Register.Instance().viewAll();
339 System.out.print("\n\n");
343 /****************************************/
344 /*********** Sébastien Miquée ***********/
345 /****************************************/
347 public void addGNode( GNode _g )
352 SuperNodeData d = null ;
353 JaceSuperNodeInterface remoteStub = null ;
355 for( int i = 0 ; i < liste.size() ; i++ )
357 d = (SuperNodeData) liste.elementAt( i ) ;
358 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
360 // if not me, I inform the other super nodes
361 remoteStub = d.getStub() ;
363 remoteStub.addGNode( _g ) ;
364 } catch( Exception e ) {
365 System.err.println( "Unable to add new GNode on SuperNode " + d.getIP() ) ;
372 public void removeGNode( GNode _g, int _mode )
377 SuperNodeData d = null ;
378 JaceSuperNodeInterface remoteStub = null ;
380 for( int i = 0 ; i < liste.size() ; i++ )
382 d = (SuperNodeData) liste.elementAt( i ) ;
383 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
385 // if not me, I inform the other super nodes
386 remoteStub = d.getStub() ;
388 remoteStub.delGNodeFromList( (Node)_g.getNode(), _mode ) ;
389 } catch( Exception e ) {
390 System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
398 protected boolean workingOnGnodes()
402 SuperNodeData d = null ;
403 JaceSuperNodeInterface remoteStub = null ;
405 for( int i = 0 ; i < liste.size() ; i++ )
407 d = (SuperNodeData) liste.elementAt( i ) ;
408 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
410 // if not me, I inform the other super nodes
411 remoteStub = d.getStub() ;
413 ok = ok && remoteStub.blockForMapping() ;
414 } catch( Exception e ) {
415 System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
423 public void setMapping( Algo al )
427 SuperNodeData d = null ;
428 JaceSuperNodeInterface remoteStub = null ;
430 for( int i = 0 ; i < liste.size() ; i++ )
432 d = (SuperNodeData) liste.elementAt( i ) ;
433 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
435 // if not me, I inform the other super nodes
436 remoteStub = d.getStub() ;
438 remoteStub.setMapping( al ) ;
439 } catch( Exception e ) {
440 System.err.println( "Unable to set mapping data on SuperNode " + d.getIP() ) ;
447 public void propagateReplaceNode( GNode _dead, GNode _remp )
449 if( _dead != null && _remp != null )
451 SuperNodeData d = null ;
452 JaceSuperNodeInterface remoteStub = null ;
454 for( int i = 0 ; i < liste.size() ; i++ )
456 d = (SuperNodeData) liste.elementAt( i ) ;
457 if( ! d.getIP().equals( LocalHost.Instance().getIP() ) )
459 // if not me, I inform the other super nodes
460 remoteStub = d.getStub() ;
462 remoteStub.updateMappedNode( _dead, _remp ) ;
463 } catch( Exception e ) {
464 System.err.println( "Unable to propagate replacing node on SuperNode " + d.getIP() ) ;