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