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(
double
eventValue);
43
};
44
45
void
46
MyModel::Start
()
47
{
48
Simulator::Schedule
(
Seconds
(10.0), &
MyModel::HandleEvent
,
this
,
Simulator::Now
().GetSeconds());
49
}
50
51
void
52
MyModel::HandleEvent
(
double
value)
53
{
54
std::cout <<
"Member method received event at "
<<
Simulator::Now
().
GetSeconds
()
55
<<
"s started at "
<< value <<
"s"
<< std::endl;
56
}
57
58
/**
59
* Simple function event handler which Starts a MyModel object.
60
*
61
* \param [in] model The MyModel object to start.
62
*/
63
void
64
ExampleFunction
(
MyModel
* model)
65
{
66
std::cout <<
"ExampleFunction received event at "
<<
Simulator::Now
().
GetSeconds
() <<
"s"
67
<< std::endl;
68
model->
Start
();
69
}
70
71
/**
72
* Simple function event handler; this function is called randomly.
73
*/
74
void
75
RandomFunction
()
76
{
77
std::cout <<
"RandomFunction received event at "
<<
Simulator::Now
().
GetSeconds
() <<
"s"
78
<< std::endl;
79
}
80
81
/** Simple function event handler; the corresponding event is cancelled. */
82
void
83
CancelledEvent
()
84
{
85
std::cout <<
"I should never be called... "
<< std::endl;
86
}
87
88
}
// unnamed namespace
89
90
int
91
main(
int
argc,
char
* argv[])
92
{
93
CommandLine
cmd
(__FILE__);
94
cmd
.Parse(argc, argv);
95
96
MyModel model;
97
Ptr<UniformRandomVariable>
v =
CreateObject<UniformRandomVariable>
();
98
v->SetAttribute(
"Min"
,
DoubleValue
(10));
99
v->SetAttribute(
"Max"
,
DoubleValue
(20));
100
101
Simulator::Schedule
(
Seconds
(10.0), &
ExampleFunction
, &model);
102
103
Simulator::Schedule
(
Seconds
(v->GetValue()), &
RandomFunction
);
104
105
EventId
id
=
Simulator::Schedule
(
Seconds
(30.0), &
CancelledEvent
);
106
Simulator::Cancel
(
id
);
107
108
Simulator::Schedule
(
Seconds
(25.0), []() {
109
std::cout <<
"Code within a lambda expression at time "
<<
Simulator::Now
().
As
(
Time::S
)
110
<< std::endl;
111
});
112
113
Simulator::Run
();
114
115
Simulator::Destroy
();
116
117
return
0;
118
}
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(double eventValue)
Simple event handler.
Definition
sample-simulator.cc:52
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
mpi-test-fixtures.h:37
ns3::Simulator::Schedule
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition
simulator.h:560
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:274
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::Now
static Time Now()
Return the current simulation virtual time.
Definition
simulator.cc:197
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
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:404
ns3::Time::GetSeconds
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition
nstime.h:392
ns3::Time::S
@ S
second
Definition
nstime.h:105
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:619
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1308
anonymous_namespace{sample-simulator.cc}::CancelledEvent
void CancelledEvent()
Simple function event handler; the corresponding event is cancelled.
Definition
sample-simulator.cc:83
anonymous_namespace{sample-simulator.cc}::ExampleFunction
void ExampleFunction(MyModel *model)
Simple function event handler which Starts a MyModel object.
Definition
sample-simulator.cc:64
anonymous_namespace{sample-simulator.cc}::RandomFunction
void RandomFunction()
Simple function event handler; this function is called randomly.
Definition
sample-simulator.cc:75
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 Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0