Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / core / GeneralNode.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18                 
19 package peersim.core;
20
21 import peersim.config.*;
22
23 /**
24 * This is the default {@link Node} class that is used to compose the
25 * {@link Network}.
26 */
27 public class GeneralNode implements Node {
28
29
30 // ================= fields ========================================
31 // =================================================================
32
33 /** used to generate unique IDs */
34 private static long counterID = -1;
35
36 /**
37 * The protocols on this node.
38 */
39 protected Protocol[] protocol = null;
40
41 /**
42 * The current index of this node in the node
43 * list of the {@link Network}. It can change any time.
44 * This is necessary to allow
45 * the implementation of efficient graph algorithms.
46 */
47 private int index;
48
49 /**
50 * The fail state of the node.
51 */
52 protected int failstate = Fallible.OK;
53
54 /**
55 * The ID of the node. It should be final, however it can't be final because
56 * clone must be able to set it.
57 */
58 private long ID;
59
60 // ================ constructor and initialization =================
61 // =================================================================
62
63 /** Used to construct the prototype node. This class currently does not
64 * have specific configuration parameters and so the parameter
65 * <code>prefix</code> is not used. It reads the protocol components
66 * (components that have type {@value peersim.core.Node#PAR_PROT}) from
67 * the configuration.
68 */
69 public GeneralNode(String prefix) {
70         
71         String[] names = Configuration.getNames(PAR_PROT);
72         CommonState.setNode(this);
73         ID=nextID();
74         protocol = new Protocol[names.length];
75         for (int i=0; i < names.length; i++) {
76                 CommonState.setPid(i);
77                 Protocol p = (Protocol) 
78                         Configuration.getInstance(names[i]);
79                 protocol[i] = p; 
80         }
81 }
82
83
84 // -----------------------------------------------------------------
85
86 public Object clone() {
87         
88         GeneralNode result = null;
89         try { result=(GeneralNode)super.clone(); }
90         catch( CloneNotSupportedException e ) {} // never happens
91         result.protocol = new Protocol[protocol.length];
92         CommonState.setNode(result);
93         result.ID=nextID();
94         for(int i=0; i<protocol.length; ++i) {
95                 CommonState.setPid(i);
96                 result.protocol[i] = (Protocol)protocol[i].clone();
97         }
98         return result;
99 }
100
101 // -----------------------------------------------------------------
102
103 /** returns the next unique ID */
104 private long nextID() {
105
106         return counterID++;
107 }
108
109 // =============== public methods ==================================
110 // =================================================================
111
112
113 public void setFailState(int failState) {
114         
115         // after a node is dead, all operations on it are errors by definition
116         if(failstate==DEAD && failState!=DEAD) throw new IllegalStateException(
117                 "Cannot change fail state: node is already DEAD");
118         switch(failState)
119         {
120                 case OK:
121                         failstate=OK;
122                         break;
123                 case DEAD:
124                         //protocol = null;
125                         index = -1;
126                         failstate = DEAD;
127                         for(int i=0;i<protocol.length;++i)
128                                 if(protocol[i] instanceof Cleanable)
129                                         ((Cleanable)protocol[i]).onKill();
130                         break;
131                 case DOWN:
132                         failstate = DOWN;
133                         break;
134                 default:
135                         throw new IllegalArgumentException(
136                                 "failState="+failState);
137         }
138 }
139
140 // -----------------------------------------------------------------
141
142 public int getFailState() { return failstate; }
143
144 // ------------------------------------------------------------------
145
146 public boolean isUp() { return failstate==OK; }
147
148 // -----------------------------------------------------------------
149
150 public Protocol getProtocol(int i) { return protocol[i]; }
151
152 //------------------------------------------------------------------
153
154 public int protocolSize() { return protocol.length; }
155
156 //------------------------------------------------------------------
157
158 public int getIndex() { return index; }
159
160 //------------------------------------------------------------------
161
162 public void setIndex(int index) { this.index = index; }
163         
164 //------------------------------------------------------------------
165
166 /**
167 * Returns the ID of this node. The IDs are generated using a counter
168 * (i.e. they are not random).
169 */
170 public long getID() { return ID; }
171
172 //------------------------------------------------------------------
173
174 public String toString() 
175 {
176         StringBuffer buffer = new StringBuffer();
177         buffer.append("ID: "+ID+" index: "+index+"\n");
178         for(int i=0; i<protocol.length; ++i)
179         {
180                 buffer.append("protocol["+i+"]="+protocol[i]+"\n");
181         }
182         return buffer.toString();
183 }
184
185 //------------------------------------------------------------------
186
187 /** Implemented as <code>(int)getID()</code>. */
188 public int hashCode() { return (int)getID(); }
189
190 }
191
192