src/stats/examples/file-helper-example.cc
changeset 11027 e943837b17ad
parent 10978 754c8256c35c
child 11432 d2656819dd54
equal deleted inserted replaced
11026:19b4146b1d39 11027:e943837b17ad
    32 
    32 
    33 using namespace ns3;
    33 using namespace ns3;
    34 
    34 
    35 NS_LOG_COMPONENT_DEFINE ("FileHelperExample");
    35 NS_LOG_COMPONENT_DEFINE ("FileHelperExample");
    36 
    36 
    37 /*
    37 //
    38  * This is our test object, an object that increments counters at
    38 // This is our test object, an object that increments a counter according
    39  * various times and emits one of them as a trace source.
    39 // to a Poisson process, and exports the (integer-valued) count as a
    40  */
    40 // trace source.
       
    41 //
    41 class Emitter : public Object
    42 class Emitter : public Object
    42 {
    43 {
    43 public:
    44 public:
    44   static TypeId GetTypeId (void);
    45   static TypeId GetTypeId (void);
    45   Emitter ();
    46   Emitter ();
    46 private:
    47 private:
    47   void DoInitialize (void);
    48   void DoInitialize (void);
    48   void Emit (void);
       
    49   void Count (void);
    49   void Count (void);
    50 
    50 
    51   TracedValue<double> m_counter;  // normally this would be integer type
    51   TracedValue<uint32_t> m_counter;
    52   Ptr<ExponentialRandomVariable> m_var;
    52   Ptr<ExponentialRandomVariable> m_var;
    53 
    53 
    54 };
    54 };
    55 
    55 
    56 NS_OBJECT_ENSURE_REGISTERED (Emitter);
    56 NS_OBJECT_ENSURE_REGISTERED (Emitter);
    78 
    78 
    79 void
    79 void
    80 Emitter::DoInitialize (void)
    80 Emitter::DoInitialize (void)
    81 {
    81 {
    82   NS_LOG_FUNCTION (this);
    82   NS_LOG_FUNCTION (this);
    83   Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
       
    84   Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
    83   Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
    85 }
       
    86 
       
    87 void
       
    88 Emitter::Emit (void)
       
    89 {
       
    90   NS_LOG_FUNCTION (this);
       
    91   NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ());
       
    92   Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
       
    93 }
    84 }
    94 
    85 
    95 void
    86 void
    96 Emitter::Count (void)
    87 Emitter::Count (void)
    97 {
    88 {
   113 
   104 
   114   Ptr<Emitter> emitter = CreateObject<Emitter> ();
   105   Ptr<Emitter> emitter = CreateObject<Emitter> ();
   115   Names::Add ("/Names/Emitter", emitter);
   106   Names::Add ("/Names/Emitter", emitter);
   116 
   107 
   117   //
   108   //
   118   // This Probe will be hooked to the Emitter's trace source object by
       
   119   // accessing it by path name in the Config database.
       
   120   //
       
   121 
       
   122   Ptr<DoubleProbe> probe = CreateObject<DoubleProbe> ();
       
   123   probe->SetName ("PathProbe");
       
   124   Names::Add ("/Names/Probe", probe);
       
   125 
       
   126   // Note, no return value is checked here.
       
   127   probe->ConnectByPath ("/Names/Emitter/Counter");
       
   128 
       
   129   //
       
   130   // This file helper will be used to put data values into a file.
   109   // This file helper will be used to put data values into a file.
   131   //
   110   //
   132 
   111 
   133   // Create the file helper.
   112   // Create the file helper.
   134   FileHelper fileHelper;
   113   FileHelper fileHelper;
   136   // Configure the file to be written.
   115   // Configure the file to be written.
   137   fileHelper.ConfigureFile ("file-helper-example",
   116   fileHelper.ConfigureFile ("file-helper-example",
   138                             FileAggregator::FORMATTED);
   117                             FileAggregator::FORMATTED);
   139 
   118 
   140   // Set the labels for this formatted output file.
   119   // Set the labels for this formatted output file.
   141   fileHelper.Set2dFormat ("Time (Seconds) = %.3e\tCount = %.0f");
   120   fileHelper.Set2dFormat ("Time (Seconds) = %.3f\tCount = %.0f");
   142 
   121 
   143   // Write the values generated by the probe.  The path that we
   122   // Write the values generated by the probe.  The path that we
   144   // provide helps to disambiguate the source of the trace.
   123   // provide helps to disambiguate the source of the trace.
   145   fileHelper.WriteProbe ("ns3::DoubleProbe",
   124   fileHelper.WriteProbe ("ns3::Uinteger32Probe",
   146                          "/Names/Probe/Output",
   125                          "/Names/Emitter/Counter",
   147                          "Output");
   126                          "Output");
   148 
   127 
   149   // The Emitter object is not associated with an ns-3 node, so
   128   // The Emitter object is not associated with an ns-3 node, so
   150   // it won't get started automatically, so we need to do this ourselves
   129   // it won't get started automatically, so we need to do this ourselves
   151   Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);
   130   Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);