-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEFPBonding.cpp
313 lines (275 loc) · 10.2 KB
/
EFPBonding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright Edgeware AB 2020, Agile Content 2021-2022
#include "EFPBonding.h"
#include "logger.h"
//Constructor
EFPBonding::EFPBonding() {
EFP_LOGGER(true, LOGG_NOTIFY, "EFPBonding constructed")
mStreamInterfaces = std::vector<std::vector<EFPInterface>>(256);
mGlobalPacketCounter = 0;
}
//Destructor
EFPBonding::~EFPBonding() {
EFP_LOGGER(true, LOGG_NOTIFY, "EFPBonding destruct")
}
//Change interface commits in a group
EFPBondingMessages EFPBonding::modifyInterfaceCommits(std::vector<EFPBonding::EFPInterfaceCommit> &rInterfacesCommit, EFPBonding::EFPBondingGroupID groupID) {
EFPGroup *targetGroup = nullptr;
for (auto &rGroup: mGroupList) {
if (rGroup.mGroupID == groupID) {
targetGroup = &rGroup;
}
}
if (targetGroup == nullptr) {
return EFPBondingMessages::groupNotFound;
}
//We found the target group
//Check validity of the data;
//We need to iterate over all interfaces before committing anything
//to make sure all data and all ID's are there and correct
double totalCommits = 0.0;
for (auto const &rThisCommit: rInterfacesCommit) {
if (rThisCommit.mCommit < 0.0) {
return EFPBondingMessages::parameterError;
}
//Is the interface part of the group?
bool lInterfaceFound = false;
for (auto const &rInterface: targetGroup->mGroupInterfaces) {
if (rInterface->mInterfaceID == rThisCommit.mInterfaceID) {
lInterfaceFound = true;
break;
}
}
if (!lInterfaceFound) {
return EFPBondingMessages::interfaceIDNotFound;
}
totalCommits += rThisCommit.mCommit;
}
if (totalCommits > 100.0) {
return EFPBondingMessages::sumCommitHigherThan100Percent;
}
if (totalCommits == 0.0) {
return EFPBondingMessages::parameterError;
}
//Here we got some commit level and the interfaces are known to us.
std::vector<std::shared_ptr<EFPInterface>> lNotModifiedInterfaces;
double totalRestCommits = 0.0;
//Set the commit for the interfaces we know and save the sum commit of the rest
for (auto const &rInterface: targetGroup->mGroupInterfaces) {
bool lThisInterfaceChanged = false;
for (auto const &rThisCommit: rInterfacesCommit) {
if (rInterface->mInterfaceID == rThisCommit.mInterfaceID) {
rInterface->mCommit = rThisCommit.mCommit;
lThisInterfaceChanged = true;
break;
}
}
if (!lThisInterfaceChanged) {
lNotModifiedInterfaces.push_back(rInterface);
totalRestCommits += rInterface->mCommit;
}
}
if (!lNotModifiedInterfaces.size()) {
//all interfaces where modified no need to balance out the rest
return EFPBondingMessages::noError;
}
double leftToBalanceOut = 100.0 - totalCommits;
if (leftToBalanceOut < 0.1 || totalRestCommits < 0.01 ) {
//we got less than 0.1% to balance out.. Then the master interface takes that load.
//We just say no error then less than 0.1% payload will end up on the master interface.
//Or the interfaces that we want to balance
return EFPBondingMessages::noError;
}
double lDiffMultiplicationFactor = leftToBalanceOut / totalRestCommits;
for (auto const &rInterface: lNotModifiedInterfaces) {
double newCommit = rInterface->mCommit * lDiffMultiplicationFactor;
rInterface->mCommit = newCommit;
}
return EFPBondingMessages::noError;
}
//Get statistics for a interface
EFPBonding::EFPStatistics EFPBonding::getStatistics(EFPBonding::EFPBondingInterfaceID interfaceID,
EFPBonding::EFPBondingGroupID groupID,
bool reset) {
EFPBonding::EFPStatistics lMyStatistics;
//Stats is for split interface
if (interfaceID && !groupID) {
for (auto &rInterfaces: mStreamInterfaces) {
for (auto &rInterface: rInterfaces) {
if (rInterface.mInterfaceID == interfaceID) {
lMyStatistics.mNoFragmentsSent += rInterface.mFragmentCounter;
if (reset) {
rInterface.mFragmentCounter = 0;
}
}
}
}
return lMyStatistics;
}
if (!interfaceID || !groupID || !mGroupList.size()) {
return lMyStatistics;
}
for (auto const &rGroup: mGroupList) {
if (rGroup.mGroupID == groupID) {
for (auto const &rInterface: rGroup.mGroupInterfaces) {
if (rInterface->mInterfaceID == interfaceID) {
lMyStatistics.mNoFragmentsSent = rInterface->mFragmentCounter;
lMyStatistics.mNoGapsCoveredFor = rInterface->mForwardMissingFragment;
lMyStatistics.mPercentOfTotalTraffic =
((double) rInterface->mFragmentCounter / (double) mGlobalPacketCounter) * 100.0;
if (reset) {
rInterface->mFragmentCounter = 0;
rInterface->mForwardMissingFragment = 0;
}
return lMyStatistics;
}
}
}
}
return lMyStatistics;
}
//Get the global packet counter
uint64_t EFPBonding::getGlobalPacketCounter() {
return mGlobalPacketCounter;
}
void EFPBonding::increaseGlobalPacketCounter() {
mGlobalPacketCounter += 1;
}
//Re-set the global packet counter
void EFPBonding::clearGlobalPacketCounter() {
mGlobalPacketCounter = 0;
}
//Generate unique interfaceID
EFPBonding::EFPBondingInterfaceID EFPBonding::generateInterfaceID() {
return mUniqueInterfaceID++;
}
EFPBondingMessages EFPBonding::addInterfaceToStreamID(EFPInterface interface, std::vector<uint8_t> &rEFPIDList) {
for (auto const &rEFPID: rEFPIDList) {
if (mStreamInterfaces[rEFPID].size()) {
for (auto const &rInterface: mStreamInterfaces[rEFPID]) {
if (rInterface.mInterfaceID == interface.mInterfaceID) {
return EFPBondingMessages::interfaceAlreadyAdded;
}
}
}
mStreamInterfaces[rEFPID].push_back(interface);
}
return EFPBondingMessages::noError;
}
EFPBondingMessages EFPBonding::removeInterfaceFromStreamID(EFPBondingInterfaceID interfaceID, std::vector<uint8_t> &rEFPIDList) {
for (auto const &rEFPID: rEFPIDList) {
if (mStreamInterfaces[rEFPID].size()) {
int lInterfaceIndex = 0;
for (auto const &rInterface: mStreamInterfaces[rEFPID]) {
if (rInterface.mInterfaceID == interfaceID) {
mStreamInterfaces[rEFPID].erase(mStreamInterfaces[rEFPID].begin() + lInterfaceIndex);
}
lInterfaceIndex++;
}
}
}
return EFPBondingMessages::noError;
}
//Add a interface group to EFPBond
EFPBonding::EFPBondingGroupID EFPBonding::addInterfaceGroup(std::vector<EFPInterface> &rInterfaces) {
if (!rInterfaces.size()) { //We need at least a interface
return 0;
}
//Auto assign offset and coverage
double lCommit = 100.0 / (double) rInterfaces.size();
double lOffset = 0.0;
bool didProvideMasterInterface = false;
EFPGroup lGroup;
lGroup.mGroupID = mUniqueGroupID;
for (auto const &rInterface: rInterfaces) {
if (rInterface.mMasterInterface) {
didProvideMasterInterface = true;
}
if (rInterface.mInterfaceLocation == nullptr) {
EFP_LOGGER(true, LOGG_ERROR, "Did not provide interface location")
return 0;
}
std::shared_ptr<EFPInterface> lThisInterface = std::make_shared<EFPInterface>();
lThisInterface->mInterfaceLocation = rInterface.mInterfaceLocation;
lThisInterface->mCommit = lCommit;
lThisInterface->mFireCounter = 0.0;
lThisInterface->mInterfaceID = rInterface.mInterfaceID;
lThisInterface->mMasterInterface = rInterface.mMasterInterface;
lThisInterface->mFragmentCounter = 0;
lThisInterface->mForwardMissingFragment = 0;
lThisInterface->mForwardMissingFragment = 0;
lGroup.mGroupInterfaces.push_back(std::move(lThisInterface));
lOffset += lCommit;
}
if (!didProvideMasterInterface) {
EFP_LOGGER(true, LOGG_ERROR, "Did not provide master interface")
return 0;
}
mGroupList.push_back(std::move(lGroup));
return mUniqueGroupID++;
}
EFPBondingMessages EFPBonding::removeGroup(EFPBondingGroupID groupID) {
int lGroupIndex = 0;
for (auto const &rGroup: mGroupList) {
if (rGroup.mGroupID == groupID) {
mGroupList.erase(mGroupList.begin() + lGroupIndex);
return EFPBondingMessages::noError;
}
lGroupIndex++;
}
return EFPBondingMessages::groupNotFound;
}
EFPBondingMessages EFPBonding::splitData(const std::vector<uint8_t> &rSubPacket, uint8_t fragmentID) {
bool noFragmentReciever = true;
if (fragmentID) {
if (mStreamInterfaces[fragmentID].size()) {
for (auto &rInterface: mStreamInterfaces[fragmentID]) {
rInterface.mInterfaceLocation(rSubPacket);
rInterface.mFragmentCounter++;
noFragmentReciever = false;
}
}
}
if (noFragmentReciever) {
return EFPBondingMessages::fragmentNotSent;
}
return EFPBondingMessages::noError;
}
EFPBondingMessages EFPBonding::distributeData(const std::vector<uint8_t> &rSubPacket, uint8_t fragmentID) {
if (!mGroupList.size()) {
return EFPBondingMessages::noGroupsFound;
}
uint64_t currentPercentage = mMonotonicPacketCounter % 100;
if (!currentPercentage) {
for (auto const &rGroup: mGroupList) {
for (auto const &rInterface: rGroup.mGroupInterfaces) {
rInterface->mFireCounter += rInterface->mCommit;
}
}
}
for (auto const &rGroup: mGroupList) {
std::vector<std::shared_ptr<EFPInterface>> lRoundRobinInterfaces;
for (auto const &rInterface: rGroup.mGroupInterfaces) {
if (rInterface->mFireCounter >= 1.0) {
lRoundRobinInterfaces.push_back(rInterface);
}
}
//This if covers for fractional calculation to integer loops (you can't send 0,113 fragments for example
//Example 1/3 equals integer 33% * 3interfaces has at 100% sent 99% data in the first revolution.
if (!lRoundRobinInterfaces.size()) {
for (auto const &rInterface: rGroup.mGroupInterfaces) {
if (rInterface->mMasterInterface) {
rInterface->mInterfaceLocation(rSubPacket);
rInterface->mFragmentCounter++;
rInterface->mForwardMissingFragment++;
}
}
} else {
uint64_t targetInterface = mMonotonicPacketCounter % lRoundRobinInterfaces.size();
lRoundRobinInterfaces[targetInterface]->mFireCounter--;
lRoundRobinInterfaces[targetInterface]->mInterfaceLocation(rSubPacket);
lRoundRobinInterfaces[targetInterface]->mFragmentCounter++;
}
}
mMonotonicPacketCounter++;
return EFPBondingMessages::noError;
}