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.*;
25 * Implements a random delay, but making sure there is exactly one call in each
26 * consecutive <code>step</code> time units.
28 public class RegRandNextCycle extends NextCycleEvent {
30 // ============================== fields ==============================
31 // ====================================================================
34 * Indicates the start of the next cycle for a particular protocol
35 * instance. If negative it means it has not been initialized yet.
37 private long nextCycleStart = -1;
39 // =============================== initialization ======================
40 // =====================================================================
44 * Calls super constructor.
46 public RegRandNextCycle(String n) {
51 // --------------------------------------------------------------------
54 * Calls super.clone().
56 public Object clone() throws CloneNotSupportedException {
62 // ========================== methods ==================================
63 // =====================================================================
67 * Returns a random delay but making sure there is exactly one invocation in each
68 * consecutive interval of length <code>step</code>. The beginning of these
69 * intervals is defined by the first invocation which is in turn defined by
70 * {@link CDScheduler} that initiates the protocol in question.
72 protected long nextDelay(long step) {
74 // at this point nextCycleStart points to the start of the next cycle
75 // (the cycle after the one in which this execution is taking place)
76 // (note that the start of the cycle is included in the cycle)
78 final long now = CommonState.getTime();
82 nextCycleStart=now+step;
85 // to be on the safe side, we do the next while loop.
86 // although currently it never executes
87 while(nextCycleStart<=now) nextCycleStart+=step;
89 // we increment nextCycleStart to point to the start of the cycle
90 // after the next cycle
93 return nextCycleStart-now-CommonState.r.nextLong(step)-1;