Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Creation of Mapping repository.
[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                         if( nodes.contains( _g ) )
160                         {
161                                 return true ;
162                         }
163                 }
164                 
165                 return false ;
166         }
167         
168         /**
169          * Initialization of indice variable.
170          */
171         public void initIndice()
172         {
173                 indice = 0 ;
174         }
175
176
177         /**
178          * Return the next computing node in the cluster,
179          * according to the indice variable.
180          * @return The next node in the cluster
181          */
182         public GNode nextGNode() 
183         {
184                 GNode ret = null ;
185                 
186                 if( indice < nb_node )
187                 {
188                         ret = nodes.get( indice ) ;
189                         indice++ ;
190                 }
191                 
192                 return ret ;
193         }
194
195
196         /**
197          * Remove a failed node from the cluster.
198          * @param _dead The failed node
199          */
200         public void removeGNode( GNode _dead ) 
201         {
202                 if( _dead != null && _dead.getCluster().equals( name ) && _dead.getSite().equals( site )  )
203                 {
204                         for( int i = 0 ; i < nodes.size() ; i++ )
205                         {
206                                 if( _dead.getId() == nodes.get( i ).getId() )
207                                 {
208                                         nodes.remove( i ) ;
209                                         nb_node-- ;
210                                         
211                                         break ;
212                                 }
213                         }
214                 }
215                 
216         }
217         
218 }
219
220 /** La programmation est un art, respectons ceux qui la pratiquent !! **/