Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8809d17388d41e3a822801469acdbe0c0192b3fd
[jaceP2P.git] / src / jaceP2P / JaceSuperNodeServer.java
1 package jaceP2P;
2
3 import java.rmi.RemoteException;
4 import java.rmi.server.UnicastRemoteObject;
5 import java.util.ArrayList;
6 import java.util.Vector;
7
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;
19
20
21 public class JaceSuperNodeServer extends UnicastRemoteObject implements
22                 JaceSuperNodeInterface {
23         private static final long serialVersionUID = 1L;
24
25         // Attributes
26         private int beat;
27         private ArrayList<GNode> gnodes = null;
28         private Algo al = null ;
29         private int count = 0 ;
30         private boolean inDemand = false, operating = false ;
31 //      private boolean daemonListChange ;
32         
33         // Constructors
34
35         public JaceSuperNodeServer(int timeBeat) throws RemoteException {
36                 super() ;
37                 beat = timeBeat ;
38                 gnodes = new ArrayList<GNode>() ;
39 //              daemonListChange = true ;
40         }
41
42         public int getSuperNodeBeat() throws RemoteException {
43                 return beat;
44         }
45
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();
52         }
53
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 +
57                 // " Daemons" ) ;
58                 SuperNodeListe.Instance().viewAll();
59         }
60
61         public void sendSurplus(Vector<?> nodes) throws RemoteException {
62                 System.out.println("Recieved " + nodes.size() + " nodes");
63
64                 for (int i = 0; i < nodes.size(); i++) {
65                         Register.Instance().getListe().add((Node) nodes.elementAt(i));
66
67                         System.out.println("Adding "
68                                         + ((Node) nodes.elementAt(i)).getName());
69
70                         try {
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()
76                                                 .get(index))
77                                                 .setNbOfNodes(Register.Instance().getSize());
78                                 new ForwardCount().start();
79                         } catch (Exception e) {
80                                 System.out.println("Error changing Server in SendSurplus : "
81                                                 + e);
82                         }
83                 }
84         }
85
86         public void setToken() throws RemoteException {
87                 System.out.println("I got Token");
88
89                 TokenThread.Instance().setToken();
90
91                 try {
92                         HeartBeatSNode.Instance().getServer().beating(true);
93
94                         System.out.println("Put token to true");
95                 } catch (Exception e) {
96                         System.out
97                                         .println("Unable to heartBeat the next SuperNode with the new Token : "
98                                                         + e);
99                 }
100         }
101
102         public void updateHeart(JaceSuperNodeInterface stub) throws RemoteException {
103                 System.out.println("I change to ping a superNode");
104
105                 HeartBeatSNode.Instance().setServer(stub);
106         }
107
108         public synchronized void removeSuperNode(SuperNodeData d)
109                         throws RemoteException {
110                 SuperNodeListe.Instance().removeSuperNode(d);
111         }
112
113         /****************************************************/
114         /****************************************************/
115
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);
121
122                 // Create the node
123                 Node noeud = new Node(workerStub);
124                 noeud.setName(workerName);
125                 noeud.setIP(workerIP);
126                 noeud.setAliveFlag(true);
127                 noeud.setAliveTime();
128                 noeud.setPort(port);
129                 noeud.setAppliName(null);
130                 noeud.setNbOfBeats(0);
131                 noeud.setId( count ) ;
132                 noeud.setId( Long.parseLong( workerIP.replace( ".", "" ) ) ) ;
133
134                 g.setNode(noeud);
135                 g.setId( noeud.getId() ) ;
136                 
137                 workingOnGnodes() ;
138                 
139                 gnodes.add(g);
140                 
141 //              daemonListChange = true ;
142
143                 // Insert the node in the Register.Instance() of the Super Node
144                 Register.Instance().addNode(noeud);
145
146                 // Register.Instance().viewAll() ;
147                 // SuperNodeListe.Instance().viewAll() ;
148
149                 // Inform the other superNode and tell them the nb of Daemon I have
150                 // registered
151                 int index = SuperNodeListe.Instance().existSuperNode(
152                                 LocalHost.Instance().getIP());
153
154                 ((SuperNodeData) SuperNodeListe.Instance().getListe().get(index))
155                                 .setNbOfNodes(Register.Instance().getSize());
156                 SuperNodeListe.Instance().forwardCountNode();
157                 
158                 SuperNodeListe.Instance().addGNode( g ) ;
159                 
160                 operating = false ;
161         }
162
163         /****************************************************/
164         /****************************************************/
165
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);
169
170                 if (noeud != null) {
171                         noeud.setAliveFlag(true);
172                         noeud.setAliveTime();
173                         noeud.incrementNbOfBeats();
174                         // -- sm modif
175                         // System.out.println( noeud.getName() + " is pinging me" ) ;
176                 } else {
177                         // System.out.println( noeud.getName() +
178                         // ".................. is not in my list" ) ;
179                 }
180         }
181
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);
186                 // -- sm modif
187                 // System.out.println( "Super Node is pinging me" ) ;
188         }
189
190
191         /*********************************/
192         /** Mapping !! Sébastien Miquée **/
193         /*********************************/
194
195         public Register getRegisterSpawner(String spawnerIP, int nbTasks, Task t,
196                         int nbNoeuds, int algo, double paramAlgo) throws RemoteException {
197
198                 // Have we the correct application ?
199                 if (t == null) {
200                         System.err.println( "Problem of class transmission !" ) ;
201                         return null ;
202                 }
203                 
204                 if( t.getDependencies( 0 ) == null )
205                 {
206                         System.err.println( "No redifinition of getDependencies() functions !" ) ;
207                         return null ;
208                 }
209                 
210
211                 /** Creation of an empty new Register **/
212                 Register reg = new Register() ;
213
214                 workingOnGnodes() ;
215                 
216                 /** Initialization of Grid architecture (G5K for now) **/
217                 Grid grid = Utils.createGridG5k(gnodes);
218                 grid.initClusters();
219
220                 /** Creation of tasks GTask **/
221                 ArrayList<GTask> ts = new ArrayList<GTask>();
222                 for (int i = 0; i < nbTasks; i++) {
223                         ts.add(new GTask( i ) ) ;
224                 }
225
226                 /** Research of dependencies **/
227                 for (int i = 0; i < nbTasks; i++) {
228                         int dep[] = null;
229                         dep = t.getDependencies(i);
230
231                         /** Adding dependencies to tasks **/
232                         for (int j = 0; j < dep.length; j++) {
233                                 if (dep[j] != -1) {
234                                         ts.get(i).addDependance(ts.get(dep[j]));
235                                 } else {
236                                         break;
237                                 }
238                         }
239                 }
240
241                 Graph graph = new Graph();
242
243                 for( int i = 0 ; i < ts.size() ; i++) 
244                 {
245                         graph.addGTask(ts.get(i));
246                 }
247
248                 // -- debug !
249                 // graph.print() ;
250
251                 // try {
252                 // Thread.sleep(10000) ;
253                 // } catch( Exception e ) {}
254
255                 // grid.print() ;
256                 // 
257                 // try {
258                 // Thread.sleep( 10000 ) ;
259                 // } catch( Exception e ) {}
260
261                 /** Selection of the mapping algorithm **/
262                 al = null ;
263
264                 switch (algo) {
265                 case 0:
266                         al = new Simple(graph, grid);
267                         break;
268                 case 1:
269                         al = new QM(graph, grid, paramAlgo);
270                         break;
271                 case 2:
272                         al = new LSM(graph, grid, paramAlgo);
273                         break;
274                 default:
275                         al = new DefaultMapping( graph, grid, gnodes ) ;
276                 }
277
278                 if (al != null) {
279                         /** Launching the Mapping **/
280                         al.map();
281
282                         /** Transforming mapping in register **/
283                         Mapping mp = al.getMapping();
284
285                         /** Creating the register **/
286                         ArrayList<GNode> ag = mp.getMappedGNodes();
287
288                         for (int i = 0; i < ag.size(); i++) {
289                                 reg.addNode((Node) ag.get(i).getNode());
290                                 gnodes.remove(ag.get(i));
291                                 Register.Instance().removeNode((Node) ag.get(i).getNode());
292                         }
293
294                         if (ag.size() != 0) {
295                                 SuperNodeListe.Instance().forwardCountNode();
296                         }
297
298                 } 
299                 
300 //              daemonListChange = false ;
301
302                 System.out.println( "Spawner returned reg: " + reg ) ;
303                 
304                 /** Mapping distribution over other Super Nodes */
305                 SuperNodeListe.Instance().setMapping( al ) ;
306                 
307                 operating = false ;
308                 
309                 /* Returning result */
310                 return reg ;
311         }
312
313         /*****************************************/
314         /****        Sébastien Miquée         ****/
315         /**                                     **/
316         /**      Recherche nouveau noeud        **/
317         /*****************************************/
318         
319         protected GNode delGNodeFromList( Node _n )
320         {
321                 GNode removedGNode = null ;
322                 
323                 if( _n != null )
324                 {               
325                         workingOnGnodes() ;
326                         
327                         for( int i = 0 ; i < gnodes.size() ; i++ )
328                         {
329                                 if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() ) 
330                                 {
331                                         removedGNode = gnodes.remove( i ) ;
332                                         break ;
333                                 }
334                         }
335                 
336 //              daemonListChange = true ;
337                 
338                         SuperNodeListe.Instance().removeGNode( removedGNode ) ;
339                         
340                         operating = false ;
341                 
342                 }
343                 
344                 return removedGNode ;
345         }
346         
347         
348         private void workingOnGnodes()
349         {
350                 boolean tmp = true ;    
351                 
352                 inDemand = false ; 
353                 operating = false ;
354             
355                 while( ! operating )
356                 {
357                         inDemand = true ;
358                         
359                         tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
360
361                         operating = tmp ;
362                         
363                         if( ! tmp )
364                         {
365                                 inDemand = false ;
366                                 
367                                 try {
368                                         Thread.sleep( 10 ) ;
369                                 } catch (InterruptedException e) {
370                                         e.printStackTrace();
371                                 }
372                         }
373                 }
374                 
375                 inDemand = false ;
376         }
377
378                 
379         public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
380         {
381                 Node node = null ;
382                 GNode remp = null, gnode = null ;
383                 
384                 /** Can we use gnodes ?**/
385                 workingOnGnodes() ;
386                 
387                 if( _deadNode != null )
388                 {
389                         gnode = delGNodeFromList( _deadNode ) ;
390                 
391                         remp = al.replaceNode( gnode, gnodes ) ;
392                 
393                         if( remp != null )
394                         {
395                                 System.out.println( "Replacing node found." ) ;
396                                 node = (Node) remp.getNode() ;
397                                 delGNodeFromList( node ) ;
398                                 SuperNodeListe.Instance().forwardCountNode();
399                         } else {
400                                 System.err.println( "Replacing node not found !!" ) ;
401                         }
402                 } else {
403                         remp = al.getOtherGNode( gnodes ) ;
404                         
405                         if( remp != null )
406                         {
407                                 System.out.println( "Other new node found." ) ;
408                                 node = (Node) remp.getNode() ;
409                                 delGNodeFromList( node ) ;
410                                 SuperNodeListe.Instance().forwardCountNode();
411                         } else {
412                                 System.err.println( "Other new node not found !!" ) ;
413                         }
414                 }
415                 
416                 /** Free the gnodes use **/
417                 operating = false ;
418                 
419                 return node ;
420         }
421         
422         
423         /**********************************************************/
424         /**********************************************************/
425
426         @Override
427         public void addGNode( GNode _g ) throws RemoteException 
428         {
429                 if( _g != null )
430                 {
431                         workingOnGnodes() ;
432                         
433                         gnodes.add( _g ) ;
434                         
435                         operating = false ;
436                 }
437         }
438         
439
440         @Override
441         public void removeGNode( GNode _g ) throws RemoteException 
442         {
443                 if( _g != null )
444                 {
445                         workingOnGnodes() ;
446                         
447                         for( int i = 0 ; i < gnodes.size() ; i++ )
448                         {
449                                 if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() ) 
450                                 {
451                                         gnodes.remove( i ) ;
452                                         break ;
453                                 }
454                         }
455                         
456                         operating = false ;
457                 }
458                 
459         }
460         
461
462         @Override
463         public void setMapping( Algo _al ) throws RemoteException 
464         {
465                 al = _al ;              
466         }
467
468         @Override
469         /**
470          * Allow or deny the use of operations on the gnodes list, in order to
471          * do a mapping operation.
472          * @author miquee
473          * @return The authorization or not to block gnodes
474          */
475         public boolean blockForMapping() 
476         {
477                 while( inDemand )
478                 {
479                         try {
480                                 Thread.sleep( 10 ) ;
481                         } catch (InterruptedException e) {
482                                 e.printStackTrace();
483                         }
484                 }
485                 
486                 if( operating )
487                 {
488                         return false ;
489                 } else {
490                         return true ;
491                 }
492         }
493         
494 }
495
496 /** ! **/