Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c85a7c5247f87f1891ee0e5ed9b2e379afb16c59
[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, authorized = 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
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 ) ) ;
225                 }
226
227                 /** Research of dependencies **/
228                 for (int i = 0; i < nbTasks; i++) {
229                         int dep[] = null;
230                         dep = t.getDependencies(i);
231
232                         /** Adding dependencies to tasks **/
233                         for (int j = 0; j < dep.length; j++) {
234                                 if (dep[j] != -1) {
235                                         ts.get(i).addDependance(ts.get(dep[j]));
236                                 } else {
237                                         break;
238                                 }
239                         }
240                 }
241                 
242
243                 Graph graph = new Graph();
244
245                 for( int i = 0 ; i < ts.size() ; i++) 
246                 {
247                         graph.addGTask(ts.get(i));
248                 }
249
250                 // -- debug !
251                 // graph.print() ;
252
253                 // try {
254                 // Thread.sleep(10000) ;
255                 // } catch( Exception e ) {}
256
257                 // grid.print() ;
258                 // 
259                 // try {
260                 // Thread.sleep( 10000 ) ;
261                 // } catch( Exception e ) {}
262
263                 /** Selection of the mapping algorithm **/
264                 al = null ;
265
266                 switch (algo) {
267                 case 0:
268                         al = new Simple(graph, grid);
269                         break;
270                 case 1:
271                         al = new QM(graph, grid, paramAlgo);
272                         break;
273                 case 2:
274                         al = new LSM(graph, grid, paramAlgo);
275                         break;
276                 default:
277                         al = new DefaultMapping( graph, grid, gnodes ) ;
278                 }
279
280                 if (al != null) {
281                         /** Launching the Mapping **/
282                         al.map();
283
284
285                         /** Transforming mapping in register **/
286                         Mapping mp = al.getMapping();
287                         
288                         
289                         /** Creating the register **/
290                         ArrayList<GNode> ag = mp.getMappedGNodes();
291
292                         authorized = true ;
293                         
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());
298                         }
299                         
300                         authorized = false ;
301
302                         if (ag.size() != 0) {
303                                 SuperNodeListe.Instance().forwardCountNode();
304                         }
305
306                 } 
307                 
308 //              daemonListChange = false ;
309
310                 System.out.println( "Spawner returned reg: " + reg ) ;
311                 
312                 /** Mapping distribution over other Super Nodes */
313                 SuperNodeListe.Instance().setMapping( al ) ;
314                 
315                 operating = false ;
316                 
317                 /* Returning result */
318                 return reg ;
319         }
320
321         /*****************************************/
322         /****        Sébastien Miquée         ****/
323         /**                                     **/
324         /**      Recherche nouveau noeud        **/
325         /*****************************************/
326         
327         protected GNode delGNodeFromList( Node _n )
328         {
329                 GNode removedGNode = null ;
330                 
331                 if( _n != null )
332                 {       
333                         boolean free = false ;
334                         
335                         if( ! operating )
336                         {
337                                 workingOnGnodes() ;
338                                 free = true ;
339                         }
340                         
341                         for( int i = 0 ; i < gnodes.size() ; i++ )
342                         {
343                                 if( ((Node)gnodes.get(i).getNode()).getId() == _n.getId() ) 
344                                 {
345                                         removedGNode = gnodes.remove( i ) ;
346                                         break ;
347                                 }
348                         }
349                 
350 //              daemonListChange = true ;
351                 
352                         SuperNodeListe.Instance().removeGNode( removedGNode ) ;
353                         
354                         if( free )
355                         {
356                                 operating = false ;
357                         }
358                 
359                 }
360                 
361                 return removedGNode ;
362         }
363         
364         
365         private void workingOnGnodes()
366         {
367                 boolean tmp = true ;    
368                 
369                 inDemand = false ; 
370                 operating = false ;
371             
372                 while( ! operating )
373                 {
374                         inDemand = true ;
375                         tmp = true ;
376                         
377                         tmp = tmp && SuperNodeListe.Instance().workingOnGnodes() ;
378
379                         operating = tmp ;
380                         
381                         if( ! tmp )
382                         {
383                                 inDemand = false ;
384                                 
385                                 try {
386                                         Thread.sleep( 10 ) ;
387                                 } catch (InterruptedException e) {
388                                         e.printStackTrace();
389                                 }
390                         }
391                 }
392                 
393                 inDemand = false ;
394         }
395
396                 
397         public Node getNewNode( String _spawnerIP, Node _deadNode ) throws RemoteException
398         {
399                 Node node = null ;
400                 GNode remp = null, gnode = null ;
401                 
402                 /** Can we use gnodes ?**/
403                 workingOnGnodes() ;
404                 
405                 if( _deadNode != null )
406                 {
407                         gnode = delGNodeFromList( _deadNode ) ;
408                 
409                         remp = al.replaceNode( gnode, gnodes ) ;
410                 
411                         if( remp != null )
412                         {
413                                 System.out.println( "Replacing node found." ) ;
414                                 node = (Node) remp.getNode() ;
415                                 
416                                 authorized = true ;
417                                 
418                                 delGNodeFromList( node ) ;
419                                 
420                                 authorized = false ;
421                                 
422                                 SuperNodeListe.Instance().forwardCountNode();
423                         } else {
424                                 System.err.println( "Replacing node not found !!" ) ;
425                         }
426                 } else {
427                         remp = al.getOtherGNode( gnodes ) ;
428                         
429                         if( remp != null )
430                         {
431                                 System.out.println( "Other new node found." ) ;
432                                 node = (Node) remp.getNode() ;
433                                 
434                                 authorized = true ;
435                                 
436                                 delGNodeFromList( node ) ;
437                                 
438                                 authorized = false ;
439                                 
440                                 SuperNodeListe.Instance().forwardCountNode();
441                         } else {
442                                 System.err.println( "Other new node not found !!" ) ;
443                         }
444                 }
445                 
446                 /** Free the gnodes use **/
447                 operating = false ;
448                 
449                 return node ;
450         }
451         
452         
453         /**********************************************************/
454         /**********************************************************/
455
456         @Override
457         public void addGNode( GNode _g ) throws RemoteException 
458         {
459                 if( _g != null )
460                 {
461                         workingOnGnodes() ;
462                         
463                         gnodes.add( _g ) ;
464                         
465                         operating = false ;
466                 }
467         }
468         
469
470         @Override
471         public void removeGNode( GNode _g ) throws RemoteException 
472         {
473                 if( _g != null )
474                 {
475                         workingOnGnodes() ;
476                         
477                         for( int i = 0 ; i < gnodes.size() ; i++ )
478                         {
479                                 if( ((Node)gnodes.get(i).getNode()).getId() == ((Node)_g.getNode()).getId() ) 
480                                 {
481                                         gnodes.remove( i ) ;
482                                         break ;
483                                 }
484                         }
485                         
486                         operating = false ;
487                 }
488                 
489         }
490         
491
492         @Override
493         public void setMapping( Algo _al ) throws RemoteException 
494         {
495                 al = _al ;              
496         }
497
498         
499         @Override
500         /**
501          * Allow or deny the use of operations on the gnodes list, in order to
502          * do a mapping operation.
503          * @author miquee
504          * @return The authorization or not to block gnodes
505          */
506         public boolean blockForMapping() throws RemoteException
507         {
508                 while( inDemand )
509                 {
510                         try {
511                                 Thread.sleep( 10 ) ;
512                         } catch (InterruptedException e) {
513                                 e.printStackTrace();
514                         }
515                 }
516                 
517                 if( operating && ! authorized )
518                 {
519                         return false ;
520                 } else {
521                         return true ;
522                 }
523         }
524         
525 }
526
527 /** ! **/