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