src/stats/examples/file-helper-example.cc
author Tom Henderson <tomh@tomh.org>
Thu, 08 Aug 2013 06:40:41 -0700
changeset 10118 848770c90669
child 10410 4d4eb8097fa3
permissions -rw-r--r--
data collection extensions for stats module
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10118
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     2
/*
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     3
 * Copyright (c) 2013 University of Washington
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     4
 *
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     7
 * published by the Free Software Foundation;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     8
 *
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    12
 * GNU General Public License for more details.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    13
 *
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    17
 *
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    18
 * This is based on double-probe-example.cc.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    19
 *
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    20
 * Author: Mitch Watrous (watrous@u.washington.edu)
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    21
 */
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    22
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    23
/*
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    24
 * This example is designed to show the main features of an
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    25
 * ns3::FileHelper.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    26
 */
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    27
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    28
#include <string>
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    29
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    30
#include "ns3/core-module.h"
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    31
#include "ns3/stats-module.h"
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    32
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    33
using namespace ns3;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    34
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    35
NS_LOG_COMPONENT_DEFINE ("FileHelperExample");
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    36
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    37
/*
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    38
 * This is our test object, an object that increments counters at
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    39
 * various times and emits one of them as a trace source.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    40
 */
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    41
class Emitter : public Object
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    42
{
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    43
public:
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    44
  static TypeId GetTypeId (void);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    45
  Emitter ();
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    46
private:
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    47
  void DoInitialize (void);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    48
  void Emit (void);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    49
  void Count (void);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    50
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    51
  TracedValue<double> m_counter;  // normally this would be integer type
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    52
  Ptr<ExponentialRandomVariable> m_var;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    53
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    54
};
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    55
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    56
NS_OBJECT_ENSURE_REGISTERED (Emitter);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    57
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    58
TypeId
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    59
Emitter::GetTypeId (void)
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    60
{
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    61
  static TypeId tid = TypeId ("ns3::Emitter")
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    62
    .AddConstructor<Emitter> ()
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    63
    .SetParent<Object> ()
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    64
    .AddTraceSource ("Counter",
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    65
                     "sample counter",
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    66
                     MakeTraceSourceAccessor (&Emitter::m_counter))
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    67
  ;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    68
  return tid;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    69
}
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    70
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    71
Emitter::Emitter (void)
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    72
{
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    73
  NS_LOG_FUNCTION (this);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    74
  m_counter = 0;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    75
  m_var = CreateObject<ExponentialRandomVariable> ();
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    76
}
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    77
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    78
void
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    79
Emitter::DoInitialize (void)
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    80
{
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    81
  NS_LOG_FUNCTION (this);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    82
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    83
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    84
}
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    85
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    86
void
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    87
Emitter::Emit (void)
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    88
{
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    89
  NS_LOG_FUNCTION (this);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    90
  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ());
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    91
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    92
}
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    93
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    94
void
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    95
Emitter::Count (void)
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    96
{
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    97
  NS_LOG_FUNCTION (this);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    98
  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ());
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    99
  m_counter += 1.0;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   100
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   101
}
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   102
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   103
int main (int argc, char *argv[])
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   104
{
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   105
  CommandLine cmd;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   106
  cmd.Parse (argc, argv);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   107
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   108
  //
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   109
  // This Emitter has a trace source object that will emit values at
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   110
  // random times.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   111
  //
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   112
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   113
  Ptr<Emitter> emitter = CreateObject<Emitter> ();
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   114
  Names::Add ("/Names/Emitter", emitter);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   115
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   116
  //
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   117
  // This Probe will be hooked to the Emitter's trace source object by
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   118
  // accessing it by path name in the Config database.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   119
  //
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   120
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   121
  Ptr<DoubleProbe> probe = CreateObject<DoubleProbe> ();
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   122
  probe->SetName ("PathProbe");
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   123
  Names::Add ("/Names/Probe", probe);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   124
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   125
  // Note, no return value is checked here.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   126
  probe->ConnectByPath ("/Names/Emitter/Counter");
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   127
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   128
  //
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   129
  // This file helper will be used to put data values into a file.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   130
  //
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   131
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   132
  // Create the file helper.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   133
  FileHelper fileHelper;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   134
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   135
  // Configure the file to be written.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   136
  fileHelper.ConfigureFile ("file-helper-example",
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   137
                            FileAggregator::FORMATTED);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   138
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   139
  // Set the labels for this formatted output file.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   140
  fileHelper.Set2dFormat ("Time (Seconds) = %.3e\tCount = %.0f");
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   141
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   142
  // Write the values generated by the probe.  The path that we
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   143
  // provide helps to disambiguate the source of the trace.
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   144
  fileHelper.WriteProbe ("ns3::DoubleProbe",
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   145
                         "/Names/Probe/Output",
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   146
                         "Output");
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   147
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   148
  // The Emitter object is not associated with an ns-3 node, so
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   149
  // it won't get started automatically, so we need to do this ourselves
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   150
  Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   151
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   152
  Simulator::Stop (Seconds (100.0));
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   153
  Simulator::Run ();
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   154
  Simulator::Destroy ();
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   155
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   156
  return 0;
848770c90669 data collection extensions for stats module
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   157
}