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.ArrayList;
9 import java.util.Random;
11 import and.Mapping.Algo;
12 import and.Mapping.GNode;
14 public class SuperNodeListe implements Cloneable {
17 public static SuperNodeListe Instance;
19 private ArrayList<Object> liste = null;
22 public SuperNodeListe() {
23 liste = new ArrayList<Object>() ;
27 public synchronized static SuperNodeListe Instance() {
28 if (Instance == null) {
29 Instance = new SuperNodeListe();
34 @SuppressWarnings("unchecked")
35 public synchronized SuperNodeListe clone() {
36 SuperNodeListe d = new SuperNodeListe();
37 // d.r = (Random) r.clone();
38 d.liste = (ArrayList<Object>) liste.clone() ;
44 // TODO change to have non-static list
45 // using a config file
46 // in order to discover new superNodes dynamicaly
48 /* Sébastien Miquée -- ajout de la lecture du fichier de config **/
49 public void staticInitialization() {
51 String home = System.getProperty("user.home");
52 String fichier = home + "/supernode.conf";
54 // lecture du fichier texte
56 InputStream ips = new FileInputStream(fichier);
57 InputStreamReader ipsr = new InputStreamReader(ips);
58 BufferedReader br = new BufferedReader(ipsr);
60 while ((adr = br.readLine()) != null) {
61 liste.add(new SuperNodeData(adr, 1098));
65 } catch (Exception e) {
66 System.err.println(e.toString());
70 // LocalHost.Instance().resolve("azur-38.sophia.grid5000.fr");
72 // LocalHost.Instance().resolve("cluster1.iut-bm.univ-fcomte.fr");
74 // LocalHost.Instance().resolve("smiths.iut-bm.univ-fcomte.fr");
75 // String adr1="193.52.61.142";
76 // String adr2="172.20.96.21";
78 // String adr3="193.52.61.140";
79 // System.out.println("adr3 = " + adr3);
80 // liste.add(new SuperNodeData(adr1, 1098));
81 // liste.add(new SuperNodeData(adr2, 1098));
82 // liste.add(new SuperNodeData(adr3, 1098));
85 public void fileInitialization(String fileName) {
86 // TODO modif JaceParser pr lire
87 // donnees ds un fichier de conf
91 public ArrayList<Object> getListe() {
95 public int getSize() {
99 // Randomly returns a super Node of the liste
100 public SuperNodeData getASuperNodeData() {
101 int index = r.nextInt() % liste.size();
105 return (SuperNodeData) liste.get(index);
108 public SuperNodeData getSuperNodeData(int i) {
109 return (SuperNodeData) liste.get(i);
112 // if returns 0, there are no nodes at all
113 public SuperNodeData getBestSuperNodeData() {
115 SuperNodeData d = null;
116 SuperNodeData return_d = null;
118 for (int i = 0; i < liste.size(); i++) {
119 d = (SuperNodeData) liste.get(i);
120 if (d.getNbOfNodes() > max) {
121 max = d.getNbOfNodes();
122 return_d = (SuperNodeData) liste.get(i);
128 public void addStub(int index, JaceSuperNodeInterface stub) {
129 ((SuperNodeData) liste.get(index)).setStub(stub);
132 public void addStubOf(String snode, int port, JaceSuperNodeInterface stub) {
134 is = existSuperNode(snode);
136 ((SuperNodeData) liste.get(is)).setStub(stub);
138 System.err.println("This superNode does not exist : " + snode);
139 System.out.println("Add the new SuperNode");
140 SuperNodeData data = new SuperNodeData(snode, port);
142 data.setNbOfNodes(0);
148 public int getTotalDaemons() {
151 for (int i = 0; i < liste.size(); i++) {
152 s = s + ((SuperNodeData) liste.get(i)).getNbOfNodes();
153 System.out.println("s=" + s + " " + Register.Instance().getSize());
158 public synchronized JaceSuperNodeInterface getStubOf(String superNode) {
160 if (liste.isEmpty()) {
163 if (superNode == null) {
165 .println("In Register.getNode : host == null !!!!!!!!!!!!!!!");
167 is = existSuperNode(superNode);
169 return ((SuperNodeData) liste.get(is)).getStub();
171 System.err.println("This superNode does not exist");
177 public synchronized int existSuperNode(String snode) {
180 .println("HostIP in existNode = "
185 while ((existe == -1) && (index < liste.size())) {
186 if (snode.equals(((SuperNodeData) liste.get(index)).getIP())) {
194 public void forwardCountNode() {
195 SuperNodeData d = null;
196 JaceSuperNodeInterface remoteStub = null;
198 for (int i = 0; i < liste.size(); i++) {
199 d = (SuperNodeData) liste.get(i);
200 if (!d.getIP().equals(LocalHost.Instance().getIP())) {
201 // if not me, I inform the other super nodes
202 remoteStub = d.getStub();
204 remoteStub.updateCountNode(LocalHost.Instance().getIP(),
205 Register.Instance().getSize());
206 // System.out.println("envoye : " +
207 // Register.Instance().getSize() + " a " + d.getIP());
208 } catch (Exception e) {
209 // System.out.println(d.getIP() + " is probably dead");
217 // compare with stub rather than IP adresses
218 public void modifCountNode(String IP, int nb) {
220 is = existSuperNode(IP);
222 SuperNodeData d = (SuperNodeData) liste.get(is);
225 System.err.println("This SuperNode does not exists in my list: " + IP);
229 public void locateSuperNodes(JaceSuperNodeInterface stub) {
233 boolean connected = false;
234 boolean inStaticList = false;
235 while (i < liste.size() && !connected) {
236 remoteIP = LocalHost.Instance().resolve(
237 ((SuperNodeData) liste.get(i)).getIP());
238 remotePort = ((SuperNodeData) liste.get(i)).getPort();
240 // If I'm not this superNode, I locate the others
241 if (!remoteIP.equals(LocalHost.Instance().getIP())) {
242 JaceSuperNodeInterface remoteStub;
245 // get the stub of remote SuperNodes
246 remoteStub = (JaceSuperNodeInterface) Naming
247 .lookup("rmi://" + remoteIP + ":" + remotePort
249 System.out.println("trying to contact " + remoteIP);
250 // add this stub in the local SuperNodeListe
251 // SuperNodeListe.Instance().addStub(i, remoteStub);
253 // Send my stub to this corresponding remote SuperNode
254 liste = (ArrayList<Object>) remoteStub.sendStub(LocalHost.Instance()
255 .getIP(), LocalHost.Instance().getPort(), stub);
256 System.out.println("Recieved List");
258 // tell this remote SuperNode that I have registered no
260 // remoteStub.updateCountNode(LocalHost.Instance().getIP(),
263 // get the nb of Daemons the remote SuperNode has already
265 // nb = remoteStub.getNbOfNodes();
266 // ((SuperNodeData)liste.get(i)).setNbOfNodes(nb);
268 // System.out.println("bien envoye mon stub a " + remoteIP);
269 System.out.println("size:" + liste.size());
271 } catch (Exception e) {
272 System.err.println("SuperNode " + remoteIP
273 + " is down or not already launched: " + e);
282 SuperNodeData data = new SuperNodeData(LocalHost.Instance()
283 .getIP(), LocalHost.Instance().getPort());
285 data.setNbOfNodes(0);
287 TokenThread.Instance().setToken();
290 .println("No superNode alive in the static List, closing application");
296 public void addSuperNode(SuperNodeData d) {
298 String name = d.getIP();
299 is = existSuperNode(name);
307 public void removeSuperNode(SuperNodeData d) {
308 String name = d.getIP();
309 int i = existSuperNode(name);
311 // System.out.println("je remove le noeud de rang " + i);
314 System.err.println("The SuperNode does not exists!");
318 public void viewAll() {
319 System.out.println("\nConfig of the superNodes :");
321 for (int i = 0; i < liste.size(); i++) {
322 SuperNodeData d = (SuperNodeData) liste.get(i);
323 if (d.getIP().equals(LocalHost.Instance().getIP())) {
324 msg = "me : " + Register.Instance().getSize();
326 System.out.println(d.getIP() + " : "
327 + ((SuperNodeData) liste.get(i)).getNbOfNodes());
330 System.out.println(msg);
331 // Register.Instance().viewAll();
332 System.out.print("\n\n");
336 /****************************************/
337 /*********** Sébastien Miquée ***********/
338 /****************************************/
340 public synchronized void addGNode( GNode _g )
345 SuperNodeData d = null ;
346 JaceSuperNodeInterface remoteStub = null ;
348 for( int i = 0 ; i < liste.size() ; i++ )
350 d = (SuperNodeData) liste.get( i ) ;
351 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
353 // if not me, I inform the other super nodes
354 remoteStub = d.getStub() ;
356 remoteStub.addGNode( _g ) ;
357 } catch( Exception e ) {
358 System.err.println( "Unable to add new GNode on SuperNode " + d.getIP() ) ;
365 public synchronized void removeGNode( GNode _g, int _mode, String _spawnerIP )
370 SuperNodeData d = null ;
371 JaceSuperNodeInterface remoteStub = null ;
373 for( int i = 0 ; i < liste.size() ; i++ )
375 d = (SuperNodeData) liste.get( i ) ;
376 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
378 // if not me, I inform the other super nodes
379 remoteStub = d.getStub() ;
381 remoteStub.delGNodeFromList( (Node)_g.getNode(), _mode, _spawnerIP ) ;
382 } catch( Exception e ) {
383 System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
390 public synchronized void removeGNode( GNode _g, int _mode )
395 SuperNodeData d = null ;
396 JaceSuperNodeInterface remoteStub = null ;
398 for( int i = 0 ; i < liste.size() ; i++ )
400 d = (SuperNodeData) liste.get( i ) ;
401 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
403 // if not me, I inform the other super nodes
404 remoteStub = d.getStub() ;
406 remoteStub.removeMappedGNode( _g, _mode ) ;
407 } catch( Exception e ) {
408 System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
415 public synchronized void setMappedGNode( GNode _g, int _mode )
420 SuperNodeData d = null ;
421 JaceSuperNodeInterface remoteStub = null ;
423 for( int i = 0 ; i < liste.size() ; i++ )
425 d = (SuperNodeData) liste.get( i ) ;
426 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
428 // if not me, I inform the other super nodes
429 remoteStub = d.getStub() ;
431 remoteStub.setMapped( _g, _mode ) ;
432 } catch( Exception e ) {
433 System.err.println( "Unable to set mapped GNode on SuperNode " + d.getIP() ) ;
441 protected boolean workingOnGnodes()
445 SuperNodeData d = null ;
446 JaceSuperNodeInterface remoteStub = null ;
448 for( int i = 0 ; i < liste.size() ; i++ )
450 d = (SuperNodeData) liste.get( i ) ;
451 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
453 // if not me, I inform the other super nodes
454 remoteStub = d.getStub() ;
456 ok = ok && remoteStub.blockForMapping() ;
457 } catch( Exception e ) {
458 System.err.println( "Unable to remove GNode on SuperNode " + d.getIP() ) ;
466 public void setMapping( Algo al )
470 SuperNodeData d = null ;
471 JaceSuperNodeInterface remoteStub = null ;
473 for( int i = 0 ; i < liste.size() ; i++ )
475 d = (SuperNodeData) liste.get( i ) ;
476 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
478 // if not me, I inform the other super nodes
479 remoteStub = d.getStub() ;
481 remoteStub.setMapping( al ) ;
482 } catch( Exception e ) {
483 System.err.println( "Unable to set mapping data on SuperNode " + d.getIP() ) ;
490 public synchronized void propagateReplaceNode( GNode _dead, GNode _remp, String _spawnerIP )
492 if( _dead != null && _remp != null )
494 SuperNodeData d = null ;
495 JaceSuperNodeInterface remoteStub = null ;
497 for( int i = 0 ; i < liste.size() ; i++ )
499 d = (SuperNodeData) liste.get( i ) ;
500 if( ! d.getIP().equals( LocalHost.Instance().getIP() ) )
502 // if not me, I inform the other super nodes
503 remoteStub = d.getStub() ;
505 remoteStub.updateMappedNode( _dead, _remp, _spawnerIP ) ;
506 } catch( Exception e ) {
507 System.err.println( "Unable to propagate replacing node on SuperNode " + d.getIP() ) ;
516 * Send to all SuperNode the deletion of a mapping algorithm.
517 * @param id The mapping algorithm's identifier
519 public void removeAlgo( String _id )
521 SuperNodeData d = null ;
522 JaceSuperNodeInterface remoteStub = null ;
524 for( int i = 0 ; i < liste.size() ; i++ )
526 d = (SuperNodeData) liste.get( i ) ;
527 if( ! d.getIP().equals( LocalHost.Instance().getIP() ) )
529 // if not me, I inform the other super nodes
530 remoteStub = d.getStub() ;
532 remoteStub.removeAlgo( _id, 1 ) ;
533 } catch( Exception e ) {
534 System.err.println( "Unable to propagate the mapping algorithm deletion " + d.getIP() ) ;