forked from CESNET/ipfixcol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sender.cpp
82 lines (67 loc) · 2.02 KB
/
Sender.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
#include "Sender.h"
#include <stdexcept>
#include <sys/time.h>
static const char *msg_module = "json sender";
#define DEFAULT_IP "127.0.0.1"
#define DEFAULT_PORT "4739"
#define DEFAULT_TYPE "UDP"
Sender::Sender(const pugi::xpath_node &config)
{
std::string ip = config.node().child_value("ip");
std::string port = config.node().child_value("port");
std::string proto = config.node().child_value("protocol");
/* Check IP address */
if (ip.empty()) {
MSG_WARNING(msg_module, "IP address not set, using default: %s", DEFAULT_IP);
ip = DEFAULT_IP;
}
/* Check port number */
if (port.empty()) {
MSG_WARNING(msg_module, "Port number not set, using default: %s", DEFAULT_PORT);
port = DEFAULT_PORT;
}
/* Check connection type */
if (proto.empty()) {
MSG_WARNING(msg_module, "Protocol not set, using default: %s", DEFAULT_TYPE);
proto = DEFAULT_TYPE;
}
/* Create sender */
sender = siso_create();
if (sender == NULL) {
throw std::runtime_error("Memory error - cannot create sender object");
}
/* Connect it */
if (siso_create_connection(sender, ip.c_str(), port.c_str(), proto.c_str()) != SISO_OK) {
std::string error = siso_get_last_err(sender);
siso_destroy(sender);
throw std::runtime_error(error);
}
gettimeofday(&connection_time, NULL);
}
Sender::~Sender()
{
siso_destroy(sender);
}
void Sender::ProcessDataRecord(const std::string &record)
{
if (siso_is_connected(sender) == 0) {
// Not connected -> try to reconnect
struct timeval current_time;
gettimeofday(¤t_time, NULL);
// Try only one reconnection per second
if (connection_time.tv_sec >= current_time.tv_sec) {
return;
}
connection_time = current_time;
if (siso_reconnect(sender) == SISO_OK) {
MSG_INFO(msg_module, "Successfully reconnected.", NULL);
} else {
MSG_WARNING(msg_module, "Reconnection failed.", NULL);
return;
}
}
if (siso_send(sender, record.c_str(), record.length()) != SISO_OK) {
MSG_ERROR(msg_module, "Failed to send JSON data (%s). Connection closed.",
siso_get_last_err(sender));
}
}