-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathHost.cpp
More file actions
179 lines (150 loc) · 5.94 KB
/
Host.cpp
File metadata and controls
179 lines (150 loc) · 5.94 KB
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
/**
* \file src/plugins/output/forwarder/src/Host.cpp
* \author Michal Sedlak <xsedla0v@stud.fit.vutbr.cz>
* \brief Host class implementation
* \date 2021
*/
/* Copyright (C) 2021 CESNET, z.s.p.o.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is'', and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/
#include "Host.h"
#include <algorithm>
Host::Host(const std::string &ident, ConnectionParams con_params, ipx_ctx_t *log_ctx,
unsigned int tmplts_resend_pkts, unsigned int tmplts_resend_secs, bool indicate_lost_msgs,
Connector &connector) :
m_ident(ident),
m_con_params(con_params),
m_log_ctx(log_ctx),
m_tmplts_resend_pkts(tmplts_resend_pkts),
m_tmplts_resend_secs(tmplts_resend_secs),
m_indicate_lost_msgs(indicate_lost_msgs),
m_connector(connector)
{
}
void
Host::setup_connection(const ipx_session *session)
{
// There shouldn't be a connection for the session yet
assert(m_session_to_connection.find(session) == m_session_to_connection.end());
// Create new connection
IPX_CTX_INFO(m_log_ctx, "Setting up new connection to %s", m_ident.c_str());
m_session_to_connection.emplace(session,
std::unique_ptr<Connection>(new Connection(
m_ident,
m_con_params,
m_log_ctx,
m_tmplts_resend_pkts,
m_tmplts_resend_secs,
m_connector)));
m_session_to_connection[session]->connect();
}
void
Host::finish_connection(const ipx_session *session)
{
IPX_CTX_INFO(m_log_ctx, "Finishing a connection to %s", m_ident.c_str());
// Get the corresponding connection and remove it from the session map
std::unique_ptr<Connection> &connection = m_session_to_connection[session];
assert(connection);
// Try to send the waiting transfers if there are any, if the connection is dropped just finish anyway
if (connection->check_connected()) {
try {
connection->advance_transfers();
} catch (const ConnectionError &) {
// Ignore...
}
}
if (connection->waiting_transfers_cnt() > 0) {
IPX_CTX_WARNING(m_log_ctx, "Dropping %zu transfers when finishing connection",
connection->waiting_transfers_cnt());
}
IPX_CTX_INFO(m_log_ctx, "Connection to %s finished", m_ident.c_str());
m_session_to_connection.erase(session);
}
bool
Host::forward_message(ipx_msg_ipfix_t *msg)
{
const ipx_session *session = ipx_msg_ipfix_get_ctx(msg)->session;
Connection &connection = *m_session_to_connection[session].get();
if (!connection.check_connected()) {
if (m_indicate_lost_msgs) {
connection.lose_message(msg);
}
return false;
}
try {
connection.advance_transfers();
if (connection.waiting_transfers_cnt() > 0) {
IPX_CTX_DEBUG(m_log_ctx, "Message to %s not forwarded because there are unsent transfers\n", m_ident.c_str());
if (m_indicate_lost_msgs) {
connection.lose_message(msg);
}
return false;
}
IPX_CTX_DEBUG(m_log_ctx, "Forwarding message to %s\n", m_ident.c_str());
connection.forward_message(msg);
} catch (const ConnectionError &err) {
IPX_CTX_ERROR(m_log_ctx, "Lost connection while forwarding: %s", err.what());
connection.connect();
return false;
}
return true;
}
void
Host::advance_transfers()
{
for (auto &p : m_session_to_connection) {
Connection &connection = *p.second.get();
if (connection.check_connected()) {
connection.advance_transfers();
}
}
}
Host::~Host()
{
for (auto &p : m_session_to_connection) {
std::unique_ptr<Connection> &connection = p.second;
// Try to send the waiting transfers if there are any, if the connection is dropped just finish anyway
if (connection->check_connected()) {
try {
connection->advance_transfers();
} catch (const ConnectionError &) {
// Ignore...
}
}
if (connection->waiting_transfers_cnt() > 0) {
IPX_CTX_WARNING(m_log_ctx, "Dropping %zu transfers when closing connection %s",
connection->waiting_transfers_cnt(), connection->ident().c_str());
}
}
IPX_CTX_INFO(m_log_ctx, "All connections to %s closed", m_ident.c_str());
}