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
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
34
using namespace
ns3
;
35
36
class
MyModel
37
{
38
public
:
39
void
Start (
void
);
40
private
:
41
void
HandleEvent (
double
eventValue);
42
};
43
44
void
45
MyModel::Start
(
void
)
46
{
47
Simulator::Schedule
(
Seconds
(10.0),
48
&
MyModel::HandleEvent
,
49
this
,
Simulator::Now
().GetSeconds ());
50
}
51
void
52
MyModel::HandleEvent
(
double
value)
53
{
54
std::cout <<
"Member method received event at "
55
<<
Simulator::Now
().
GetSeconds
()
56
<<
"s started at "
<< value <<
"s"
<< std::endl;
57
}
58
59
static
void
60
ExampleFunction
(
MyModel
*model)
61
{
62
std::cout <<
"ExampleFunction received event at "
63
<<
Simulator::Now
().
GetSeconds
() <<
"s"
<< std::endl;
64
model->
Start
();
65
}
66
67
static
void
68
RandomFunction
(
void
)
69
{
70
std::cout <<
"RandomFunction received event at "
71
<<
Simulator::Now
().
GetSeconds
() <<
"s"
<< std::endl;
72
}
73
74
static
void
75
CancelledEvent
(
void
)
76
{
77
std::cout <<
"I should never be called... "
<< std::endl;
78
}
79
80
int
main (
int
argc,
char
*argv[])
81
{
82
CommandLine
cmd;
83
cmd.
Parse
(argc, argv);
84
85
MyModel
model;
86
Ptr<UniformRandomVariable>
v = CreateObject<UniformRandomVariable> ();
87
v->
SetAttribute
(
"Min"
,
DoubleValue
(10));
88
v->
SetAttribute
(
"Max"
,
DoubleValue
(20));
89
90
Simulator::Schedule
(
Seconds
(10.0), &
ExampleFunction
, &model);
91
92
Simulator::Schedule
(
Seconds
(v->
GetValue
()), &
RandomFunction
);
93
94
EventId
id
=
Simulator::Schedule
(
Seconds
(30.0), &
CancelledEvent
);
95
Simulator::Cancel
(
id
);
96
97
Simulator::Run
();
98
99
Simulator::Destroy
();
100
}
ns3::Ptr< UniformRandomVariable >
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition:
simulator.cc:200
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:311
MyModel::Start
void Start(void)
Definition:
sample-simulator.cc:45
ns3::Simulator::Schedule
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition:
simulator.h:819
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition:
nstime.h:327
CancelledEvent
static void CancelledEvent(void)
Definition:
sample-simulator.cc:75
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:201
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:164
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
MyModel::HandleEvent
void HandleEvent(double eventValue)
Definition:
sample-simulator.cc:52
ns3::UniformRandomVariable::GetValue
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
Definition:
random-variable-stream.cc:172
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition:
simulator.cc:223
RandomFunction
static void RandomFunction(void)
Definition:
sample-simulator.cc:68
ns3::EventId
An identifier for simulation events.
Definition:
event-id.h:53
MyModel
Definition:
sample-simulator.cc:36
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:859
ns3::CommandLine::Parse
void Parse(int argc, char *argv[])
Parse the program arguments.
Definition:
command-line.cc:112
ExampleFunction
static void ExampleFunction(MyModel *model)
Definition:
sample-simulator.cc:60
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition:
double.h:41
ns3::ObjectBase::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition:
object-base.cc:190
src
core
examples
sample-simulator.cc
Generated on Thu Feb 5 2015 18:33:33 for ns-3 by
1.8.9.1