Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / cdsim / CDState.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.cdsim;
20
21 import peersim.core.CommonState;
22
23
24 /**
25  * This is the common state of a cycle driven simulation that all objects see.
26  * It contains additional information, specific to the cycle driven model,
27  * in addition to the info in {@link peersim.core.CommonState}.
28  */
29 public class CDState extends CommonState {
30
31
32 // ======================= fields ==================================
33 // =================================================================
34
35 /**
36  * Current time within the current cycle.
37  * Note that {@link #cycle} gives the cycle id to which this value is relative.
38  */
39 private static int ctime = -1;
40
41 /**
42  * Current cycle in the simulation. It makes sense only in the case of a
43  * cycle based simulator, that is, cycle based simulators will maintain this
44  * value, others will not. It still makes sense to keep it separate from
45  * {@link #time} because it is an int, while time is a long.
46  */
47 private static int cycle = -1;
48
49
50 // ======================== initialization =========================
51 // =================================================================
52
53
54 static {}
55
56 /** to avoid construction */
57 private CDState() {}
58
59 // ======================= methods =================================
60 // =================================================================
61
62
63 /**
64 * Returns true if and only if there is a cycle driven simulation going on.
65 */
66 public static boolean isCD() { return cycle >= 0; }
67
68 //-----------------------------------------------------------------
69
70 /**
71  * Returns the current cycle.
72  * Note that {@link #getTime()} returns the same value.
73  * @throws UnsupportedOperationException if no cycle-driven state is available
74  */
75 public static int getCycle()
76 {
77         if( cycle >= 0 ) return cycle;
78         else throw new UnsupportedOperationException(
79                 "Cycle driven state accessed when "+
80                 "no cycle state information is available.");
81 }
82
83 //-----------------------------------------------------------------
84
85 /**
86  * Sets current cycle. Resets also cycle time to 0. It also calls
87  * {@link #setTime(long)} with the given parameter, to make sure 
88  * {@link #getTime()} is indeed independent of the simulation model.
89  */
90 public static void setCycle(int t)
91 {
92         cycle = t;
93         ctime = 0;
94         setTime(t);
95 }
96
97 //-----------------------------------------------------------------
98
99 /**
100  * Returns current cycle as an Integer object.
101  * @throws UnsupportedOperationException if no cycle-driven state is available
102  */
103 public static Integer getCycleObj()
104 {
105         if( cycle >= 0 ) return Integer.valueOf(cycle);
106         else throw new UnsupportedOperationException(
107                 "Cycle driven state accessed when "+
108                 "no cycle state information is available.");
109 }
110
111 //-----------------------------------------------------------------
112
113 /**
114  * Returns the current time within the current cycle.
115  * Note that the time returned by {@link #getCycle} is the cycle id
116  * in this case. In other words, it returns the number of nodes that have
117  * already been visited in a given cycle.
118  * @throws UnsupportedOperationException if no cycle-driven state is available
119  */
120 public static int getCycleT()
121 {
122         if( ctime >= 0 ) return ctime;
123         else throw new UnsupportedOperationException(
124                 "Cycle driven state accessed when "+
125                 "no cycle state information is available.");
126 }
127
128 // -----------------------------------------------------------------
129
130 public static void setCycleT(int t)
131 {
132         ctime = t;
133 }
134 }
135
136