A Discrete-Event Network Simulator
API
 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
53 
54 };
55 
57 
58 TypeId
59 Emitter::GetTypeId (void)
60 {
61  static TypeId tid = TypeId ("ns3::Emitter")
63  .SetParent<Object> ()
64  .AddTraceSource ("Counter",
65  "sample 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
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",
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 ();
155 
156  return 0;
157 }
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
TypeId AddConstructor(void)
Definition: type-id.h:418
static void Run(void)
Definition: simulator.cc:157
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:824
static TypeId GetTypeId(void)
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr obj.
Definition: names.cc:615
virtual void ConnectByPath(std::string path)
connect to a trace source provided by a config path
Definition: double-probe.cc:93
int main(int argc, char *argv[])
NS_OBJECT_ENSURE_REGISTERED(AntennaModel)
Helper class used to put data values into a file.
Definition: file-helper.h:37
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Definition: simulator.cc:121
void Count(void)
void DoInitialize(void)
TracedValue< double > m_counter
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
void ConfigureFile(const std::string &outputFileNameWithoutExtension, enum FileAggregator::FileType fileType=FileAggregator::SPACE_SEPARATED)
Definition: file-helper.cc:68
static Time Now(void)
Definition: simulator.cc:180
NS_LOG_COMPONENT_DEFINE("PacketLossCounter")
static void Stop(void)
Definition: simulator.cc:165
void SetName(std::string name)
Set the object's name. All spaces are replaced by underscores.
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
void Initialize(void)
Definition: object.cc:179
void WriteProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource)
Definition: file-helper.cc:90
void Parse(int argc, char *argv[])
a base class which provides memory management and object aggregation
Definition: object.h:63
a unique identifier for an interface.
Definition: type-id.h:49
void Emit(void)
void Set2dFormat(const std::string &format)
Sets the 2D format string for the C-style sprintf() function.
Definition: file-helper.cc:379