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.edsim;
21 import peersim.core.*;
22 import peersim.cdsim.CDProtocol;
25 * This class is used to wrap a {@link CDProtocol} instance into an event so
26 * that it can be used in the event based simulation engine. This class is
27 * responsible for calling {@link CDProtocol#nextCycle} and also to schedule the
28 * consecutive cycle. In the configuration of an event driven simulation
29 * {@link CDProtocol}s can be configured using {@link CDScheduler}, which places
30 * appropriate instances of this events in the queue.
33 * Note that reimplementing method {@link #nextDelay} of this class allows for
34 * arbitrary scheduling, including adaptively changing or irregular cycle
40 public class NextCycleEvent implements Cloneable {
42 // =============================== initialization ======================
43 // =====================================================================
46 * Reads configuration to initialize the object. Extending classes should
47 * have a constructor with the same signature, often as simple as
48 * <code>super(n)</code>.
50 public NextCycleEvent(String n) {
53 // --------------------------------------------------------------------
56 * Returns a clone of the object. Overriding this method is necessary and
57 * typically is as simple as <code>return super.clone()</code>. In general,
58 * always use <code>super.clone()</code> to obtain the object to be returned
59 * on which you can perform optional deep cloning operations (arrays, etc).
61 public Object clone() throws CloneNotSupportedException {
66 // ========================== methods ==================================
67 // =====================================================================
70 * Executes the nextCycle method of the protocol, and schedules the next
71 * call using the delay returned by {@link #nextDelay}. If the next
72 * execution time as defined by the delay is outside of the valid times as
73 * defined by {@link CDScheduler#sch}, then the next event is not scheduled.
74 * Note that this means that this protocol will no longer be scheduled
75 * because the next event after the next event is scheduled by the next
78 public final void execute() {
80 int pid = CommonState.getPid();
81 Node node = CommonState.getNode();
82 CDProtocol cdp = (CDProtocol) node.getProtocol(pid);
83 cdp.nextCycle(node, pid);
84 long delay = nextDelay(CDScheduler.sch[pid].step);
86 if (CommonState.getTime() + delay < CDScheduler.sch[pid].until)
87 EDSimulator.add(delay, this, node, pid);
90 // --------------------------------------------------------------------
93 * Calculates the delay until the next execution of the protocol. This
94 * default implementation returns a constant delay equal to the step
95 * parameter (cycle length in this case) of the schedule of this event (as
96 * set in the config file).
98 protected long nextDelay(long step) {