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
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
28
using namespace
ns3;
29
30
class
MyModel
31
{
32
public
:
33
void
Start (
void
);
34
private
:
35
void
HandleEvent (
double
eventValue);
36
};
37
38
void
39
MyModel::Start
(
void
)
40
{
41
Simulator::Schedule
(Seconds (10.0),
42
&
MyModel::HandleEvent
,
43
this
,
Simulator::Now
().GetSeconds ());
44
}
45
void
46
MyModel::HandleEvent
(
double
value)
47
{
48
std::cout <<
"Member method received event at "
49
<<
Simulator::Now
().
GetSeconds
()
50
<<
"s started at "
<< value <<
"s"
<< std::endl;
51
}
52
53
static
void
54
ExampleFunction
(
MyModel
*model)
55
{
56
std::cout <<
"ExampleFunction received event at "
57
<<
Simulator::Now
().
GetSeconds
() <<
"s"
<< std::endl;
58
model->
Start
();
59
}
60
61
static
void
62
RandomFunction
(
void
)
63
{
64
std::cout <<
"RandomFunction received event at "
65
<<
Simulator::Now
().
GetSeconds
() <<
"s"
<< std::endl;
66
}
67
68
static
void
69
CancelledEvent
(
void
)
70
{
71
std::cout <<
"I should never be called... "
<< std::endl;
72
}
73
74
int
main
(
int
argc,
char
*argv[])
75
{
76
CommandLine
cmd;
77
cmd.
Parse
(argc, argv);
78
79
MyModel
model;
80
Ptr<UniformRandomVariable>
v = CreateObject<UniformRandomVariable> ();
81
v->
SetAttribute
(
"Min"
,
DoubleValue
(10));
82
v->
SetAttribute
(
"Max"
,
DoubleValue
(20));
83
84
Simulator::Schedule
(Seconds (10.0), &
ExampleFunction
, &model);
85
86
Simulator::Schedule
(Seconds (v->
GetValue
()), &
RandomFunction
);
87
88
EventId
id
=
Simulator::Schedule
(Seconds (30.0), &
CancelledEvent
);
89
Simulator::Cancel
(
id
);
90
91
Simulator::Run
();
92
93
Simulator::Destroy
();
94
}
ns3::Ptr< UniformRandomVariable >
ns3::Simulator::Run
static void Run(void)
Run the simulation until one of:
Definition:
simulator.cc:157
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
MyModel::Start
void Start(void)
Definition:
sample-simulator.cc:39
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:824
ns3::Time::GetSeconds
double GetSeconds(void) const
Definition:
nstime.h:274
CancelledEvent
static void CancelledEvent(void)
Definition:
sample-simulator.cc:69
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:152
ns3::Simulator::Destroy
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition:
simulator.cc:121
MyModel::HandleEvent
void HandleEvent(double eventValue)
Definition:
sample-simulator.cc:46
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:174
ns3::Simulator::Now
static Time Now(void)
Return the "current simulation time".
Definition:
simulator.cc:180
main
int main(int argc, char *argv[])
Definition:
sample-simulator.cc:74
RandomFunction
static void RandomFunction(void)
Definition:
sample-simulator.cc:62
ns3::EventId
an identifier for simulation events.
Definition:
event-id.h:46
MyModel
Definition:
sample-simulator.cc:30
ns3::CommandLine::Parse
void Parse(int argc, char *argv[])
Parse the program arguments.
Definition:
command-line.cc:104
ExampleFunction
static void ExampleFunction(MyModel *model)
Definition:
sample-simulator.cc:54
ns3::DoubleValue
Hold a floating point type.
Definition:
double.h:41
ns3::ObjectBase::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Definition:
object-base.cc:161
src
core
examples
sample-simulator.cc
Generated on Sat Apr 19 2014 14:06:51 for ns-3 by
1.8.6