satellite-static-bstp.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 Magister Solutions Ltd
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: Jani Puttonen <jani.puttonen@magister.fi>
19  */
20 
21 #include "satellite-static-bstp.h"
22 
23 #include "../utils/satellite-env-variables.h"
24 
25 #include <ns3/log.h>
26 #include <ns3/singleton.h>
27 
28 #include <algorithm>
29 #include <istream>
30 #include <sstream>
31 
32 NS_LOG_COMPONENT_DEFINE("SatStaticBstp");
33 
34 namespace ns3
35 {
36 
38  : m_bstp(),
39  m_currentIterator(0),
40  m_beamGwMap(),
41  m_beamFeederFreqIdMap(),
42  m_enabledBeams()
43 {
44  NS_LOG_FUNCTION(this);
45  NS_ASSERT(false);
46 }
47 
48 SatStaticBstp::SatStaticBstp(std::string fileName)
49  : m_bstp(),
50  m_currentIterator(0)
51 {
52  NS_LOG_FUNCTION(this);
53 
54  std::string dataPath = Singleton<SatEnvVariables>::Get()->LocateDataDirectory() + "/";
55 
56  // Load satellite configuration file
57  LoadBstp(dataPath + fileName);
58 }
59 
60 void
61 SatStaticBstp::LoadBstp(std::string filePathName)
62 {
63  NS_LOG_FUNCTION(this << filePathName);
64 
65  // READ FROM THE SPECIFIED INPUT FILE
66  std::ifstream* ifs = new std::ifstream(filePathName.c_str(), std::ifstream::in);
67 
68  if (!ifs->is_open())
69  {
70  // script might be launched by test.py, try a different base path
71  delete ifs;
72  filePathName = "../../" + filePathName;
73  ifs = new std::ifstream(filePathName.c_str(), std::ifstream::in);
74 
75  if (!ifs->is_open())
76  {
77  NS_FATAL_ERROR("The file " << filePathName << " is not found.");
78  }
79  }
80 
81  std::string line;
82 
83  // Read the lines in the file one by one
84  while (std::getline(*ifs, line, '\n'))
85  {
86  // Convert the line to stringstream
87  std::stringstream ssLine;
88  ssLine << line;
89  std::string col;
90  std::vector<uint32_t> tempVector;
91 
92  // Go through the line with a comma as a delimiter, convert
93  // the read strings to double and add to temp container.
94  while (std::getline(ssLine, col, ','))
95  {
96  std::stringstream ssCol;
97  ssCol << col;
98  uint32_t uCol;
99  ssCol >> uCol;
100  tempVector.push_back(uCol);
101  }
102 
103  NS_ASSERT(tempVector.size() >= 2);
104 
105  m_bstp.push_back(tempVector);
106  }
107 
108  NS_ASSERT(!m_bstp.empty());
109 
110  ifs->close();
111  delete ifs;
112 }
113 
114 std::vector<uint32_t>
116 {
117  NS_LOG_FUNCTION(this);
118 
119  uint32_t iter = m_currentIterator;
120 
121  NS_ASSERT(iter < m_bstp.size());
122 
128  if (m_currentIterator >= m_bstp.size())
129  {
130  m_currentIterator = 0;
131  }
132 
133  return m_bstp.at(iter);
134 }
135 
136 void
138  uint32_t userFreqId,
139  uint32_t feederFreqId,
140  uint32_t gwId)
141 {
142  NS_LOG_FUNCTION(this << beamId << userFreqId << feederFreqId << gwId);
143 
144  NS_ASSERT(userFreqId == 1);
145 
146  m_beamGwMap.insert(std::make_pair(beamId, gwId));
147  m_beamFeederFreqIdMap.insert(std::make_pair(beamId, feederFreqId));
148 
149  m_enabledBeams.push_back(beamId);
150 }
151 
152 void
154 {
155  NS_LOG_FUNCTION(this);
156 
157  if (m_bstp.empty())
158  {
159  NS_FATAL_ERROR("BSTP container is empty!");
160  }
161 
162  std::vector<uint32_t> enabledBeams = m_enabledBeams;
163  std::map<uint32_t, uint32_t> beamIds;
164  std::map<uint32_t, std::vector<uint32_t>> gwFeederFreqs;
165 
166  // All lines
167  for (uint32_t i = 0; i < m_bstp.size(); i++)
168  {
169  beamIds.clear();
170  gwFeederFreqs.clear();
171 
172  // A single BSTP configuration
173  std::vector<uint32_t> confEntry = m_bstp.at(i);
174  for (uint32_t j = 1; j < confEntry.size(); j++)
175  {
176  uint32_t beamId = confEntry.at(j);
177 
178  std::vector<uint32_t>::iterator eIt =
179  std::find(enabledBeams.begin(), enabledBeams.end(), beamId);
180  if (eIt != enabledBeams.end())
181  {
182  enabledBeams.erase(eIt);
183  }
184 
185  // One beam id can only exist once at each line
186  if ((beamIds.insert(std::make_pair(beamId, 0))).second == false)
187  {
188  NS_FATAL_ERROR("Beam id: " << confEntry.at(j)
189  << " is located twice in the BSTP line: " << i);
190  }
191 
192  // Find the GW and feeder freq for this beamId
193  std::map<uint32_t, uint32_t>::iterator ffIt = m_beamFeederFreqIdMap.find(beamId);
194  std::map<uint32_t, uint32_t>::iterator gwIt = m_beamGwMap.find(beamId);
195 
196  // Check that the beam is enabled!
197  if (ffIt != m_beamFeederFreqIdMap.end() && gwIt != m_beamGwMap.end())
198  {
199  uint32_t feederFreqId = ffIt->second;
200  uint32_t gwId = gwIt->second;
201 
202  // Find GW and add this feeder freq for it.
203  std::map<uint32_t, std::vector<uint32_t>>::iterator findGw =
204  gwFeederFreqs.find(gwId);
205 
206  // If it is not found, add a proper vector for it.
207  if (findGw == gwFeederFreqs.end())
208  {
209  std::vector<uint32_t> ffIds;
210  ffIds.push_back(feederFreqId);
211  gwFeederFreqs.insert(std::make_pair(gwId, ffIds));
212  }
213  else
214  {
215  // Find whether we have already stored this feeder freq for
216  // this GW!
217  std::vector<uint32_t>::iterator findFfIt =
218  std::find(findGw->second.begin(), findGw->second.end(), feederFreqId);
219 
220  // If we have, we should trigger a fatal error since GW cannot serve
221  // two beams with the same feeder freq at the same time.
222  if (findFfIt != findGw->second.end())
223  {
224  NS_FATAL_ERROR("Feeder link freq id: " << feederFreqId
225  << " already found for GW: "
226  << findGw->first);
227  }
228 
229  // Otherwise just store the feeder freq
230  findGw->second.push_back(feederFreqId);
231  }
232  }
233  else
234  {
235  NS_LOG_WARN("Beam id: " << beamId << " is not enabled, but it located at BSTP!");
236  }
237  }
238  }
239 
240  // All enabled spot-beams need to have at least one
241  // scheduling entry
242  if (enabledBeams.size() > 0)
243  {
244  NS_FATAL_ERROR("All enabled beams are not in the BSTP configuration!");
245  }
246 }
247 
248 } // namespace ns3
std::map< uint32_t, uint32_t > m_beamGwMap
void LoadBstp(std::string filePathName)
Load BSTP configuration from a file.
void CheckValidity()
Check validity of the individual BSTP configuration line.
std::vector< uint32_t > GetNextConf() const
Get the next configuration file.
std::vector< std::vector< uint32_t > > m_bstp
std::map< uint32_t, uint32_t > m_beamFeederFreqIdMap
std::vector< uint32_t > m_enabledBeams
SatStaticBstp()
Default constructor.
void AddEnabledBeamInfo(uint32_t beamId, uint32_t userFreqId, uint32_t feederFreqId, uint32_t gwId)
Add the information about which spot-beams are enabled in this simulation.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.