src/stats/examples/gnuplot-helper-example.cc
author Peter D. Barnes, Jr. <barnes26@llnl.gov>
Thu, 02 Oct 2014 21:17:48 -0700
changeset 10978 754c8256c35c
parent 10652 dc18deba4502
child 11027 e943837b17ad
permissions -rw-r--r--
TracedValue callback function signatures.

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2013 University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * This is based on double-probe-example.cc.
 *
 * Author: Mitch Watrous (watrous@u.washington.edu)
 */

/*
 * This example is designed to show the main features of an
 * ns3::GnuplotHelper.
 */

#include <string>

#include "ns3/core-module.h"
#include "ns3/stats-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("GnuplotHelperExample");

/*
 * This is our test object, an object that increments counters at
 * various times and emits one of them as a trace source.
 */
class Emitter : public Object
{
public:
  static TypeId GetTypeId (void);
  Emitter ();
private:
  void DoInitialize (void);
  void Emit (void);
  void Count (void);

  TracedValue<double> m_counter;  // normally this would be integer type
  Ptr<ExponentialRandomVariable> m_var;

};

NS_OBJECT_ENSURE_REGISTERED (Emitter);

TypeId
Emitter::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::Emitter")
    .AddConstructor<Emitter> ()
    .SetParent<Object> ()
    .AddTraceSource ("Counter",
                     "sample counter",
                     MakeTraceSourceAccessor (&Emitter::m_counter),
                     "ns3::TracedValue::DoubleCallback")
  ;
  return tid;
}

Emitter::Emitter (void)
{
  NS_LOG_FUNCTION (this);
  m_counter = 0;
  m_var = CreateObject<ExponentialRandomVariable> ();
}

void
Emitter::DoInitialize (void)
{
  NS_LOG_FUNCTION (this);
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
}

void
Emitter::Emit (void)
{
  NS_LOG_FUNCTION (this);
  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ());
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
}

void
Emitter::Count (void)
{
  NS_LOG_FUNCTION (this);
  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ());
  m_counter += 1.0;
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
}

int main (int argc, char *argv[])
{
  CommandLine cmd;
  cmd.Parse (argc, argv);

  //
  // This Emitter has a trace source object that will emit values at
  // random times.
  //

  Ptr<Emitter> emitter = CreateObject<Emitter> ();
  Names::Add ("/Names/Emitter", emitter);

  //
  // This Probe will be hooked to the Emitter's trace source object by
  // accessing it by path name in the Config database.
  //

  Ptr<DoubleProbe> probe = CreateObject<DoubleProbe> ();
  probe->SetName ("PathProbe");
  Names::Add ("/Names/Probe", probe);

  // Note, no return value is checked here.
  probe->ConnectByPath ("/Names/Emitter/Counter");

  //
  // This gnuplot helper will be used to produce output used to make
  // gnuplot plots.
  //

  // Create the gnuplot helper.
  GnuplotHelper plotHelper;

  // Configure the plot.
  plotHelper.ConfigurePlot ("gnuplot-helper-example",
                            "Emitter Counts vs. Time",
                            "Time (Seconds)",
                            "Emitter Count",
                            "png");

  // Plot the values generated by the probe.  The path that we provide
  // helps to disambiguate the source of the trace.
  plotHelper.PlotProbe ("ns3::DoubleProbe",
                        "/Names/Probe/Output",
                        "Output",
                        "Emitter Count",
                        GnuplotAggregator::KEY_INSIDE);

  // The Emitter object is not associated with an ns-3 node, so
  // it won't get started automatically, so we need to do this ourselves
  Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);

  Simulator::Stop (Seconds (100.0));
  Simulator::Run ();
  Simulator::Destroy ();

  return 0;
}