Skip to content

Commit 03d2927

Browse files
Merge pull request #10 from Unit-X/star-486-add-statistics-callback
STAR-486 Implement support for statistic callbacks
2 parents 300b998 + bed3e17 commit 03d2927

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

RISTNet.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ int RISTNetReceiver::clientDisconnect(void *pArg, rist_peer *pPeer) {
173173
return 0;
174174
}
175175

176+
int RISTNetReceiver::gotStatistics(void *pArg, const rist_stats *stats) {
177+
RISTNetReceiver *lWeakSelf = static_cast<RISTNetReceiver*>(pArg);
178+
if (lWeakSelf->statisticsCallback) {
179+
lWeakSelf->statisticsCallback(*stats);
180+
}
181+
return rist_stats_free(stats);
182+
}
183+
176184
//---------------------------------------------------------------------------------------------------------------------
177185
// RISTNetReceiver -- Callbacks --- End
178186
//---------------------------------------------------------------------------------------------------------------------
@@ -333,6 +341,13 @@ bool RISTNetReceiver::initReceiver(std::vector<std::string> &rURLList,
333341
return false;
334342
}
335343

344+
lStatus = rist_stats_callback_set(mRistContext, 1000, gotStatistics, this);
345+
if (lStatus) {
346+
LOGGER(true, LOGG_ERROR, "rist_stats_callback_set fail.")
347+
destroyReceiver();
348+
return false;
349+
}
350+
336351
lStatus = rist_start(mRistContext);
337352
if (lStatus) {
338353
LOGGER(true, LOGG_ERROR, "rist_receiver_start fail.")
@@ -456,6 +471,14 @@ int RISTNetSender::clientDisconnect(void *pArg, rist_peer *pPeer) {
456471
return 0;
457472
}
458473

474+
int RISTNetSender::gotStatistics(void *pArg, const rist_stats *stats) {
475+
RISTNetSender *lWeakSelf = static_cast<RISTNetSender*>(pArg);
476+
if (lWeakSelf->statisticsCallback) {
477+
lWeakSelf->statisticsCallback(*stats);
478+
}
479+
return rist_stats_free(stats);
480+
}
481+
459482
//---------------------------------------------------------------------------------------------------------------------
460483
// RISTNetSender -- Callbacks --- End
461484
//---------------------------------------------------------------------------------------------------------------------
@@ -609,6 +632,13 @@ bool RISTNetSender::initSender(std::vector<std::tuple<std::string,int>> &rPeerLi
609632
return false;
610633
}
611634

635+
lStatus = rist_stats_callback_set(mRistContext, 1000, gotStatistics, this);
636+
if (lStatus) {
637+
LOGGER(true, LOGG_ERROR, "rist_stats_callback_set fail.")
638+
destroySender();
639+
return false;
640+
}
641+
612642
lStatus = rist_start(mRistContext);
613643
if (lStatus) {
614644
LOGGER(true, LOGG_ERROR, "rist_sender_start fail.")

RISTNet.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ class RISTNetReceiver {
196196
*/
197197
static void getVersion(uint32_t &rCppWrapper, uint32_t &rRistMajor, uint32_t &rRistMinor);
198198

199-
//To be implemented
200-
//void getInfo();
201-
202199
/**
203200
* @brief Data receive callback
204201
*
@@ -242,6 +239,9 @@ class RISTNetReceiver {
242239
/// Callback handling disconnecting clients
243240
std::function<void(const std::shared_ptr<NetworkConnection>&, const rist_peer&)> clientDisconnectedCallback = nullptr;
244241

242+
/// Callback for statistics, called once every second
243+
std::function<void(const rist_stats& statistics)> statisticsCallback = nullptr;
244+
245245
// Delete copy and move constructors and assign operators
246246
RISTNetReceiver(RISTNetReceiver const &) = delete; // Copy construct
247247
RISTNetReceiver(RISTNetReceiver &&) = delete; // Move construct
@@ -265,11 +265,14 @@ class RISTNetReceiver {
265265
// Private method called when a client disconnects
266266
static int clientDisconnect(void *pArg, rist_peer *pPeer);
267267

268+
// Private method called when a statistics are delivered
269+
static int gotStatistics(void *pArg, const rist_stats *stats);
270+
268271
// The context of a RIST receiver
269272
rist_ctx *mRistContext = nullptr;
270273

271274
// The configuration of the RIST receiver
272-
rist_peer_config mRistPeerConfig = {0};
275+
rist_peer_config mRistPeerConfig{};
273276

274277
// The mutex protecting the list. since the list can be accessed from both librist and the C++ layer
275278
std::mutex mClientListMtx;
@@ -434,9 +437,6 @@ class RISTNetSender {
434437
*/
435438
static void getVersion(uint32_t &rCppWrapper, uint32_t &rRistMajor, uint32_t &rRistMinor);
436439

437-
//To be implemented
438-
//void getInfo();
439-
440440
/**
441441
* @brief OOB Data receive callback (__NULLABLE)
442442
*
@@ -467,6 +467,9 @@ class RISTNetSender {
467467
/// Callback handling disconnecting clients
468468
std::function<void(const std::shared_ptr<NetworkConnection>&, const rist_peer&)> clientDisconnectedCallback = nullptr;
469469

470+
/// Callback for statistics, called once every second
471+
std::function<void(const rist_stats& statistics)> statisticsCallback = nullptr;
472+
470473
// Delete copy and move constructors and assign operators
471474
RISTNetSender(RISTNetSender const &) = delete; // Copy construct
472475
RISTNetSender(RISTNetSender &&) = delete; // Move construct
@@ -487,11 +490,14 @@ class RISTNetSender {
487490
// Private method called when a client disconnects
488491
static int clientDisconnect(void *pArg, rist_peer *pPeer);
489492

493+
// Private method called when statistics are delivered
494+
static int gotStatistics(void *pArg, const rist_stats *stats);
495+
490496
// The context of a RIST sender
491497
rist_ctx *mRistContext = nullptr;
492498

493499
// The configuration of the RIST sender
494-
rist_peer_config mRistPeerConfig = {0};
500+
rist_peer_config mRistPeerConfig{};
495501

496502
// The mutex protecting the list. since the list can be accessed from both librist and the C++ layer
497503
std::mutex mClientListMtx;

test/TestRist.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,29 @@ TEST_F(TestFixture, SendReceive) {
213213
return 0;
214214
};
215215

216+
std::atomic<size_t> receiverStatisticCounter = 0;
217+
mReceiver->statisticsCallback = [&](const rist_stats& stats) {
218+
receiverStatisticCounter++;
219+
};
220+
std::atomic<size_t> senderStatisticCounter = 0;
221+
mSender->statisticsCallback = [&](const rist_stats& stats) {
222+
senderStatisticCounter++;
223+
};
224+
216225
std::vector<uint8_t> sendBuffer(kBufferSize);
217226
for (auto i = 0; i < kSentPackets; i++) {
218227
std::this_thread::sleep_for(std::chrono::milliseconds(50));
219228
std::fill(sendBuffer.begin(), sendBuffer.end(), '0' + i);
220229
EXPECT_TRUE(mSender->sendData(static_cast<const uint8_t*>(sendBuffer.data()), sendBuffer.size()));
221230
}
222231

232+
std::this_thread::sleep_for(std::chrono::seconds(10));
233+
234+
size_t numCallsReceiver = receiverStatisticCounter;
235+
EXPECT_GE(numCallsReceiver, 9) << "Expected the statistics to be delivered at least 9 times for receiver";
236+
size_t numCallsSender = senderStatisticCounter;
237+
EXPECT_GE(numCallsSender, 9) << "Expected the statistics to be delivered at least 9 times for sender";
238+
223239
{
224240
std::unique_lock<std::mutex> lock(receiverMutex);
225241
bool successfulWait =

0 commit comments

Comments
 (0)