From af4701c237eabed0b46523aa2147f2a0c2dbde48 Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Mon, 7 Jun 2021 06:30:59 -0700 Subject: [PATCH] Internal change PiperOrigin-RevId: 377900845 --- cpp/core/internal/client_proxy.cc | 188 ++++++++------ cpp/core/internal/client_proxy.h | 2 + .../internal/offline_service_controller.cc | 31 ++- .../internal/service_controller_router.cc | 241 +++++++++--------- cpp/core/internal/service_controller_router.h | 3 +- 5 files changed, 269 insertions(+), 196 deletions(-) diff --git a/cpp/core/internal/client_proxy.cc b/cpp/core/internal/client_proxy.cc index 04ff47e3..76ebffd4 100644 --- a/cpp/core/internal/client_proxy.cc +++ b/cpp/core/internal/client_proxy.cc @@ -47,6 +47,9 @@ std::int64_t ClientProxy::GetClientId() const { return client_id_; } std::string ClientProxy::GetLocalEndpointId() { if (local_endpoint_id_.empty()) { local_endpoint_id_ = GenerateLocalEndpointId(); + NEARBY_LOGS(INFO) << "ClientProxy [Local Endpoint Generated]: client=" + << GetClientId() + << "; endpoint_id=" << local_endpoint_id_; } return local_endpoint_id_; } @@ -54,11 +57,10 @@ std::string ClientProxy::GetLocalEndpointId() { std::string ClientProxy::GenerateLocalEndpointId() { if (high_vis_mode_) { if (!local_high_vis_mode_cache_endpoint_id_.empty()) { - NEARBY_LOG(INFO, - "ClientProxy [Local Endpoint not Generated but return Cache]: " - "client=%p; " - "local_high_vis_mode_cache_endpoint_id_=%s", - this, local_high_vis_mode_cache_endpoint_id_.c_str()); + NEARBY_LOGS(INFO) + << "ClientProxy [Local Endpoint Re-using cached endpoint id]: client=" + << GetClientId() << "; local_high_vis_mode_cache_endpoint_id_=" + << local_high_vis_mode_cache_endpoint_id_; return local_high_vis_mode_cache_endpoint_id_; } } @@ -68,9 +70,8 @@ std::string ClientProxy::GenerateLocalEndpointId() { // 4) Use only the first kEndpointIdLength bytes to make ID. ByteArray id_hash = Crypto::Sha256(absl::StrCat("client", prng_.NextInt64())); std::string id = Base64Utils::Encode(id_hash).substr(0, kEndpointIdLength); - NEARBY_LOG( - INFO, "ClientProxy [Local Endpoint Generated]: client=%p; endpoint_id=%s", - this, id.c_str()); + NEARBY_LOGS(INFO) << "ClientProxy [Local Endpoint Generated]: client=" + << GetClientId() << "; endpoint_id=" << id; return id; } @@ -90,15 +91,15 @@ void ClientProxy::StartedAdvertising( absl::Span mediums, const ConnectionOptions& advertising_options) { MutexLock lock(&mutex_); - NEARBY_LOG(INFO, "ClientProxy [StartedAdvertising]: client=%p; ", this); + NEARBY_LOGS(INFO) << "ClientProxy [StartedAdvertising]: client=" + << GetClientId(); if (high_vis_mode_) { local_high_vis_mode_cache_endpoint_id_ = local_endpoint_id_; - NEARBY_LOG( - INFO, - "ClientProxy [High Visibility Mode Adv, Cache EndpointId]: client=%p; " - "local_high_vis_mode_cache_endpoint_id_=%s", - this, local_high_vis_mode_cache_endpoint_id_.c_str()); + NEARBY_LOGS(INFO) + << "ClientProxy [High Visibility Mode Adv, Cache EndpointId]: client=" + << GetClientId() << "; local_high_vis_mode_cache_endpoint_id_=" + << local_high_vis_mode_cache_endpoint_id_; CancelClearLocalHighVisModeCacheEndpointIdAlarm(); } @@ -108,7 +109,8 @@ void ClientProxy::StartedAdvertising( void ClientProxy::StoppedAdvertising() { MutexLock lock(&mutex_); - NEARBY_LOG(INFO, "ClientProxy [StoppedAdvertising]: client=%p; ", this); + NEARBY_LOGS(INFO) << "ClientProxy [StoppedAdvertising]: client=" + << GetClientId(); if (IsAdvertising()) { advertising_info_.Clear(); @@ -182,23 +184,20 @@ void ClientProxy::OnEndpointFound(const std::string& service_id, proto::connections::Medium medium) { MutexLock lock(&mutex_); - NEARBY_LOG(INFO, - "ClientProxy [Endpoint Found]: [enter] id=%s; service=%s; info=%s", - endpoint_id.c_str(), service_id.c_str(), - absl::BytesToHexString(endpoint_info.data()).c_str()); + NEARBY_LOGS(INFO) << "ClientProxy [Endpoint Found]: [enter] id=" + << endpoint_id << "; service=" << service_id << "; info=" + << absl::BytesToHexString(endpoint_info.data()); if (!IsDiscoveringServiceId(service_id)) { - NEARBY_LOG(INFO, - "ClientProxy [Endpoint Found]: Ignoring event for id=%s because " - "this client is not discovering", - endpoint_id.c_str()); + NEARBY_LOGS(INFO) << "ClientProxy [Endpoint Found]: Ignoring event for id=" + << endpoint_id + << " because this client is not discovering."; return; } if (discovered_endpoint_ids_.count(endpoint_id)) { - NEARBY_LOG(WARNING, - "ClientProxy [Endpoint Found]: Ignoring event for id=%s because " - "this client already reported this endpoint as found", - endpoint_id.c_str()); + NEARBY_LOGS(WARNING) + << "ClientProxy [Endpoint Found]: Ignoring event for id=" << endpoint_id + << " because this client has already reported this endpoint as found."; return; } @@ -211,8 +210,8 @@ void ClientProxy::OnEndpointLost(const std::string& service_id, const std::string& endpoint_id) { MutexLock lock(&mutex_); - NEARBY_LOG(INFO, "ClientProxy [Endpoint Lost]: [enter] id=%s; service=%s", - endpoint_id.c_str(), service_id.c_str()); + NEARBY_LOGS(INFO) << "ClientProxy [Endpoint Lost]: [enter] id=" << endpoint_id + << "; service=" << service_id; if (!IsDiscoveringServiceId(service_id)) { NEARBY_LOG(INFO, "ClientProxy [Endpoint Lost]: Ignoring event for id=%s because " @@ -223,10 +222,9 @@ void ClientProxy::OnEndpointLost(const std::string& service_id, const auto it = discovered_endpoint_ids_.find(endpoint_id); if (it == discovered_endpoint_ids_.end()) { - NEARBY_LOG(WARNING, - "ClientProxy [Endpoint Lost]: Ignoring event for id=%s because " - "this client has not yet reported this endpoint as found", - endpoint_id.c_str()); + NEARBY_LOGS(WARNING) + << "ClientProxy [Endpoint Lost]: Ignoring event for id=" << endpoint_id + << " because this client has not yet reported this endpoint as found"; return; } @@ -253,10 +251,10 @@ void ClientProxy::OnConnectionInitiated(const std::string& endpoint_id, // (can not use c++17 features, until chromium does) we unpack manually. auto& pair_iter = result.first; bool inserted = result.second; - NEARBY_LOG(INFO, - "ClientProxy [Connection Initiated]: add Connection: client=%p, " - "id=%s; inserted=%d", - this, endpoint_id.c_str(), inserted); + NEARBY_LOGS(INFO) + << "ClientProxy [Connection Initiated]: add Connection: client=" + << GetClientId() << "; endpoint_id=" << endpoint_id + << "; inserted=" << inserted; DCHECK(inserted); const Connection& item = pair_iter->second; // Notify the client. @@ -275,9 +273,9 @@ void ClientProxy::OnConnectionAccepted(const std::string& endpoint_id) { MutexLock lock(&mutex_); if (!HasPendingConnectionToEndpoint(endpoint_id)) { - NEARBY_LOG( - INFO, "ClientProxy [Connection Accepted]: no pending connection; id=%s", - endpoint_id.c_str()); + NEARBY_LOGS(INFO) << "ClientProxy [Connection Accepted]: no pending " + "connection; endpoint_id=" + << endpoint_id; return; } @@ -294,9 +292,9 @@ void ClientProxy::OnConnectionRejected(const std::string& endpoint_id, MutexLock lock(&mutex_); if (!HasPendingConnectionToEndpoint(endpoint_id)) { - NEARBY_LOG( - INFO, "ClientProxy [Connection Rejected]: no pending connection; id=%s", - endpoint_id.c_str()); + NEARBY_LOGS(INFO) << "ClientProxy [Connection Rejected]: no pending " + "connection; endpoint_id=" + << endpoint_id; return; } @@ -315,6 +313,8 @@ void ClientProxy::OnBandwidthChanged(const std::string& endpoint_id, const Connection* item = LookupConnection(endpoint_id); if (item != nullptr) { item->connection_listener.bandwidth_changed_cb(endpoint_id, new_medium); + NEARBY_LOGS(INFO) << "ClientProxy [reporting onBandwidthChanged]: client=" + << GetClientId() << "; endpoint_id=" << endpoint_id; } } @@ -439,10 +439,9 @@ void ClientProxy::LocalEndpointAcceptedConnection( MutexLock lock(&mutex_); if (HasLocalEndpointResponded(endpoint_id)) { - NEARBY_LOG( - INFO, - "ClientProxy [Local Accepted]: local endpoint has responded; id=%s", - endpoint_id.c_str()); + NEARBY_LOGS(INFO) + << "ClientProxy [Local Accepted]: local endpoint has responded; id=" + << endpoint_id; return; } @@ -458,10 +457,9 @@ void ClientProxy::LocalEndpointRejectedConnection( MutexLock lock(&mutex_); if (HasLocalEndpointResponded(endpoint_id)) { - NEARBY_LOG( - INFO, - "ClientProxy [Local Rejected]: local endpoint has responded; id=%s", - endpoint_id.c_str()); + NEARBY_LOGS(INFO) + << "ClientProxy [Local Rejected]: local endpoint has responded; id=" + << endpoint_id; return; } @@ -473,10 +471,9 @@ void ClientProxy::RemoteEndpointAcceptedConnection( MutexLock lock(&mutex_); if (HasRemoteEndpointResponded(endpoint_id)) { - NEARBY_LOG( - INFO, - "ClientProxy [Remote Accepted]: remote endpoint has responded; id=%s", - endpoint_id.c_str()); + NEARBY_LOGS(INFO) + << "ClientProxy [Remote Accepted]: remote endpoint has responded; id=" + << endpoint_id; return; } @@ -488,10 +485,9 @@ void ClientProxy::RemoteEndpointRejectedConnection( MutexLock lock(&mutex_); if (HasRemoteEndpointResponded(endpoint_id)) { - NEARBY_LOG( - INFO, - "ClientProxy [Remote Rejected]: remote endpoint has responded; id=%s", - endpoint_id.c_str()); + NEARBY_LOGS(INFO) + << "ClientProxy [Remote Rejected]: remote endpoint has responded; id=" + << endpoint_id; return; } @@ -573,6 +569,9 @@ void ClientProxy::OnPayload(const std::string& endpoint_id, Payload payload) { if (IsConnectedToEndpoint(endpoint_id)) { const Connection* item = LookupConnection(endpoint_id); if (item != nullptr) { + NEARBY_LOGS(INFO) << "ClientProxy [reporting onPayloadReceived]: client=" + << GetClientId() << "; endpoint_id=" << endpoint_id + << " ; payload_id=" << payload.GetId(); item->payload_listener.payload_cb(endpoint_id, std::move(payload)); } } @@ -598,6 +597,20 @@ void ClientProxy::OnPayloadProgress(const std::string& endpoint_id, Connection* item = LookupConnection(endpoint_id); if (item != nullptr) { item->payload_listener.payload_progress_cb(endpoint_id, info); + + if (info.status == PayloadProgressInfo::Status::kInProgress) { + NEARBY_LOGS(VERBOSE) + << "ClientProxy [reporting onPayloadProgress]: client=" + << GetClientId() << "; endpoint_id=" << endpoint_id + << "; payload_id=" << info.payload_id + << ", payload_status=" << ToString(info.status); + } else { + NEARBY_LOGS(INFO) + << "ClientProxy [reporting onPayloadProgress]: client=" + << GetClientId() << "; endpoint_id=" << endpoint_id + << "; payload_id=" << info.payload_id + << ", payload_status=" << ToString(info.status); + } } } } @@ -648,14 +661,16 @@ ConnectionOptions ClientProxy::GetDiscoveryOptions() const { void ClientProxy::EnterHighVisibilityMode() { MutexLock lock(&mutex_); - NEARBY_LOG(INFO, "ClientProxy [EnterHighVisibilityMode]: client=%p; ", this); + NEARBY_LOGS(INFO) << "ClientProxy [EnterHighVisibilityMode]: client=" + << GetClientId(); high_vis_mode_ = true; } void ClientProxy::ExitHighVisibilityMode() { MutexLock lock(&mutex_); - NEARBY_LOG(INFO, "ClientProxy [ExitHighVisibilityMode]: client=%p; ", this); + NEARBY_LOGS(INFO) << "ClientProxy [ExitHighVisibilityMode]: client=" + << GetClientId(); high_vis_mode_ = false; ScheduleClearLocalHighVisModeCacheEndpointIdAlarm(); @@ -664,22 +679,34 @@ void ClientProxy::ExitHighVisibilityMode() { void ClientProxy::ScheduleClearLocalHighVisModeCacheEndpointIdAlarm() { CancelClearLocalHighVisModeCacheEndpointIdAlarm(); - if (local_high_vis_mode_cache_endpoint_id_.empty()) return; + if (local_high_vis_mode_cache_endpoint_id_.empty()) { + NEARBY_LOGS(VERBOSE) << "ClientProxy [There is no cached local high power " + "advertising endpoint Id]: client=" + << GetClientId(); + return; + } // Schedule to clear cache high visibility mode advertisement endpoint id in // 30s. - NEARBY_LOG(INFO, - "ClientProxy [High Visibility Mode Adv, Schedule to Clear Cache " - "EndpointId]: client=%p; " - "local_high_vis_mode_cache_endpoint_id_=%s", - this, local_high_vis_mode_cache_endpoint_id_.c_str()); - clear_local_high_vis_mode_cache_endpoint_id_alarm_ = CancelableAlarm( - "clear_high_power_endpoint_id_cache", - [this]() { - MutexLock lock(&mutex_); - local_high_vis_mode_cache_endpoint_id_.clear(); - }, - kHighPowerAdvertisementEndpointIdCacheTimeout, &single_thread_executor_); + NEARBY_LOGS(INFO) << "ClientProxy [High Visibility Mode Adv, Schedule to " + "Clear Cache EndpointId]: client=" + << GetClientId() + << "; local_high_vis_mode_cache_endpoint_id_=" + << local_high_vis_mode_cache_endpoint_id_; + clear_local_high_vis_mode_cache_endpoint_id_alarm_ = + CancelableAlarm( + "clear_high_power_endpoint_id_cache", + [this]() { + MutexLock lock(&mutex_); + NEARBY_LOGS(INFO) + << "ClientProxy [Cleared cached local high power advertising " + "endpoint Id.]: client=" + << GetClientId() << "; local_high_vis_mode_cache_endpoint_id_=" + << local_high_vis_mode_cache_endpoint_id_; + local_high_vis_mode_cache_endpoint_id_.clear(); + }, + kHighPowerAdvertisementEndpointIdCacheTimeout, + &single_thread_executor_); } void ClientProxy::CancelClearLocalHighVisModeCacheEndpointIdAlarm() { @@ -689,6 +716,19 @@ void ClientProxy::CancelClearLocalHighVisModeCacheEndpointIdAlarm() { } } +std::string ClientProxy::ToString(PayloadProgressInfo::Status status) const { + switch (status) { + case PayloadProgressInfo::Status::kSuccess: + return std::string("Success"); + case PayloadProgressInfo::Status::kFailure: + return std::string("Failure"); + case PayloadProgressInfo::Status::kInProgress: + return std::string("In Progress"); + case PayloadProgressInfo::Status::kCanceled: + return std::string("Cancelled"); + } +} + } // namespace connections } // namespace nearby } // namespace location diff --git a/cpp/core/internal/client_proxy.h b/cpp/core/internal/client_proxy.h index d84d24d9..fa258924 100644 --- a/cpp/core/internal/client_proxy.h +++ b/cpp/core/internal/client_proxy.h @@ -239,6 +239,8 @@ class ClientProxy final { void ScheduleClearLocalHighVisModeCacheEndpointIdAlarm(); void CancelClearLocalHighVisModeCacheEndpointIdAlarm(); + std::string ToString(PayloadProgressInfo::Status status) const; + mutable RecursiveMutex mutex_; Prng prng_; std::int64_t client_id_; diff --git a/cpp/core/internal/offline_service_controller.cc b/cpp/core/internal/offline_service_controller.cc index fab7a4f7..a86eea9d 100644 --- a/cpp/core/internal/offline_service_controller.cc +++ b/cpp/core/internal/offline_service_controller.cc @@ -16,6 +16,8 @@ #include +#include "absl/strings/str_join.h" + namespace location { namespace nearby { namespace connections { @@ -23,20 +25,26 @@ namespace connections { OfflineServiceController::~OfflineServiceController() { Stop(); } void OfflineServiceController::Stop() { + NEARBY_LOGS(INFO) << "Initiating shutdown of OfflineServiceController."; if (stop_.Set(true)) return; payload_manager_.DisconnectFromEndpointManager(); pcp_manager_.DisconnectFromEndpointManager(); + NEARBY_LOGS(INFO) << "OfflineServiceController has shut down."; } Status OfflineServiceController::StartAdvertising( ClientProxy* client, const std::string& service_id, const ConnectionOptions& options, const ConnectionRequestInfo& info) { if (stop_) return {Status::kOutOfOrderApiCall}; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " requested advertising to start."; return pcp_manager_.StartAdvertising(client, service_id, options, info); } void OfflineServiceController::StopAdvertising(ClientProxy* client) { if (stop_) return; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " requested advertising to stop."; pcp_manager_.StopAdvertising(client); } @@ -44,11 +52,15 @@ Status OfflineServiceController::StartDiscovery( ClientProxy* client, const std::string& service_id, const ConnectionOptions& options, const DiscoveryListener& listener) { if (stop_) return {Status::kOutOfOrderApiCall}; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " requested discovery to start."; return pcp_manager_.StartDiscovery(client, service_id, options, listener); } void OfflineServiceController::StopDiscovery(ClientProxy* client) { if (stop_) return; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " requested discovery to stop."; pcp_manager_.StopDiscovery(client); } @@ -63,6 +75,8 @@ Status OfflineServiceController::RequestConnection( ClientProxy* client, const std::string& endpoint_id, const ConnectionRequestInfo& info, const ConnectionOptions& options) { if (stop_) return {Status::kOutOfOrderApiCall}; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " requested a connection to endpoint_id=" << endpoint_id; return pcp_manager_.RequestConnection(client, endpoint_id, info, options); } @@ -70,12 +84,18 @@ Status OfflineServiceController::AcceptConnection( ClientProxy* client, const std::string& endpoint_id, const PayloadListener& listener) { if (stop_) return {Status::kOutOfOrderApiCall}; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " accepted the connection with endpoint_id=" + << endpoint_id; return pcp_manager_.AcceptConnection(client, endpoint_id, listener); } Status OfflineServiceController::RejectConnection( ClientProxy* client, const std::string& endpoint_id) { if (stop_) return {Status::kOutOfOrderApiCall}; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " rejected the connection with endpoint_id=" + << endpoint_id; return pcp_manager_.RejectConnection(client, endpoint_id); } @@ -83,7 +103,7 @@ void OfflineServiceController::InitiateBandwidthUpgrade( ClientProxy* client, const std::string& endpoint_id) { if (stop_) return; NEARBY_LOGS(INFO) << "Client " << client->GetClientId() - << " initiated a manual bandwidth upgrade with endpoint id=" + << " initiated a manual bandwidth upgrade with endpoint_id=" << endpoint_id; bwu_manager_.InitiateBwuForEndpoint(client, endpoint_id); } @@ -92,18 +112,27 @@ void OfflineServiceController::SendPayload( ClientProxy* client, const std::vector& endpoint_ids, Payload payload) { if (stop_) return; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " is sending payload_id=" << payload.GetId() + << " to endpoint_ids={" << absl::StrJoin(endpoint_ids, ",") + << "}"; payload_manager_.SendPayload(client, endpoint_ids, std::move(payload)); } Status OfflineServiceController::CancelPayload(ClientProxy* client, std::int64_t payload_id) { if (stop_) return {Status::kOutOfOrderApiCall}; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " cancelled payload_id=" << payload_id; return payload_manager_.CancelPayload(client, payload_id); } void OfflineServiceController::DisconnectFromEndpoint( ClientProxy* client, const std::string& endpoint_id) { if (stop_) return; + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " requested a disconnection from endpoint_id=" + << endpoint_id; endpoint_manager_.UnregisterEndpoint(client, endpoint_id); } diff --git a/cpp/core/internal/service_controller_router.cc b/cpp/core/internal/service_controller_router.cc index 653f9003..e21e7ae7 100644 --- a/cpp/core/internal/service_controller_router.cc +++ b/cpp/core/internal/service_controller_router.cc @@ -47,8 +47,14 @@ const std::size_t kEndpointIdLength = 4u; const std::size_t kMaxEndpointInfoLength = 131u; } // namespace +ServiceControllerRouter::ServiceControllerRouter( + std::function factory) + : service_controller_factory_(std::move(factory)) { + NEARBY_LOGS(INFO) << "ServiceControllerRouter going up."; +} + ServiceControllerRouter::~ServiceControllerRouter() { - NEARBY_LOG(INFO, "ServiceControllerRouter going down."); + NEARBY_LOGS(INFO) << "ServiceControllerRouter going down."; if (service_controller_) { service_controller_->Stop(); @@ -98,26 +104,26 @@ void ServiceControllerRouter::StartDiscovery(ClientProxy* client, const ConnectionOptions& options, const DiscoveryListener& listener, const ResultCallback& callback) { - RouteToServiceController("scr-start-discovery", [this, client, - service_id = - std::string(service_id), - options, listener, - callback]() { - Status status = AcquireServiceControllerForClient(client, options.strategy); - if (!status.Ok()) { - callback.result_cb(status); - return; - } + RouteToServiceController( + "scr-start-discovery", + [this, client, service_id = std::string(service_id), options, listener, + callback]() { + Status status = + AcquireServiceControllerForClient(client, options.strategy); + if (!status.Ok()) { + callback.result_cb(status); + return; + } - if (client->IsDiscovering()) { - callback.result_cb({Status::kAlreadyDiscovering}); - return; - } + if (client->IsDiscovering()) { + callback.result_cb({Status::kAlreadyDiscovering}); + return; + } - status = service_controller_->StartDiscovery(client, service_id, options, - listener); - callback.result_cb(status); - }); + status = service_controller_->StartDiscovery(client, service_id, + options, listener); + callback.result_cb(status); + }); } void ServiceControllerRouter::StopDiscovery(ClientProxy* client, @@ -134,37 +140,38 @@ void ServiceControllerRouter::InjectEndpoint( ClientProxy* client, absl::string_view service_id, const OutOfBandConnectionMetadata& metadata, const ResultCallback& callback) { - RouteToServiceController("scr-inject-endpoint", [this, client, - service_id = - std::string(service_id), - metadata, callback]() { - // Currently, Bluetooth is the only supported medium for endpoint injection. - if (metadata.medium != Medium::BLUETOOTH || - metadata.remote_bluetooth_mac_address.size() != kMacAddressLength) { - callback.result_cb({Status::kError}); - return; - } + RouteToServiceController( + "scr-inject-endpoint", + [this, client, service_id = std::string(service_id), metadata, + callback]() { + // Currently, Bluetooth is the only supported medium for endpoint + // injection. + if (metadata.medium != Medium::BLUETOOTH || + metadata.remote_bluetooth_mac_address.size() != kMacAddressLength) { + callback.result_cb({Status::kError}); + return; + } - if (metadata.endpoint_id.size() != kEndpointIdLength) { - callback.result_cb({Status::kError}); - return; - } + if (metadata.endpoint_id.size() != kEndpointIdLength) { + callback.result_cb({Status::kError}); + return; + } - if (metadata.endpoint_info.Empty() || - metadata.endpoint_info.size() > kMaxEndpointInfoLength) { - callback.result_cb({Status::kError}); - return; - } + if (metadata.endpoint_info.Empty() || + metadata.endpoint_info.size() > kMaxEndpointInfoLength) { + callback.result_cb({Status::kError}); + return; + } - if (!ClientHasAcquiredServiceController(client) || - !client->IsDiscovering()) { - callback.result_cb({Status::kOutOfOrderApiCall}); - return; - } + if (!ClientHasAcquiredServiceController(client) || + !client->IsDiscovering()) { + callback.result_cb({Status::kOutOfOrderApiCall}); + return; + } - service_controller_->InjectEndpoint(client, service_id, metadata); - callback.result_cb({Status::kSuccess}); - }); + service_controller_->InjectEndpoint(client, service_id, metadata); + callback.result_cb({Status::kSuccess}); + }); } void ServiceControllerRouter::RequestConnection( @@ -203,32 +210,33 @@ void ServiceControllerRouter::AcceptConnection(ClientProxy* client, absl::string_view endpoint_id, const PayloadListener& listener, const ResultCallback& callback) { - RouteToServiceController("scr-accept-connection", [this, client, - endpoint_id = std::string( - endpoint_id), - listener, callback]() { - if (!ClientHasAcquiredServiceController(client)) { - callback.result_cb({Status::kOutOfOrderApiCall}); - return; - } + RouteToServiceController( + "scr-accept-connection", + [this, client, endpoint_id = std::string(endpoint_id), listener, + callback]() { + if (!ClientHasAcquiredServiceController(client)) { + callback.result_cb({Status::kOutOfOrderApiCall}); + return; + } - if (client->IsConnectedToEndpoint(endpoint_id)) { - callback.result_cb({Status::kAlreadyConnectedToEndpoint}); - return; - } + if (client->IsConnectedToEndpoint(endpoint_id)) { + callback.result_cb({Status::kAlreadyConnectedToEndpoint}); + return; + } - if (client->HasLocalEndpointResponded(endpoint_id)) { - NEARBY_LOG(INFO, - "[ServiceControllerRouter:Accept]: Client has local " - "endpoint responded; id=%s", - endpoint_id.c_str()); - callback.result_cb({Status::kOutOfOrderApiCall}); - return; - } + if (client->HasLocalEndpointResponded(endpoint_id)) { + NEARBY_LOGS(WARNING) + << "Client " << client->GetClientId() + << " invoked acceptConnectionRequest() after having already " + "accepted/rejected the connection to endpoint(id=" + << endpoint_id << ")"; + callback.result_cb({Status::kOutOfOrderApiCall}); + return; + } - callback.result_cb( - service_controller_->AcceptConnection(client, endpoint_id, listener)); - }); + callback.result_cb(service_controller_->AcceptConnection( + client, endpoint_id, listener)); + }); } void ServiceControllerRouter::RejectConnection(ClientProxy* client, @@ -250,10 +258,11 @@ void ServiceControllerRouter::RejectConnection(ClientProxy* client, } if (client->HasLocalEndpointResponded(endpoint_id)) { - NEARBY_LOG(INFO, - "[ServiceControllerRouter:Reject]: Client has local " - "endpoint responded; id=%s", - endpoint_id.c_str()); + NEARBY_LOGS(WARNING) + << "Client " << client->GetClientId() + << " invoked rejectConnectionRequest() after having already " + "accepted/rejected the connection to endpoint(id=" + << endpoint_id << ")"; callback.result_cb({Status::kOutOfOrderApiCall}); return; } @@ -296,28 +305,27 @@ void ServiceControllerRouter::SendPayload( const std::vector endpoints = std::vector(endpoint_ids.begin(), endpoint_ids.end()); - RouteToServiceController( - "scr-send-payload", - [this, client, shared_payload, endpoints, callback]() { - if (!ClientHasAcquiredServiceController(client)) { - callback.result_cb({Status::kOutOfOrderApiCall}); - return; - } + RouteToServiceController("scr-send-payload", [this, client, shared_payload, + endpoints, callback]() { + if (!ClientHasAcquiredServiceController(client)) { + callback.result_cb({Status::kOutOfOrderApiCall}); + return; + } - if (!ClientHasConnectionToAtLeastOneEndpoint(client, endpoints)) { - callback.result_cb({Status::kEndpointUnknown}); - return; - } + if (!ClientHasConnectionToAtLeastOneEndpoint(client, endpoints)) { + callback.result_cb({Status::kEndpointUnknown}); + return; + } - service_controller_->SendPayload(client, endpoints, - std::move(*shared_payload)); + service_controller_->SendPayload(client, endpoints, + std::move(*shared_payload)); - // At this point, we've queued up the send Payload request with the - // ServiceController; any further failures (e.g. one of the endpoints is - // unknown, goes away, or otherwise fails) will be returned to the - // client as a PayloadTransferUpdate. - callback.result_cb({Status::kSuccess}); - }); + // At this point, we've queued up the send Payload request with the + // ServiceController; any further failures (e.g. one of the endpoints is + // unknown, goes away, or otherwise fails) will be returned to the + // client as a PayloadTransferUpdate. + callback.result_cb({Status::kSuccess}); + }); } void ServiceControllerRouter::CancelPayload(ClientProxy* client, @@ -362,16 +370,16 @@ void ServiceControllerRouter::StopAllEndpoints(ClientProxy* client, // without further posting it. client->CancelAllEndpoints(); - RouteToServiceController("scr-stop-all-endpoints", [this, client, - callback]() { - NEARBY_LOGS(INFO) << "Client " << client->GetClientId() - << " has requested us to stop all endpoints. We will now " - "reset the client."; - if (ClientHasAcquiredServiceController(client)) { - DoneWithStrategySessionForClient(client); - } - callback.result_cb({Status::kSuccess}); - }); + RouteToServiceController( + "scr-stop-all-endpoints", [this, client, callback]() { + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " has requested us to stop all endpoints. We will " + "now reset the client."; + if (ClientHasAcquiredServiceController(client)) { + DoneWithStrategySessionForClient(client); + } + callback.result_cb({Status::kSuccess}); + }); } void ServiceControllerRouter::ClientDisconnecting( @@ -380,16 +388,15 @@ void ServiceControllerRouter::ClientDisconnecting( // without further posting it. client->CancelAllEndpoints(); - RouteToServiceController("scr-client-disconnecting", [this, client, - callback]() { - if (ClientHasAcquiredServiceController(client)) { - DoneWithStrategySessionForClient(client); - NEARBY_LOG(INFO, - "[ServiceControllerRouter:Disconnect]: Client has completed " - "the client's connection"); - } - callback.result_cb({Status::kSuccess}); - }); + RouteToServiceController( + "scr-client-disconnecting", [this, client, callback]() { + if (ClientHasAcquiredServiceController(client)) { + DoneWithStrategySessionForClient(client); + NEARBY_LOGS(INFO) << "Client " << client->GetClientId() + << " has completed the client's connection."; + } + callback.result_cb({Status::kSuccess}); + }); } Status ServiceControllerRouter::AcquireServiceControllerForClient( @@ -419,18 +426,14 @@ Status ServiceControllerRouter::AcquireServiceControllerForClient( bool is_the_only_client_of_service_controller = clients_.size() == 1 && ClientHasAcquiredServiceController(client); if (!is_the_only_client_of_service_controller) { - NEARBY_LOG(INFO, - "[ServiceControllerRouter:AcquireServiceControllerForClient]: " - "Client has already active strategy."); + NEARBY_LOGS(INFO) << "Client has already active strategy."; return {Status::kAlreadyHaveActiveStrategy}; } // If the client still has connected endpoints, they must disconnect before // they can switch. if (!client->GetConnectedEndpoints().empty()) { - NEARBY_LOG(INFO, - "[ServiceControllerRouter:AcquireServiceControllerForClient]: " - "Client has connected endpoints."); + NEARBY_LOGS(INFO) << "Client has connected endpoints."; return {Status::kOutOfOrderApiCall}; } @@ -497,7 +500,7 @@ bool ServiceControllerRouter::ClientHasConnectionToAtLeastOneEndpoint( Status ServiceControllerRouter::UpdateCurrentServiceControllerAndStrategy( Strategy strategy) { if (!strategy.IsValid()) { - NEARBY_LOG(INFO, "Strategy is not valid."); + NEARBY_LOGS(INFO) << "Strategy is not valid."; return {Status::kError}; } diff --git a/cpp/core/internal/service_controller_router.h b/cpp/core/internal/service_controller_router.h index fa209e63..62f070f3 100644 --- a/cpp/core/internal/service_controller_router.h +++ b/cpp/core/internal/service_controller_router.h @@ -53,8 +53,7 @@ namespace connections { // of a ServiceController interface, which does the actual job. class ServiceControllerRouter { public: - explicit ServiceControllerRouter(std::function factory) - : service_controller_factory_(std::move(factory)) {} + explicit ServiceControllerRouter(std::function factory); ~ServiceControllerRouter(); ServiceControllerRouter(ServiceControllerRouter&&) = default; ServiceControllerRouter& operator=(ServiceControllerRouter&&) = default;