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/scalar-collector.h>
39 #include <ns3/string.h>
40 
41 #include <list>
42 #include <sstream>
43 #include <utility>
44 
45 NS_LOG_COMPONENT_DEFINE("SatStatsBeamServiceTimeHelper");
46 
47 namespace ns3
48 {
49 
50 NS_OBJECT_ENSURE_REGISTERED(SatStatsBeamServiceTimeHelper);
51 
53  : SatStatsHelper(satHelper)
54 {
55  NS_LOG_FUNCTION(this << satHelper);
56 }
57 
59 {
60  NS_LOG_FUNCTION(this);
61 }
62 
63 TypeId // static
65 {
66  static TypeId tid = TypeId("ns3::SatStatsBeamServiceTimeHelper").SetParent<SatStatsHelper>();
67  return tid;
68 }
69 
70 void
72 {
73  NS_LOG_FUNCTION(this);
74 
76  {
77  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
78  << " is not a valid output type for this statistics.");
79  }
80 
82  {
84  << " is not a valid identifier type for this statistics.");
85  }
86 
87  // Setup aggregator.
88  std::string dataLabel = "service_time";
89 
90  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
91  "OutputFileName",
92  StringValue(GetOutputFileName()),
93  "MultiFileMode",
94  BooleanValue(false),
95  "EnableContextPrinting",
96  BooleanValue(true),
97  "GeneralHeading",
98  StringValue("% beam_id service_time"));
99 
100  // Prepare collector map
101  NS_LOG_INFO(this << " Creating new collectors for beams");
102 
103  m_collectorMap.SetType("ns3::ScalarCollector");
104  m_collectorMap.SetAttribute("OutputType", EnumValue(ScalarCollector::OUTPUT_TYPE_SUM));
105  m_collectorMap.SetAttribute("InputDataType",
106  EnumValue(ScalarCollector::INPUT_DATA_TYPE_DOUBLE));
107 
108  // Create the callback for trace source
109  Callback<void, std::string, Time> beamServiceCallback =
111 
112  // Connect SatGwMac of each beam by identifier (global, gw, beam) to callback
113  NodeContainer gwNodes = GetSatHelper()->GetBeamHelper()->GetGwNodes();
114  for (auto node = gwNodes.Begin(); node != gwNodes.End(); node++)
115  {
116  for (uint32_t i = 0; i < (*node)->GetNDevices(); i++)
117  {
118  Ptr<SatNetDevice> dev = DynamicCast<SatNetDevice>((*node)->GetDevice(i));
119  if (dev == NULL)
120  continue;
121  Ptr<SatMac> mac = dev->GetMac();
122  if (mac == NULL)
123  continue;
124 
125  // Connect the trace source
126  uint32_t beamId = mac->GetBeamId();
127  std::ostringstream context;
128  context << beamId;
129  const bool ret =
130  mac->TraceConnect("BeamServiceTime", context.str(), beamServiceCallback);
131  NS_ASSERT_MSG(ret, "Error connecting to BeamServiceTime of beam " << beamId);
132  NS_LOG_INFO(this << " successfully connected"
133  << " with beam " << beamId);
134  }
135  }
136 
137 } // end of `void DoInstall ();`
138 
139 void
141 {
142  NS_LOG_FUNCTION(this << context << time.GetSeconds());
143 
144  // convert context to number
145  std::stringstream ss(context);
146  uint32_t beamId;
147  if (!(ss >> beamId))
148  {
149  NS_FATAL_ERROR("Cannot convert '" << context << "' to number");
150  }
151 
152  Ptr<DataCollectionObject> o = m_collectorMap.Get(beamId);
153  if (o == nullptr)
154  {
155  std::ostringstream name;
156  name << beamId;
157  m_collectorMap.SetAttribute("Name", StringValue(name.str()));
158  m_collectorMap.Create(beamId);
159  o = m_collectorMap.Get(beamId);
160 
161  bool success =
162  o->TraceConnect("Output",
163  context,
164  MakeCallback(&MultiFileAggregator::Write1d,
165  DynamicCast<MultiFileAggregator>(m_aggregator)));
166  NS_ASSERT(success);
167  }
168 
169  Ptr<ScalarCollector> s = o->GetObject<ScalarCollector>();
170  NS_ASSERT(s != nullptr);
171 
172  // Pass the sample to the collector.
173  s->TraceSinkDouble(0, time.GetSeconds());
174 
175 } // end of `void BeamServiceCallback (std::string, uint32_t)`
176 
177 } // 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.
Ptr< const SatHelper > GetSatHelper() const
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.