11 #ifndef BB_SERVICE_ANNOUNCER_HPP_INCLUDED
12 #define BB_SERVICE_ANNOUNCER_HPP_INCLUDED
18 #include <boost/asio.hpp>
21 namespace networking {
47 boost::asio::io_service& io_service,
48 const std::string& service_name,
49 const unsigned short service_port,
50 const unsigned short multicast_port = 30001,
51 const boost::asio::ip::address& multicast_address = boost::asio::ip::address::from_string(
"239.255.0.1")
53 : endpoint_(multicast_address, multicast_port)
54 , socket_(io_service, endpoint_.protocol())
56 , service_name_(service_name)
57 , service_port_(service_port)
63 void handle_send_to(
const boost::system::error_code& error)
67 std::cerr << error.message() << std::endl;
71 timer_.expires_from_now(boost::posix_time::seconds(1));
73 [
this](
const boost::system::error_code& error)
75 this->handle_timeout(error);
80 void handle_timeout(
const boost::system::error_code& error)
88 std::cerr << error.message() << std::endl;
94 std::ostringstream os;
95 boost::system::error_code error_code;
99 <<
":" << boost::asio::ip::host_name(error_code)
100 <<
":" << service_port_;
104 std::cerr << error_code.message() << std::endl;
109 socket_.async_send_to(
110 boost::asio::buffer(message_), endpoint_,
111 [
this](
const boost::system::error_code& error, std::size_t )
113 this->handle_send_to(error);
119 boost::asio::ip::udp::endpoint endpoint_;
120 boost::asio::ip::udp::socket socket_;
121 boost::asio::deadline_timer timer_;
122 std::string message_;
123 const std::string service_name_;
124 const unsigned short service_port_;
service_announcer(boost::asio::io_service &io_service, const std::string &service_name, const unsigned short service_port, const unsigned short multicast_port=30001, const boost::asio::ip::address &multicast_address=boost::asio::ip::address::from_string("239.255.0.1"))
Definition: service_announcer.hpp:46
Definition: service_announcer.hpp:38