Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0561682be5c221f18494eb0a72764fb4cd95fb81
[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 String site ;
21         private int indice ;
22         
23         
24         /**
25          * Default constructor.
26          */
27         public Cluster()
28         {
29                 nb_node = 0 ;
30                 name = "" ;
31                 nodes = new ArrayList<GNode>() ;
32                 site = "" ;
33                 indice = 0 ;
34         }
35         
36         
37         /**
38          * Constructor.
39          * @param _nb The amount of computing nodes in the cluster
40          */
41         public Cluster( int _nb )
42         {
43                 nb_node = _nb ;
44                 name = "" ;
45                 nodes = new ArrayList<GNode>() ;
46                 site = "" ;
47                 indice = 0 ;
48                 
49                 
50                 for( int i = 0 ; i < nb_node ; i++ )
51                 {
52                         nodes.add( new GNode() ) ;
53                 }
54         }
55         
56         
57         /**
58          * Constructor.
59          * @param _nb The amount of computing nodes in the cluster
60          * @param _name Cluster's name
61          */
62         public Cluster( int _nb, String _name )
63         {
64                 nb_node = _nb ;
65                 name = _name ;
66                 nodes = new ArrayList<GNode>() ;
67                 site = "" ;
68                 indice = 0 ;
69                 
70                 for( int i = 0 ; i < nb_node ; i++ )
71                 {
72                         nodes.add( new GNode() ) ;
73                 }
74         }
75         
76         
77         /**
78          * Set the name of the cluster.
79          * @param _name Cluster's name
80          */
81         public void setName( String _name )
82         {
83                 name = _name ;
84         }
85         
86         
87         /**
88          * Adding a computing node to the cluster.
89          * @param _n Node to be add
90          */
91         public void addGNode( GNode _n )
92         {
93                 _n.setInCluster( true ) ;
94                 nodes.add( _n ) ;
95
96                 nb_node++ ;
97         }
98         
99         
100         /**
101          * Return the list of computing nodes which are in the cluster.
102          * @return The list of nodes
103          */
104         public ArrayList<GNode> getGNodes()
105         {
106                 return nodes ;
107         }
108         
109         
110         /**
111          * Return cluster's name.
112          * @return Cluster's name
113          */
114         public String getName()
115         {
116                 return name ;
117         }
118         
119         
120         /**
121          * Return the amount of computing nodes in the cluster.
122          * @return The amount of nodes
123          */
124         public int getNbGNode()
125         {
126                 return nb_node ;
127         }
128         
129         
130         /**
131          * Set the site in which the cluster is.
132          * @param _site Site's name
133          */
134         public void setSite( String _site )
135         {
136                 site = _site ;
137         }
138         
139         
140         /**
141          * Return the site's name in which the cluster is.
142          * @return The site's name
143          */
144         public String getSite()
145         {
146                 return site ;
147         }
148
149         
150         /**
151          * Test if a computing node is in the cluster.
152          * @param _g The node to be tested
153          * @return True is _g is in, False else
154          */
155         public boolean isIn( GNode _g )
156         {
157                 if( _g != null )
158                 {
159                         for( int i = 0; i < nodes.size(); i ++ )
160                         {
161                                 if( nodes.get( i ).getId() == _g.getId() )
162                                         return true ;
163                         }
164                 }
165                 
166                 return false ;
167         }
168         
169         /**
170          * Initialization of indice variable.
171          */
172         public void initIndice()
173         {
174                 indice = 0 ;
175         }
176
177
178         /**
179          * Return the next computing node in the cluster,
180          * according to the indice variable.
181          * @return The next node in the cluster
182          */
183         public GNode nextGNode() 
184         {
185                 GNode ret = null ;
186                 
187                 if( indice < nb_node )
188                 {
189                         ret = nodes.get( indice ) ;
190                         indice++ ;
191                 }
192                 
193                 return ret ;
194         }
195
196
197         /**
198          * Remove a failed node from the cluster.
199          * @param _dead The failed node
200          */
201         public void removeGNode( GNode _dead ) 
202         {
203                 if( _dead != null && _dead.getCluster().equals( name ) && _dead.getSite().equals( site )  )
204                 {
205                         for( int i = 0 ; i < nodes.size() ; i++ )
206                         {
207                                 if( _dead.getId() == nodes.get( i ).getId() )
208                                 {
209                                         nodes.remove( i ) ;
210                                         nb_node-- ;
211                                         
212                                         break ;
213                                 }
214                         }
215                 }
216                 
217         }
218         
219 }
220
221 /** La programmation est un art, respectons ceux qui la pratiquent !! **/