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.*;
25 * It generalizes its superclass so that the list of protocols to run can
26 * be specified. The superclass ({@link FullNextCycle}) always runs all the
27 * {@link CDProtocol}s.
29 public class NextCycle extends FullNextCycle {
32 // ============== fields ===============================================
33 // =====================================================================
37 * Gives the list of protocols (whitespace separated) that need to be
41 private static final String PAR_PROTS = "protocol";
43 private final int[] pids;
46 // =============== initialization ======================================
47 // =====================================================================
50 * reads configuration parameters and the {@link Scheduler}s.
52 public NextCycle(String prefix) {
56 String prots = Configuration.getString(prefix+"."+PAR_PROTS);
57 String[] protnames = prots.split("\\s");
58 pids = new int[protnames.length];
59 for(int i=0; i<protnames.length; ++i)
61 pids[i] = Configuration.lookupPid(protnames[i]);
65 // =============== methods =============================================
66 // =====================================================================
69 * Execute the configured protocols on all nodes.
70 * It works exactly as {@link FullNextCycle#execute}, only just the configured
71 * protocols are iterated over.
73 public boolean execute() {
75 final int cycle=CDState.getCycle();
76 if( shuffle ) rperm.reset( Network.size() );
77 for(int j=0; j<Network.size(); ++j)
81 node = Network.get(CDState.r.nextInt(Network.size()));
83 node = Network.get(rperm.next());
85 node = Network.get(j);
86 if( !node.isUp() ) continue;
87 CDState.setNode(node);
91 // Check if the protocol should be executed, given the
92 // associated scheduler.
93 if (!protSchedules[pid].active(cycle))
97 Protocol protocol = node.getProtocol(pid);
98 if( protocol instanceof CDProtocol )
100 ((CDProtocol)protocol).nextCycle(node, pid);
101 if( !node.isUp() ) break;