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.Configuration;
24 * A protocol that stores links. It does nothing apart from that.
25 * It is useful to model a static link-structure
26 * (topology). The only function of this protocol is to serve as a source of
27 * neighborhood information for other protocols.
29 public class IdleProtocol implements Protocol, Linkable
32 // --------------------------------------------------------------------------
34 // --------------------------------------------------------------------------
37 * Default init capacity
39 private static final int DEFAULT_INITIAL_CAPACITY = 10;
42 * Initial capacity. Defaults to {@value #DEFAULT_INITIAL_CAPACITY}.
45 private static final String PAR_INITCAP = "capacity";
47 // --------------------------------------------------------------------------
49 // --------------------------------------------------------------------------
52 protected Node[] neighbors;
54 /** Actual number of neighbors in the array */
57 // --------------------------------------------------------------------------
59 // --------------------------------------------------------------------------
61 public IdleProtocol(String s)
63 neighbors = new Node[Configuration.getInt(s + "." + PAR_INITCAP,
64 DEFAULT_INITIAL_CAPACITY)];
68 //--------------------------------------------------------------------------
72 IdleProtocol ip = null;
73 try { ip = (IdleProtocol) super.clone(); }
74 catch( CloneNotSupportedException e ) {} // never happens
75 ip.neighbors = new Node[neighbors.length];
76 System.arraycopy(neighbors, 0, ip.neighbors, 0, len);
81 // --------------------------------------------------------------------------
83 // --------------------------------------------------------------------------
85 public boolean contains(Node n)
87 for (int i = 0; i < len; i++) {
88 if (neighbors[i] == n)
94 // --------------------------------------------------------------------------
96 /** Adds given node if it is not already in the network. There is no limit
97 * to the number of nodes that can be added. */
98 public boolean addNeighbor(Node n)
100 for (int i = 0; i < len; i++) {
101 if (neighbors[i] == n)
104 if (len == neighbors.length) {
105 Node[] temp = new Node[3 * neighbors.length / 2];
106 System.arraycopy(neighbors, 0, temp, 0, neighbors.length);
114 // --------------------------------------------------------------------------
116 public Node getNeighbor(int i)
121 // --------------------------------------------------------------------------
128 // --------------------------------------------------------------------------
132 if (len == neighbors.length)
134 Node[] temp = new Node[len];
135 System.arraycopy(neighbors, 0, temp, 0, len);
139 // --------------------------------------------------------------------------
141 public String toString()
143 if( neighbors == null ) return "DEAD!";
144 StringBuffer buffer = new StringBuffer();
145 buffer.append("len=" + len + " maxlen=" + neighbors.length + " [");
146 for (int i = 0; i < len; ++i) {
147 buffer.append(neighbors[i].getIndex() + " ");
149 return buffer.append("]").toString();
152 // --------------------------------------------------------------------------