-
Notifications
You must be signed in to change notification settings - Fork 53
/
nmmsender.hh
87 lines (82 loc) · 2.31 KB
/
nmmsender.hh
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
#pragma once
#include <string>
#include <deque>
#include <map>
#include <atomic>
#include "navmon.pb.h"
#include <thread>
#include <mutex>
#include "zstdwrap.hh"
#include "comboaddress.hh"
#include "swrappers.hh"
#include "sclasses.hh"
class NMMSender
{
struct Destination
{
int fd{-1};
std::string dst;
std::string fname;
bool listener{false};
std::deque<std::string> queue;
std::mutex mut;
void emitNMM(const std::string& out, bool compress);
std::vector<Destination> clients;
};
public:
void addDestination(int fd)
{
auto d = std::make_unique<Destination>();
d->fd = fd;
std::lock_guard<std::mutex> l(d_destslock);
d_dests.push_back(std::move(d));
}
void addDestination(const std::string& dest)
{
auto d = std::make_unique<Destination>();
d->dst = dest;
std::lock_guard<std::mutex> l(d_destslock);
d_dests.push_back(std::move(d));
}
void addListener(const std::string& dest)
{
auto d = std::make_unique<Destination>();
d->dst = dest;
d->listener = true;
std::lock_guard<std::mutex> l(d_destslock);
d_dests.push_back(std::move(d));
}
void launch()
{
for(auto& d : d_dests) {
if(d->listener) {
d_thread.emplace_back(std::move(std::make_unique<std::thread>(&NMMSender::acceptorThread, this, d.get())));
}
else if(!d->dst.empty()) {
d_thread.emplace_back(std::move(std::make_unique<std::thread>(&NMMSender::sendTCPThread, this, d.get())));
}
}
}
void sendTCPThread(Destination* d);
void acceptorThread(Destination* d);
void forwarderThread(Destination* d, NMMSender* there);
void sendTCPListenerThread(Destination* d, int fd, ComboAddress remote);
void sendLoop(Destination* d, SocketCommunicator& sc, std::unique_ptr<ZStdCompressor>& zsc, Socket& s, std::map<uint32_t, std::string>& unacked, time_t connStartTime);
void emitNMM(const NavMonMessage& nmm);
void emitNMM(const std::string& out);
bool d_debug{false};
bool d_compress{false}; // set BEFORE launch
bool d_pleaseQuit{false};
~NMMSender()
{
if(!d_thread.empty()) {
d_pleaseQuit = true;
for(auto& t : d_thread)
t->join();
}
}
private:
std::mutex d_destslock;
std::vector<std::unique_ptr<Destination>> d_dests;
std::vector<std::unique_ptr<std::thread>> d_thread;
};