diff --git a/libxbot-service-interface/src/ServiceDiscoveryImpl.cpp b/libxbot-service-interface/src/ServiceDiscoveryImpl.cpp index d7c3d7e..b2175e9 100644 --- a/libxbot-service-interface/src/ServiceDiscoveryImpl.cpp +++ b/libxbot-service-interface/src/ServiceDiscoveryImpl.cpp @@ -29,8 +29,7 @@ std::vector registered_callbacks_{}; ServiceDiscoveryImpl *instance_ = nullptr; -bool ServiceDiscoveryImpl::GetEndpoint(const std::string &uid, uint32_t &ip, - uint16_t &port) { +bool ServiceDiscoveryImpl::GetEndpoint(const std::string &uid, uint32_t &ip, uint16_t &port) { std::unique_lock lk(sd_mutex_); if (discovered_services_.contains(uid)) { auto &service_info = discovered_services_.at(uid); @@ -57,28 +56,24 @@ bool ServiceDiscoveryImpl::Start() { return true; } -void ServiceDiscoveryImpl::RegisterCallbacks( - ServiceDiscoveryCallbacks *callbacks) { +void ServiceDiscoveryImpl::RegisterCallbacks(ServiceDiscoveryCallbacks *callbacks) { if (callbacks == nullptr) { return; } std::unique_lock lk(sd_mutex_); - const auto &it = std::find(registered_callbacks_.begin(), - registered_callbacks_.end(), callbacks); + const auto &it = std::find(registered_callbacks_.begin(), registered_callbacks_.end(), callbacks); if (it == registered_callbacks_.end()) { registered_callbacks_.push_back(callbacks); } } -void ServiceDiscoveryImpl::UnregisterCallbacks( - ServiceDiscoveryCallbacks *callbacks) { +void ServiceDiscoveryImpl::UnregisterCallbacks(ServiceDiscoveryCallbacks *callbacks) { if (callbacks == nullptr) { return; } std::unique_lock lk(sd_mutex_); while (true) { - const auto &it = std::find(registered_callbacks_.begin(), - registered_callbacks_.end(), callbacks); + const auto &it = std::find(registered_callbacks_.begin(), registered_callbacks_.end(), callbacks); if (it != registered_callbacks_.end()) { registered_callbacks_.erase(it); } else { @@ -87,8 +82,7 @@ void ServiceDiscoveryImpl::UnregisterCallbacks( } } -std::unique_ptr ServiceDiscoveryImpl::GetServiceInfo( - const std::string &uid) { +std::unique_ptr ServiceDiscoveryImpl::GetServiceInfo(const std::string &uid) { std::unique_lock lk(sd_mutex_); if (!discovered_services_.contains(uid)) { return nullptr; @@ -96,11 +90,9 @@ std::unique_ptr ServiceDiscoveryImpl::GetServiceInfo( return std::make_unique(discovered_services_.at(uid)); } -std::unique_ptr> -ServiceDiscoveryImpl::GetAllServices() { +std::unique_ptr> ServiceDiscoveryImpl::GetAllServices() { std::unique_lock lk(sd_mutex_); - return std::make_unique>( - discovered_services_); + return std::make_unique>(discovered_services_); } bool ServiceDiscoveryImpl::DropService(const std::string &uid) { std::unique_lock lk(sd_mutex_); @@ -143,22 +135,18 @@ void Run() { if (sd_socket_.ReceivePacket(sender_ip, sender_port, packet)) { // Check, if packet has at least enough space for our header if (packet.size() >= sizeof(datatypes::XbotHeader)) { - const auto header = - reinterpret_cast(packet.data()); + const auto header = reinterpret_cast(packet.data()); - if (header->message_type != - datatypes::MessageType::SERVICE_ADVERTISEMENT) { - spdlog::warn( - "Service Discovery socket got non-service discovery message"); + if (header->message_type != datatypes::MessageType::SERVICE_ADVERTISEMENT) { + spdlog::warn("Service Discovery socket got non-service discovery message"); continue; } // Validate reported length - if (packet.size() == - header->payload_size + sizeof(datatypes::XbotHeader)) { + if (packet.size() == header->payload_size + sizeof(datatypes::XbotHeader)) { try { - const auto json = nlohmann::json::from_cbor( - packet.begin() + sizeof(datatypes::XbotHeader), packet.end()); + const auto json = + nlohmann::json::from_cbor(packet.begin() + sizeof(datatypes::XbotHeader), packet.end()); // Build the ServiceInfo object from the received data. ServiceInfo info = json; @@ -179,12 +167,9 @@ void Run() { if (discovered_services_.contains(info.unique_id_)) { // Check, if service endpoint was updated // (every thing else is constant) and update - if (auto &old_service_info = - discovered_services_.at(info.unique_id_); - old_service_info.ip != info.ip || - old_service_info.port != info.port) { - spdlog::info("Endpoint updated (ID: {}, new endpoint: {})", - info.unique_id_, + if (auto &old_service_info = discovered_services_.at(info.unique_id_); + old_service_info.ip != info.ip || old_service_info.port != info.port) { + spdlog::info("Endpoint updated (ID: {}, new endpoint: {})", info.unique_id_, EndpointIntToString(info.ip, info.port)); // Backup the old infos, so that we can pass them to the // callback @@ -197,14 +182,12 @@ void Run() { // Notify callbacks for (const auto &callback : registered_callbacks_) { - callback->OnEndpointChanged(info.unique_id_, old_ip, - old_port, info.ip, info.port); + callback->OnEndpointChanged(info.unique_id_, old_ip, old_port, info.ip, info.port); } } } else { - spdlog::info("Found new service (ID: {}, endpoint: {})", - info.unique_id_, - EndpointIntToString(info.ip, info.port)); + spdlog::info("Found new service (Type: {}, ID: {}, endpoint: {})", info.description.type, + info.unique_id_, EndpointIntToString(info.ip, info.port)); discovered_services_.emplace(info.unique_id_, info); // Notify callbacks for (const auto &callback : registered_callbacks_) { @@ -213,8 +196,7 @@ void Run() { } } } catch (std::exception &e) { - spdlog::error("Got exception during service discovery: {}", - e.what()); + spdlog::error("Got exception during service discovery: {}", e.what()); } } }