Roll forward to cl/338482889

Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I9850950db8bd84f0904ea1a413151887f52098cf
This commit is contained in:
Alexey Polyudov
2020-10-22 10:47:34 -07:00
parent d68e53cf03
commit 2155b3ddeb
542 changed files with 15219 additions and 42295 deletions
+82 -117
View File
@@ -1,152 +1,117 @@
#ifndef CORE_INTERNAL_SERVICE_CONTROLLER_ROUTER_H_
#define CORE_INTERNAL_SERVICE_CONTROLLER_ROUTER_H_
#include <set>
#include <memory>
#include <string>
#include <vector>
#include "core/internal/client_proxy.h"
#include "core/internal/service_controller.h"
#include "core/options.h"
#include "core/params.h"
#include "platform/port/string.h"
#include "platform/ptr.h"
#include "platform/runnable.h"
#include "platform/base/runnable.h"
#include "platform/public/single_thread_executor.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
namespace location {
namespace nearby {
namespace connections {
namespace service_controller_router {
template <typename>
class StartAdvertisingRunnable;
template <typename>
class StopAdvertisingRunnable;
template <typename>
class StartDiscoveryRunnable;
template <typename>
class StopDiscoveryRunnable;
template <typename>
class SendConnectionRequestRunnable;
template <typename>
class AcceptConnectionRequestRunnable;
template <typename>
class RejectConnectionRequestRunnable;
template <typename>
class InitiateBandwidthUpgradeRunnable;
template <typename>
class SendPayloadRunnable;
template <typename>
class CancelPayloadRunnable;
template <typename>
class DisconnectFromEndpointRunnable;
template <typename>
class StopAllEndpointsRunnable;
template <typename>
class ClientDisconnectingRunnable;
} // namespace service_controller_router
template <typename Platform>
// ServiceControllerRouter: this class is an implementation detail of a
// location::nearby::Core class. The latter delegates all of its activities to
// the former.
//
// All the activities are documented in the public API class:
// https://source.corp.google.com/piper///depot/google3/core_v2/core.h
//
// In every method, ClientProxy* represents the client app which receives
// notifications from Nearby Connections service and forwards them to the app.
// The rest of arguments have the same meaning as the corresponding
// methods in the definition of location::nearby::Core API.
//
// Every activity is handled the same way:
// 1) all the arguments to the call are captured by value;
// 2) the actual processing is scheduled on a private single-threaded executor,
// which makes locking unnecessary, when internal data is being manipulated.
// 3) activity handlers are delegating much of their work to an implementation
// of a ServiceController interface, which does the actual job.
class ServiceControllerRouter {
public:
ServiceControllerRouter();
explicit ServiceControllerRouter(std::function<ServiceController*()> factory)
: service_controller_factory_(std::move(factory)) {}
~ServiceControllerRouter();
ServiceControllerRouter(ServiceControllerRouter&&) = default;
ServiceControllerRouter& operator=(ServiceControllerRouter&&) = default;
void startAdvertising(
Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<StartAdvertisingParams> start_advertising_params);
void stopAdvertising(Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<StopAdvertisingParams> stop_advertising_params);
void StartAdvertising(ClientProxy* client, absl::string_view service_id,
const ConnectionOptions& options,
const ConnectionRequestInfo& info,
const ResultCallback& callback);
void StopAdvertising(ClientProxy* client, const ResultCallback& callback);
void startDiscovery(Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<StartDiscoveryParams> start_discovery_params);
void stopDiscovery(Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<StopDiscoveryParams> stop_discovery_params);
void StartDiscovery(ClientProxy* client, absl::string_view service_id,
const ConnectionOptions& options,
const DiscoveryListener& listener,
const ResultCallback& callback);
void StopDiscovery(ClientProxy* client, const ResultCallback& callback);
void requestConnection(
Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<RequestConnectionParams> request_connection_params);
void acceptConnection(
Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<AcceptConnectionParams> accept_connection_params);
void rejectConnection(
Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<RejectConnectionParams> reject_connection_params);
void InjectEndpoint(ClientProxy* client,
absl::string_view service_id,
const OutOfBandConnectionMetadata& metadata,
const ResultCallback& callback);
void initiateBandwidthUpgrade(Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<InitiateBandwidthUpgradeParams>
initiate_bandwidth_upgrade_params);
void RequestConnection(ClientProxy* client, absl::string_view endpoint_id,
const ConnectionRequestInfo& info,
const ConnectionOptions& options,
const ResultCallback& callback);
void AcceptConnection(ClientProxy* client, absl::string_view endpoint_id,
const PayloadListener& listener,
const ResultCallback& callback);
void RejectConnection(ClientProxy* client, absl::string_view endpoint_id,
const ResultCallback& callback);
void sendPayload(Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<SendPayloadParams> send_payload_params);
void cancelPayload(Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<CancelPayloadParams> cancel_payload_params);
void InitiateBandwidthUpgrade(ClientProxy* client,
absl::string_view endpoint_id,
const ResultCallback& callback);
void disconnectFromEndpoint(
Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<DisconnectFromEndpointParams> disconnect_from_endpoint_params);
void stopAllEndpoints(
Ptr<ClientProxy<Platform> > client_proxy,
ConstPtr<StopAllEndpointsParams> stop_all_endpoint_params);
void SendPayload(ClientProxy* client,
absl::Span<const std::string> endpoint_ids, Payload payload,
const ResultCallback& callback);
void CancelPayload(ClientProxy* client, std::uint64_t payload_id,
const ResultCallback& callback);
void clientDisconnecting(Ptr<ClientProxy<Platform> > client_proxy);
void DisconnectFromEndpoint(ClientProxy* client,
absl::string_view endpoint_id,
const ResultCallback& callback);
void StopAllEndpoints(ClientProxy* client, const ResultCallback& callback);
void ClientDisconnecting(ClientProxy* client, const ResultCallback& callback);
private:
template <typename>
friend class service_controller_router::StartAdvertisingRunnable;
template <typename>
friend class service_controller_router::StopAdvertisingRunnable;
template <typename>
friend class service_controller_router::StartDiscoveryRunnable;
template <typename>
friend class service_controller_router::StopDiscoveryRunnable;
template <typename>
friend class service_controller_router::SendConnectionRequestRunnable;
template <typename>
friend class service_controller_router::AcceptConnectionRequestRunnable;
template <typename>
friend class service_controller_router::RejectConnectionRequestRunnable;
template <typename>
friend class service_controller_router::InitiateBandwidthUpgradeRunnable;
template <typename>
friend class service_controller_router::SendPayloadRunnable;
template <typename>
friend class service_controller_router::CancelPayloadRunnable;
template <typename>
friend class service_controller_router::DisconnectFromEndpointRunnable;
template <typename>
friend class service_controller_router::StopAllEndpointsRunnable;
template <typename>
friend class service_controller_router::ClientDisconnectingRunnable;
friend class ServiceControllerRouterTest;
static bool ClientHasConnectionToAtLeastOneEndpoint(
ClientProxy* client, const std::vector<std::string>& remote_endpoint_ids);
static bool clientHasConnectionToAtLeastOneEndpoint(
Ptr<ClientProxy<Platform> > client_proxy,
const std::vector<std::string>& remote_endpoint_ids);
void RouteToServiceController(Runnable runnable);
void routeToServiceController(Ptr<Runnable> runnable);
Status AcquireServiceControllerForClient(ClientProxy* client,
Strategy strategy);
bool ClientHasAcquiredServiceController(ClientProxy* client) const;
void ReleaseServiceControllerForClient(ClientProxy* client);
void DoneWithStrategySessionForClient(ClientProxy* client);
Status UpdateCurrentServiceControllerAndStrategy(Strategy strategy);
Status::Value acquireServiceControllerForClient(
Ptr<ClientProxy<Platform> > client_proxy, const Strategy& strategy);
bool clientHasAquiredServiceController(
Ptr<ClientProxy<Platform> > client_proxy);
void releaseServiceControllerForClient(
Ptr<ClientProxy<Platform> > client_proxy);
void doneWithStrategySessionForClient(
Ptr<ClientProxy<Platform> > client_proxy);
Status::Value updateCurrentServiceControllerAndStrategy(
const Strategy& strategy);
std::set<Ptr<ClientProxy<Platform> > > current_service_controller_clients_;
Ptr<ServiceController<Platform> > current_service_controller_;
Ptr<Strategy> current_strategy_;
ScopedPtr<Ptr<typename Platform::SingleThreadExecutorType> > serializer_;
std::shared_ptr<ServiceControllerRouter<Platform>> self_{this, [](void*){}};
absl::flat_hash_set<ClientProxy*> clients_;
std::function<ServiceController*()> service_controller_factory_;
std::unique_ptr<ServiceController> service_controller_;
Strategy current_strategy_;
SingleThreadExecutor serializer_;
};
} // namespace connections
} // namespace nearby
} // namespace location
#include "core/internal/service_controller_router.cc"
#endif // CORE_INTERNAL_SERVICE_CONTROLLER_ROUTER_H_