5 #include "ace/Time_Value.h" 15 const ACE_INET_Addr & address,
16 ACE_SOCK_Dgram & write_socket,
17 ACE_SOCK_Dgram_Mcast & read_socket,
21 : settings_ (settings), id_ (id), context_ (0), address_ (address),
22 read_socket_ (read_socket),
23 write_socket_ (write_socket),
24 send_monitor_ (send_monitor),
25 receive_monitor_ (receive_monitor),
26 packet_scheduler_ (packet_scheduler)
46 "MulticastTransportReadThread::init:" \
47 " setting rules to %s\n",
51 #ifndef _MADARA_NO_KARL_ 54 #endif // _MADARA_NO_KARL_ 59 "MulticastTransportReadThread::init:" \
60 " no permanent rules were set\n");
72 "MulticastTransportReadThread::close:" \
73 " Error unsubscribing to multicast address\n");
81 const char * print_prefix,
98 ssize_t bytes_sent = 0;
99 uint64_t packet_size = (uint64_t)result;
107 " fragmenting %d byte packet (%" PRIu64
" bytes is max fragment size)\n",
114 for (FragmentMap::iterator i = map.begin (); i != map.end ();
119 " Sending fragment %d\n",
122 int send_attempts = -1;
123 ssize_t actual_sent = -1;
125 while (actual_sent < 0 &&
139 bytes_sent = (uint64_t)actual_sent;
143 " Sent packet of size %" PRIu64
"\n",
144 print_prefix, bytes_sent);
148 else if (actual_sent == ECONNRESET)
152 " WARNING: Remote socket disappeared during send (ECONNRESET)\n",
155 else if (actual_sent == EINTR)
159 " Local socket was interrupted during send (EINTR)\n",
162 else if (actual_sent == EWOULDBLOCK)
166 " Send would have blocked (EWOULDBLOCK)\n",
169 else if (actual_sent == ENOTCONN)
173 " Send reports socket is not connected (ENOTCONN)\n",
176 else if (actual_sent == EADDRINUSE)
180 " Send reports the interface is busy (EADDRINUSE)\n",
183 else if (actual_sent == EBADF)
187 " Send socket is invalid (EBADF)\n",
194 " Packet was not sent due to unknown error (%d)\n",
195 print_prefix, (int)actual_sent);
208 " Sent fragments totalling %" PRIu64
" bytes\n",
219 " Sending packet of size %ld\n",
220 print_prefix, result);
222 int send_attempts = -1;
223 ssize_t actual_sent = -1;
225 while (actual_sent < 0 &&
238 bytes_sent = (uint64_t)actual_sent;
243 " Sent packet of size %" PRIu64
"\n",
244 print_prefix, bytes_sent);
248 else if (actual_sent == ECONNRESET)
253 " WARNING: Remote socket disappeared during send (ECONNRESET)\n",
256 else if (actual_sent == EINTR)
261 " Local socket was interrupted during send (EINTR)\n",
264 else if (actual_sent == EWOULDBLOCK)
269 " Send would have blocked (EWOULDBLOCK)\n",
272 else if (actual_sent == ENOTCONN)
277 " Send reports socket is not connected (ENOTCONN)\n",
280 else if (actual_sent == EADDRINUSE)
285 " Send reports the interface is busy (EADDRINUSE)\n",
288 else if (actual_sent == EBADF)
293 " Send socket is invalid (EBADF)\n",
301 " Packet was not sent due to unknown error (%d)\n",
302 print_prefix, (int)actual_sent);
309 " Send bandwidth = %" PRIu64
" B/s\n",
321 ACE_Time_Value wait_time (1);
322 ACE_INET_Addr remote;
326 const char * print_prefix =
"MulticastTransportReadThread::run";
330 " entering main service loop.\n", print_prefix);
338 " Unable to allocate buffer of size " PRIu32
". Exiting thread.\n",
347 " entering a recv on the socket.\n",
356 if (remote.get_host_addr () != 0)
361 " received a message header of %lld bytes from %s:%d\n",
363 (
long long)bytes_read,
364 remote.get_host_addr (), (int)remote.get_port_number ());
371 " received %lld bytes from unknown host\n",
373 (
long long)bytes_read);
377 std::stringstream remote_host;
378 remote_host << remote.get_host_addr ();
380 remote_host << remote.get_port_number ();
384 #ifndef _MADARA_NO_KARL_
388 remote_host.str ().c_str (), header);
392 if (header->
ttl > 0 && rebroadcast_records.size () > 0 &&
396 header->
ttl = std::min (
399 rebroadcast (print_prefix, header, rebroadcast_records);
410 " wait timeout on new messages. Proceeding to next wait\n",
416 " finished iteration.\n",
uint64_t get_bytes_per_second(void)
Queries the monitor for the current bandwidth utilization per second over the past window...
MADARA_Export void frag(char *source, uint32_t fragment_size, FragmentMap &map)
Breaks a large packet into smaller packets.
MulticastTransportReadThread(const TransportSettings &settings, const std::string &id, const ACE_INET_Addr &address, ACE_SOCK_Dgram &write_socket, ACE_SOCK_Dgram_Mcast &read_socket, BandwidthMonitor &send_monitor, BandwidthMonitor &receive_monitor, PacketScheduler &packet_scheduler)
Constructor.
PacketScheduler & packet_scheduler_
scheduler for mimicking target network conditions
void run(void)
The main loop internals for the read thread.
Provides scheduler for dropping packets.
MADARA_Export utility::Refcounter< logger::Logger > global_logger
const std::string id_
host:port identifier of this process
knowledge::ThreadSafeContext * context_
knowledge context
void init(knowledge::KnowledgeBase &knowledge)
Initializes MADARA context-related items.
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.
ACE_SOCK_Dgram & write_socket_
underlying socket for sending
T * get_ptr(void)
get the underlying pointer
bool no_receiving
if true, never receive over transport
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
This class provides a distributed knowledge base to users.
Parses incoming expression strings into a parse tree and generates an expression tree from the parse ...
BandwidthMonitor & receive_monitor_
monitor for receiving bandwidth usage
::std::map< std::string, KnowledgeRecord > KnowledgeMap
bool no_sending
if true, never send over transport
void add(uint64_t size)
Adds a message to the monitor.
madara::knowledge::CompiledExpression on_data_received_
data received rules, defined in Transport settings
std::string on_data_received_logic
logic to be evaluated after every successful update
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.
int MADARA_Export process_received_update(const char *buffer, uint32_t bytes_read, const std::string &id, knowledge::ThreadSafeContext &context, const QoSTransportSettings &settings, BandwidthMonitor &send_monitor, BandwidthMonitor &receive_monitor, knowledge::KnowledgeMap &rebroadcast_records, knowledge::CompiledExpression &on_data_received, const char *print_prefix, const char *remote_host, MessageHeader *&header)
Processes a received update, updates monitors, fills rebroadcast records according to settings filter...
uint32_t queue_length
Length of the buffer used to store history of events.
void rebroadcast(const char *print_prefix, MessageHeader *header, const knowledge::KnowledgeMap &records)
Sends a rebroadcast packet.
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
int MADARA_Export prep_rebroadcast(knowledge::ThreadSafeContext &context, char *buffer, int64_t &buffer_remaining, const QoSTransportSettings &settings, const char *print_prefix, MessageHeader *header, const knowledge::KnowledgeMap &records, PacketScheduler &packet_scheduler)
Preps a buffer for rebroadcasting records to other agents on the network.
Provides monitoring capability of a transport's bandwidth.
const QoSTransportSettings settings_
quality-of-service transport settings
Provides functions and classes for the distributed knowledge base.
ACE_SOCK_Dgram_Mcast & read_socket_
The multicast socket we are reading from.
std::map< uint32_t, const char * > FragmentMap
Map of fragment identifiers to fragments.
BandwidthMonitor & send_monitor_
monitor for sending bandwidth usage
void cleanup(void)
Cleanup function called by thread manager.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
ACE_INET_Addr address_
The multicast address we are subscribing to.
madara::utility::ScopedArray< char > buffer_
buffer for receiving
uint32_t max_fragment_size
Maximum allowed fragment size for partitioning large messages.
int resend_attempts
Maximum number of attempts to resend if transport is busy.
unsigned char get_participant_ttl(void) const
Returns the maximum time to live participation of this transport in rebroadcasting of other agent's m...