A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
core
examples
sample-simulator.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2010 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
* Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
21
#include <iostream>
22
#include "ns3/simulator.h"
23
#include "ns3/nstime.h"
24
#include "ns3/command-line.h"
25
#include "ns3/double.h"
26
#include "ns3/random-variable-stream.h"
27
35
using namespace
ns3
;
36
37
namespace
{
38
40
class
MyModel
41
{
42
public
:
44
void
Start (
void
);
45
private
:
51
void
HandleEvent (
double
eventValue);
52
};
53
54
void
55
MyModel::Start (
void
)
56
{
57
Simulator::Schedule
(
Seconds
(10.0),
58
&MyModel::HandleEvent,
59
this
,
Simulator::Now
().GetSeconds ());
60
}
61
void
62
MyModel::HandleEvent (
double
value)
63
{
64
std::cout <<
"Member method received event at "
65
<<
Simulator::Now
().
GetSeconds
()
66
<<
"s started at "
<< value <<
"s"
<< std::endl;
67
}
68
74
static
void
75
ExampleFunction
(
MyModel
*model)
76
{
77
std::cout <<
"ExampleFunction received event at "
78
<<
Simulator::Now
().
GetSeconds
() <<
"s"
<< std::endl;
79
model->
Start
();
80
}
81
85
static
void
86
RandomFunction
(
void
)
87
{
88
std::cout <<
"RandomFunction received event at "
89
<<
Simulator::Now
().
GetSeconds
() <<
"s"
<< std::endl;
90
}
91
93
static
void
94
CancelledEvent
(
void
)
95
{
96
std::cout <<
"I should never be called... "
<< std::endl;
97
}
98
99
}
// unnamed namespace
100
101
102
int
main (
int
argc,
char
*argv[])
103
{
104
CommandLine
cmd
;
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::Run
();
120
121
Simulator::Destroy
();
122
}
anonymous_namespace{sample-simulator.cc}::CancelledEvent
static void CancelledEvent(void)
Simple function event handler; the corresponding event is cancelled.
Definition:
sample-simulator.cc:94
ns3::Ptr< UniformRandomVariable >
anonymous_namespace{sample-simulator.cc}::MyModel
Simple model object to illustrate event handling.
Definition:
sample-simulator.cc:40
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition:
nstime.h:355
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition:
simulator.cc:226
second.cmd
cmd
Definition:
second.py:35
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:346
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition:
simulator.h:1381
anonymous_namespace{sample-simulator.cc}::MyModel::Start
void Start(void)
Start model execution by scheduling a HandleEvent.
Definition:
sample-simulator.cc:55
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:213
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:190
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::UniformRandomVariable::GetValue
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
Definition:
random-variable-stream.cc:181
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition:
simulator.cc:249
ns3::EventId
An identifier for simulation events.
Definition:
event-id.h:53
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1014
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition:
double.h:41
anonymous_namespace{sample-simulator.cc}::ExampleFunction
static void ExampleFunction(MyModel *model)
Simple function event handler which Starts a MyModel object.
Definition:
sample-simulator.cc:75
anonymous_namespace{sample-simulator.cc}::RandomFunction
static void RandomFunction(void)
Simple function event handler; this function is called randomly.
Definition:
sample-simulator.cc:86
ns3::ObjectBase::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition:
object-base.cc:185
Generated on Wed Nov 7 2018 10:01:49 for ns-3 by
1.8.14