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
}
src
core
examples
sample-simulator.cc
Generated on Tue May 14 2013 11:08:17 for ns-3 by
1.8.1.2