A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
file-helper-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * This is based on double-probe-example.cc.
7 *
8 * Author: Mitch Watrous (watrous@u.washington.edu)
9 */
10
11/*
12 * This example is designed to show the main features of an
13 * ns3::FileHelper.
14 */
15
16#include "ns3/core-module.h"
17#include "ns3/stats-module.h"
18
19#include <string>
20
21using namespace ns3;
22
23NS_LOG_COMPONENT_DEFINE("FileHelperExample");
24
25/**
26 * This is our test object, an object that increments a counter according
27 * to a Poisson process, and exports the (integer-valued) count as a
28 * trace source.
29 */
30class Emitter : public Object
31{
32 public:
33 /**
34 * Register this type.
35 * \return The TypeId.
36 */
37 static TypeId GetTypeId();
39
40 private:
41 void DoInitialize() override;
42 /// Counts how many time this function is called.
43 void Count();
44
45 TracedValue<uint32_t> m_counter; //!< Simple counter
46 Ptr<ExponentialRandomVariable> m_var; //!< Random number generator.
47};
48
50
53{
54 static TypeId tid = TypeId("ns3::Emitter")
56 .SetGroupName("Stats")
57 .AddConstructor<Emitter>()
58 .AddTraceSource("Counter",
59 "sample counter",
61 "ns3::TracedValueCallback::Double");
62 return tid;
63}
64
66{
67 NS_LOG_FUNCTION(this);
68 m_counter = 0;
70}
71
72void
74{
75 NS_LOG_FUNCTION(this);
76 Simulator::Schedule(Seconds(m_var->GetValue()), &Emitter::Count, this);
77}
78
79void
81{
82 NS_LOG_FUNCTION(this);
83 NS_LOG_DEBUG("Counting at " << Simulator::Now().As(Time::S));
84 m_counter += 1.0;
85 Simulator::Schedule(Seconds(m_var->GetValue()), &Emitter::Count, this);
86}
87
88int
89main(int argc, char* argv[])
90{
91 CommandLine cmd(__FILE__);
92 cmd.Parse(argc, argv);
93
94 //
95 // This Emitter has a trace source object that will emit values at
96 // random times.
97 //
98
100 Names::Add("/Names/Emitter", emitter);
101
102 //
103 // This file helper will be used to put data values into a file.
104 //
105
106 // Create the file helper.
107 FileHelper fileHelper;
108
109 // Configure the file to be written.
110 fileHelper.ConfigureFile("file-helper-example", FileAggregator::FORMATTED);
111
112 // Set the labels for this formatted output file.
113 fileHelper.Set2dFormat("Time (Seconds) = %.3f\tCount = %.0f");
114
115 // Write the values generated by the probe. The path that we
116 // provide helps to disambiguate the source of the trace.
117 fileHelper.WriteProbe("ns3::Uinteger32Probe", "/Names/Emitter/Counter", "Output");
118
119 // The Emitter object is not associated with an ns-3 node, so
120 // it won't get started automatically, so we need to do this ourselves
122
123 Simulator::Stop(Seconds(100.0));
126
127 return 0;
128}
This is our test object, an object that increments counters at various times and emits one of them as...
void DoInitialize() override
Initialize() implementation.
static TypeId GetTypeId()
Register this type.
TracedValue< uint32_t > m_counter
Simple counter.
TracedValue< double > m_counter
Sample counter, normally this would be integer type.
static TypeId GetTypeId()
Register this type.
void Count()
Counts how many time this function is called.
Ptr< ExponentialRandomVariable > m_var
Random number generator.
Parse command-line arguments.
Helper class used to put data values into a file.
Definition file-helper.h:29
void Set2dFormat(const std::string &format)
Sets the 2D format string for the C-style sprintf() function.
void WriteProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource)
void ConfigureFile(const std::string &outputFileNameWithoutExtension, FileAggregator::FileType fileType=FileAggregator::SPACE_SEPARATED)
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
A base class which provides memory management and object aggregation.
Definition object.h:78
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition object.cc:203
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
@ S
second
Definition nstime.h:105
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.