3 import java.rmi.RemoteException;
4 import java.rmi.server.UnicastRemoteObject;
5 import java.util.ArrayList;
6 import java.util.Vector;
8 import and.Mapping.Algo;
9 import and.Mapping.DefaultMapping;
10 import and.Mapping.GNode;
11 import and.Mapping.GTask;
12 import and.Mapping.Graph;
13 import and.Mapping.Grid;
14 import and.Mapping.LSM;
15 import and.Mapping.Mapping;
16 import and.Mapping.QM;
17 import and.Mapping.Simple;
18 import and.Mapping.Utils;
21 public class JaceSuperNodeServer extends UnicastRemoteObject implements
22 JaceSuperNodeInterface {
23 private static final long serialVersionUID = 1L;
27 private ArrayList<GNode> gnodes = null;
28 private Algo al = null ;
29 private int count = 0 ;
30 private boolean inDemand = false, operating = false, authorized = false ;
31 // private boolean daemonListChange ;
35 public JaceSuperNodeServer(int timeBeat) throws RemoteException {
38 gnodes = new ArrayList<GNode>() ;
39 // daemonListChange = true ;
42 public int getSuperNodeBeat() throws RemoteException {
46 public Vector<?> sendStub(String IP, int port, JaceSuperNodeInterface stub)
47 throws RemoteException {
48 SuperNodeListe.Instance().addStubOf(IP, port, stub);
49 System.out.println("Added new superNode (" + IP
50 + ") to list and return List");
51 return SuperNodeListe.Instance().getListe();
54 public void updateCountNode(String IP, int nb) throws RemoteException {
55 SuperNodeListe.Instance().modifCountNode(IP, nb);
56 // System.out.println( "SuperNode " + IP + " has registered " + nb +
58 SuperNodeListe.Instance().viewAll();
61 public void sendSurplus(Vector<?> nodes) throws RemoteException {
62 System.out.println("Recieved " + nodes.size() + " nodes");
64 for (int i = 0; i < nodes.size(); i++) {
65 Register.Instance().getListe().add((Node) nodes.elementAt(i));
67 System.out.println("Adding "
68 + ((Node) nodes.elementAt(i)).getName());
71 ((Node) nodes.elementAt(i)).getStub().updateHeart(
72 LocalHost.Instance().getSuperNodeStub());
73 int index = SuperNodeListe.Instance().existSuperNode(
74 LocalHost.Instance().getIP());
75 ((SuperNodeData) SuperNodeListe.Instance().getListe()
77 .setNbOfNodes(Register.Instance().getSize());
78 new ForwardCount().start();
79 } catch (Exception e) {
80 System.out.println("Error changing Server in SendSurplus : "
86 public void setToken() throws RemoteException {
87 System.out.println("I got Token");
89 TokenThread.Instance().setToken();
92 HeartBeatSNode.Instance().getServer().beating(true);
94 System.out.println("Put token to true");
95 } catch (Exception e) {
97 .println("Unable to heartBeat the next SuperNode with the new Token : "
102 public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
103 System.out.println("I change to ping a superNode");
105 HeartBeatSNode.Instance().setServer(stub);
108 public synchronized void removeSuperNode(SuperNodeData d)
109 throws RemoteException {
110 SuperNodeListe.Instance().removeSuperNode(d);
113 /****************************************************/
114 /****************************************************/
116 // Register a Daemon in the Register.Instance() of the SuperNode
117 public synchronized void workerRegistering(JaceInterface workerStub,
118 String workerIP, String workerName, int port, GNode g)
119 throws RemoteException {
120 System.out.println("CONNEXION of " + workerName);
123 Node noeud = new Node(workerStub);
124 noeud.setName(workerName);
125 noeud.setIP(workerIP);
126 noeud.setAliveFlag(true);
127 noeud.setAliveTime();
129 noeud.setAppliName(null);
130 noeud.setNbOfBeats(0);
131 noeud.setId( count ) ;
132 noeud.setId( Long.parseLong( workerIP.replace( ".", "" ) ) ) ;
135 g.setId( noeud.getId() ) ;
141 // daemonListChange = true ;
143 // Insert the node in the Register.Instance() of the Super Node
144 Register.Instance().addNode(noeud);
146 // Register.Instance().viewAll() ;
147 // SuperNodeListe.Instance().viewAll() ;
149 // Inform the other superNode and tell them the nb of Daemon I have
151 int index = SuperNodeListe.Instance().existSuperNode(
152 LocalHost.Instance().getIP());
154 ((SuperNodeData) SuperNodeListe.Instance().getListe().get(index))
155 .setNbOfNodes(Register.Instance().getSize());
156 SuperNodeListe.Instance().forwardCountNode();
158 SuperNodeListe.Instance().addGNode( g ) ;
163 /****************************************************/
164 /****************************************************/
166 // HeartBeat that detects if a Daemon is dead or alive
167 public void beating(JaceInterface stub) throws RemoteException {
168 Node noeud = Register.Instance().getNodeOfStub(stub);
171 noeud.setAliveFlag(true);
172 noeud.setAliveTime();
173 noeud.incrementNbOfBeats();
175 // System.out.println( noeud.getName() + " is pinging me" ) ;
177 // System.out.println( noeud.getName() +
178 // ".................. is not in my list" ) ;
182 // HeartBeat that detects if a Super Node is dead or alive
183 public void beating(boolean token) throws RemoteException {
184 ScanThreadSuperNode.Instance().setAliveTime();
185 ScanThreadSuperNode.Instance().setToken(token);
187 // System.out.println( "Super Node is pinging me" ) ;
191 /*********************************/
192 /** Mapping !! Sébastien Miquée **/
193 /*********************************/
195 public Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
196 int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
198 // Have we the correct application ?
200 System.err.println( "Problem of class transmission !" ) ;
204 if( t.getDependencies( 0 ) == null )
206 System.err.println( "No redifinition of getDependencies() functions !" ) ;
211 /** Creation of an empty new Register **/
212 Register reg = new Register() ;
216 /** Initialization of Grid architecture (G5K for now) **/
217 Grid grid = Utils.createGridG5k(gnodes);
221 /** Creation of tasks GTask **/
222 ArrayList<GTask> ts = new ArrayList<GTask>();
223 for (int i = 0; i < nbTasks; i++) {
224 ts.add(new GTask( i ) ) ;
227 /** Research of dependencies **/
228 for (int i = 0; i < nbTasks; i++) {
230 dep = t.getDependencies(i);
232 /** Adding dependencies to tasks **/
233 for (int j = 0; j < dep.length; j++) {
235 ts.get(i).addDependance(ts.get(dep[j]));
243 Graph graph = new Graph();
245 for( int i = 0 ; i < ts.size() ; i++)
247 graph.addGTask(ts.get(i));
254 // Thread.sleep(10000) ;
255 // } catch( Exception e ) {}
260 // Thread.sleep( 10000 ) ;
261 // } catch( Exception e ) {}
263 /** Selection of the mapping algorithm **/
268 al = new Simple(graph, grid);
271 al = new QM(graph, grid, paramAlgo);
274 al = new LSM(graph, grid, paramAlgo);
277 al = new DefaultMapping( graph, grid, gnodes ) ;
281 /** Launching the Mapping **/
285 /** Transforming mapping in register **/
286 Mapping mp = al.getMapping();
289 /** Creating the register **/
290 ArrayList<GNode> ag = mp.getMappedGNodes();
294 for (int i = 0; i < ag.size(); i++) {
295 reg.addNode((Node) ag.get(i).getNode());
296 gnodes.remove(ag.get(i));
297 Register.Instance().removeNode((Node) ag.get(i).getNode());
302 if (ag.size() != 0) {
303 SuperNodeListe.Instance().forwardCountNode();
308 // daemonListChange = false ;
310 System.out.println( "Spawner returned reg: " + reg ) ;
312 /** Mapping distribution over other Super Nodes */
313 SuperNodeListe.Instance().setMapping( al ) ;
317 /* Returning result */
321 /*****************************************/
322 /**** Sébastien Miquée ****/
324 /** Recherche nouveau noeud **/
325 /*****************************************/
327 protected GNode delGNodeFromList( Node _n )
329 GNode removedGNode = null ;
333 boolean free = false ;
341 for( int i = 0 ; i < gnodes.size() ; i++ )
343 if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() )
345 removedGNode = gnodes.remove( i ) ;
350 // daemonListChange = true ;
352 SuperNodeListe.Instance().removeGNode( removedGNode ) ;
361 return removedGNode ;
365 private void workingOnGnodes()
377 tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
387 } catch (InterruptedException e) {
397 public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
400 GNode remp = null, gnode = null ;
402 /** Can we use gnodes ?**/
405 if( _deadNode != null )
407 gnode = delGNodeFromList( _deadNode ) ;
409 remp = al.replaceNode( gnode, gnodes ) ;
413 System.out.println( "Replacing node found." ) ;
414 node = (Node) remp.getNode() ;
418 delGNodeFromList( node ) ;
422 SuperNodeListe.Instance().forwardCountNode();
424 System.err.println( "Replacing node not found !!" ) ;
427 remp = al.getOtherGNode( gnodes ) ;
431 System.out.println( "Other new node found." ) ;
432 node = (Node) remp.getNode() ;
436 delGNodeFromList( node ) ;
440 SuperNodeListe.Instance().forwardCountNode();
442 System.err.println( "Other new node not found !!" ) ;
446 /** Free the gnodes use **/
453 /**********************************************************/
454 /**********************************************************/
457 public void addGNode( GNode _g ) throws RemoteException
471 public void removeGNode( GNode _g ) throws RemoteException
477 for( int i = 0 ; i < gnodes.size() ; i++ )
479 if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() )
493 public void setMapping( Algo _al ) throws RemoteException
501 * Allow or deny the use of operations on the gnodes list, in order to
502 * do a mapping operation.
504 * @return The authorization or not to block gnodes
506 public boolean blockForMapping() throws RemoteException
512 } catch (InterruptedException e) {
517 if( operating && ! authorized )