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
49 @SuppressWarnings("unchecked")
50 public void staticInitialization() {
52 String home = System.getProperty("user.home");
53 String fichier = home + "/supernode.conf";
55 // lecture du fichier texte
57 InputStream ips = new FileInputStream(fichier);
58 InputStreamReader ipsr = new InputStreamReader(ips);
59 BufferedReader br = new BufferedReader(ipsr);
61 while ((adr = br.readLine()) != null) {
62 liste.add(new SuperNodeData(adr, 1098));
66 } catch (Exception e) {
67 System.out.println(e.toString());
71 // LocalHost.Instance().resolve("azur-38.sophia.grid5000.fr");
73 // LocalHost.Instance().resolve("cluster1.iut-bm.univ-fcomte.fr");
75 // LocalHost.Instance().resolve("smiths.iut-bm.univ-fcomte.fr");
76 // String adr1="193.52.61.142";
77 // String adr2="172.20.96.21";
79 // String adr3="193.52.61.140";
80 // System.out.println("adr3 = " + adr3);
81 // liste.add(new SuperNodeData(adr1, 1098));
82 // liste.add(new SuperNodeData(adr2, 1098));
83 // liste.add(new SuperNodeData(adr3, 1098));
86 public void fileInitialization(String fileName) {
87 // TODO modif JaceParser pr lire
88 // donnees ds un fichier de conf
91 @SuppressWarnings("unchecked")
92 public Vector getListe() {
96 public int getSize() {
100 // Randomly returns a super Node of the liste
101 public SuperNodeData getASuperNodeData() {
102 int index = r.nextInt() % liste.size();
106 return (SuperNodeData) liste.elementAt(index);
109 public SuperNodeData getSuperNodeData(int i) {
110 return (SuperNodeData) liste.elementAt(i);
113 // if returns 0, there are no nodes at all
114 public SuperNodeData getBestSuperNodeData() {
116 SuperNodeData d = null;
117 SuperNodeData return_d = null;
119 for (int i = 0; i < liste.size(); i++) {
120 d = (SuperNodeData) liste.elementAt(i);
121 if (d.getNbOfNodes() > max) {
122 max = d.getNbOfNodes();
123 return_d = (SuperNodeData) liste.elementAt(i);
129 public void addStub(int index, JaceSuperNodeInterface stub) {
130 ((SuperNodeData) liste.elementAt(index)).setStub(stub);
133 @SuppressWarnings("unchecked")
134 public void addStubOf(String snode, int port, JaceSuperNodeInterface stub) {
136 is = existSuperNode(snode);
138 ((SuperNodeData) liste.elementAt(is)).setStub(stub);
140 System.out.println("this superNode does not exist : " + snode);
141 System.out.println("add the new SuperNode");
142 SuperNodeData data = new SuperNodeData(snode, port);
144 data.setNbOfNodes(0);
150 public int getTotalDaemons() {
153 for (int i = 0; i < liste.size(); i++) {
154 s = s + ((SuperNodeData) liste.get(i)).getNbOfNodes();
155 System.out.println("s=" + s + " " + Register.Instance().getSize());
160 public synchronized JaceSuperNodeInterface getStubOf(String superNode) {
162 if (liste.isEmpty()) {
165 if (superNode == null) {
167 .println("In Register.getNode : host == null !!!!!!!!!!!!!!!");
169 is = existSuperNode(superNode);
171 return ((SuperNodeData) liste.get(is)).getStub();
173 System.out.println("this superNode does not exist");
179 public synchronized int existSuperNode(String snode) {
182 .println("!!!!!!!!!!!!!!!!!!!!!!!!! hostIP in existNode = "
187 while ((existe == -1) && (index < liste.size())) {
188 if (snode.equals(((SuperNodeData) liste.get(index)).getIP())) {
196 public void forwardCountNode() {
197 SuperNodeData d = null;
198 JaceSuperNodeInterface remoteStub = null;
200 for (int i = 0; i < liste.size(); i++) {
201 d = (SuperNodeData) liste.elementAt(i);
202 if (!d.getIP().equals(LocalHost.Instance().getIP())) {
203 // if not me, I inform the other super nodes
204 remoteStub = d.getStub();
206 remoteStub.updateCountNode(LocalHost.Instance().getIP(),
207 Register.Instance().getSize());
208 // System.out.println("envoye : " +
209 // Register.Instance().getSize() + " a " + d.getIP());
210 } catch (Exception e) {
211 // System.out.println(d.getIP() + " is probably dead");
219 // compare with stub rather than IP adresses
220 public void modifCountNode(String IP, int nb) {
222 is = existSuperNode(IP);
224 SuperNodeData d = (SuperNodeData) liste.elementAt(is);
227 System.out.println("ce superNode existe pas ds ma liste : " + IP);
231 @SuppressWarnings("unchecked")
232 public void locateSuperNodes(JaceSuperNodeInterface stub) {
236 boolean connected = false;
237 boolean inStaticList = false;
238 while (i < liste.size() && !connected) {
239 remoteIP = LocalHost.Instance().resolve(
240 ((SuperNodeData) liste.elementAt(i)).getIP());
241 remotePort = ((SuperNodeData) liste.elementAt(i)).getPort();
243 // If I'm not this superNode, I locate the others
244 if (!remoteIP.equals(LocalHost.Instance().getIP())) {
245 JaceSuperNodeInterface remoteStub;
248 // get the stub of remote SuperNodes
249 remoteStub = (JaceSuperNodeInterface) Naming
250 .lookup("rmi://" + remoteIP + ":" + remotePort
252 System.out.println("trying to contact " + remoteIP);
253 // add this stub in the local SuperNodeListe
254 // SuperNodeListe.Instance().addStub(i, remoteStub);
256 // Send my stub to this corresponding remote SuperNode
257 liste = (Vector<?>) remoteStub.sendStub(LocalHost.Instance()
258 .getIP(), LocalHost.Instance().getPort(), stub);
259 System.out.println("Recieved List");
261 // tell this remote SuperNode that I have registered no
263 // remoteStub.updateCountNode(LocalHost.Instance().getIP(),
266 // get the nb of Daemons the remote SuperNode has already
268 // nb = remoteStub.getNbOfNodes();
269 // ((SuperNodeData)liste.elementAt(i)).setNbOfNodes(nb);
271 // System.out.println("bien envoye mon stub a " + remoteIP);
272 System.out.println("size:" + liste.size());
274 } catch (Exception e) {
275 System.out.println("SuperNode " + remoteIP
276 + " is down or not already launched: " + e);
285 SuperNodeData data = new SuperNodeData(LocalHost.Instance()
286 .getIP(), LocalHost.Instance().getPort());
288 data.setNbOfNodes(0);
290 TokenThread.Instance().setToken();
293 .println("no superNode alive in the static List, closing application");
299 @SuppressWarnings("unchecked")
300 public void addSuperNode(SuperNodeData d) {
302 String name = d.getIP();
303 is = existSuperNode(name);
305 liste.setElementAt(d, is);
311 public void removeSuperNode(SuperNodeData d) {
312 String name = d.getIP();
313 int i = existSuperNode(name);
315 // System.out.println("je remove le noeud de rang " + i);
318 System.out.println("super node existe pas");
322 public void viewAll() {
323 System.out.println("\nConfig of the superNodes :");
325 for (int i = 0; i < liste.size(); i++) {
326 SuperNodeData d = (SuperNodeData) liste.elementAt(i);
327 if (d.getIP().equals(LocalHost.Instance().getIP())) {
328 msg = "me : " + Register.Instance().getSize();
330 System.out.println(d.getIP() + " : "
331 + ((SuperNodeData) liste.elementAt(i)).getNbOfNodes());
334 System.out.println(msg);
335 // Register.Instance().viewAll();
336 System.out.print("\n\n");
340 /****************************************/
341 /*********** Sébastien Miquée ***********/
342 /****************************************/
344 public void addGNode( GNode _g )
349 SuperNodeData d = null ;
350 JaceSuperNodeInterface remoteStub = null ;
352 for( int i = 0 ; i < liste.size() ; i++ )
354 d = (SuperNodeData) liste.elementAt( i ) ;
355 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
357 // if not me, I inform the other super nodes
358 remoteStub = d.getStub() ;
360 remoteStub.addGNode( _g ) ;
361 } catch( Exception e ) {
362 System.err.println( "Unable to add new GNode at SuperNode " + d.getIP() ) ;
369 public void removeGNode( GNode _g )
374 SuperNodeData d = null ;
375 JaceSuperNodeInterface remoteStub = null ;
377 for( int i = 0 ; i < liste.size() ; i++ )
379 d = (SuperNodeData) liste.elementAt( i ) ;
380 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
382 // if not me, I inform the other super nodes
383 remoteStub = d.getStub() ;
385 remoteStub.removeGNode( _g ) ;
386 } catch( Exception e ) {
387 System.err.println( "Unable to remove GNode at SuperNode " + d.getIP() ) ;
394 public void setMapping( Algo al )
398 SuperNodeData d = null ;
399 JaceSuperNodeInterface remoteStub = null ;
401 for( int i = 0 ; i < liste.size() ; i++ )
403 d = (SuperNodeData) liste.elementAt( i ) ;
404 if( ! d.getIP().equals(LocalHost.Instance().getIP() ) )
406 // if not me, I inform the other super nodes
407 remoteStub = d.getStub() ;
409 remoteStub.setMapping( al ) ;
410 } catch( Exception e ) {
411 System.err.println( "Unable to set mapping data at SuperNode " + d.getIP() ) ;