A Discrete-Event Network Simulator
API
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 a counter according
39 // to a Poisson process, and exports the (integer-valued) count as a
40 // trace source.
41 //
42 class Emitter : public Object
43 {
44 public:
45  static TypeId GetTypeId (void);
46  Emitter ();
47 private:
48  void DoInitialize (void);
49  void Count (void);
50 
51  TracedValue<uint32_t> m_counter;
53 };
54 
56 
57 TypeId
58 Emitter::GetTypeId (void)
59 {
60  static TypeId tid = TypeId ("ns3::Emitter")
62  .SetParent<Object> ()
63  .AddTraceSource ("Counter",
64  "sample counter",
66  "ns3::TracedValue::DoubleCallback")
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::Count, this);
83 }
84 
85 void
86 Emitter::Count (void)
87 {
88  NS_LOG_FUNCTION (this);
89  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ());
90  m_counter += 1.0;
91  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
92 }
93 
94 int main (int argc, char *argv[])
95 {
96  CommandLine cmd;
97  cmd.Parse (argc, argv);
98 
99  //
100  // This Emitter has a trace source object that will emit values at
101  // random times.
102  //
103 
104  Ptr<Emitter> emitter = CreateObject<Emitter> ();
105  Names::Add ("/Names/Emitter", emitter);
106 
107  //
108  // This gnuplot helper will be used to produce output used to make
109  // gnuplot plots.
110  //
111 
112  // Create the gnuplot helper.
113  GnuplotHelper plotHelper;
114 
115  // Configure the plot. Arguments include file prefix, plot title,
116  // x-label, y-label, and output file type
117  plotHelper.ConfigurePlot ("gnuplot-helper-example",
118  "Emitter Count vs. Time",
119  "Time (Seconds)",
120  "Emitter Count",
121  "png");
122 
123  // Create a probe. Because the trace source we are interested in is
124  // of type uint32_t, we specify the type of probe to use by the first
125  // argument specifying its ns3 TypeId.
126  plotHelper.PlotProbe ("ns3::Uinteger32Probe",
127  "/Names/Emitter/Counter",
128  "Output",
129  "Emitter Count",
131 
132  // The Emitter object is not associated with an ns-3 node, so
133  // it won't get started automatically, so we need to do this ourselves
135 
136  Simulator::Stop (Seconds (100.0));
137  Simulator::Run ();
139 
140  return 0;
141 }
#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:454
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
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.
Definition: simulator.cc:200
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
TracedValue< double > m_counter
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:819
static TypeId GetTypeId(void)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr obj.
Definition: names.cc:615
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:201
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
void Count(void)
void DoInitialize(void)
Initialize() implementation.
Helper class used to make gnuplot plots.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:182
void Parse(int argc, char *argv[])
Parse the program arguments.
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:51