Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / core / IdleProtocol.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.Configuration;
22
23 /**
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.
28  */
29 public class IdleProtocol implements Protocol, Linkable
30 {
31
32 // --------------------------------------------------------------------------
33 // Parameters
34 // --------------------------------------------------------------------------
35
36 /**
37  * Default init capacity
38  */
39 private static final int DEFAULT_INITIAL_CAPACITY = 10;
40
41 /**
42  * Initial capacity. Defaults to {@value #DEFAULT_INITIAL_CAPACITY}.
43  * @config
44  */
45 private static final String PAR_INITCAP = "capacity";
46
47 // --------------------------------------------------------------------------
48 // Fields
49 // --------------------------------------------------------------------------
50
51 /** Neighbors */
52 protected Node[] neighbors;
53
54 /** Actual number of neighbors in the array */
55 protected int len;
56
57 // --------------------------------------------------------------------------
58 // Initialization
59 // --------------------------------------------------------------------------
60
61 public IdleProtocol(String s)
62 {
63         neighbors = new Node[Configuration.getInt(s + "." + PAR_INITCAP,
64                         DEFAULT_INITIAL_CAPACITY)];
65         len = 0;
66 }
67
68 //--------------------------------------------------------------------------
69
70 public Object clone()
71 {
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);
77         ip.len = len;
78         return ip;
79 }
80
81 // --------------------------------------------------------------------------
82 // Methods
83 // --------------------------------------------------------------------------
84
85 public boolean contains(Node n)
86 {
87         for (int i = 0; i < len; i++) {
88                 if (neighbors[i] == n)
89                         return true;
90         }
91         return false;
92 }
93
94 // --------------------------------------------------------------------------
95
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)
99 {
100         for (int i = 0; i < len; i++) {
101                 if (neighbors[i] == n)
102                         return false;
103         }
104         if (len == neighbors.length) {
105                 Node[] temp = new Node[3 * neighbors.length / 2];
106                 System.arraycopy(neighbors, 0, temp, 0, neighbors.length);
107                 neighbors = temp;
108         }
109         neighbors[len] = n;
110         len++;
111         return true;
112 }
113
114 // --------------------------------------------------------------------------
115
116 public Node getNeighbor(int i)
117 {
118         return neighbors[i];
119 }
120
121 // --------------------------------------------------------------------------
122
123 public int degree()
124 {
125         return len;
126 }
127
128 // --------------------------------------------------------------------------
129
130 public void pack()
131 {
132         if (len == neighbors.length)
133                 return;
134         Node[] temp = new Node[len];
135         System.arraycopy(neighbors, 0, temp, 0, len);
136         neighbors = temp;
137 }
138
139 // --------------------------------------------------------------------------
140
141 public String toString()
142 {
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() + " ");
148         }
149         return buffer.append("]").toString();
150 }
151
152 // --------------------------------------------------------------------------
153
154 public void onKill()
155 {
156         neighbors = null;
157         len = 0;
158 }
159
160 }