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.cdsim;
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.util.RandPermutation;
26 * Control to run a cycle of the cycle driven simulation.
27 * This does not need to be explicitly configured (although you can do it for
30 public class FullNextCycle implements Control {
33 // ============== fields ===============================================
34 // =====================================================================
38 * The type of the getPair function. This parameter is of historic interest and
39 * was needed in a publication we wrote. You don't need to care about this.
40 * But if you wanna know: if set to "rand", then in a cycle the simulator
41 * does not simply iterate through the nodes, but instead picks a random one
42 * N times, where N is the network size.
45 private static final String PAR_GETPAIR = "getpair";
48 * Shuffle iteration order if set. Not set by default. If set, then nodes are
49 * iterated in a random order. However, in the network the nodes actually
50 * stay in the order they originally were. The price for leaving the
51 * network untouched is memory: we need to store the permutation we use
52 * to iterate the network.
55 private static final String PAR_SHUFFLE = "shuffle";
57 // --------------------------------------------------------------------
59 protected final boolean getpair_rand;
61 protected final boolean shuffle;
63 /** Holds the protocol schedulers of this simulation */
64 protected Scheduler[] protSchedules = null;
66 /** The random permutation to use if config par {@value #PAR_SHUFFLE} is set. */
67 protected RandPermutation rperm = new RandPermutation( CDState.r );
69 // =============== initialization ======================================
70 // =====================================================================
73 * Reads config parameters and {@link Scheduler}s.
75 public FullNextCycle(String prefix) {
77 getpair_rand = Configuration.contains(prefix+"."+PAR_GETPAIR);
78 shuffle = Configuration.contains(prefix+"."+PAR_SHUFFLE);
80 // load protocol schedulers
81 String[] names = Configuration.getNames(Node.PAR_PROT);
82 protSchedules = new Scheduler[names.length];
83 for(int i=0; i<names.length; ++i)
85 protSchedules[i] = new Scheduler(names[i]);
89 // =============== methods =============================================
90 // =====================================================================
93 * Execute all the {@link CDProtocol}s on all nodes that are up.
94 * If the node goes down as a result of the execution of a protocol, then
95 * the rest of the protocols on that node are not executed and we move on
97 * It sets the {@link CDState} appropriately.
98 * @return always false
100 public boolean execute() {
102 final int cycle=CDState.getCycle();
103 if( shuffle ) rperm.reset( Network.size() );
104 for(int j=0; j<Network.size(); ++j)
108 node = Network.get(CDState.r.nextInt(Network.size()));
110 node = Network.get(rperm.next());
112 node = Network.get(j);
113 if( !node.isUp() ) continue;
114 CDState.setNode(node);
115 CDState.setCycleT(j);
116 final int len = node.protocolSize();
117 for(int k=0; k<len; ++k)
119 // Check if the protocol should be executed, given the
120 // associated scheduler.
121 if (!protSchedules[k].active(cycle))
125 Protocol protocol = node.getProtocol(k);
126 if( protocol instanceof CDProtocol )
128 ((CDProtocol)protocol).nextCycle(node, k);
129 if( !node.isUp() ) break;