-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiverPool.cpp
90 lines (78 loc) · 2.32 KB
/
ReceiverPool.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
#include <string>
#include <iostream>
#include <chrono>
#include <thread>
#include "common.h"
#include "ReceiverPool.h"
#include "NanomsgReceiver.h"
#include "ZeroMQReceiver.h"
#include "VMAReceiver.h"
#include "RsocketsReceiver.h"
namespace transport {
// Default constructor
ReceiverPool::ReceiverPool(Transport transport, const std::string &address, const size_t port = 24242, const size_t threads = 1) {
switch (transport) {
case Transport::TRANSPORT_NANOMSG:
cout << "starting " << threads << " Nanomsg server threads" << endl;
for (size_t i=0; i< threads; i++) {
receivers.emplace_back(std::thread(NanomsgReceiver(i, address, port +i, *this)));
}
break;
case Transport::TRANSPORT_ZMQ:
cout << "starting " << threads << " ZeroMQ server threads" << endl;
for (size_t i=0; i< threads; i++) {
receivers.emplace_back(std::thread(ZeroMQReceiver(i, address, port +i, *this)));
}
break;
case Transport::TRANSPORT_VMA:
cout << "starting " << threads << " VMA server threads" << endl;
for (size_t i=0; i< threads; i++) {
receivers.emplace_back(std::thread(VMAReceiver(i, address, port +i, *this)));
}
break;
case Transport::TRANSPORT_RDMACM:
cout << "starting " << threads << " Rsockets server threads" << endl;
for (size_t i=0; i< threads; i++) {
receivers.emplace_back(std::thread(RsocketsReceiver(i, address, port +i, *this)));
}
break;
}
cout << "Starting Statistic thread" << endl;
std::thread([this]() {
while (true)
{
print_stats();
std::this_thread::sleep_for(interval);
}
}).detach();
}
ReceiverPool::~ReceiverPool() {
for (auto &receiver: receivers)
receiver.join();
}
ReceiverPool &
ReceiverPool::Instance(Transport transport, const std::string &address, const size_t port, const size_t threads) {
static ReceiverPool pool{transport, address, port, threads};
return pool;
}
size_t
ReceiverPool::size() {
return receivers.size();
}
void
ReceiverPool::register_stats(ReceiverStats *stats) {
receiverStats.emplace_back(stats);
}
void
ReceiverPool::print_stats() {
ReceiverStats total_stats{};
size_t i{0};
for(ReceiverStats *stats: receiverStats) {
cout << "Thread " << ++i << " stats: " << endl;
total_stats.sum(stats);
stats->print_summary(interval);
}
cout << "Total stats: " << endl;
total_stats.print_summary(interval, false);
}
}