Line data Source code
1 : #include "bitrate.hpp"
2 :
3 : #include <fstream>
4 :
5 168 : BitRate::BitRate(double bitRate) {
6 168 : this->bitRate = bitRate;
7 168 : this->bitRateStr = std::to_string(bitRate);
8 168 : }
9 :
10 17 : std::vector<std::shared_ptr<BitRate>> BitRate::selectBitrateMethod(std::string fileName, int networkType) {
11 17 : switch (networkType) {
12 2 : case MB:
13 2 : return readBitRateFileMB(fileName);
14 : break;
15 15 : default:
16 15 : return readBitRateFile(fileName);
17 : }
18 : }
19 :
20 174 : void BitRate::addModulation(std::string modulation, int slots, double reach) {
21 174 : this->modulation.push_back(modulation);
22 174 : this->slots.push_back(slots);
23 174 : this->reach.push_back(reach);
24 174 : }
25 :
26 25 : void BitRate::addModulation(std::string modulation, int slots, double reach, std::vector<char> band,std::vector<int> slotsPerBand, std::vector<double> reachPerBand) {
27 25 : this->modulation.push_back(modulation);
28 25 : this->slots.push_back(slots);
29 25 : this->reach.push_back(reach);
30 25 : this->bands.push_back(band);
31 25 : this->slotsPerBand.push_back(slotsPerBand);
32 25 : this->reachPerBand.push_back(reachPerBand);
33 25 : }
34 :
35 10019 : std::string BitRate::getModulation(int pos) {
36 10019 : if (pos >= this->modulation.size()) {
37 : throw std::runtime_error(
38 14 : "Bitrate " + this->bitRateStr + " does not have more than " +
39 21 : std::to_string(this->modulation.size()) + " modulations.");
40 : }
41 10012 : return this->modulation[pos];
42 : }
43 :
44 0 : int BitRate::getDistanceAdaptive(const float &length) {
45 0 : int bestModulation = -1;
46 0 : double minReach = std::numeric_limits<double>::max();
47 :
48 0 : for (int i = 0; i < this->getNumberOfModulations(); ++i) {
49 0 : double reach = this->getReach(i);
50 0 : if (reach >= length && reach < minReach) {
51 0 : minReach = reach;
52 0 : bestModulation = i;
53 : }
54 : }
55 :
56 0 : return bestModulation;
57 : }
58 :
59 2 : char BitRate::getBand(int modulation,int pos){
60 2 : if (modulation >= this->bands.size()) {
61 : throw std::runtime_error(
62 0 : "Bitrate " + this->bitRateStr + " does not have more than " +
63 0 : std::to_string(this->bands.size()) + " modulations.");
64 : }
65 2 : if (pos >= this->bands[modulation].size()) {
66 : throw std::runtime_error(
67 0 : "Bitrate " + this->bitRateStr + " does not have more than " +
68 0 : std::to_string(this->bands[modulation].size()) + " bands.");
69 : }
70 2 : return this->bands[modulation][pos];
71 :
72 : }
73 10 : double BitRate::getReachPerBand(int modulation,int pos){
74 10 : if (modulation >= this->reachPerBand.size()) {
75 : throw std::runtime_error(
76 0 : "Bitrate " + this->bitRateStr + " does not have more than " +
77 0 : std::to_string(this->reachPerBand.size()) + " modulations.");
78 : }
79 10 : if (pos >= this->reachPerBand[modulation].size()) {
80 : throw std::runtime_error(
81 0 : "Bitrate " + this->bitRateStr + " does not have more than " +
82 0 : std::to_string(this->reachPerBand[modulation].size()) + " bands.");
83 : }
84 10 : return this->reachPerBand[modulation][pos];
85 : }
86 :
87 10 : int BitRate::getNumberOfSlotsPerBand(int modulation,int pos){
88 10 : if (modulation >= this->slotsPerBand.size()) {
89 : throw std::runtime_error(
90 0 : "Bitrate " + this->bitRateStr + " does not have more than " +
91 0 : std::to_string(this->slotsPerBand.size()) + " modulations.");
92 : }
93 10 : if (pos >= this->slotsPerBand[modulation].size()) {
94 : throw std::runtime_error(
95 0 : "Bitrate " + this->bitRateStr + " does not have more than " +
96 0 : std::to_string(this->slotsPerBand[modulation].size()) + " bands.");
97 : }
98 10 : return this->slotsPerBand[modulation][pos];
99 : }
100 :
101 53419 : int BitRate::getNumberOfSlots(int pos) {
102 53419 : if (pos >= this->slots.size()) {
103 : throw std::runtime_error(
104 2 : "Bitrate " + this->bitRateStr + " does not have more than " +
105 3 : std::to_string(this->slots.size()) + " modulations.");
106 : }
107 53418 : return this->slots[pos];
108 : }
109 :
110 10008 : double BitRate::getReach(int pos) {
111 10008 : if (pos >= this->reach.size()) {
112 : throw std::runtime_error(
113 2 : "Bitrate " + this->bitRateStr + " does not have more than " +
114 3 : std::to_string(this->reach.size()) + " modulations.");
115 : }
116 10007 : return this->reach[pos];
117 : }
118 :
119 18 : std::vector<std::shared_ptr<BitRate>> BitRate::readBitRateFile(std::string fileName) {
120 36 : std::ifstream file(fileName);
121 18 : if (!file.is_open()) {
122 0 : throw std::runtime_error("Could not open file: " + fileName);
123 : }
124 36 : nlohmann::ordered_json bitRate;
125 18 : std::vector<std::shared_ptr<BitRate>> vect = std::vector<std::shared_ptr<BitRate>>();
126 :
127 18 : file >> bitRate;
128 :
129 108 : for (auto& x : bitRate.items()) {
130 90 : int bitrate = stoi(x.key()); // BITRATE
131 :
132 90 : int numberOfModulations = x.value().size();
133 :
134 180 : std::shared_ptr<BitRate> aux = std::make_shared<BitRate>(bitrate);
135 198 : for (int i = 0; i < numberOfModulations; i++) {
136 216 : for (auto& w : x.value()[i].items()) {
137 216 : std::string modulation = w.key();
138 108 : int reach = w.value()["reach"];
139 108 : int slots = w.value()["slots"];
140 :
141 : // exceptions
142 108 : if (reach && slots < 0) {
143 : throw std::runtime_error(
144 0 : "value entered for slots and reach is less than zero");
145 : }
146 :
147 108 : if (reach < 0) {
148 0 : throw std::runtime_error("value entered for reach is less than zero");
149 : }
150 :
151 108 : if (slots < 0) {
152 0 : throw std::runtime_error("value entered for slots is less than zero");
153 : }
154 108 : aux->addModulation(modulation, slots, reach);
155 : }
156 : }
157 90 : vect.push_back(aux);
158 : }
159 :
160 36 : return vect;
161 : }
162 :
163 2 : std::vector<std::shared_ptr<BitRate>> BitRate::readBitRateFileMB(std::string fileName) {
164 4 : std::ifstream file(fileName);
165 2 : if (!file.is_open()) {
166 0 : throw std::runtime_error("Could not open file: " + fileName);
167 : }
168 4 : nlohmann::ordered_json bitRate;
169 2 : std::vector<std::shared_ptr<BitRate>> vect = std::vector<std::shared_ptr<BitRate>>();
170 :
171 2 : file >> bitRate;
172 :
173 8 : for (auto& x : bitRate.items()) {
174 6 : int bitrate = stoi(x.key()); // BITRATE
175 :
176 6 : int numberOfModulations = x.value().size();
177 :
178 12 : std::shared_ptr<BitRate> aux = std::make_shared<BitRate>(bitrate);
179 30 : for (int i = 0; i < numberOfModulations; i++) {
180 48 : for (auto& w : x.value()[i].items()) {
181 48 : std::string modulation = w.key();
182 24 : int numberOfBands = w.value().size();
183 24 : int slots=0;
184 24 : int reach=0;
185 48 : std::vector<char> band;
186 48 : std::vector<int> slotsPB;
187 48 : std::vector<double> reachPB;
188 72 : for (int j = 0; j < numberOfBands; j++) {
189 96 : for (auto& k : w.value()[j].items()){
190 96 : std::string charValue = k.key();
191 48 : band.push_back(charValue[0]);
192 48 : slotsPB.push_back(k.value()["slots"]);
193 48 : reachPB.push_back(k.value()["reach"]);
194 48 : reach+= k.value()["reach"].get<int>();
195 48 : slots+= k.value()["slots"].get<int>();
196 : // exceptions
197 48 : if (reach && slots < 0) {
198 : throw std::runtime_error(
199 0 : "value entered for slots and reach is less than zero");
200 : }
201 :
202 48 : if (reach < 0) {
203 0 : throw std::runtime_error("value entered for reach is less than zero");
204 : }
205 :
206 48 : if (slots < 0) {
207 0 : throw std::runtime_error("value entered for slots is less than zero");
208 : }
209 :
210 :
211 : }
212 :
213 : }
214 24 : aux->addModulation(modulation, slots, reach, band,slotsPB,reachPB);
215 : }
216 :
217 : }
218 6 : vect.push_back(aux);
219 : }
220 :
221 4 : return vect;
222 : }
223 :
224 10010 : std::string BitRate::getBitRateStr() { return this->bitRateStr; }
225 :
226 10010 : double BitRate::getBitRate() { return this->bitRate; }
227 :
228 2 : int BitRate::getNumberOfModulations() { return this->modulation.size(); }
229 :
230 0 : int BitRate::getNumberOfBands() { return this->bands[0].size(); }
231 2 : int BitRate::getNumberOfBands(int mod) { return this->bands[mod].size(); }
232 :
233 0 : std::map<char, int> BitRate::getPosBands(int mod){
234 0 : if (mod >= this->bands.size()) {
235 : throw std::runtime_error(
236 0 : "Bitrate " + this->bitRateStr + " does not have more than " +
237 0 : std::to_string(this->bands.size()) + " modulations.");
238 : }
239 0 : std::map<char, int> posBands;
240 0 : for(int i=0;i<this->bands[mod].size();i++){
241 0 : posBands[this->bands[mod][i]]=i;
242 : }
243 0 : return posBands;
244 : }
|