-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgalapagos_net_tcp_server_send.hpp
More file actions
219 lines (177 loc) · 6.44 KB
/
galapagos_net_tcp_server_send.hpp
File metadata and controls
219 lines (177 loc) · 6.44 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//===============================
// AUTHOR : Naif Tarafdar
// CREATE DATE : April 20, 2019
//===============================
#ifndef __GALAPAGOS_NET_TCP_SERVER_SEND_HPP__ // if x.h hasn't been included yet...
#define __GALAPAGOS_NET_TCP_SERVER_SEND_HPP__
#include <cstdlib>
#include <iostream>
#include <thread>
#include <memory>
#include <utility>
#include <boost/asio.hpp>
#include "galapagos_interface.hpp"
#include "galapagos_net_tcp_session.hpp"
#if LOG_LEVEL > 0
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/fmt/bin_to_hex.h"
#endif
namespace galapagos{
namespace net{
/** @brief Class for the tcp_server_send, responsible for sending packets to sessions.
Runs as daemon in background, with an s_axis interface. If receives something on s_axis, then checks to see if a session exists, if it does then writes into s_axis of session, else creates a new session and then writes into s_axis of that session.
@author Naif Tarafdar
@date April 20, 2019
*/
template <class T>
class tcp_server_send{
public:
#if LOG_LEVEL > 0
tcp_server_send(short _port, boost::asio::io_context * _io_context,
tcp_session_container <T> * _sessions,
done_clean * _dc,
interface <T> * _s_axis,
std::shared_ptr <spdlog::logger> _logger
);
#endif
tcp_server_send(short _port, boost::asio::io_context * _io_context,
tcp_session_container <T> * _sessions,
done_clean * _dc,
interface <T> * _s_axis
);
void prepare(short _port, boost::asio::io_context * _io_context,
tcp_session_container <T> * _sessions,
done_clean * _dc,
interface <T> * _s_axis
);
~tcp_server_send(){;}
void send_new(std::string ip_addr);
void start();
done_clean * dc;
private:
short port;
void send_loop();
boost::asio::io_context * io_context;
boost::asio::io_context io_context_local;
tcp_session_container <T> * sessions;
std::unique_ptr <std::thread> t_send;
#if LOG_LEVEL > 0
std::shared_ptr<spdlog::logger> logger;
#endif
_done_struct done_struct;
interface <T> * s_axis;
};
}//net namespace
}//galapagos namespace
using namespace galapagos::net;
template <class T>
void tcp_server_send<T>::prepare(short _port,
boost::asio::io_context * _io_context,
tcp_session_container <T> * _sessions,
done_clean * _dc,
interface <T> * _s_axis)
{
dc = _dc;
port = _port;
io_context = _io_context;
sessions = _sessions;
s_axis = _s_axis;
}
#if LOG_LEVEL > 0
/**Constructor for galapagos::net::tcp_server_send
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[in] _port port number for tcp server listening to packets
@param[in] _io_context pointer to io_context needed to communicate over socket
@param[in] _done pointer to boolean that indicates node is done processing all local kernels
@param[in] _done_mutex mutex managing _done pointer
@param[in] _mutex_packets_in_flight mutex associated with packets_in_flight
@param[in] _packets_in_flight stores how many packets have left the router to be consumed by external drivers
@param[in] shared_ptr of logger used globally to log
*/
template <class T>
tcp_server_send<T>::tcp_server_send(short _port,
boost::asio::io_context * _io_context,
tcp_session_container <T> * _sessions,
done_clean * _dc,
interface <T> * _s_axis,
std::shared_ptr <spdlog::logger> _logger
)
{
prepare(_port, _io_context, _sessions, _dc, _s_axis);
logger = _logger;
}
#endif
template <class T>
tcp_server_send<T>::tcp_server_send(short _port,
boost::asio::io_context * _io_context,
tcp_session_container <T> * _sessions,
done_clean * _dc,
interface <T> * _s_axis
// std::shared_ptr <spdlog::logger> _logger
)
{
prepare(_port, _io_context, _sessions, _dc, _s_axis);
}
template <class T>
void tcp_server_send<T>::start(){
t_send = std::make_unique<std::thread>(&tcp_server_send::send_loop,this);
t_send->detach();
}
/**Reads from input and writes to session
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
*/
template <class T>
void tcp_server_send<T>::send_loop(){
do{
if(!s_axis->empty()){
short dest = s_axis->get_head_dest();
#if LOG_LEVEL > 0
logger->debug("tcp_server_send, in send loop, sending buffer to dest{0:d}", dest);
#endif
std::string ip_addr = sessions->get_ip_addr(dest);
bool session_found = sessions->find(ip_addr);
if (!session_found){
send_new(ip_addr);
}
else{
sessions->get_s_axis(ip_addr)->splice(s_axis);
#if LOG_LEVEL > 0
logger->debug("spliced(in loop) list now has {0:d} elements", s_axis->size());
#endif
}
}
}while(!dc->is_done());
dc->clean();
}
/**Creates a TCP session before sending data to session
@tparam T the type of data used within each galapagos packet (default ap_uint<64>)
@param[in] ip_addr address to create session
*/
template <class T>
void tcp_server_send<T>::send_new(std::string ip_addr){
boost::asio::ip::tcp::socket s(io_context_local);
boost::asio::ip::tcp::resolver resolver(io_context_local);
std::ostringstream convert;
convert << port;
std::string port_str = convert.str();
bool send_successful = false;
while(!send_successful){
try{
boost::asio::connect(s, resolver.resolve((char *)ip_addr.c_str(), (char *)port_str.c_str()));
send_successful = true;
sessions->add_session(std::move(s), &io_context_local);
#if LOG_LEVEL > 0
logger->debug("tcp_server_send, send_new created socket");
#endif
sessions->get_s_axis(ip_addr)->splice(s_axis);
#if LOG_LEVEL > 0
logger->debug("spliced(in new) list now has {0:d} elements", s_axis->size());
#endif
}
catch(const boost::system::system_error& ex){
send_successful = false;
}
}
}
#endif