2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
21 import peersim.config.*;
24 * This is the default {@link Node} class that is used to compose the
27 public class GeneralNode implements Node {
30 // ================= fields ========================================
31 // =================================================================
33 /** used to generate unique IDs */
34 private static long counterID = -1;
37 * The protocols on this node.
39 protected Protocol[] protocol = null;
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.
50 * The fail state of the node.
52 protected int failstate = Fallible.OK;
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.
60 // ================ constructor and initialization =================
61 // =================================================================
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
69 public GeneralNode(String prefix) {
71 String[] names = Configuration.getNames(PAR_PROT);
72 CommonState.setNode(this);
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]);
84 // -----------------------------------------------------------------
86 public Object clone() {
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);
94 for(int i=0; i<protocol.length; ++i) {
95 CommonState.setPid(i);
96 result.protocol[i] = (Protocol)protocol[i].clone();
101 // -----------------------------------------------------------------
103 /** returns the next unique ID */
104 private long nextID() {
109 // =============== public methods ==================================
110 // =================================================================
113 public void setFailState(int failState) {
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");
127 for(int i=0;i<protocol.length;++i)
128 if(protocol[i] instanceof Cleanable)
129 ((Cleanable)protocol[i]).onKill();
135 throw new IllegalArgumentException(
136 "failState="+failState);
140 // -----------------------------------------------------------------
142 public int getFailState() { return failstate; }
144 // ------------------------------------------------------------------
146 public boolean isUp() { return failstate==OK; }
148 // -----------------------------------------------------------------
150 public Protocol getProtocol(int i) { return protocol[i]; }
152 //------------------------------------------------------------------
154 public int protocolSize() { return protocol.length; }
156 //------------------------------------------------------------------
158 public int getIndex() { return index; }
160 //------------------------------------------------------------------
162 public void setIndex(int index) { this.index = index; }
164 //------------------------------------------------------------------
167 * Returns the ID of this node. The IDs are generated using a counter
168 * (i.e. they are not random).
170 public long getID() { return ID; }
172 //------------------------------------------------------------------
174 public String toString()
176 StringBuffer buffer = new StringBuffer();
177 buffer.append("ID: "+ID+" index: "+index+"\n");
178 for(int i=0; i<protocol.length; ++i)
180 buffer.append("protocol["+i+"]="+protocol[i]+"\n");
182 return buffer.toString();
185 //------------------------------------------------------------------
187 /** Implemented as <code>(int)getID()</code>. */
188 public int hashCode() { return (int)getID(); }