A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
sample-simulator.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2010 INRIA
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7
*/
8
9
#include "ns3/command-line.h"
10
#include "ns3/double.h"
11
#include "ns3/nstime.h"
12
#include "ns3/random-variable-stream.h"
13
#include "ns3/simulator.h"
14
15
#include <iostream>
16
17
/**
18
* @file
19
* @ingroup core-examples
20
* @ingroup simulator
21
* Example program demonstrating use of various Schedule functions.
22
*/
23
24
using namespace
ns3
;
25
26
namespace
27
{
28
29
/** Simple model object to illustrate event handling. */
30
class
MyModel
31
{
32
public
:
33
/** Start model execution by scheduling a HandleEvent. */
34
void
Start
();
35
36
private
:
37
/**
38
* Simple event handler.
39
*
40
* @param [in] eventValue Event argument.
41
*/
42
void
HandleEvent
(
Time
eventValue);
43
};
44
45
void
46
MyModel::Start
()
47
{
48
Simulator::Schedule
(
Seconds
(10), &
MyModel::HandleEvent
,
this
,
Simulator::Now
());
49
50
Simulator::Schedule
(
Seconds
(11), []() {
51
std::cout <<
"Lambda scheduled from within a class method at "
52
<<
Simulator::Now
().
As
(
Time::S
) << std::endl;
53
});
54
}
55
56
void
57
MyModel::HandleEvent
(
Time
value)
58
{
59
std::cout <<
"Member method received event at "
<<
Simulator::Now
().
As
(
Time::S
)
60
<<
" started at "
<< value.As(
Time::S
) << std::endl;
61
}
62
63
/**
64
* Simple function event handler which Starts a MyModel object.
65
*
66
* @param [in] model The MyModel object to start.
67
*/
68
void
69
ExampleFunction
(
MyModel
* model)
70
{
71
std::cout <<
"ExampleFunction received event at "
<<
Simulator::Now
().
As
(
Time::S
) << std::endl;
72
model->
Start
();
73
}
74
75
/**
76
* Simple function event handler; this function is called randomly.
77
*/
78
void
79
RandomFunction
()
80
{
81
std::cout <<
"RandomFunction received event at "
<<
Simulator::Now
().
As
(
Time::S
) << std::endl;
82
}
83
84
/** Simple function event handler; the corresponding event is cancelled. */
85
void
86
CancelledEvent
()
87
{
88
std::cout <<
"I should never be called... "
<< std::endl;
89
}
90
91
}
// unnamed namespace
92
93
int
94
main(
int
argc,
char
* argv[])
95
{
96
CommandLine
cmd
(__FILE__);
97
cmd
.Parse(argc, argv);
98
99
MyModel model;
100
Ptr<UniformRandomVariable>
v
=
CreateObject<UniformRandomVariable>
();
101
v
->SetAttribute(
"Min"
,
DoubleValue
(10));
102
v
->SetAttribute(
"Max"
,
DoubleValue
(20));
103
104
Simulator::Schedule
(
Seconds
(10), &
ExampleFunction
, &model);
105
106
Simulator::Schedule
(
Seconds
(
v
->GetValue()), &
RandomFunction
);
107
108
EventId
id
=
Simulator::Schedule
(
Seconds
(30), &
CancelledEvent
);
109
Simulator::Cancel
(
id
);
110
111
Simulator::Schedule
(
Seconds
(25), []() {
112
std::cout <<
"Code within a lambda expression at time "
<<
Simulator::Now
().
As
(
Time::S
)
113
<< std::endl;
114
});
115
116
Simulator::Run
();
117
118
Simulator::Destroy
();
119
120
return
0;
121
}
v
uint32_t v
Definition
cairo-wideint.c:749
anonymous_namespace{sample-simulator.cc}::MyModel
Simple model object to illustrate event handling.
Definition
sample-simulator.cc:31
anonymous_namespace{sample-simulator.cc}::MyModel::Start
void Start()
Start model execution by scheduling a HandleEvent.
Definition
sample-simulator.cc:46
anonymous_namespace{sample-simulator.cc}::MyModel::HandleEvent
void HandleEvent(Time eventValue)
Simple event handler.
Definition
sample-simulator.cc:57
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition
double.h:31
ns3::EventId
An identifier for simulation events.
Definition
event-id.h:45
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:70
ns3::Simulator::Schedule
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition
simulator.h:580
ns3::Simulator::Cancel
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition
simulator.cc:268
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:125
ns3::Simulator::Now
static Time Now()
Return the current simulation virtual time.
Definition
simulator.cc:191
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:161
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition
nstime.h:95
ns3::Time::As
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition
time.cc:408
ns3::Time::S
@ S
second
Definition
nstime.h:106
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:627
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1273
anonymous_namespace{sample-simulator.cc}::CancelledEvent
void CancelledEvent()
Simple function event handler; the corresponding event is cancelled.
Definition
sample-simulator.cc:86
anonymous_namespace{sample-simulator.cc}::ExampleFunction
void ExampleFunction(MyModel *model)
Simple function event handler which Starts a MyModel object.
Definition
sample-simulator.cc:69
anonymous_namespace{sample-simulator.cc}::RandomFunction
void RandomFunction()
Simple function event handler; this function is called randomly.
Definition
sample-simulator.cc:79
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
second.cmd
cmd
Definition
second.py:29
src
core
examples
sample-simulator.cc
Generated on
for ns-3 by
1.15.0