Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4d59d3d2bf22f67f1ecbe8e1588a8330c4adc119
[mapping.git] / src / and / Mapping / Cluster.java
1 package and.Mapping ;
2
3
4 import java.io.Serializable;
5 import java.util.ArrayList;
6
7
8 /**
9  * Class representing a computing nodes cluster
10  * @author Sébastien Miquée
11  *
12  */
13 public class Cluster implements Serializable
14 {
15         private static final long serialVersionUID = 1L;
16         
17 //      private int nb_node ;
18         private String name ;
19         private ArrayList<GNode> nodes ;
20         private ArrayList<GNode> freenodes ;
21         private String site ;
22 //      private int indice ;
23         
24         
25         /**
26          * Default constructor.
27          */
28         public Cluster()
29         {
30 //              nb_node = 0 ;
31                 name = "" ;
32                 nodes = new ArrayList<GNode>() ;
33                 freenodes = new ArrayList<GNode>() ;
34                 site = "" ;
35 //              indice = 0 ;
36         }
37         
38         
39 //      /**
40 //       * Constructor.
41 //       * @param _nb The amount of computing nodes in the cluster
42 //       */
43 //      public Cluster( int _nb )
44 //      {
45 //              nb_node = _nb ;
46 //              name = "" ;
47 //              nodes = new ArrayList<GNode>() ;
48 //              freenodes = new ArrayList<GNode>() ;
49 //              site = "" ;
50 ////            indice = 0 ;
51 //              
52 //              
53 //              for( int i = 0 ; i < nb_node ; i++ )
54 //              {
55 //                      nodes.add( new GNode() ) ;
56 //              }
57 //      }
58         
59         
60 //      /**
61 //       * Constructor.
62 //       * @param _nb The amount of computing nodes in the cluster
63 //       * @param _name Cluster's name
64 //       */
65 //      public Cluster( int _nb, String _name )
66 //      {
67 ////            nb_node = _nb ;
68 //              name = _name ;
69 //              nodes = new ArrayList<GNode>() ;
70 //              site = "" ;
71 ////            indice = 0 ;
72 //              
73 //              for( int i = 0 ; i < nb_node ; i++ )
74 //              {
75 //                      nodes.add( new GNode() ) ;
76 //              }
77 //      }
78         
79         
80         /**
81          * Set the name of the cluster.
82          * @param _name Cluster's name
83          */
84         public void setName( String _name )
85         {
86                 name = _name ;
87         }
88         
89         
90         /**
91          * Adding a computing node to the cluster.
92          * @param _n Node to be add
93          */
94         public void addGNode( GNode _n )
95         {
96                 if( _n != null )
97                 {
98                         _n.setInCluster( true ) ;
99                         nodes.add( _n ) ;
100
101 //                      nb_node++ ;
102                 
103                         if( ! _n.getMapped() )
104                         {
105                                 freenodes.add( _n ) ;
106                         }
107                 }
108         }
109         
110         
111         /**
112          * Return the list of computing nodes which are in the cluster.
113          * @return The list of nodes
114          */
115         public ArrayList<GNode> getGNodes()
116         {
117                 return nodes ;
118         }
119         
120         
121         /**
122          * Return cluster's name.
123          * @return Cluster's name
124          */
125         public String getName()
126         {
127                 return name ;
128         }
129         
130         
131         /**
132          * Return the amount of computing nodes in the cluster.
133          * @return The amount of nodes
134          */
135         public int getNbGNode()
136         {
137 //              return nb_node ;
138                 return nodes.size() ;
139         }
140         
141         
142         /**
143          * Set the site in which the cluster is.
144          * @param _site Site's name
145          */
146         public void setSite( String _site )
147         {
148                 site = _site ;
149         }
150         
151         
152         /**
153          * Return the site's name in which the cluster is.
154          * @return The site's name
155          */
156         public String getSite()
157         {
158                 return site ;
159         }
160
161         
162         /**
163          * Test if a computing node is in the cluster.
164          * @param _g The node to be tested
165          * @return The position of the node
166          */
167         public int isIn( GNode _g )
168         {
169                 int pos = -1 ;
170                 
171                 if( _g != null )
172                 {
173                         for( int i = 0 ; i < nodes.size() ; i ++ )
174                         {
175                                 if( nodes.get( i ).getId() == _g.getId() )
176                                 {
177                                         pos = i ;
178                                         break ;
179                                 }
180                                 
181                         }
182                 }
183                 
184                 return pos ;
185         }
186         
187 //      /**
188 //       * Initialization of indice variable.
189 //       */
190 //      public void initIndice()
191 //      {
192 //              indice = 0 ;
193 //      }
194
195
196         /**
197          * Return the next computing node in the cluster,
198          * according to the indice variable.
199          * @return The next node in the cluster
200          */
201         public GNode nextGNode() 
202         {
203                 GNode ret = null ;
204                 
205 //              if( indice < nb_node )
206 //              {
207 //                      ret = nodes.get( indice ) ;
208 //                      indice++ ;
209 //              }
210                 if( freenodes.size() > 0 )
211                 {
212                         ret = freenodes.get( 0 ) ;
213                 }
214                 
215                 return ret ;
216         }
217
218
219         /**
220          * Remove a failed node from the cluster.
221          * @param _dead The failed node
222          */
223         public void removeGNode( GNode _dead ) 
224         {
225                 if( _dead != null && _dead.getCluster().equals( name ) && _dead.getSite().equals( site )  )
226                 {
227                         for( int i = 0 ; i < nodes.size() ; i++ )
228                         {
229                                 if( _dead.getId() == nodes.get( i ).getId() )
230                                 {
231                                         freenodes.remove( nodes.remove( i ) ) ;
232 //                                      nb_node-- ;
233                                         
234                                         break ;
235                                 }
236                         }
237                 }
238         }
239         
240         
241         /**
242          * Set the mapped status of a node of the cluster.
243          * @param _g The mapped node
244          * @param _status The status of the node
245          */
246         public void setGNodeStatus( GNode _g, boolean _status )
247         {
248                 if( _g != null )
249                 {
250                         for( int i = 0 ; i < nodes.size() ; i++ )
251                         {
252                                 if( nodes.get( i ).getId() == _g.getId() )
253                                 {
254                                         nodes.get( i ).setMapped( _status ) ;
255                                         
256                                         if( _status ) 
257                                         {
258                                                 freenodes.remove( nodes.get(i) ) ;
259                                         } else {
260                                                 if( ! freenodes.contains( nodes.get( i ) ) )
261                                                         freenodes.add( nodes.get( i ) ) ;
262                                         }
263                                 }
264                         }
265                 }
266         }
267         
268         
269         /**
270          * Return the amount of available free nodes in the cluster.
271          * @return The amount of free nodes
272          */
273         public int getNbFreeNodes()
274         {
275                 return freenodes.size() ;
276         }
277         
278         
279         /**
280          * Compute and return the real available computing power of the cluster,
281          * including the heterogeneity degree of the platform. 
282          * @param _het The heterogeneity degree of the platform
283          * @return The real available computing power
284          */
285         public double getAvailablePower( double _het )
286         {
287                 double ret = 0 ;
288                 
289                 /** If there is some available nodes **/
290                 if( freenodes.size() > 0 )
291                 {
292                         double het = _het ;
293                         double totalPower = 0 ;
294                         
295                         if( het == 0 )
296                                 het = 0.00001 ;
297                         
298                         for( int i = 0 ; i < freenodes.size() ; i++ )
299                         {
300                                 totalPower += freenodes.get( i ).getPower() ;
301                         }
302                         
303                         ret = Math.pow( ( totalPower / freenodes.size() ), ( 2 * het) ) *
304                               ( freenodes.size() / ( het * het) ) ;
305                 }
306                 
307                 return ret ;
308         }
309
310
311         /**
312          * Initialization of computing nodes in this cluster. Set all
313          * of these nodes to be not mapped on.
314          */
315         public void initGNodes() 
316         {
317                 /** Initialization of local nodes            **/
318                 /** and reconstruction of the freenodes list **/
319                 freenodes = null ;
320                 freenodes = new ArrayList<GNode>() ;
321                 
322                 for( int i = 0 ; i < nodes.size() ; i++ )
323                 {
324                         nodes.get( i ).setMapped( false ) ;
325                         
326                         freenodes.add( nodes.get( i ) ) ;
327                 }
328         }
329         
330 }
331
332 /** La programmation est un art, respectons ceux qui la pratiquent !! **/