8 #include "ace/INET_Addr.h" 9 #include "ace/SOCK_Dgram.h" 17 :
Base (id, config, context)
49 "UdpRegistryServer::cleanup:" \
50 " Error closing write socket\n");
57 "UdpRegistryServer::cleanup:" \
58 " Error closing read socket\n");
85 "UdpRegistryClient::setup:" \
86 " adding server %s to registry lookup list\n",
96 "UdpRegistryServer::setup:" \
97 " socket failed to open\n");
101 int rcv_buff_size = 0;
102 int opt_len =
sizeof (int);
105 (
void *)&send_buff_size, &opt_len);
108 (
void *)&rcv_buff_size, &opt_len);
111 "UdpRegistryServer::setup:" \
112 " default socket buff size is send=%d, rcv=%d\n",
113 send_buff_size, rcv_buff_size);
115 if (send_buff_size < tar_buff_size)
118 "UdpRegistryServer::setup:" \
119 " setting send buff size to settings.queue_length (%d)\n",
123 (
void *)&tar_buff_size, opt_len);
126 (
void *)&send_buff_size, &opt_len);
129 "UdpRegistryServer::setup:" \
130 " current socket buff size is send=%d, rcv=%d\n",
131 send_buff_size, rcv_buff_size);
134 if (rcv_buff_size < tar_buff_size)
137 "UdpRegistryServer::setup:" \
138 " setting rcv buff size to settings.queue_length (%d)\n",
142 (
void *)&tar_buff_size, opt_len);
145 (
void *)&rcv_buff_size, &opt_len);
148 "UdpRegistryServer::setup:" \
149 " current socket buff size is send=%d, rcv=%d\n",
150 send_buff_size, rcv_buff_size);
162 "UdpRegistryServer::setup:" \
163 " Error subscribing to udp address %s:%d\n",
164 local.get_host_addr (), local.get_port_number ());
169 "UdpRegistryServer::setup:" \
170 " Success subscribing to udp address %s:%d\n",
171 local.get_host_addr (), local.get_port_number ());
174 int rcv_buff_size = 0;
175 int opt_len =
sizeof (int);
179 bare_socket.get_option (SOL_SOCKET, SO_RCVBUF,
180 (
void *)&rcv_buff_size, &opt_len);
183 "UdpRegistryServer::setup:" \
184 " default socket buff size is send=%d, rcv=%d\n",
185 send_buff_size, rcv_buff_size);
187 if (send_buff_size < tar_buff_size)
190 "UdpRegistryServer::setup:" \
191 " setting send buff size to settings.queue_length (%d)\n",
194 bare_socket.set_option (SOL_SOCKET, SO_SNDBUF,
195 (
void *)&tar_buff_size, opt_len);
197 bare_socket.get_option (SOL_SOCKET, SO_SNDBUF,
198 (
void *)&send_buff_size, &opt_len);
201 "UdpRegistryServer::setup:" \
202 " current socket buff size is send=%d, rcv=%d\n",
203 send_buff_size, rcv_buff_size);
206 if (rcv_buff_size < tar_buff_size)
209 "UdpRegistryServer::setup:" \
210 " setting rcv buff size to settings.queue_length (%d)\n",
213 bare_socket.set_option (SOL_SOCKET, SO_RCVBUF,
214 (
void *)&tar_buff_size, opt_len);
216 bare_socket.get_option (SOL_SOCKET, SO_RCVBUF,
217 (
void *)&rcv_buff_size, &opt_len);
220 "UdpRegistryServerReadThread::setup:" \
221 " current socket buff size is send=%d, rcv=%d\n",
222 send_buff_size, rcv_buff_size);
238 "UdpRegistryServerReadThread::setup:" \
244 std::stringstream thread_name;
245 thread_name <<
"read";
263 const char * print_prefix =
"UdpRegistryServer::send_data";
271 std::vector <std::string>
hosts;
275 for (
size_t i = 0; i < hosts.size (); ++i)
279 " adding %s to client send list\n",
280 print_prefix, hosts[i].c_str ());
282 clients_[hosts[i]].set (hosts[i].c_str ());
285 result =
prep_send (orig_updates, print_prefix);
289 uint64_t bytes_sent = 0;
290 uint64_t packet_size = (uint64_t)result;
298 " fragmenting %" PRIu64
" byte packet (%" PRIu32
" bytes is max fragment size)\n",
305 for (FragmentMap::iterator i = map.begin (); i != map.end (); ++i, ++j)
310 for (std::map <std::string, ACE_INET_Addr>::const_iterator addr =
317 " Sending fragment %d\n",
320 int send_attempts = -1;
321 ssize_t actual_sent = -1;
323 while (actual_sent < 0 &&
330 i->second, frag_size, addr->second);
339 "%s: Send result was %d of %d byte fragment to %s\n",
340 print_prefix, (int)actual_sent,
341 (
int)frag_size, addr->first.c_str ());
345 bytes_sent = (uint64_t)actual_sent;
349 " Sent packet of size %" PRIu64
"\n",
350 print_prefix, bytes_sent);
354 else if (actual_sent == ECONNRESET)
358 " WARNING: Remote socket disappeared during send (ECONNRESET)\n",
361 else if (actual_sent == EINTR)
365 " Local socket was interrupted during send (EINTR)\n",
368 else if (actual_sent == EWOULDBLOCK)
372 " Send would have blocked (EWOULDBLOCK)\n",
375 else if (actual_sent == ENOTCONN)
379 " Send reports socket is not connected (ENOTCONN)\n",
382 else if (actual_sent == EADDRINUSE)
386 " Send reports the interface is busy (EADDRINUSE)\n",
389 else if (actual_sent == EBADF)
393 " Send socket is invalid (EBADF)\n",
400 " Packet was not sent due to unknown error (%d)\n",
401 print_prefix, (int)actual_sent);
415 " Sent fragments totalling %" PRIu64
" bytes\n",
416 print_prefix, bytes_sent);
422 for (std::map <std::string, ACE_INET_Addr>::const_iterator i =
429 " Sending packet of size %ld to %s\n",
430 print_prefix, result, i->first.c_str ());
432 int send_attempts = -1;
433 ssize_t actual_sent = -1;
435 while (actual_sent < 0 &&
442 (ssize_t)result, i->second);
448 bytes_sent += actual_sent;
452 " Sent packet of size %d to %s\n",
453 print_prefix, (int)actual_sent, i->first.c_str ());
457 else if (actual_sent == ECONNRESET)
461 " WARNING: Remote socket disappeared during send (ECONNRESET)\n",
464 else if (actual_sent == EINTR)
468 " Local socket was interrupted during send (EINTR)\n",
471 else if (actual_sent == EWOULDBLOCK)
475 " Send would have blocked (EWOULDBLOCK)\n",
478 else if (actual_sent == ENOTCONN)
482 " Send reports socket is not connected (ENOTCONN)\n",
485 else if (actual_sent == EADDRINUSE)
489 " Send reports the interface is busy (EADDRINUSE)\n",
492 else if (actual_sent == EBADF)
496 " Send socket is invalid (EBADF)\n",
503 " Packet was not sent due to unknown error (%d)\n",
504 print_prefix, (int)actual_sent);
511 result = (long) bytes_sent;
515 " Send bandwidth = %" PRIu64
" B/s\n",
QoSTransportSettings settings_
int setup(void)
all subclasses should call this method at the end of its setup
long send_data(const madara::knowledge::KnowledgeRecords &updates)
Sends a list of knowledge updates to listeners.
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...
void invalidate_transport(void)
Invalidates a transport to indicate it is shutting down.
knowledge::KnowledgeBase knowledge_
knowledge base for threads to use
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.
virtual ~UdpRegistryServer()
Destructor.
void keys(std::vector< std::string > &curkeys) const
Returns the keys within the map.
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.
std::vector< std::string > sync_keys(void)
Syncs the keys from the knowledge base.
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.
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.
knowledge::containers::Map endpoints_
Thread for reading registry updates through a UDP socket.
T * get_ptr(void)
get the underlying pointer
bool no_receiving
if true, never receive over transport
std::string write_domain
All class members are accessible to users for easy setup.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
::std::map< std::string, KnowledgeRecord * > KnowledgeRecords
std::map< std::string, ACE_INET_Addr > clients_
registry clients
bool no_sending
if true, never send over transport
ACE_SOCK_Dgram read_socket_
The socket we are reading from.
UdpRegistryServer(const std::string &id, madara::knowledge::ThreadSafeContext &context, TransportSettings &config, bool launch_transport)
Constructor.
void add(uint64_t size)
Adds a message to the monitor.
int reliability(void) const
Accesses reliability setting.
const std::string id_
host:port identifier of this process
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
uint32_t queue_length
Length of the buffer used to store history of events.
void terminate(const std::string name)
Requests a specific thread to terminate.
std::map< std::string, ACE_INET_Addr > servers_
registry servers
ACE_SOCK_Dgram write_socket_
underlying socket for sending
void close(void)
Closes this transport.
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.
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.
threads::Threader read_threads_
threads for reading knowledge updates
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.