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 java.util.Arrays;
22 import peersim.config.Configuration;
23 import peersim.core.Node;
24 import peersim.core.Control;
27 * A protocol that is not really a protocol, but a trick to carry out all
28 * kinds of tasks during the simulation. Many users will probably not need it,
29 * but it is a nice way to e.g. run controls at any time, not only between cycles.
31 public class DaemonProtocol implements CDProtocol {
34 // ========================= fields =================================
35 // ==================================================================
39 * This is the prefix for network dynamism managers.
42 private static final String PAR_CTRL = "control";
45 * The controls will be run according to this frequency.
46 * It is interpreted within a cycle, in terms of cycle time
47 * ({@link CDState#getCycleT}). The first cycletime is 0.
51 private static final String PAR_STEP = "cstep";
53 // --------------------------------------------------------------------
55 private static Control[] controls=null;
57 private static int step;
59 // ========================= initialization =========================
60 // ==================================================================
63 public DaemonProtocol(String s)
65 step = Configuration.getInt(s+"."+PAR_STEP,1);
67 String[] names = Configuration.getNames(s+"."+PAR_CTRL);
68 controls = new Control[names.length];
69 for(int i=0; i<names.length; ++i)
71 controls[i]=(Control)Configuration.getInstance(names[i]);
73 System.err.println(s+": loaded controls "+Arrays.asList(names));
76 // ------------------------------------------------------------------
78 public Object clone() {
80 DaemonProtocol ip = null;
81 try { ip=(DaemonProtocol)super.clone(); }
82 catch( CloneNotSupportedException e ) {} // never happens
87 // ========================= methods =================================
88 // ===================================================================
92 * Runs the configured controls if {@link CDState#getCycleT} %
93 * {@value #PAR_STEP}=0.
95 public void nextCycle( Node node, int protocolID ) {
97 if( CDState.getCycleT() % step != 0 ) return;
98 for(int j=0; j<controls.length; ++j) controls[j].execute();