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
file-helper-example.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2013 University of Washington
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
* This is based on double-probe-example.cc.
19
*
20
* Author: Mitch Watrous (watrous@u.washington.edu)
21
*/
22
23
/*
24
* This example is designed to show the main features of an
25
* ns3::FileHelper.
26
*/
27
28
#include <string>
29
30
#include "ns3/core-module.h"
31
#include "ns3/stats-module.h"
32
33
using namespace
ns3;
34
35
NS_LOG_COMPONENT_DEFINE
(
"FileHelperExample"
);
36
37
/*
38
* This is our test object, an object that increments counters at
39
* various times and emits one of them as a trace source.
40
*/
41
class
Emitter
:
public
Object
42
{
43
public
:
44
static
TypeId
GetTypeId (
void
);
45
Emitter
();
46
private
:
47
void
DoInitialize (
void
);
48
void
Emit (
void
);
49
void
Count (
void
);
50
51
TracedValue<double>
m_counter;
// normally this would be integer type
52
Ptr<ExponentialRandomVariable>
m_var;
53
54
};
55
56
NS_OBJECT_ENSURE_REGISTERED
(
Emitter
);
57
58
TypeId
59
Emitter::GetTypeId
(
void
)
60
{
61
static
TypeId
tid =
TypeId
(
"ns3::Emitter"
)
62
.
AddConstructor
<
Emitter
> ()
63
.SetParent<Object> ()
64
.AddTraceSource (
"Counter"
,
65
"sample counter"
,
66
MakeTraceSourceAccessor
(&
Emitter::m_counter
))
67
;
68
return
tid;
69
}
70
71
Emitter::Emitter
(
void
)
72
{
73
NS_LOG_FUNCTION
(
this
);
74
m_counter = 0;
75
m_var = CreateObject<ExponentialRandomVariable> ();
76
}
77
78
void
79
Emitter::DoInitialize
(
void
)
80
{
81
NS_LOG_FUNCTION
(
this
);
82
Simulator::Schedule
(Seconds (m_var->GetValue ()), &
Emitter::Emit
,
this
);
83
Simulator::Schedule
(Seconds (m_var->GetValue ()), &
Emitter::Count
,
this
);
84
}
85
86
void
87
Emitter::Emit
(
void
)
88
{
89
NS_LOG_FUNCTION
(
this
);
90
NS_LOG_DEBUG
(
"Emitting at "
<<
Simulator::Now
().GetSeconds ());
91
Simulator::Schedule
(Seconds (m_var->GetValue ()), &
Emitter::Emit
,
this
);
92
}
93
94
void
95
Emitter::Count
(
void
)
96
{
97
NS_LOG_FUNCTION
(
this
);
98
NS_LOG_DEBUG
(
"Counting at "
<<
Simulator::Now
().GetSeconds ());
99
m_counter += 1.0;
100
Simulator::Schedule
(Seconds (m_var->GetValue ()), &
Emitter::Count
,
this
);
101
}
102
103
int
main
(
int
argc,
char
*argv[])
104
{
105
CommandLine
cmd;
106
cmd.
Parse
(argc, argv);
107
108
//
109
// This Emitter has a trace source object that will emit values at
110
// random times.
111
//
112
113
Ptr<Emitter>
emitter = CreateObject<Emitter> ();
114
Names::Add
(
"/Names/Emitter"
, emitter);
115
116
//
117
// This Probe will be hooked to the Emitter's trace source object by
118
// accessing it by path name in the Config database.
119
//
120
121
Ptr<DoubleProbe>
probe = CreateObject<DoubleProbe> ();
122
probe->
SetName
(
"PathProbe"
);
123
Names::Add
(
"/Names/Probe"
, probe);
124
125
// Note, no return value is checked here.
126
probe->
ConnectByPath
(
"/Names/Emitter/Counter"
);
127
128
//
129
// This file helper will be used to put data values into a file.
130
//
131
132
// Create the file helper.
133
FileHelper
fileHelper;
134
135
// Configure the file to be written.
136
fileHelper.
ConfigureFile
(
"file-helper-example"
,
137
FileAggregator::FORMATTED
);
138
139
// Set the labels for this formatted output file.
140
fileHelper.
Set2dFormat
(
"Time (Seconds) = %.3e\tCount = %.0f"
);
141
142
// Write the values generated by the probe. The path that we
143
// provide helps to disambiguate the source of the trace.
144
fileHelper.
WriteProbe
(
"ns3::DoubleProbe"
,
145
"/Names/Probe/Output"
,
146
"Output"
);
147
148
// The Emitter object is not associated with an ns-3 node, so
149
// it won't get started automatically, so we need to do this ourselves
150
Simulator::Schedule
(Seconds (0.0), &
Emitter::Initialize
, emitter);
151
152
Simulator::Stop
(Seconds (100.0));
153
Simulator::Run
();
154
Simulator::Destroy
();
155
156
return
0;
157
}
src
stats
examples
file-helper-example.cc
Generated on Fri Aug 30 2013 01:43:02 for ns-3 by
1.8.1.2