From 2d4edf5b18e7029be355017f436c96a9f66fd9a0 Mon Sep 17 00:00:00 2001 From: hai007 Date: Fri, 12 Feb 2021 12:18:55 -0800 Subject: [PATCH] cl/356658736 Add Stop() to ServiceController API. --- cpp/core/internal/BUILD | 1 - cpp/core/internal/mock_service_controller.h | 1 + .../internal/offline_service_controller.cc | 12 ++ .../internal/offline_service_controller.h | 2 +- cpp/core/internal/service_controller.h | 7 + .../internal/service_controller_router.cc | 7 +- cpp/core/internal/service_controller_router.h | 3 +- .../internal/stoppable_service_controller.h | 131 ------------------ 8 files changed, 25 insertions(+), 139 deletions(-) delete mode 100644 cpp/core/internal/stoppable_service_controller.h diff --git a/cpp/core/internal/BUILD b/cpp/core/internal/BUILD index 803df6ad..f6829072 100644 --- a/cpp/core/internal/BUILD +++ b/cpp/core/internal/BUILD @@ -62,7 +62,6 @@ cc_library( "pcp_manager.h", "service_controller.h", "service_controller_router.h", - "stoppable_service_controller.h", "webrtc_bwu_handler.h", "webrtc_endpoint_channel.h", "wifi_lan_bwu_handler.h", diff --git a/cpp/core/internal/mock_service_controller.h b/cpp/core/internal/mock_service_controller.h index d89e47e1..a6745d77 100644 --- a/cpp/core/internal/mock_service_controller.h +++ b/cpp/core/internal/mock_service_controller.h @@ -17,6 +17,7 @@ namespace connections { */ class MockServiceController : public ServiceController { public: + MOCK_METHOD(void, Stop, (), (override)); MOCK_METHOD(Status, StartAdvertising, (ClientProxy * client, const std::string& service_id, const ConnectionOptions& options, diff --git a/cpp/core/internal/offline_service_controller.cc b/cpp/core/internal/offline_service_controller.cc index c1008a4f..d4c0a97a 100644 --- a/cpp/core/internal/offline_service_controller.cc +++ b/cpp/core/internal/offline_service_controller.cc @@ -17,48 +17,57 @@ void OfflineServiceController::Stop() { Status OfflineServiceController::StartAdvertising( ClientProxy* client, const std::string& service_id, const ConnectionOptions& options, const ConnectionRequestInfo& info) { + if (stop_) return {Status::kOutOfOrderApiCall}; return pcp_manager_.StartAdvertising(client, service_id, options, info); } void OfflineServiceController::StopAdvertising(ClientProxy* client) { + if (stop_) return; pcp_manager_.StopAdvertising(client); } Status OfflineServiceController::StartDiscovery( ClientProxy* client, const std::string& service_id, const ConnectionOptions& options, const DiscoveryListener& listener) { + if (stop_) return {Status::kOutOfOrderApiCall}; return pcp_manager_.StartDiscovery(client, service_id, options, listener); } void OfflineServiceController::StopDiscovery(ClientProxy* client) { + if (stop_) return; pcp_manager_.StopDiscovery(client); } void OfflineServiceController::InjectEndpoint( ClientProxy* client, const std::string& service_id, const OutOfBandConnectionMetadata& metadata) { + if (stop_) return; pcp_manager_.InjectEndpoint(client, service_id, metadata); } Status OfflineServiceController::RequestConnection( ClientProxy* client, const std::string& endpoint_id, const ConnectionRequestInfo& info, const ConnectionOptions& options) { + if (stop_) return {Status::kOutOfOrderApiCall}; return pcp_manager_.RequestConnection(client, endpoint_id, info, options); } Status OfflineServiceController::AcceptConnection( ClientProxy* client, const std::string& endpoint_id, const PayloadListener& listener) { + if (stop_) return {Status::kOutOfOrderApiCall}; return pcp_manager_.AcceptConnection(client, endpoint_id, listener); } Status OfflineServiceController::RejectConnection( ClientProxy* client, const std::string& endpoint_id) { + if (stop_) return {Status::kOutOfOrderApiCall}; return pcp_manager_.RejectConnection(client, endpoint_id); } 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=" << endpoint_id; @@ -68,16 +77,19 @@ void OfflineServiceController::InitiateBandwidthUpgrade( void OfflineServiceController::SendPayload( ClientProxy* client, const std::vector& endpoint_ids, Payload payload) { + if (stop_) return; payload_manager_.SendPayload(client, endpoint_ids, std::move(payload)); } Status OfflineServiceController::CancelPayload(ClientProxy* client, std::int64_t payload_id) { + if (stop_) return {Status::kOutOfOrderApiCall}; return payload_manager_.CancelPayload(client, payload_id); } void OfflineServiceController::DisconnectFromEndpoint( ClientProxy* client, const std::string& endpoint_id) { + if (stop_) return; endpoint_manager_.UnregisterEndpoint(client, endpoint_id); } diff --git a/cpp/core/internal/offline_service_controller.h b/cpp/core/internal/offline_service_controller.h index bc77e201..e74c7a02 100644 --- a/cpp/core/internal/offline_service_controller.h +++ b/cpp/core/internal/offline_service_controller.h @@ -61,7 +61,7 @@ class OfflineServiceController : public ServiceController { void DisconnectFromEndpoint(ClientProxy* client, const std::string& endpoint_id) override; - void Stop(); + void Stop() override; private: // Note that the order of declaration of these is crucial, because we depend diff --git a/cpp/core/internal/service_controller.h b/cpp/core/internal/service_controller.h index ecd80e8a..aa11b040 100644 --- a/cpp/core/internal/service_controller.h +++ b/cpp/core/internal/service_controller.h @@ -34,6 +34,13 @@ class ServiceController { ServiceController(const ServiceController&) = delete; ServiceController& operator=(const ServiceController&) = delete; + // Stops and disables service controller. + // + // When service controller is stopped all API call fail early. + // Note that all Core, ClientProxy objects referencing this service + // controller are affected. + virtual void Stop() = 0; + // Starts advertising an endpoint for a local app. virtual Status StartAdvertising(ClientProxy* client, const std::string& service_id, diff --git a/cpp/core/internal/service_controller_router.cc b/cpp/core/internal/service_controller_router.cc index e28d842d..6a44ad4a 100644 --- a/cpp/core/internal/service_controller_router.cc +++ b/cpp/core/internal/service_controller_router.cc @@ -40,7 +40,7 @@ ServiceControllerRouter::~ServiceControllerRouter() { .GetFlags() .disable_released_service_controller) { if (service_controller_) { - service_controller_->Shutdown(); + service_controller_->Stop(); } } else { service_controller_.reset(); @@ -427,7 +427,7 @@ void ServiceControllerRouter::ReleaseServiceControllerForClient( if (FeatureFlags::GetInstance() .GetFlags() .disable_released_service_controller) { - service_controller_->Shutdown(); + service_controller_->Stop(); } if (clients_.empty()) { @@ -477,8 +477,7 @@ Status ServiceControllerRouter::UpdateCurrentServiceControllerAndStrategy( return {Status::kError}; } - service_controller_ = absl::make_unique( - service_controller_factory_()); + service_controller_.reset(service_controller_factory_()); current_strategy_ = strategy; return {Status::kSuccess}; diff --git a/cpp/core/internal/service_controller_router.h b/cpp/core/internal/service_controller_router.h index 9a2817e2..26eb7bf2 100644 --- a/cpp/core/internal/service_controller_router.h +++ b/cpp/core/internal/service_controller_router.h @@ -7,7 +7,6 @@ #include "core/internal/client_proxy.h" #include "core/internal/service_controller.h" -#include "core/internal/stoppable_service_controller.h" #include "core/options.h" #include "core/params.h" #include "platform/base/runnable.h" @@ -106,7 +105,7 @@ class ServiceControllerRouter { absl::flat_hash_set clients_; std::function service_controller_factory_; - std::unique_ptr service_controller_; + std::unique_ptr service_controller_; Strategy current_strategy_; SingleThreadExecutor serializer_; }; diff --git a/cpp/core/internal/stoppable_service_controller.h b/cpp/core/internal/stoppable_service_controller.h deleted file mode 100644 index 4e1f1be6..00000000 --- a/cpp/core/internal/stoppable_service_controller.h +++ /dev/null @@ -1,131 +0,0 @@ -#ifndef CORE_INTERNAL_STOPPABLE_SERVICE_CONTROLLER_H_ -#define CORE_INTERNAL_STOPPABLE_SERVICE_CONTROLLER_H_ - -#include - -#include "core/internal/service_controller.h" -#include "core/status.h" -#include "platform/public/atomic_boolean.h" - -namespace location { -namespace nearby { -namespace connections { - -// A ServiceController proxy that can be shut down. -// When shut down, the API calls are not forwarded to the real controller. -// StoppableServiceController takes over ownership of ServiceController. -class StoppableServiceController : public ServiceController { - public: - explicit StoppableServiceController(ServiceController* controller) - : service_controller_{controller} {} - ~StoppableServiceController() override = default; - - void Shutdown() { stopped_.Set(true); } - - Status StartAdvertising(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, - const ConnectionRequestInfo& info) override { - if (stopped_) { - return {Status::kError}; - } - return service_controller_->StartAdvertising(client, service_id, options, - info); - } - - void StopAdvertising(ClientProxy* client) override { - if (stopped_) { - return; - } - service_controller_->StopAdvertising(client); - } - - Status StartDiscovery(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, - const DiscoveryListener& listener) override { - if (stopped_) { - return {Status::kError}; - } - return service_controller_->StartDiscovery(client, service_id, options, - listener); - } - void StopDiscovery(ClientProxy* client) override { - if (stopped_) { - return; - } - service_controller_->StopDiscovery(client); - } - - void InjectEndpoint(ClientProxy* client, const std::string& service_id, - const OutOfBandConnectionMetadata& metadata) override { - if (stopped_) { - return; - } - service_controller_->InjectEndpoint(client, service_id, metadata); - } - - Status RequestConnection(ClientProxy* client, const std::string& endpoint_id, - const ConnectionRequestInfo& info, - const ConnectionOptions& options) override { - if (stopped_) { - return {Status::kError}; - } - return service_controller_->RequestConnection(client, endpoint_id, info, - options); - } - Status AcceptConnection(ClientProxy* client, const std::string& endpoint_id, - const PayloadListener& listener) override { - if (stopped_) { - return {Status::kError}; - } - return service_controller_->AcceptConnection(client, endpoint_id, listener); - } - Status RejectConnection(ClientProxy* client, - const std::string& endpoint_id) override { - if (stopped_) { - return {Status::kError}; - } - return service_controller_->RejectConnection(client, endpoint_id); - } - - void InitiateBandwidthUpgrade(ClientProxy* client, - const std::string& endpoint_id) override { - if (stopped_) { - return; - } - service_controller_->InitiateBandwidthUpgrade(client, endpoint_id); - } - - void SendPayload(ClientProxy* client, - const std::vector& endpoint_ids, - Payload payload) override { - if (stopped_) { - return; - } - service_controller_->SendPayload(client, endpoint_ids, std::move(payload)); - } - - Status CancelPayload(ClientProxy* client, Payload::Id payload_id) override { - if (stopped_) { - return {Status::kError}; - } - return service_controller_->CancelPayload(client, payload_id); - } - - void DisconnectFromEndpoint(ClientProxy* client, - const std::string& endpoint_id) override { - if (stopped_) { - return; - } - service_controller_->DisconnectFromEndpoint(client, endpoint_id); - } - - private: - std::unique_ptr service_controller_; - AtomicBoolean stopped_{false}; -}; - -} // namespace connections -} // namespace nearby -} // namespace location - -#endif // CORE_INTERNAL_STOPPABLE_SERVICE_CONTROLLER_H_