A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
simulator.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2005,2006 INRIA
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
#include "ns3/core-config.h"
21
#include "
simulator.h
"
22
#include "
simulator-impl.h
"
23
#include "
scheduler.h
"
24
#include "
map-scheduler.h
"
25
#include "
event-impl.h
"
26
27
#include "
ptr.h
"
28
#include "
string.h
"
29
#include "
object-factory.h
"
30
#include "
global-value.h
"
31
#include "
assert.h
"
32
#include "
log.h
"
33
34
#include <cmath>
35
#include <fstream>
36
#include <list>
37
#include <vector>
38
#include <iostream>
39
40
// Note: Logging in this file is largely avoided due to the
41
// number of calls that are made to these functions and the possibility
42
// of causing recursions leading to stack overflow
43
44
NS_LOG_COMPONENT_DEFINE
(
"Simulator"
);
45
46
namespace
ns3 {
47
48
GlobalValue
g_simTypeImpl
=
GlobalValue
(
"SimulatorImplementationType"
,
49
"The object class to use as the simulator implementation"
,
50
StringValue
(
"ns3::DefaultSimulatorImpl"
),
51
MakeStringChecker ());
52
53
GlobalValue
g_schedTypeImpl
=
GlobalValue
(
"SchedulerType"
,
54
"The object class to use as the scheduler implementation"
,
55
TypeIdValue
(
MapScheduler::GetTypeId
()),
56
MakeTypeIdChecker ());
57
58
static
void
59
TimePrinter
(std::ostream &os)
60
{
61
os <<
Simulator::Now
().
GetSeconds
() <<
"s"
;
62
}
63
64
static
void
65
NodePrinter
(std::ostream &os)
66
{
67
if
(
Simulator::GetContext
() == 0xffffffff)
68
{
69
os <<
"-1"
;
70
}
71
else
72
{
73
os <<
Simulator::GetContext
();
74
}
75
}
76
77
static
SimulatorImpl
**
PeekImpl
(
void
)
78
{
79
static
SimulatorImpl
*impl = 0;
80
return
&impl;
81
}
82
83
static
SimulatorImpl
*
GetImpl
(
void
)
84
{
85
SimulatorImpl
**pimpl =
PeekImpl
();
86
/* Please, don't include any calls to logging macros in this function
87
* or pay the price, that is, stack explosions.
88
*/
89
if
(*pimpl == 0)
90
{
91
{
92
ObjectFactory
factory;
93
StringValue
s
;
94
95
g_simTypeImpl
.
GetValue
(s);
96
factory.
SetTypeId
(s.
Get
());
97
*pimpl =
GetPointer
(factory.
Create
<
SimulatorImpl
> ());
98
}
99
{
100
ObjectFactory
factory;
101
StringValue
s
;
102
g_schedTypeImpl
.
GetValue
(s);
103
factory.
SetTypeId
(s.
Get
());
104
(*pimpl)->SetScheduler (factory);
105
}
106
107
//
108
// Note: we call LogSetTimePrinter _after_ creating the implementation
109
// object because the act of creation can trigger calls to the logging
110
// framework which would call the TimePrinter function which would call
111
// Simulator::Now which would call Simulator::GetImpl, and, thus, get us
112
// in an infinite recursion until the stack explodes.
113
//
114
LogSetTimePrinter
(&
TimePrinter
);
115
LogSetNodePrinter
(&
NodePrinter
);
116
}
117
return
*pimpl;
118
}
119
120
void
121
Simulator::Destroy
(
void
)
122
{
123
NS_LOG_FUNCTION_NOARGS
();
124
125
SimulatorImpl
**pimpl =
PeekImpl
();
126
if
(*pimpl == 0)
127
{
128
return
;
129
}
130
/* Note: we have to call LogSetTimePrinter (0) below because if we do not do
131
* this, and restart a simulation after this call to Destroy, (which is
132
* legal), Simulator::GetImpl will trigger again an infinite recursion until
133
* the stack explodes.
134
*/
135
LogSetTimePrinter
(0);
136
LogSetNodePrinter
(0);
137
(*pimpl)->Destroy ();
138
(*pimpl)->Unref ();
139
*pimpl = 0;
140
}
141
142
void
143
Simulator::SetScheduler
(
ObjectFactory
schedulerFactory)
144
{
145
NS_LOG_FUNCTION
(schedulerFactory);
146
GetImpl
()->
SetScheduler
(schedulerFactory);
147
}
148
149
bool
150
Simulator::IsFinished
(
void
)
151
{
152
NS_LOG_FUNCTION_NOARGS
();
153
return
GetImpl
()->
IsFinished
();
154
}
155
156
void
157
Simulator::Run
(
void
)
158
{
159
NS_LOG_FUNCTION_NOARGS
();
160
Time::ClearMarkedTimes
();
161
GetImpl
()->
Run
();
162
}
163
164
void
165
Simulator::Stop
(
void
)
166
{
167
NS_LOG_FUNCTION_NOARGS
();
168
NS_LOG_LOGIC
(
"stop"
);
169
GetImpl
()->
Stop
();
170
}
171
172
void
173
Simulator::Stop
(
Time
const
&time)
174
{
175
NS_LOG_FUNCTION
(time);
176
GetImpl
()->
Stop
(time);
177
}
178
179
Time
180
Simulator::Now
(
void
)
181
{
182
/* Please, don't include any calls to logging macros in this function
183
* or pay the price, that is, stack explosions.
184
*/
185
return
GetImpl
()->
Now
();
186
}
187
188
Time
189
Simulator::GetDelayLeft
(
const
EventId
&
id
)
190
{
191
NS_LOG_FUNCTION
(&
id
);
192
return
GetImpl
()->
GetDelayLeft
(
id
);
193
}
194
195
EventId
196
Simulator::Schedule
(
Time
const
&time,
const
Ptr<EventImpl>
&ev)
197
{
198
return
DoSchedule
(time,
GetPointer
(ev));
199
}
200
201
EventId
202
Simulator::ScheduleNow
(
const
Ptr<EventImpl>
&ev)
203
{
204
return
DoScheduleNow
(
GetPointer
(ev));
205
}
206
void
207
Simulator::ScheduleWithContext
(uint32_t context,
const
Time
&time,
EventImpl
*impl)
208
{
209
return
GetImpl
()->
ScheduleWithContext
(context, time, impl);
210
}
211
EventId
212
Simulator::ScheduleDestroy
(
const
Ptr<EventImpl>
&ev)
213
{
214
return
DoScheduleDestroy
(
GetPointer
(ev));
215
}
216
EventId
217
Simulator::DoSchedule
(
Time
const
&time,
EventImpl
*impl)
218
{
219
return
GetImpl
()->
Schedule
(time, impl);
220
}
221
EventId
222
Simulator::DoScheduleNow
(
EventImpl
*impl)
223
{
224
return
GetImpl
()->
ScheduleNow
(impl);
225
}
226
EventId
227
Simulator::DoScheduleDestroy
(
EventImpl
*impl)
228
{
229
return
GetImpl
()->
ScheduleDestroy
(impl);
230
}
231
232
233
EventId
234
Simulator::Schedule
(
Time
const
&time,
void
(*f)(
void
))
235
{
236
return
DoSchedule
(time,
MakeEvent
(f));
237
}
238
239
void
240
Simulator::ScheduleWithContext
(uint32_t context,
Time
const
&time,
void
(*f)(
void
))
241
{
242
return
ScheduleWithContext
(context, time,
MakeEvent
(f));
243
}
244
245
EventId
246
Simulator::ScheduleNow
(
void
(*f)(
void
))
247
{
248
return
DoScheduleNow
(
MakeEvent
(f));
249
}
250
251
EventId
252
Simulator::ScheduleDestroy
(
void
(*f)(
void
))
253
{
254
return
DoScheduleDestroy
(
MakeEvent
(f));
255
}
256
257
void
258
Simulator::Remove
(
const
EventId
&ev)
259
{
260
if
(*
PeekImpl
() == 0)
261
{
262
return
;
263
}
264
return
GetImpl
()->
Remove
(ev);
265
}
266
267
void
268
Simulator::Cancel
(
const
EventId
&ev)
269
{
270
if
(*
PeekImpl
() == 0)
271
{
272
return
;
273
}
274
return
GetImpl
()->
Cancel
(ev);
275
}
276
277
bool
278
Simulator::IsExpired
(
const
EventId
&
id
)
279
{
280
if
(*
PeekImpl
() == 0)
281
{
282
return
true
;
283
}
284
return
GetImpl
()->
IsExpired
(
id
);
285
}
286
287
Time
Now
(
void
)
288
{
289
return
Time
(
Simulator::Now
());
290
}
291
292
Time
293
Simulator::GetMaximumSimulationTime
(
void
)
294
{
295
NS_LOG_FUNCTION_NOARGS
();
296
return
GetImpl
()->
GetMaximumSimulationTime
();
297
}
298
299
uint32_t
300
Simulator::GetContext
(
void
)
301
{
302
return
GetImpl
()->
GetContext
();
303
}
304
305
uint32_t
306
Simulator::GetSystemId
(
void
)
307
{
308
NS_LOG_FUNCTION_NOARGS
();
309
310
if
(*
PeekImpl
() != 0)
311
{
312
return
GetImpl
()->
GetSystemId
();
313
}
314
else
315
{
316
return
0;
317
}
318
}
319
320
void
321
Simulator::SetImplementation
(
Ptr<SimulatorImpl>
impl)
322
{
323
NS_LOG_FUNCTION
(impl);
324
if
(*
PeekImpl
() != 0)
325
{
326
NS_FATAL_ERROR
(
"It is not possible to set the implementation after calling any Simulator:: function. Call Simulator::SetImplementation earlier or after Simulator::Destroy."
);
327
}
328
*
PeekImpl
() =
GetPointer
(impl);
329
// Set the default scheduler
330
ObjectFactory
factory;
331
StringValue
s
;
332
g_schedTypeImpl
.
GetValue
(s);
333
factory.
SetTypeId
(s.
Get
());
334
impl->
SetScheduler
(factory);
335
//
336
// Note: we call LogSetTimePrinter _after_ creating the implementation
337
// object because the act of creation can trigger calls to the logging
338
// framework which would call the TimePrinter function which would call
339
// Simulator::Now which would call Simulator::GetImpl, and, thus, get us
340
// in an infinite recursion until the stack explodes.
341
//
342
LogSetTimePrinter
(&
TimePrinter
);
343
LogSetNodePrinter
(&
NodePrinter
);
344
}
345
Ptr<SimulatorImpl>
346
Simulator::GetImplementation
(
void
)
347
{
348
NS_LOG_FUNCTION_NOARGS
();
349
return
GetImpl
();
350
}
351
352
353
354
}
// namespace ns3
355
src
core
model
simulator.cc
Generated on Fri Aug 30 2013 01:42:47 for ns-3 by
1.8.1.2