[Nearby Connections][C++] Track bandwidth upgrade mediums for each endpoint (2/4)

Consolidate BWU service ID bookkeeping into the BaseBwuHandler class. This eliminates duplicated code in per-medium BWU handlers and allows us to easily augment the bookkeeping with endpoint IDs in a subsequent CL.

Also, localize all service ID wrapping to the BaseBwuHandler class. A postfix is appended to the service ID during bandwidth upgrade, e.g., "NearbyShare_INITIATOR_BWU"; this is done to distinguish a medium's use for advertising/discovery vs. its use for device-to-device connections.

This CL should theoretically be a no-op, but per-medium BWU handlers were inconsistent in their implementations.

PiperOrigin-RevId: 442019603
This commit is contained in:
nohle
2022-04-15 08:19:19 -07:00
committed by Copybara-Service
parent 463d391fcd
commit dee3e9cfb4
14 changed files with 391 additions and 398 deletions
+25 -14
View File
@@ -15,40 +15,51 @@
#ifndef CORE_INTERNAL_BASE_BWU_HANDLER_H_
#define CORE_INTERNAL_BASE_BWU_HANDLER_H_
#include <cstdint>
#include <memory>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/time/clock.h"
#include "connections/implementation/bwu_handler.h"
#include "connections/implementation/client_proxy.h"
#include "connections/implementation/endpoint_channel_manager.h"
#include "internal/platform/cancelable_alarm.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/scheduled_executor.h"
#include "internal/platform/single_thread_executor.h"
namespace location {
namespace nearby {
namespace connections {
// Manages the bookkeeping common to all medium handlers. Notably, it tracks all
// of the service IDs (TODO: and endpoint IDs) that initiated a bandwidth
// upgrade.
class BaseBwuHandler : public BwuHandler {
public:
using ClientIntroduction = BwuNegotiationFrame::ClientIntroduction;
explicit BaseBwuHandler(BwuNotifications bwu_notifications);
BaseBwuHandler(EndpointChannelManager& channel_manager,
BwuNotifications bwu_notifications)
: channel_manager_(&channel_manager),
bwu_notifications_(std::move(bwu_notifications)) {}
~BaseBwuHandler() override = default;
// BwuHandler implementation:
ByteArray InitializeUpgradedMediumForEndpoint(
ClientProxy* client, const std::string& service_id,
const std::string& endpoint_id) final;
void RevertInitiatorState() final;
protected:
// Invoked by InitializeUpgradedMediumForEndpoint and RevertInitiatorState,
// respectively, to handle medium-specific logic.
virtual ByteArray HandleInitializeUpgradedMediumForEndpoint(
ClientProxy* client, const std::string& upgrade_service_id,
const std::string& endpoint_id) = 0;
virtual void HandleRevertInitiatorStateForService(
const std::string& upgrade_service_id) = 0;
// Represents the incoming Socket the Initiator has gotten after initializing
// its upgraded bandwidth medium.
EndpointChannelManager* GetEndpointChannelManager();
EndpointChannelManager* channel_manager_;
BwuNotifications bwu_notifications_;
private:
// The set of service IDs that are initiating a bandwidth upgrade. Not used
// for service IDs that respond to bandwidth upgrade requests from another
// device; only tracked by the initiator.
// TODO(nohle): track endpoint IDs as well.
absl::flat_hash_set<std::string> active_service_ids_;
};
} // namespace connections