8 #include "ace/INET_Addr.h" 9 #include "ace/SOCK_Dgram.h" 17 :
Base (id, config, context)
47 "UdpTransport::cleanup:" \
48 " Error closing write socket\n");
55 "UdpTransport::cleanup:" \
56 " Error closing read socket\n");
89 "UdpTransport::setup:" \
90 " socket failed to open\n");
94 int rcv_buff_size = 0;
95 int opt_len =
sizeof (int);
98 (
void *)&send_buff_size, &opt_len);
101 (
void *)&rcv_buff_size, &opt_len);
104 "UdpTransport::setup:" \
105 " default socket buff size is send=%d, rcv=%d\n",
106 send_buff_size, rcv_buff_size);
108 if (send_buff_size < tar_buff_size)
111 "UdpTransport::setup:" \
112 " setting send buff size to settings.queue_length (%d)\n",
116 (
void *)&tar_buff_size, opt_len);
119 (
void *)&send_buff_size, &opt_len);
122 "UdpTransport::setup:" \
123 " current socket buff size is send=%d, rcv=%d\n",
124 send_buff_size, rcv_buff_size);
127 if (rcv_buff_size < tar_buff_size)
130 "UdpTransport::setup:" \
131 " setting rcv buff size to settings.queue_length (%d)\n",
135 (
void *)&tar_buff_size, opt_len);
138 (
void *)&rcv_buff_size, &opt_len);
141 "UdpTransport::setup:" \
142 " current socket buff size is send=%d, rcv=%d\n",
143 send_buff_size, rcv_buff_size);
155 "UdpTransport::setup:" \
156 " Error subscribing to udp address %s:%d\n",
157 local.get_host_addr (), local.get_port_number ());
162 "UdpTransport::setup:" \
163 " Success subscribing to udp address %s:%d\n",
164 local.get_host_addr (), local.get_port_number ());
167 int rcv_buff_size = 0;
168 int opt_len =
sizeof (int);
172 bare_socket.get_option (SOL_SOCKET, SO_RCVBUF,
173 (
void *)&rcv_buff_size, &opt_len);
176 "UdpTransport::setup:" \
177 " default socket buff size is send=%d, rcv=%d\n",
178 send_buff_size, rcv_buff_size);
180 if (send_buff_size < tar_buff_size)
183 "UdpTransport::setup:" \
184 " setting send buff size to settings.queue_length (%d)\n",
187 bare_socket.set_option (SOL_SOCKET, SO_SNDBUF,
188 (
void *)&tar_buff_size, opt_len);
190 bare_socket.get_option (SOL_SOCKET, SO_SNDBUF,
191 (
void *)&send_buff_size, &opt_len);
194 "UdpTransport::setup:" \
195 " current socket buff size is send=%d, rcv=%d\n",
196 send_buff_size, rcv_buff_size);
199 if (rcv_buff_size < tar_buff_size)
202 "UdpTransport::setup:" \
203 " setting rcv buff size to settings.queue_length (%d)\n",
206 bare_socket.set_option (SOL_SOCKET, SO_RCVBUF,
207 (
void *)&tar_buff_size, opt_len);
209 bare_socket.get_option (SOL_SOCKET, SO_RCVBUF,
210 (
void *)&rcv_buff_size, &opt_len);
213 "UdpTransportReadThread::setup:" \
214 " current socket buff size is send=%d, rcv=%d\n",
215 send_buff_size, rcv_buff_size);
231 "UdpTransportReadThread::setup:" \
237 std::stringstream thread_name;
238 thread_name <<
"read";
256 const char * print_prefix =
"UdpTransport::send_data";
262 result =
prep_send (orig_updates, print_prefix);
266 uint64_t bytes_sent = 0;
267 uint64_t packet_size = (uint64_t)result;
275 " fragmenting %" PRIu64
" byte packet (%" PRIu32
" bytes is max fragment size)\n",
282 for (FragmentMap::iterator i = map.begin (); i != map.end (); ++i, ++j)
287 for (std::map <std::string, ACE_INET_Addr>::const_iterator addr =
294 " Sending fragment %d\n",
297 int send_attempts = -1;
298 ssize_t actual_sent = -1;
300 while (actual_sent < 0 &&
307 i->second, frag_size, addr->second);
316 "%s: Send result was %d of %d byte fragment to %s\n",
317 print_prefix, actual_sent, frag_size, addr->first.c_str ());
321 bytes_sent = (uint64_t)actual_sent;
325 " Sent packet of size %" PRIu64
"\n",
326 print_prefix, bytes_sent);
330 else if (actual_sent == ECONNRESET)
334 " WARNING: Remote socket disappeared during send (ECONNRESET)\n",
337 else if (actual_sent == EINTR)
341 " Local socket was interrupted during send (EINTR)\n",
344 else if (actual_sent == EWOULDBLOCK)
348 " Send would have blocked (EWOULDBLOCK)\n",
351 else if (actual_sent == ENOTCONN)
355 " Send reports socket is not connected (ENOTCONN)\n",
358 else if (actual_sent == EADDRINUSE)
362 " Send reports the interface is busy (EADDRINUSE)\n",
365 else if (actual_sent == EBADF)
369 " Send socket is invalid (EBADF)\n",
376 " Packet was not sent due to unknown error (%d)\n",
377 print_prefix, (int)actual_sent);
391 " Sent fragments totalling %" PRIu64
" bytes\n",
392 print_prefix, bytes_sent);
398 for (std::map <std::string, ACE_INET_Addr>::const_iterator i =
405 " Sending packet of size %ld\n",
406 print_prefix, result);
408 int send_attempts = -1;
409 ssize_t actual_sent = -1;
411 while (actual_sent < 0 &&
418 (ssize_t)result, i->second);
424 bytes_sent += actual_sent;
428 " Sent packet of size %" PRIu64
"\n",
429 print_prefix, (int)actual_sent);
433 else if (actual_sent == ECONNRESET)
437 " WARNING: Remote socket disappeared during send (ECONNRESET)\n",
440 else if (actual_sent == EINTR)
444 " Local socket was interrupted during send (EINTR)\n",
447 else if (actual_sent == EWOULDBLOCK)
451 " Send would have blocked (EWOULDBLOCK)\n",
454 else if (actual_sent == ENOTCONN)
458 " Send reports socket is not connected (ENOTCONN)\n",
461 else if (actual_sent == EADDRINUSE)
465 " Send reports the interface is busy (EADDRINUSE)\n",
468 else if (actual_sent == EBADF)
472 " Send socket is invalid (EBADF)\n",
479 " Packet was not sent due to unknown error (%d)\n",
480 print_prefix, (int)actual_sent);
487 result = (long) bytes_sent;
491 " Send bandwidth = %" PRIu64
" B/s\n",
int setup(void)
all subclasses should call this method at the end of its setup
QoSTransportSettings settings_
long prep_send(const madara::knowledge::KnowledgeRecords &orig_updates, const char *print_prefix)
Preps a message for sending.
uint64_t get_bytes_per_second(void)
Queries the monitor for the current bandwidth utilization per second over the past window...
UdpTransport(const std::string &id, madara::knowledge::ThreadSafeContext &context, TransportSettings &config, bool launch_transport)
Constructor.
void invalidate_transport(void)
Invalidates a transport to indicate it is shutting down.
MADARA_Export void frag(char *source, uint32_t fragment_size, FragmentMap &map)
Breaks a large packet into smaller packets.
int validate_transport(void)
Validates a transport to indicate it is not shutting down.
This class stores variables and their values for use by any entity needing state information in a thr...
std::vector< std::string > hosts
Host information for transports that require it.
knowledge::KnowledgeBase knowledge_
knowledge base for threads to use
std::map< std::string, ACE_INET_Addr > addresses_
double read_thread_hertz
number of valid messages allowed to be received per second.
MADARA_Export double sleep(double sleep_time)
Sleeps for a certain amount of time.
Holds basic transport settings.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
void close(void)
Closes this transport.
BandwidthMonitor receive_monitor_
monitor for receiving bandwidth usage
void run(const std::string name, BaseThread *thread, bool paused=false)
Starts a new thread and executes the provided user thread once.
threads::Threader read_threads_
threads for reading knowledge updates
T * get_ptr(void)
get the underlying pointer
bool no_receiving
if true, never receive over transport
::std::map< std::string, KnowledgeRecord * > KnowledgeRecords
long send_data(const madara::knowledge::KnowledgeRecords &updates)
Sends a list of knowledge updates to listeners.
bool no_sending
if true, never send over transport
void add(uint64_t size)
Adds a message to the monitor.
ACE_SOCK_Dgram write_socket_
underlying socket for sending
const std::string id_
host:port identifier of this process
ACE_SOCK_Dgram read_socket_
The socket we are reading from.
double slack_time
time to sleep between sends and rebroadcasts
MADARA_Export void delete_fragments(FragmentMap &map)
Deletes fragments within a fragment map and clears the map.
uint32_t read_threads
the number of read threads to start
int reliability(void) const
Accesses reliability setting.
uint32_t queue_length
Length of the buffer used to store history of events.
virtual ~UdpTransport()
Destructor.
void terminate(const std::string name)
Requests a specific thread to terminate.
std::map< uint32_t, const char * > FragmentMap
Map of fragment identifiers to fragments.
PacketScheduler packet_scheduler_
scheduler for dropping packets to simulate network issues
madara::utility::ScopedArray< char > buffer_
buffer for sending
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
virtual int setup(void)
all subclasses should call this method at the end of its setup
void set_data_plane(knowledge::KnowledgeBase &data_plane)
Sets the data plane for new threads.
Base class from which all transports must be derived.
Thread for reading knowledge updates through a UDP socket.
uint32_t max_fragment_size
Maximum allowed fragment size for partitioning large messages.
bool wait(const std::string name, const knowledge::WaitSettings &ws=knowledge::WaitSettings())
Wait for a specific thread to complete.
BandwidthMonitor send_monitor_
monitor for sending bandwidth usage
madara::knowledge::ThreadSafeContext & context_
int resend_attempts
Maximum number of attempts to resend if transport is busy.
void use(ThreadSafeContext &original)
Refer to and use another knowledge base's context.