Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / edsim / RegRandNextCycle.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18
19 package peersim.edsim;
20
21 import peersim.core.*;
22
23
24 /**
25 * Implements a random delay, but making sure there is exactly one call in each
26 * consecutive <code>step</code> time units.
27 */
28 public class RegRandNextCycle extends NextCycleEvent {
29
30 // ============================== fields ==============================
31 // ====================================================================
32
33 /**
34 * Indicates the start of the next cycle for a particular protocol
35 * instance. If negative it means it has not been initialized yet.
36 */
37 private long nextCycleStart = -1;
38
39 // =============================== initialization ======================
40 // =====================================================================
41
42
43 /**
44 * Calls super constructor.
45 */
46 public RegRandNextCycle(String n) {
47
48         super(n);
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Calls super.clone().
55 */
56 public Object clone() throws CloneNotSupportedException {
57         
58         return super.clone();
59 }
60
61
62 // ========================== methods ==================================
63 // =====================================================================
64
65
66 /**
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.
71 */
72 protected long nextDelay(long step) {
73         
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)
77         
78         final long now = CommonState.getTime();
79         if(nextCycleStart<0)
80         {
81                 // not initialized
82                 nextCycleStart=now+step;
83         }
84         
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;
88         
89         // we increment nextCycleStart to point to the start of the cycle
90         // after the next cycle
91         nextCycleStart+=step;
92         
93         return nextCycleStart-now-CommonState.r.nextLong(step)-1;
94 }
95
96 }
97
98