A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
gnuplot-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::GnuplotHelper.
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 ("GnuplotHelperExample");
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 gnuplot helper will be used to produce output used to make
130  // gnuplot plots.
131  //
132 
133  // Create the gnuplot helper.
134  GnuplotHelper plotHelper;
135 
136  // Configure the plot.
137  plotHelper.ConfigurePlot ("gnuplot-helper-example",
138  "Emitter Counts vs. Time",
139  "Time (Seconds)",
140  "Emitter Count",
141  "png");
142 
143  // Plot the values generated by the probe. The path that we provide
144  // helps to disambiguate the source of the trace.
145  plotHelper.PlotProbe ("ns3::DoubleProbe",
146  "/Names/Probe/Output",
147  "Output",
148  "Emitter Count",
150 
151  // The Emitter object is not associated with an ns-3 node, so
152  // it won't get started automatically, so we need to do this ourselves
153  Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);
154 
155  Simulator::Stop (Seconds (100.0));
156  Simulator::Run ();
158 
159  return 0;
160 }
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
TypeId AddConstructor(void)
Definition: type-id.h:418
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
void PlotProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource, const std::string &title, enum GnuplotAggregator::KeyLocation keyLocation=GnuplotAggregator::KEY_INSIDE)
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
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:825
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
void ConfigurePlot(const std::string &outputFileNameWithoutExtension, const std::string &title, const std::string &xLegend, const std::string &yLegend, const std::string &terminalType="png")
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void Count(void)
void DoInitialize(void)
This method is called only once by Object::Initialize.
TracedValue< double > m_counter
Helper class used to make gnuplot plots.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
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)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:179
int main(int argc, char *argv[])
void Parse(int argc, char *argv[])
Parse the program arguments.
a base class which provides memory management and object aggregation
Definition: object.h:64
a unique identifier for an interface.
Definition: type-id.h:49
void Emit(void)