diff --git a/aseba/thymio-device-manager/aseba_node_registery.cpp b/aseba/thymio-device-manager/aseba_node_registery.cpp index b1ecec9b0..e0b5c0b06 100644 --- a/aseba/thymio-device-manager/aseba_node_registery.cpp +++ b/aseba/thymio-device-manager/aseba_node_registery.cpp @@ -16,8 +16,6 @@ aseba_node_registery::aseba_node_registery(boost::asio::execution_context& io_co , m_discovery_socket(static_cast(io_context)) , m_nodes_service_desc("mobsya") { m_nodes_service_desc.name(fmt::format("Thymio Device Manager on {}", boost::asio::ip::host_name())); - - // update_discovery(); } void aseba_node_registery::add_node(std::shared_ptr node) { @@ -114,48 +112,43 @@ void aseba_node_registery::set_ws_endpoint(const boost::asio::ip::tcp::endpoint& m_ws_endpoint = endpoint; } -void aseba_node_registery::set_discovery() { - update_discovery(); +void aseba_node_registery::announce_on_zeroconf() { + // Make sure we have entered the event loop before announcing ourself + boost::asio::post(boost::asio::get_associated_executor(this), + boost::bind(&aseba_node_registery::do_announce_on_zeroconf, this)); } -void aseba_node_registery::update_discovery() { - - if(m_updating_discovery) { - m_discovery_needs_update = true; - return; - } +void aseba_node_registery::do_announce_on_zeroconf() { m_nodes_service_desc.properties(build_discovery_properties()); - - m_discovery_needs_update = false; - m_updating_discovery = true; m_discovery_socket.async_announce( m_nodes_service_desc, - std::bind(&aseba_node_registery::on_update_discovery_complete, this, std::placeholders::_1)); + std::bind(&aseba_node_registery::on_announce_complete, this, std::placeholders::_1)); } -void aseba_node_registery::on_update_discovery_complete(const boost::system::error_code& ec) { +void aseba_node_registery::on_announce_complete(const boost::system::error_code& ec) { if(ec) { mLogError("Discovery : {}", ec.message()); - m_discovery_needs_update = true; } else { mLogTrace("Discovery : update complete"); } - m_updating_discovery = false; - if(m_discovery_needs_update) { - boost::asio::post(boost::asio::get_associated_executor(this), - boost::bind(&aseba_node_registery::update_discovery, this)); - } + + // Refresh the zeroconf info every few seconds in case they get lost + // or corrupted. + auto timer = std::make_shared(get_io_context()); + timer->expires_from_now(boost::posix_time::seconds(5)); + auto cb = boost::asio::bind_executor(get_io_context().get_executor(), [timer, this](const boost::system::error_code& ec) { + do_announce_on_zeroconf(); + }); + timer->async_wait(std::move(cb)); } aware::contact::property_map_type aseba_node_registery::build_discovery_properties() const { aware::contact::property_map_type map; map["uuid"] = boost::uuids::to_string(m_service_uid); - map["_ws-port"] = "8597"; //HACK adding some unused description to correct bug in avahi if(m_ws_endpoint.port()) map["ws-port"] = std::to_string(m_ws_endpoint.port()); mLogTrace("=> WS port discovery on {}", map["ws-port"]); - map["_ws-port2"] = "8597"; //HACK adding some unused description to correct bug in avahi return map; } diff --git a/aseba/thymio-device-manager/aseba_node_registery.h b/aseba/thymio-device-manager/aseba_node_registery.h index 64d72a22b..44e5a9ba9 100644 --- a/aseba/thymio-device-manager/aseba_node_registery.h +++ b/aseba/thymio-device-manager/aseba_node_registery.h @@ -30,7 +30,7 @@ class aseba_node_registery : public boost::asio::detail::service_base node_from_id(const node_id&) const; @@ -48,8 +48,8 @@ class aseba_node_registery : public boost::asio::detail::service_base& node) const; @@ -73,9 +73,6 @@ class aseba_node_registery : public boost::asio::detail::service_base, node_id, aseba_node::status)> m_node_status_changed_signal; friend class node_status_monitor; diff --git a/aseba/thymio-device-manager/main.cpp b/aseba/thymio-device-manager/main.cpp index 77e554968..1d29fe3bd 100644 --- a/aseba/thymio-device-manager/main.cpp +++ b/aseba/thymio-device-manager/main.cpp @@ -50,25 +50,18 @@ void run_service(boost::asio::io_context& ctx) { [[maybe_unused]] mobsya::wireless_configurator_service& ws = boost::asio::make_service(ctx); - // ws.enable(); + ws.enable(); // Create a server for regular tcp connection mobsya::application_server tcp_server(ctx, 0); node_registery.set_tcp_endpoint(tcp_server.endpoint()); - tcp_server.accept(); mobsya::aseba_tcp_acceptor aseba_tcp_acceptor(ctx); // Create a server for websocket mobsya::application_server websocket_server(ctx, 8597); node_registery.set_ws_endpoint(websocket_server.endpoint()); - websocket_server.accept(); - - // Enable Bonjour, Zeroconf - node_registery.set_discovery(); - mLogTrace("=> TCP Server connected on {}", tcp_server.endpoint().port()); - mLogTrace("=> WS Server connected on {}", websocket_server.endpoint().port()); #ifdef MOBSYA_TDM_ENABLE_USB mobsya::usb_server usb_server(ctx, {mobsya::THYMIO2_DEVICE_ID, mobsya::THYMIO_WIRELESS_DEVICE_ID}); @@ -79,6 +72,16 @@ void run_service(boost::asio::io_context& ctx) { serial_server.accept(); #endif aseba_tcp_acceptor.accept(); + websocket_server.accept(); + tcp_server.accept(); + + mLogTrace("=> TCP Server started on {}", tcp_server.endpoint().port()); + mLogTrace("=> WS Server started on {}", websocket_server.endpoint().port()); + + // Enable Bonjour/Zeroconf + // Make sure this is done after the different servers are started and accept connections + // So that clients can connect to discovered services + node_registery.announce_on_zeroconf(); ctx.run(); } diff --git a/third_party/aware b/third_party/aware index d311a2d76..d5ca79656 160000 --- a/third_party/aware +++ b/third_party/aware @@ -1 +1 @@ -Subproject commit d311a2d76d3ac27966caf29dacc72aa51bdeaf09 +Subproject commit d5ca796566045dcb4c7d9c76c12868e4aabc1821