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.
19 package peersim.dynamics;
21 import peersim.config.Configuration;
22 import peersim.core.*;
25 * Makes the network size oscillate.
26 * The network size will be the function of time, parameterized by this
27 * parameter. The size function is
28 * <code>avg+sin(time*pi/{@value #PAR_PERIOD})*ampl</code> where
29 * <code>avg=({@value #PAR_MAX}+{@value #PAR_MIN})/2</code> and
30 * <code>ampl=({@value #PAR_MAX}-{@value #PAR_MIN})/2</code>.
31 * This function is independent of how many times this class is executed, that
32 * is, whenever it is executed, it takes the current time and sets the network
35 public class OscillatingNetwork implements Control
38 //--------------------------------------------------------------------------
40 //--------------------------------------------------------------------------
43 * Config parameter which gives the prefix of node initializers. An arbitrary
44 * number of node initializers can be specified (Along with their parameters).
45 * These will be applied
46 * on the newly created nodes. The initializers are ordered according to
47 * alphabetical order if their ID.
50 control.0 DynamicNetwork
51 control.0.init.0 RandNI
53 control.0.init.0.protocol somelinkable
58 private static final String PAR_INIT = "init";
61 * Nodes are added until the size specified by this parameter is reached. The
62 * network will never exceed this size as a result of this class.
63 * If not set, there will be no limit on the size of the network.
66 private static final String PAR_MAX = "maxsize";
69 * Nodes are removed until the size specified by this parameter is reached. The
70 * network will never go below this size as a result of this class.
74 private static final String PAR_MIN = "minsize";
77 * Config parameter used to define the length of one period of the oscillation.
78 * The network size will be the function of time, parameterized by this
79 * parameter. The size function is
80 * <code>avg+sin(time*pi/{@value #PAR_PERIOD})*ampl</code> where
81 * <code>avg=({@value #PAR_MAX}+{@value #PAR_MIN})/2</code> and
82 * <code>ampl=({@value #PAR_MAX}-{@value #PAR_MIN})/2</code>.
85 private static final String PAR_PERIOD = "period";
88 //--------------------------------------------------------------------------
90 //--------------------------------------------------------------------------
93 private final int period;
96 private final int minsize;
99 private final int maxsize;
101 /** New nodes initializers */
102 private final NodeInitializer[] inits;
105 //--------------------------------------------------------------------------
107 //--------------------------------------------------------------------------
110 * Standard constructor that reads the configuration parameters. Invoked by the
113 * the configuration prefix for this class
115 public OscillatingNetwork(String prefix)
118 period = Configuration.getInt(prefix + "." + PAR_PERIOD);
120 Configuration.getInt(
121 prefix + "." + PAR_MAX,
123 minsize = Configuration.getInt(prefix + "." + PAR_MIN, 0);
125 Object[] tmp = Configuration.getInstanceArray(prefix + "." + PAR_INIT);
126 inits = new NodeInitializer[tmp.length];
127 for (int i = 0; i < tmp.length; ++i)
129 inits[i] = (NodeInitializer) tmp[i];
133 //--------------------------------------------------------------------------
135 //--------------------------------------------------------------------------
138 * Adds n nodes to the network. Extending classes can implement any algorithm to
139 * do that. The default algorithm adds the given number of nodes after calling
140 * all the configured initializers on them.
143 * the number of nodes to add, must be non-negative.
145 protected void add(int n)
147 for (int i = 0; i < n; ++i) {
148 Node newnode = (Node) Network.prototype.clone();
149 for (int j = 0; j < inits.length; ++j) {
150 inits[j].initialize(newnode);
152 Network.add(newnode);
156 // ------------------------------------------------------------------
159 * Removes n nodes from the network. Extending classes can implement any
160 * algorithm to do that. The default algorithm removes <em>random</em>
161 * nodes <em>permanently</em> simply by calling {@link Network#remove(int)}.
162 * @param n the number of nodes to remove
164 protected void remove(int n)
166 for (int i = 0; i < n; ++i) {
167 Network.remove(CommonState.r.nextInt(Network.size()));
171 // ------------------------------------------------------------------
174 * Takes the current time and sets the network size according to a periodic
176 * The size function is
177 * <code>avg+sin(time*pi/{@value #PAR_PERIOD})*ampl</code> where
178 * <code>avg=({@value #PAR_MAX}+{@value #PAR_MIN})/2</code> and
179 * <code>ampl=({@value #PAR_MAX}-{@value #PAR_MIN})/2</code>.
180 * Calls {@link #add(int)} or {@link #remove} depending on whether the size
181 * needs to be increased or decreased to get the desired size.
182 * @return always false
184 public boolean execute()
186 long time = CommonState.getTime();
187 int amplitude = (maxsize - minsize) / 2;
188 int newsize = (maxsize + minsize) / 2 +
189 (int) (Math.sin(((double) time) / period * Math.PI) *
191 int diff = newsize - Network.size();