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
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU 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 General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*
17
* Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
*/
19
20
#include "ns3/command-line.h"
21
#include "ns3/double.h"
22
#include "ns3/nstime.h"
23
#include "ns3/random-variable-stream.h"
24
#include "ns3/simulator.h"
25
26
#include <iostream>
27
35
using namespace
ns3
;
36
37
namespace
38
{
39
41
class
MyModel
42
{
43
public
:
45
void
Start();
46
47
private
:
53
void
HandleEvent(
double
eventValue);
54
};
55
56
void
57
MyModel::Start
()
58
{
59
Simulator::Schedule
(
Seconds
(10.0), &
MyModel::HandleEvent
,
this
,
Simulator::Now
().GetSeconds());
60
}
61
62
void
63
MyModel::HandleEvent
(
double
value)
64
{
65
std::cout <<
"Member method received event at "
<<
Simulator::Now
().
GetSeconds
()
66
<<
"s started at "
<< value <<
"s"
<< std::endl;
67
}
68
74
void
75
ExampleFunction
(
MyModel
* model)
76
{
77
std::cout <<
"ExampleFunction received event at "
<<
Simulator::Now
().
GetSeconds
() <<
"s"
78
<< std::endl;
79
model->
Start
();
80
}
81
85
void
86
RandomFunction
()
87
{
88
std::cout <<
"RandomFunction received event at "
<<
Simulator::Now
().
GetSeconds
() <<
"s"
89
<< std::endl;
90
}
91
93
void
94
CancelledEvent
()
95
{
96
std::cout <<
"I should never be called... "
<< std::endl;
97
}
98
99
}
// unnamed namespace
100
101
int
102
main(
int
argc,
char
* argv[])
103
{
104
CommandLine
cmd
(__FILE__);
105
cmd
.Parse(argc, argv);
106
107
MyModel model;
108
Ptr<UniformRandomVariable>
v = CreateObject<UniformRandomVariable>();
109
v->SetAttribute(
"Min"
,
DoubleValue
(10));
110
v->SetAttribute(
"Max"
,
DoubleValue
(20));
111
112
Simulator::Schedule
(
Seconds
(10.0), &
ExampleFunction
, &model);
113
114
Simulator::Schedule
(
Seconds
(v->GetValue()), &
RandomFunction
);
115
116
EventId
id
=
Simulator::Schedule
(
Seconds
(30.0), &
CancelledEvent
);
117
Simulator::Cancel
(
id
);
118
119
Simulator::Schedule
(
Seconds
(25.0), []() {
120
std::cout <<
"Code within a lambda expression at time "
<<
Simulator::Now
().
As
(
Time::S
)
121
<< std::endl;
122
});
123
124
Simulator::Run
();
125
126
Simulator::Destroy
();
127
128
return
0;
129
}
anonymous_namespace{sample-simulator.cc}::MyModel
Simple model object to illustrate event handling.
Definition:
sample-simulator.cc:42
anonymous_namespace{sample-simulator.cc}::MyModel::Start
void Start()
Start model execution by scheduling a HandleEvent.
Definition:
sample-simulator.cc:57
anonymous_namespace{sample-simulator.cc}::MyModel::HandleEvent
void HandleEvent(double eventValue)
Simple event handler.
Definition:
sample-simulator.cc:63
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:232
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition:
double.h:42
ns3::EventId
An identifier for simulation events.
Definition:
event-id.h:55
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:78
ns3::Simulator::Schedule
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition:
simulator.h:558
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:276
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:140
ns3::Simulator::Now
static Time Now()
Return the current simulation virtual time.
Definition:
simulator.cc:199
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition:
simulator.cc:176
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:415
ns3::Time::GetSeconds
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition:
nstime.h:402
ns3::Time::S
@ S
second
Definition:
nstime.h:116
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1325
anonymous_namespace{sample-simulator.cc}::CancelledEvent
void CancelledEvent()
Simple function event handler; the corresponding event is cancelled.
Definition:
sample-simulator.cc:94
anonymous_namespace{sample-simulator.cc}::ExampleFunction
void ExampleFunction(MyModel *model)
Simple function event handler which Starts a MyModel object.
Definition:
sample-simulator.cc:75
anonymous_namespace{sample-simulator.cc}::RandomFunction
void RandomFunction()
Simple function event handler; this function is called randomly.
Definition:
sample-simulator.cc:86
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
second.cmd
ns cmd
Definition:
second.py:33
src
core
examples
sample-simulator.cc
Generated on Wed Sep 27 2023 18:44:00 for ns-3 by
1.9.6