MADARA  3.4.1
MulticastTransport.cpp
Go to the documentation of this file.
1 
5 
11 
12 #include <iostream>
14 
15 namespace madara
16 {
17 namespace transport
18 {
19 namespace mcast = ip::multicast;
20 
23  bool launch_transport)
24  : UdpTransport(id, context, config, false)
25 {
26  if (launch_transport)
27  setup();
28 }
29 
30 int MulticastTransport::setup_read_thread(double hertz, const std::string& name)
31 {
32  read_threads_.run(hertz, name, new MulticastTransportReadThread(*this));
33 
34  return 0;
35 }
36 
38 {
39  try
40  {
41  asio::socket_base::reuse_address reuse_option(true);
42  socket_.set_option(reuse_option);
43  }
44  catch (const boost::system::system_error& e)
45  {
47  "MulticastTransport::setup:"
48  " Error setting up read socket: %s\n",
49  e.what());
50 
51  this->invalidate_transport();
52  return -1;
53  }
54 
56  {
57  return -1;
58  }
59 
60  try
61  {
62  socket_.set_option(mcast::join_group(addresses_[0].address()));
63  }
64  catch (const boost::system::system_error& e)
65  {
67  "MulticastTransport::setup_read_socket:"
68  " Error subscribing: %s\n",
69  e.what());
70 
71  this->invalidate_transport();
72  return -1;
73  }
74 
75  return 0;
76 }
77 
79 {
81  {
82  return -1;
83  }
84 
85  try
86  {
87  socket_.set_option(mcast::hops(1));
88  }
89  catch (const boost::system::system_error& e)
90  {
92  "MulticastTransport::setup_write_socket:"
93  " Error setting TTL: %s\n",
94  e.what());
95 
96  this->invalidate_transport();
97  return -1;
98  }
99 
100  return 0;
101 }
102 }
103 }
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
This class stores variables and their values for use by any entity needing state information in a thr...
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
void run(const std::string &name, BaseThread *thread, bool paused=false)
Starts a new thread and executes the provided user thread once.
Definition: Threader.cpp:62
madara::knowledge::ThreadSafeContext & context_
Definition: Transport.h:135
void invalidate_transport(void)
Invalidates a transport to indicate it is shutting down.
Definition: Transport.inl:31
threads::Threader read_threads_
threads for reading knowledge updates
std::vector< udp::endpoint > addresses_
holds all multicast addresses we are sending to
udp::socket socket_
underlying socket
int setup() override
all subclasses should call this method at the end of its setup
MulticastTransport(const std::string &id, madara::knowledge::ThreadSafeContext &context, TransportSettings &config, bool launch_transport)
Constructor.
int setup_read_thread(double hertz, const std::string &name) override
Holds basic transport settings.
UDP-based transport for knowledge.
Definition: UdpTransport.h:39
constexpr string_t string
Copyright(c) 2020 Galois.