Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / edsim / NextCycleEvent.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 import peersim.cdsim.CDProtocol;
23
24 /**
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.
31  *
32  * <p>
33  * Note that reimplementing method {@link #nextDelay} of this class allows for
34  * arbitrary scheduling, including adaptively changing or irregular cycle
35  * lengths, etc.
36  *
37  * @see CDScheduler
38  * @see CDProtocol
39  */
40 public class NextCycleEvent implements Cloneable {
41
42         // =============================== initialization ======================
43         // =====================================================================
44
45         /**
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>.
49          */
50         public NextCycleEvent(String n) {
51         }
52
53         // --------------------------------------------------------------------
54
55         /**
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).
60          */
61         public Object clone() throws CloneNotSupportedException {
62
63                 return super.clone();
64         }
65
66         // ========================== methods ==================================
67         // =====================================================================
68
69         /**
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
76          * event.
77          */
78         public final void execute() {
79
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);
85
86                 if (CommonState.getTime() + delay < CDScheduler.sch[pid].until)
87                         EDSimulator.add(delay, this, node, pid);
88         }
89
90         // --------------------------------------------------------------------
91
92         /**
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).
97          */
98         protected long nextDelay(long step) {
99
100                 return step;
101         }
102
103 }