satellite-stats-waveform-usage-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: Budiarto Herman <budiarto.herman@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/enum.h>
28 #include <ns3/fatal-error.h>
29 #include <ns3/log.h>
30 #include <ns3/multi-file-aggregator.h>
31 #include <ns3/node-container.h>
32 #include <ns3/satellite-beam-helper.h>
33 #include <ns3/satellite-beam-scheduler.h>
34 #include <ns3/satellite-helper.h>
35 #include <ns3/satellite-ncc.h>
36 #include <ns3/scalar-collector.h>
37 #include <ns3/string.h>
38 
39 #include <list>
40 #include <sstream>
41 #include <utility>
42 
43 NS_LOG_COMPONENT_DEFINE("SatStatsWaveformUsageHelper");
44 
45 namespace ns3
46 {
47 
48 NS_OBJECT_ENSURE_REGISTERED(SatStatsWaveformUsageHelper);
49 
51  : SatStatsHelper(satHelper)
52 {
53  NS_LOG_FUNCTION(this << satHelper);
54 }
55 
57 {
58  NS_LOG_FUNCTION(this);
59 }
60 
61 TypeId // static
63 {
64  static TypeId tid = TypeId("ns3::SatStatsWaveformUsageHelper").SetParent<SatStatsHelper>();
65  return tid;
66 }
67 
68 void
70 {
71  NS_LOG_FUNCTION(this);
72 
74  {
75  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
76  << " is not a valid output type for this statistics.");
77  }
78 
81  {
83  << " is not a valid identifier type for this statistics.");
84  }
85 
86  // Setup aggregator.
87  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
88  "OutputFileName",
89  StringValue(GetOutputFileName()),
90  "MultiFileMode",
91  BooleanValue(false),
92  "EnableContextPrinting",
93  BooleanValue(true),
94  "GeneralHeading",
95  StringValue(GetIdentifierHeading("usage_count")));
96 
97  Callback<void, std::string, uint32_t> waveformUsageCallback =
99 
100  // Setup probes.
101  Ptr<SatBeamHelper> beamHelper = GetSatHelper()->GetBeamHelper();
102  NS_ASSERT(beamHelper != nullptr);
103  Ptr<SatNcc> ncc = beamHelper->GetNcc();
104  NS_ASSERT(ncc != nullptr);
105  std::list<std::pair<uint32_t, uint32_t>> beams = beamHelper->GetBeams();
106 
107  for (std::list<std::pair<uint32_t, uint32_t>>::const_iterator it = beams.begin();
108  it != beams.end();
109  ++it)
110  {
111  std::ostringstream context;
112  context << GetIdentifierForBeam(it->first, it->second);
113 
114  Ptr<SatBeamScheduler> s = ncc->GetBeamScheduler(it->first, it->second);
115  NS_ASSERT_MSG(s != nullptr, "Error finding beam " << it->second);
116  const bool ret = s->TraceConnect("WaveformTrace", context.str(), waveformUsageCallback);
117  NS_ASSERT_MSG(ret, "Error connecting to WaveformTrace of beam " << it->second);
118  NS_LOG_INFO(this << " successfully connected"
119  << " with beam " << it->second);
120  }
121 
122 } // end of `void DoInstall ();`
123 
124 std::string
126 {
127  switch (GetIdentifierType())
128  {
130  return "% global waveform_id " + dataLabel;
131 
133  return "% gw_id waveform_id " + dataLabel;
134 
136  return "% beam_id waveform_id " + dataLabel;
137 
138  default:
139  NS_FATAL_ERROR("SatStatsWaveformUsageHelper - Invalid identifier type");
140  break;
141  }
142  return "";
143 }
144 
145 void
146 SatStatsWaveformUsageHelper::WaveformUsageCallback(std::string context, uint32_t waveformId)
147 {
148  NS_LOG_FUNCTION(this << context << waveformId);
149 
150  // convert context to number
151  std::stringstream ss(context);
152  uint32_t identifier;
153  if (!(ss >> identifier))
154  {
155  NS_FATAL_ERROR("Cannot convert '" << context << "' to number");
156  }
157 
158  std::map<uint32_t, CollectorMap>::iterator it = m_collectors.find(waveformId);
159  if (it == m_collectors.end())
160  {
161  // Newly discovered waveform ID
162  NS_LOG_INFO(this << " Creating new collectors for waveform ID " << waveformId);
163  CollectorMap collectorMap;
164  collectorMap.SetType("ns3::ScalarCollector");
165  collectorMap.SetAttribute("InputDataType",
166  EnumValue(ScalarCollector::INPUT_DATA_TYPE_UINTEGER));
167  collectorMap.SetAttribute("OutputType", EnumValue(ScalarCollector::OUTPUT_TYPE_SUM));
168  uint32_t n = 0;
169 
170  /*
171  * Create a new set of collectors. Its name consists of two integers:
172  * - the first is the identifier ID (beam ID, GW ID, or simply zero for
173  * global);
174  * - the second is the frame ID.
175  */
176  switch (GetIdentifierType())
177  {
179  std::ostringstream name;
180  name << "0 " << waveformId;
181  collectorMap.SetAttribute("Name", StringValue(name.str()));
182  collectorMap.Create(0);
183  n++;
184  break;
185  }
186 
188  NodeContainer gws = GetSatHelper()->GetBeamHelper()->GetGwNodes();
189  for (NodeContainer::Iterator it = gws.Begin(); it != gws.End(); ++it)
190  {
191  const uint32_t gwId = GetGwId(*it);
192  std::ostringstream name;
193  name << gwId << " " << waveformId;
194  collectorMap.SetAttribute("Name", StringValue(name.str()));
195  collectorMap.Create(gwId);
196  n++;
197  }
198  break;
199  }
200 
202  std::list<std::pair<uint32_t, uint32_t>> beams =
203  GetSatHelper()->GetBeamHelper()->GetBeams();
204  for (std::list<std::pair<uint32_t, uint32_t>>::const_iterator it = beams.begin();
205  it != beams.end();
206  ++it)
207  {
208  const uint32_t satId = (it->first);
209  const uint32_t beamId = (it->second);
210  std::ostringstream name;
211  name << satId << "-" << beamId << " " << waveformId;
212  collectorMap.SetAttribute("Name", StringValue(name.str()));
213  collectorMap.Create(SatConstVariables::MAX_BEAMS_PER_SATELLITE * (satId + 1) +
214  beamId);
215  n++;
216  }
217  break;
218  }
219 
220  default:
221  NS_FATAL_ERROR("SatStatsWaveformUsageHelper - Invalid identifier type");
222  break;
223  }
224 
225  collectorMap.ConnectToAggregator("Output", m_aggregator, &MultiFileAggregator::Write1d);
226  NS_LOG_INFO(this << " created " << n << " instance(s)"
227  << " of " << collectorMap.GetType().GetName() << " for "
229 
230  std::pair<std::map<uint32_t, CollectorMap>::iterator, bool> ret;
231  ret = m_collectors.insert(std::make_pair(waveformId, collectorMap));
232  NS_ASSERT(ret.second);
233  it = ret.first;
234 
235  } // end of `if (it == m_collectors.end ())`
236 
237  NS_ASSERT(it != m_collectors.end());
238 
239  // Find the collector with the right identifier.
240  Ptr<DataCollectionObject> collector = it->second.Get(identifier);
241  NS_ASSERT_MSG(collector != nullptr, "Unable to find collector with identifier " << identifier);
242  Ptr<ScalarCollector> c = collector->GetObject<ScalarCollector>();
243  NS_ASSERT(c != nullptr);
244 
245  // Pass the sample to the collector.
246  c->TraceSinkUinteger32(0, 1);
247 
248 } // end of `void WaveformUsageCallback (std::string, uint32_t)`
249 
250 } // end of namespace ns3
Parent abstract class of all satellite statistics helpers.
uint32_t GetIdentifierForBeam(uint32_t satId, uint32_t beamId) const
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
uint32_t GetGwId(Ptr< Node > gwNode) const
SatStatsWaveformUsageHelper(Ptr< const SatHelper > satHelper)
std::map< uint32_t, CollectorMap > m_collectors
Two-dimensional map of collectors, indexed by the waveform ID and then by the identifier.
std::string GetIdentifierHeading(std::string dataLabel) const
void WaveformUsageCallback(std::string context, uint32_t waveformId)
Ptr< DataCollectionObject > m_aggregator
The aggregator created by this helper.
void DoInstall()
Install the probes, collectors, and aggregators necessary to produce the statistics output.
static TypeId GetTypeId()
inherited from ObjectBase base class
constexpr uint32_t MAX_BEAMS_PER_SATELLITE
Maximum number of beams per satellite.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.