satellite-stats-beam-service-time-helper.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Magister Solutions
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  * Author: Lauri Sormunen <lauri.sormunen@magister.fi>
19  *
20  */
21 
23 
24 #include <ns3/boolean.h>
25 #include <ns3/callback.h>
26 #include <ns3/data-collection-object.h>
27 #include <ns3/distribution-collector.h>
28 #include <ns3/enum.h>
29 #include <ns3/fatal-error.h>
30 #include <ns3/log.h>
31 #include <ns3/multi-file-aggregator.h>
32 #include <ns3/node-container.h>
33 #include <ns3/satellite-beam-helper.h>
34 #include <ns3/satellite-beam-scheduler.h>
35 #include <ns3/satellite-gw-mac.h>
36 #include <ns3/satellite-helper.h>
37 #include <ns3/satellite-ncc.h>
38 #include <ns3/satellite-topology.h>
39 #include <ns3/scalar-collector.h>
40 #include <ns3/singleton.h>
41 #include <ns3/string.h>
42 
43 #include <list>
44 #include <sstream>
45 #include <string>
46 #include <utility>
47 
48 NS_LOG_COMPONENT_DEFINE("SatStatsBeamServiceTimeHelper");
49 
50 namespace ns3
51 {
52 
53 NS_OBJECT_ENSURE_REGISTERED(SatStatsBeamServiceTimeHelper);
54 
56  : SatStatsHelper(satHelper)
57 {
58  NS_LOG_FUNCTION(this << satHelper);
59 }
60 
62 {
63  NS_LOG_FUNCTION(this);
64 }
65 
66 TypeId // static
68 {
69  static TypeId tid = TypeId("ns3::SatStatsBeamServiceTimeHelper").SetParent<SatStatsHelper>();
70  return tid;
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION(this);
77 
79  {
80  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
81  << " is not a valid output type for this statistics.");
82  }
83 
85  {
87  << " is not a valid identifier type for this statistics.");
88  }
89 
90  // Setup aggregator.
91  std::string dataLabel = "service_time";
92 
93  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
94  "OutputFileName",
95  StringValue(GetOutputFileName()),
96  "MultiFileMode",
97  BooleanValue(false),
98  "EnableContextPrinting",
99  BooleanValue(true),
100  "GeneralHeading",
101  StringValue("% beam_id service_time"));
102 
103  // Prepare collector map
104  NS_LOG_INFO(this << " Creating new collectors for beams");
105 
106  m_collectorMap.SetType("ns3::ScalarCollector");
107  m_collectorMap.SetAttribute("OutputType", EnumValue(ScalarCollector::OUTPUT_TYPE_SUM));
108  m_collectorMap.SetAttribute("InputDataType",
109  EnumValue(ScalarCollector::INPUT_DATA_TYPE_DOUBLE));
110 
111  // Create the callback for trace source
112  Callback<void, std::string, Time> beamServiceCallback =
114 
115  // Connect SatGwMac of each beam by identifier (global, gw, beam) to callback
116  NodeContainer gwNodes = Singleton<SatTopology>::Get()->GetGwNodes();
117  for (auto node = gwNodes.Begin(); node != gwNodes.End(); node++)
118  {
119  for (uint32_t i = 0; i < (*node)->GetNDevices(); i++)
120  {
121  Ptr<SatNetDevice> dev = DynamicCast<SatNetDevice>((*node)->GetDevice(i));
122  if (dev == nullptr)
123  continue;
124  Ptr<SatMac> mac = dev->GetMac();
125  if (mac == nullptr)
126  continue;
127 
128  // Connect the trace source
129  uint32_t beamId = mac->GetBeamId();
130  std::ostringstream context;
131  context << beamId;
132  const bool ret =
133  mac->TraceConnect("BeamServiceTime", context.str(), beamServiceCallback);
134  NS_ASSERT_MSG(ret, "Error connecting to BeamServiceTime of beam " << beamId);
135  NS_LOG_INFO(this << " successfully connected"
136  << " with beam " << beamId);
137  }
138  }
139 
140 } // end of `void DoInstall ();`
141 
142 void
144 {
145  NS_LOG_FUNCTION(this << context << time.GetSeconds());
146 
147  // convert context to number
148  std::stringstream ss(context);
149  uint32_t beamId;
150  if (!(ss >> beamId))
151  {
152  NS_FATAL_ERROR("Cannot convert '" << context << "' to number");
153  }
154 
155  Ptr<DataCollectionObject> o = m_collectorMap.Get(beamId);
156  if (o == nullptr)
157  {
158  std::ostringstream name;
159  name << beamId;
160  m_collectorMap.SetAttribute("Name", StringValue(name.str()));
161  m_collectorMap.Create(beamId);
162  o = m_collectorMap.Get(beamId);
163 
164  bool success =
165  o->TraceConnect("Output",
166  context,
167  MakeCallback(&MultiFileAggregator::Write1d,
168  DynamicCast<MultiFileAggregator>(m_aggregator)));
169  NS_ASSERT(success);
170  }
171 
172  Ptr<ScalarCollector> s = o->GetObject<ScalarCollector>();
173  NS_ASSERT(s != nullptr);
174 
175  // Pass the sample to the collector.
176  s->TraceSinkDouble(0, time.GetSeconds());
177 
178 } // end of `void BeamServiceCallback (std::string, uint32_t)`
179 
180 } // end of namespace ns3
void DoInstall()
Install the probes, collectors, and aggregators necessary to produce the statistics output.
static TypeId GetTypeId()
inherited from ObjectBase base class
CollectorMap m_collectorMap
Two-dimensional map of collectors, indexed first by the the frame type identifier and second by the g...
SatStatsBeamServiceTimeHelper(Ptr< const SatHelper > satHelper)
Constructor.
Ptr< DataCollectionObject > m_aggregator
The aggregator created by this helper.
void BeamServiceCallback(std::string context, Time time)
Parent abstract class of all satellite statistics helpers.
IdentifierType_t GetIdentifierType() const
static std::string GetOutputTypeName(OutputType_t outputType)
static std::string GetIdentifierTypeName(IdentifierType_t identifierType)
Ptr< DataCollectionObject > CreateAggregator(std::string aggregatorTypeId, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue())
Create the aggregator according to the output type.
virtual std::string GetOutputFileName() const
Compute the path and file name where statistics output should be written to.
OutputType_t GetOutputType() const
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.