Logo AND Algorithmique Numérique Distribuée

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