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.
21 import org.simgrid.msg.Msg;
23 import peersim.Simulator;
24 import peersim.config.*;
25 import peersim.util.*;
26 import psgsim.PSGPlatform;
29 * This is the common state of the simulation all objects see.
30 * Static singleton. One of its purposes is
31 * simplification of parameter structures and increasing efficiency by putting
32 * state information here instead of passing parameters.
34 * <em>The set methods should not be used by applications</em>,
36 * components. Use them only if you know exactly what you are doing, e.g.
37 * if you are so advanced that you can write your own simulation engine.
38 * Ideally, they should not be visible, but due to the lack of more
39 * flexibility in java access rights, we are forced to make them public.
41 public class CommonState
44 //======================= constants ===============================
45 //=================================================================
48 * Constant that can be used as a value of simulation phase.
49 * It means that the simulation has finished.
52 public static final int POST_SIMULATION = 1;
55 * Constant that can be used as a value of simulation phase.
56 * It means that the simulation phase information has not been set (unknown).
59 public static final int PHASE_UNKNOWN = 0;
61 // ======================= fields ==================================
62 // =================================================================
65 * Current time. Note that this value is simulator independent, all simulation
66 * models have a notion related to time. For example, in the cycle based model,
67 * the cycle id gives time, while in even driven simulations there is a more
68 * realistic notion of time.
70 private static long time = 0;
73 * The maximal value {@link #time} can ever take.
75 private static long endtime = -1;
78 * Number of used bits in the long representation of time, calculated
79 * based on the endtime.
81 private static int toshift = -1;
84 * Information about where exactly the simulation is.
86 private static int phase = PHASE_UNKNOWN;
91 private static int pid;
96 private static Node node;
99 * This source of randomness should be used by all components.
100 * This field is public because it doesn't matter if it changes
101 * during an experiment (although it shouldn't) until no other sources of
102 * randomness are used within the system. Besides, we can save the cost
103 * of calling a wrapper method, which is important because this is needed
106 public static ExtendedRandom r = null;
109 // ======================== initialization =========================
110 // =================================================================
113 * Configuration parameter used to define which random generator
114 * class should be used. If not specified, the default implementation
115 * {@link ExtendedRandom} is used. User-specified random generators
116 * must extend class {@link ExtendedRandom}.
119 public static final String PAR_RANDOM = "random";
122 * Configuration parameter used to initialize the random seed.
123 * If it is not specified the current time is used.
126 public static final String PAR_SEED = "random.seed";
130 * Initializes the field {@link r} according to the configuration.
131 * Assumes that the configuration is already
137 Configuration.getLong(PAR_SEED,System.currentTimeMillis());
138 initializeRandom(seed);
142 /** Does nothing. To avoid construction but allow extension. */
143 protected CommonState() {}
145 // ======================= methods =================================
146 // =================================================================
150 * Returns current time. In event-driven simulations, returns the current
151 * time (a long-value).
152 * In cycle-driven simulations, returns the current cycle (a long that
153 * can safely be cast into an integer).
155 public static long getTime()
157 /* if the engine simulator used is PSG (simId=2 */
158 if(Simulator.getSimID()==2)
159 return (long) PSGPlatform.getTime();
164 //-----------------------------------------------------------------
167 * Returns current time in integer format. The purpose is to enhance the
168 * performance of protocols (ints are smaller and faster) when absolute
169 * precision is not required. It assumes that endtime has been set via
170 * {@link #setEndTime} by the simulation engine. It uses the endtime for
171 * the optimal mapping to get the maximal precision.
172 * In particular, in the cycle
173 * based model, time is the same as cycle which can be safely cast into
174 * integer, so no precision is lost.
176 public static int getIntTime()
178 return (int)(time>>toshift);
181 //-----------------------------------------------------------------
184 * Sets the current time.
186 public static void setTime(long t)
191 //-----------------------------------------------------------------
195 * It is the maximal value {@link #getTime} ever returns. If it's negative, it
196 * means the endtime is not known.
198 public static long getEndTime()
203 //-----------------------------------------------------------------
208 public static void setEndTime(long t)
211 throw new RuntimeException("You can set endtime only once");
213 throw new RuntimeException("No negative values are allowed");
216 toshift = 32-Long.numberOfLeadingZeros(t);
217 if( toshift<0 ) toshift = 0;
220 //-----------------------------------------------------------------
223 * Returns the simulation phase. Currently the following phases are
226 * <li>{@link #PHASE_UNKNOWN} phase is unknown</li>
227 * <li>{@link #POST_SIMULATION} the simulation is completed</li>
230 public static int getPhase()
235 // -----------------------------------------------------------------
237 public static void setPhase(int p)
242 // -----------------------------------------------------------------
245 * Returns the current protocol identifier. In other words, control is
246 * held by the indicated protocol on node {@link #getNode}.
248 public static int getPid()
253 //-----------------------------------------------------------------
255 /** Sets the current protocol identifier.*/
256 public static void setPid(int p)
261 //-----------------------------------------------------------------
264 * Returns the current node. When a protocol is executing, it is the node
265 * hosting the protocol.
267 public static Node getNode()
272 //-----------------------------------------------------------------
274 /** Sets the current node */
275 public static void setNode(Node n)
280 //-----------------------------------------------------------------
282 public static void initializeRandom(long seed)
285 r = (ExtendedRandom) Configuration.getInstance(PAR_RANDOM, new ExtendedRandom(seed));
290 //-----------------------------------------------------------------
293 public static void main(String pars[]) {
295 setEndTime(Long.parseLong(pars[0]));
296 setTime(Long.parseLong(pars[1]));
297 System.err.println(getTime()+" "+getIntTime());