mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Merge branch 'google3'
Change-Id: Ibebf84b98939ee8e5006beedc34d225e8c2dd413
This commit is contained in:
+3
-1
@@ -49,7 +49,9 @@ cc_library(
|
||||
":types",
|
||||
"//platform:types",
|
||||
"//platform:utils",
|
||||
"//platform/impl/sample",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//platform/impl/shared/sample:sample_wifi_medium",
|
||||
"//platform/port:string",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -48,8 +48,9 @@ target_link_libraries(core_build_test
|
||||
absl::strings
|
||||
core
|
||||
core_types
|
||||
platform_impl_default_lock
|
||||
platform_impl_sample
|
||||
platform_impl_g3
|
||||
platform_impl_shared_posix_lock
|
||||
platform_impl_shared_sample
|
||||
platform_port_string
|
||||
platform_types
|
||||
platform_utils
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "core/core.h"
|
||||
@@ -6,9 +5,10 @@
|
||||
#include "core/params.h"
|
||||
#include "core/payload.h"
|
||||
#include "core/status.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/byte_array.h"
|
||||
#include "platform/file_impl.h"
|
||||
#include "platform/impl/sample/sample_platform.h"
|
||||
#include "platform/impl/shared/sample/sample_wifi_medium.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
|
||||
@@ -16,6 +16,8 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
using TestPlatform = platform::ImplementationPlatform;
|
||||
|
||||
class ResultListenerImpl : public ResultListener {
|
||||
public:
|
||||
void onResult(Status::Value status) override {}
|
||||
@@ -53,7 +55,7 @@ class PayloadListenerImpl : public PayloadListener {
|
||||
};
|
||||
|
||||
void check_compilation() {
|
||||
Core<sample::SamplePlatform> core;
|
||||
Core<TestPlatform> core;
|
||||
|
||||
const string name = "name";
|
||||
const string service_id = "service_id";
|
||||
|
||||
+12
-7
@@ -32,15 +32,20 @@ namespace connections {
|
||||
* SystemClock
|
||||
* ConditionVariable
|
||||
*
|
||||
* The Platform class must also provide typedefs for the following subset of
|
||||
* primitives to identify the concrete classes:
|
||||
* A sample Platform definitions can be found at
|
||||
* //platform/impl/shared/sample/sample_platform.cc
|
||||
*
|
||||
* SingleThreadExecutorType
|
||||
* MultiThreadExecutorType
|
||||
* ScheduledExecutorType
|
||||
* It is no longer necessary to parametrize system types with a platform type.
|
||||
* New, recommended approach is to define platform support by implementing
|
||||
* static methods of "location::nearby::platform::ImplementationPlatform" class.
|
||||
* every library class that needs platform support, must include platform
|
||||
* header "platform/api/platform.h" and use it.
|
||||
* To keep textual compatibility, one could define the following alias
|
||||
* "using Platform = platform::ImplementationPlatform;".
|
||||
* this will replace the "template <typename Platform>" declaration.
|
||||
*
|
||||
* A sample Platform class can be found at
|
||||
* //platform/impl/sample/sample_platform.h
|
||||
* As an added benefit, this will allow to not include *.cc files from *.h,
|
||||
* and let more static analysis happen at compiler stage.
|
||||
*/
|
||||
template <typename Platform>
|
||||
class Core {
|
||||
|
||||
+33
-8
@@ -1,38 +1,39 @@
|
||||
cc_library(
|
||||
name = "internal",
|
||||
srcs = [
|
||||
"bandwidth_upgrade_manager.cc",
|
||||
"base_bandwidth_upgrade_handler.cc",
|
||||
"base_endpoint_channel.cc",
|
||||
"ble_advertisement.cc",
|
||||
"ble_endpoint_channel.cc",
|
||||
"bluetooth_device_name.cc",
|
||||
"bluetooth_endpoint_channel.cc",
|
||||
"endpoint_channel_manager.cc",
|
||||
"internal_payload.cc",
|
||||
"internal_payload.h",
|
||||
"loop_runner.cc",
|
||||
"loop_runner.h",
|
||||
"offline_frames.cc",
|
||||
"wifi_lan_endpoint_channel.cc",
|
||||
"wifi_lan_service_info.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"bandwidth_upgrade_handler.h",
|
||||
"bandwidth_upgrade_manager.cc",
|
||||
"bandwidth_upgrade_manager.h",
|
||||
"base_bandwidth_upgrade_handler.cc",
|
||||
"base_bandwidth_upgrade_handler.h",
|
||||
"base_endpoint_channel.cc",
|
||||
"base_endpoint_channel.h",
|
||||
"base_pcp_handler.cc",
|
||||
"base_pcp_handler.h",
|
||||
"ble_advertisement.h",
|
||||
"ble_compat.h",
|
||||
"ble_endpoint_channel.cc",
|
||||
"ble_endpoint_channel.h",
|
||||
"bluetooth_device_name.h",
|
||||
"bluetooth_endpoint_channel.cc",
|
||||
"bluetooth_endpoint_channel.h",
|
||||
"client_proxy.cc",
|
||||
"client_proxy.h",
|
||||
"encryption_runner.cc",
|
||||
"encryption_runner.h",
|
||||
"endpoint_channel.h",
|
||||
"endpoint_channel_manager.cc",
|
||||
"endpoint_channel_manager.h",
|
||||
"endpoint_manager.cc",
|
||||
"endpoint_manager.h",
|
||||
@@ -58,6 +59,7 @@ cc_library(
|
||||
"service_controller.h",
|
||||
"service_controller_router.cc",
|
||||
"service_controller_router.h",
|
||||
"wifi_lan_endpoint_channel.h",
|
||||
"wifi_lan_service_info.h",
|
||||
"wifi_lan_upgrade_handler.cc",
|
||||
"wifi_lan_upgrade_handler.h",
|
||||
@@ -73,7 +75,6 @@ cc_library(
|
||||
"//platform:types",
|
||||
"//platform:utils",
|
||||
"//platform/api",
|
||||
"//platform/port:down_cast",
|
||||
"//platform/port:string",
|
||||
"//proto:connections_enums_portable_proto",
|
||||
"//net/proto2/compat/public:proto2_lite",
|
||||
@@ -82,13 +83,29 @@ cc_library(
|
||||
],
|
||||
)
|
||||
|
||||
# TODO(apolyudov): remove when api v2 rework is done.
|
||||
cc_library(
|
||||
name = "message_lite",
|
||||
hdrs = [
|
||||
"message_lite.h",
|
||||
],
|
||||
visibility = [
|
||||
"//core:__subpackages__",
|
||||
"//core_v2:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"//net/proto2/compat/public:proto2_lite",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "base_endpoint_channel_test",
|
||||
srcs = ["base_endpoint_channel_test.cc"],
|
||||
deps = [
|
||||
":internal",
|
||||
"//platform:utils",
|
||||
"//platform/impl/default",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//proto:connections_enums_portable_proto",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
@@ -100,6 +117,8 @@ cc_test(
|
||||
deps = [
|
||||
":internal",
|
||||
"//platform:utils",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//platform/port:string",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
@@ -110,6 +129,8 @@ cc_test(
|
||||
srcs = ["ble_advertisement_test.cc"],
|
||||
deps = [
|
||||
":internal",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//platform/port:string",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
@@ -121,6 +142,8 @@ cc_test(
|
||||
deps = [
|
||||
":internal",
|
||||
"//platform:utils",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//platform/port:string",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
@@ -135,6 +158,8 @@ cc_test(
|
||||
":internal",
|
||||
"//proto/connections:offline_wire_formats_portable_proto",
|
||||
"//platform:types",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -2,14 +2,21 @@ add_library(core_internal STATIC)
|
||||
|
||||
target_sources(core_internal
|
||||
PRIVATE
|
||||
bandwidth_upgrade_manager.cc
|
||||
base_bandwidth_upgrade_handler.cc
|
||||
base_endpoint_channel.cc
|
||||
ble_advertisement.cc
|
||||
ble_endpoint_channel.cc
|
||||
bluetooth_device_name.cc
|
||||
bluetooth_endpoint_channel.cc
|
||||
endpoint_channel_manager.cc
|
||||
internal_payload.cc
|
||||
internal_payload.h
|
||||
loop_runner.cc
|
||||
loop_runner.h
|
||||
offline_frames.cc
|
||||
wifi_lan_service_info.cc
|
||||
wifi_lan_endpoint_channel.cc
|
||||
PUBLIC
|
||||
bandwidth_upgrade_handler.h
|
||||
bandwidth_upgrade_manager.h
|
||||
@@ -39,6 +46,7 @@ target_sources(core_internal
|
||||
pcp_manager.h
|
||||
service_controller.h
|
||||
service_controller_router.h
|
||||
wifi_lan_endpoint_channel.h
|
||||
wifi_lan_upgrade_handler.h
|
||||
)
|
||||
|
||||
@@ -76,9 +84,9 @@ target_link_libraries(core_internal_test
|
||||
gmock
|
||||
gtest
|
||||
gtest_main
|
||||
platform_impl_default
|
||||
platform_impl_default_cond_var
|
||||
platform_impl_default_lock
|
||||
platform_impl_g3
|
||||
platform_impl_shared_posix_condition_variable
|
||||
platform_impl_shared_posix_lock
|
||||
platform_port_string
|
||||
platform_utils
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "core/internal/client_proxy.h"
|
||||
#include "proto/connections/offline_wire_formats.pb.h"
|
||||
#include "platform/api/count_down_latch.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
|
||||
@@ -13,9 +14,10 @@ namespace connections {
|
||||
|
||||
// Defines the set of methods that need to be implemented to handle the
|
||||
// per-Medium-specific operations needed to upgrade an EndpointChannel.
|
||||
template <typename Platform>
|
||||
class BandwidthUpgradeHandler {
|
||||
public:
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
|
||||
virtual ~BandwidthUpgradeHandler() {}
|
||||
|
||||
// Reverts any changes made to the device in the process of upgrading
|
||||
|
||||
@@ -6,38 +6,32 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
template <typename Platform>
|
||||
BandwidthUpgradeManager<Platform>::BandwidthUpgradeManager(
|
||||
BandwidthUpgradeManager::BandwidthUpgradeManager(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager)
|
||||
: endpoint_manager_(endpoint_manager),
|
||||
bandwidth_upgrade_handlers_(),
|
||||
current_bandwidth_upgrade_handler_() {}
|
||||
|
||||
template <typename Platform>
|
||||
BandwidthUpgradeManager<Platform>::~BandwidthUpgradeManager() {
|
||||
BandwidthUpgradeManager::~BandwidthUpgradeManager() {
|
||||
// TODO(ahlee): Make sure we don't repeat the mistake fixed in cl/201883908.
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BandwidthUpgradeManager<Platform>::initiateBandwidthUpgradeForEndpoint(
|
||||
void BandwidthUpgradeManager::initiateBandwidthUpgradeForEndpoint(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id,
|
||||
proto::connections::Medium medium) {}
|
||||
|
||||
template <typename Platform>
|
||||
void BandwidthUpgradeManager<Platform>::processIncomingOfflineFrame(
|
||||
void BandwidthUpgradeManager::processIncomingOfflineFrame(
|
||||
ConstPtr<OfflineFrame> offline_frame, const string& from_endpoint_id,
|
||||
Ptr<ClientProxy<Platform> > to_client_proxy,
|
||||
proto::connections::Medium current_medium) {}
|
||||
|
||||
template <typename Platform>
|
||||
void BandwidthUpgradeManager<Platform>::processEndpointDisconnection(
|
||||
void BandwidthUpgradeManager::processEndpointDisconnection(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id,
|
||||
Ptr<CountDownLatch> process_disconnection_barrier) {}
|
||||
|
||||
template <typename Platform>
|
||||
bool BandwidthUpgradeManager<Platform>::setCurrentBandwidthUpgradeHandler(
|
||||
bool BandwidthUpgradeManager::setCurrentBandwidthUpgradeHandler(
|
||||
proto::connections::Medium medium) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "core/internal/endpoint_manager.h"
|
||||
#include "core/internal/medium_manager.h"
|
||||
#include "proto/connections/offline_wire_formats.pb.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
@@ -19,14 +20,15 @@ namespace connections {
|
||||
|
||||
// Manages all known {@link BandwidthUpgradeHandler} implementations, delegating
|
||||
// operations to the appropriate one as per the parameters passed in.
|
||||
template <typename Platform>
|
||||
class BandwidthUpgradeManager
|
||||
: public EndpointManager<Platform>::IncomingOfflineFrameProcessor {
|
||||
: public EndpointManager<
|
||||
platform::ImplementationPlatform>::IncomingOfflineFrameProcessor {
|
||||
public:
|
||||
BandwidthUpgradeManager(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager);
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
|
||||
BandwidthUpgradeManager(Ptr<MediumManager<Platform>> medium_manager,
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<EndpointManager<Platform>> endpoint_manager);
|
||||
~BandwidthUpgradeManager() override;
|
||||
|
||||
// This is the point on the initiator side where the
|
||||
@@ -50,17 +52,14 @@ class BandwidthUpgradeManager
|
||||
bool setCurrentBandwidthUpgradeHandler(proto::connections::Medium medium);
|
||||
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager_;
|
||||
typedef std::map<proto::connections::Medium,
|
||||
Ptr<BandwidthUpgradeHandler<Platform> > >
|
||||
typedef std::map<proto::connections::Medium, Ptr<BandwidthUpgradeHandler>>
|
||||
BandwidthUpgradeHandlersMap;
|
||||
BandwidthUpgradeHandlersMap bandwidth_upgrade_handlers_;
|
||||
Ptr<BandwidthUpgradeHandler<Platform> > current_bandwidth_upgrade_handler_;
|
||||
Ptr<BandwidthUpgradeHandler> current_bandwidth_upgrade_handler_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#include "core/internal/bandwidth_upgrade_manager.cc"
|
||||
|
||||
#endif // CORE_INTERNAL_BANDWIDTH_UPGRADE_MANAGER_H_
|
||||
|
||||
@@ -4,138 +4,115 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
namespace {
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
}
|
||||
|
||||
namespace base_bandwidth_upgrade_handler {
|
||||
|
||||
template <typename Platform>
|
||||
class RevertRunnable : public Runnable {
|
||||
public:
|
||||
void run() {}
|
||||
void run() override {}
|
||||
};
|
||||
|
||||
template <typename Platform>
|
||||
class InitiateBandwidthUpgradeForEndpointRunnable : public Runnable {
|
||||
public:
|
||||
void run() {}
|
||||
void run() override {}
|
||||
};
|
||||
|
||||
template <typename Platform>
|
||||
class ProcessEndpointDisconnectionRunnable : public Runnable {
|
||||
public:
|
||||
void run() {}
|
||||
void run() override {}
|
||||
};
|
||||
|
||||
template <typename Platform>
|
||||
class ProcessBandwidthUpgradeNegotiationFrameRunnable : public Runnable {
|
||||
public:
|
||||
void run() {}
|
||||
void run() override {}
|
||||
};
|
||||
|
||||
} // namespace base_bandwidth_upgrade_handler
|
||||
|
||||
template <typename Platform>
|
||||
BaseBandwidthUpgradeHandler<Platform>::BaseBandwidthUpgradeHandler(
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager)
|
||||
BaseBandwidthUpgradeHandler::BaseBandwidthUpgradeHandler(
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager)
|
||||
: endpoint_channel_manager_(endpoint_channel_manager),
|
||||
alarm_executor_(),
|
||||
serial_executor_(),
|
||||
alarm_executor_(nullptr),
|
||||
serial_executor_(nullptr),
|
||||
previous_endpoint_channels_(),
|
||||
in_progress_upgrades_(),
|
||||
safe_to_close_write_timestamps_() {}
|
||||
|
||||
template <typename Platform>
|
||||
BaseBandwidthUpgradeHandler<Platform>::~BaseBandwidthUpgradeHandler() {}
|
||||
BaseBandwidthUpgradeHandler::~BaseBandwidthUpgradeHandler() {}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::revert() {}
|
||||
void BaseBandwidthUpgradeHandler::revert() {}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::processEndpointDisconnection(
|
||||
void BaseBandwidthUpgradeHandler::processEndpointDisconnection(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id,
|
||||
Ptr<CountDownLatch> process_disconnection_barrier) {}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::initiateBandwidthUpgradeForEndpoint(
|
||||
void BaseBandwidthUpgradeHandler::initiateBandwidthUpgradeForEndpoint(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id) {}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::
|
||||
processBandwidthUpgradeNegotiationFrame(
|
||||
ConstPtr<BandwidthUpgradeNegotiationFrame>
|
||||
bandwidth_upgrade_negotiation,
|
||||
Ptr<ClientProxy<Platform> > to_client_proxy,
|
||||
const string& from_endpoint_id,
|
||||
proto::connections::Medium current_medium) {}
|
||||
void BaseBandwidthUpgradeHandler::processBandwidthUpgradeNegotiationFrame(
|
||||
ConstPtr<BandwidthUpgradeNegotiationFrame> bandwidth_upgrade_negotiation,
|
||||
Ptr<ClientProxy<Platform> > to_client_proxy, const string& from_endpoint_id,
|
||||
proto::connections::Medium current_medium) {}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannelManager<Platform> >
|
||||
BaseBandwidthUpgradeHandler<Platform>::getEndpointChannelManager() {
|
||||
Ptr<EndpointChannelManager>
|
||||
BaseBandwidthUpgradeHandler::getEndpointChannelManager() {
|
||||
return endpoint_channel_manager_;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::onIncomingConnection(
|
||||
void BaseBandwidthUpgradeHandler::onIncomingConnection(
|
||||
Ptr<IncomingSocketConnection> incoming_socket_connection) {}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::runOnBandwidthUpgradeHandlerThread(
|
||||
void BaseBandwidthUpgradeHandler::runOnBandwidthUpgradeHandlerThread(
|
||||
Ptr<Runnable> runnable) {}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::runUpgradeProtocol(
|
||||
void BaseBandwidthUpgradeHandler::runUpgradeProtocol(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id,
|
||||
Ptr<EndpointChannel> new_endpoint_channel) {}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::
|
||||
processBandwidthUpgradePathAvailableEvent(
|
||||
const string& endpoint_id, Ptr<ClientProxy<Platform> > client_proxy,
|
||||
ConstPtr<BandwidthUpgradeNegotiationFrame::UpgradePathInfo>
|
||||
upgrade_path_info,
|
||||
proto::connections::Medium current_medium) {}
|
||||
void BaseBandwidthUpgradeHandler::processBandwidthUpgradePathAvailableEvent(
|
||||
const string& endpoint_id, Ptr<ClientProxy<Platform> > client_proxy,
|
||||
ConstPtr<BandwidthUpgradeNegotiationFrame::UpgradePathInfo>
|
||||
upgrade_path_info,
|
||||
proto::connections::Medium current_medium) {}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel> BaseBandwidthUpgradeHandler<Platform>::
|
||||
processBandwidthUpgradePathAvailableEventInternal(
|
||||
const string& endpoint_id, Ptr<ClientProxy<Platform> > client_proxy,
|
||||
ConstPtr<BandwidthUpgradeNegotiationFrame::UpgradePathInfo>
|
||||
upgrade_path_info) {
|
||||
Ptr<EndpointChannel>
|
||||
BaseBandwidthUpgradeHandler::processBandwidthUpgradePathAvailableEventInternal(
|
||||
const string& endpoint_id, Ptr<ClientProxy<Platform> > client_proxy,
|
||||
ConstPtr<BandwidthUpgradeNegotiationFrame::UpgradePathInfo>
|
||||
upgrade_path_info) {
|
||||
return Ptr<EndpointChannel>();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::processLastWriteToPriorChannelEvent(
|
||||
void BaseBandwidthUpgradeHandler::processLastWriteToPriorChannelEvent(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id) {}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::processSafeToClosePriorChannelEvent(
|
||||
void BaseBandwidthUpgradeHandler::processSafeToClosePriorChannelEvent(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id) {}
|
||||
|
||||
template <typename Platform>
|
||||
std::int64_t BaseBandwidthUpgradeHandler<Platform>::calculateCloseDelay(
|
||||
std::int64_t BaseBandwidthUpgradeHandler::calculateCloseDelay(
|
||||
const string& endpoint_id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
std::int64_t
|
||||
BaseBandwidthUpgradeHandler<Platform>::getMillisSinceSafeCloseWritten(
|
||||
std::int64_t BaseBandwidthUpgradeHandler::getMillisSinceSafeCloseWritten(
|
||||
const string& endpoint_id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// TODO(ahlee): This will differ from the Java code as we don't have to handle
|
||||
// analytics in the C++ code.
|
||||
template <typename Platform>
|
||||
void BaseBandwidthUpgradeHandler<Platform>::
|
||||
void BaseBandwidthUpgradeHandler::
|
||||
attemptToRecordBandwidthUpgradeErrorForUnknownEndpoint(
|
||||
proto::connections::BandwidthUpgradeResult result,
|
||||
proto::connections::BandwidthUpgradeErrorStage error_stage) {}
|
||||
|
||||
// TODO(ahlee): This will differ from the Java code (previously threw an
|
||||
// UpgradeException).
|
||||
template <typename Platform>
|
||||
Ptr<BandwidthUpgradeNegotiationFrame::ClientIntroduction>
|
||||
BaseBandwidthUpgradeHandler<Platform>::readClientIntroductionFrame(
|
||||
BaseBandwidthUpgradeHandler::readClientIntroductionFrame(
|
||||
Ptr<EndpointChannel> endpoint_channel) {
|
||||
return Ptr<BandwidthUpgradeNegotiationFrame::ClientIntroduction>();
|
||||
}
|
||||
|
||||
@@ -19,13 +19,9 @@ namespace connections {
|
||||
|
||||
namespace base_bandwidth_upgrade_handler {
|
||||
|
||||
template <typename>
|
||||
class RevertRunnable;
|
||||
template <typename>
|
||||
class InitiateBandwidthUpgradeForEndpointRunnable;
|
||||
template <typename>
|
||||
class ProcessEndpointDisconnectionRunnable;
|
||||
template <typename>
|
||||
class ProcessBandwidthUpgradeNegotiationFrameRunnable;
|
||||
|
||||
} // namespace base_bandwidth_upgrade_handler
|
||||
@@ -55,26 +51,28 @@ class ProcessBandwidthUpgradeNegotiationFrameRunnable;
|
||||
// BANDWIDTH_UPGRADE_NEGOTIATION.SAFE_TO_CLOSE_PRIOR_CHANNEL from the
|
||||
// other, and upon doing so, close the prior EndpointChannel.
|
||||
// </ul>
|
||||
template <typename Platform>
|
||||
class BaseBandwidthUpgradeHandler : public BandwidthUpgradeHandler<Platform> {
|
||||
class BaseBandwidthUpgradeHandler : public BandwidthUpgradeHandler {
|
||||
public:
|
||||
BaseBandwidthUpgradeHandler(
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager);
|
||||
~BaseBandwidthUpgradeHandler();
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
|
||||
void revert();
|
||||
explicit BaseBandwidthUpgradeHandler(
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager);
|
||||
~BaseBandwidthUpgradeHandler() override;
|
||||
|
||||
void revert() override;
|
||||
void processEndpointDisconnection(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id,
|
||||
Ptr<CountDownLatch> process_disconnection_barrier);
|
||||
Ptr<CountDownLatch> process_disconnection_barrier) override;
|
||||
// Initiates the bandwidth upgrade and sends an UPGRADE_PATH_AVAILABLE
|
||||
// OfflineFrame.
|
||||
void initiateBandwidthUpgradeForEndpoint(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id);
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
const string& endpoint_id) override;
|
||||
void processBandwidthUpgradeNegotiationFrame(
|
||||
ConstPtr<BandwidthUpgradeNegotiationFrame> bandwidth_upgrade_negotiation,
|
||||
Ptr<ClientProxy<Platform> > to_client_proxy,
|
||||
const string& from_endpoint_id,
|
||||
proto::connections::Medium current_medium);
|
||||
proto::connections::Medium current_medium) override;
|
||||
|
||||
protected:
|
||||
// Represents the incoming Socket the Initiator has gotten after initializing
|
||||
@@ -117,7 +115,7 @@ class BaseBandwidthUpgradeHandler : public BandwidthUpgradeHandler<Platform> {
|
||||
// @BandwidthUpgradeHandlerThread
|
||||
virtual proto::connections::Medium getUpgradeMedium() = 0;
|
||||
|
||||
Ptr<EndpointChannelManager<Platform> > getEndpointChannelManager();
|
||||
Ptr<EndpointChannelManager> getEndpointChannelManager();
|
||||
// Common functionality to take an incoming connection and go through the
|
||||
// upgrade process.
|
||||
// @BandwidthUpgradeHandlerThread
|
||||
@@ -126,15 +124,11 @@ class BaseBandwidthUpgradeHandler : public BandwidthUpgradeHandler<Platform> {
|
||||
void runOnBandwidthUpgradeHandlerThread(Ptr<Runnable> runnable);
|
||||
|
||||
private:
|
||||
template <typename>
|
||||
friend class base_bandwidth_upgrade_handler::RevertRunnable;
|
||||
template <typename>
|
||||
friend class base_bandwidth_upgrade_handler::
|
||||
InitiateBandwidthUpgradeForEndpointRunnable;
|
||||
template <typename>
|
||||
friend class base_bandwidth_upgrade_handler::
|
||||
ProcessEndpointDisconnectionRunnable;
|
||||
template <typename>
|
||||
friend class base_bandwidth_upgrade_handler::
|
||||
ProcessBandwidthUpgradeNegotiationFrameRunnable;
|
||||
|
||||
@@ -162,7 +156,7 @@ class BaseBandwidthUpgradeHandler : public BandwidthUpgradeHandler<Platform> {
|
||||
Ptr<BandwidthUpgradeNegotiationFrame::ClientIntroduction>
|
||||
readClientIntroductionFrame(Ptr<EndpointChannel> endpoint_channel);
|
||||
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager_;
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager_;
|
||||
ScopedPtr<Ptr<typename Platform::ScheduledExecutorType> > alarm_executor_;
|
||||
ScopedPtr<Ptr<typename Platform::SingleThreadExecutorType> > serial_executor_;
|
||||
// Stores each upgraded endpoint's previous EndpointChannel (that was
|
||||
@@ -184,6 +178,4 @@ class BaseBandwidthUpgradeHandler : public BandwidthUpgradeHandler<Platform> {
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#include "core/internal/base_bandwidth_upgrade_handler.cc"
|
||||
|
||||
#endif // CORE_INTERNAL_BASE_BANDWIDTH_UPGRADE_HANDLER_H_
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/synchronized.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
|
||||
@@ -11,6 +12,8 @@ namespace connections {
|
||||
|
||||
namespace {
|
||||
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
|
||||
std::int32_t bytesToInt(ConstPtr<ByteArray> bytes) {
|
||||
const char* int_bytes = bytes->getData();
|
||||
|
||||
@@ -33,36 +36,36 @@ ConstPtr<ByteArray> intToBytes(std::int32_t value) {
|
||||
return MakeConstPtr(new ByteArray(int_bytes, sizeof(int_bytes)));
|
||||
}
|
||||
|
||||
ExceptionOr<ConstPtr<ByteArray> > readExactly(Ptr<InputStream> reader,
|
||||
std::int64_t size) {
|
||||
ExceptionOr<ConstPtr<ByteArray>> readExactly(Ptr<InputStream> reader,
|
||||
std::int64_t size) {
|
||||
string buffer;
|
||||
std::int64_t remaining_size = size;
|
||||
|
||||
while (remaining_size > 0) {
|
||||
ExceptionOr<ConstPtr<ByteArray> > read_bytes = reader->read(remaining_size);
|
||||
ExceptionOr<ConstPtr<ByteArray>> read_bytes = reader->read(remaining_size);
|
||||
if (!read_bytes.ok()) {
|
||||
if (Exception::IO == read_bytes.exception()) {
|
||||
return ExceptionOr<ConstPtr<ByteArray> >(read_bytes.exception());
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(read_bytes.exception());
|
||||
}
|
||||
}
|
||||
// Avoid leaks.
|
||||
ScopedPtr<ConstPtr<ByteArray> > scoped_read_bytes(read_bytes.result());
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_read_bytes(read_bytes.result());
|
||||
|
||||
// In Java, EOFException is a sub-variant of IOException.
|
||||
if (scoped_read_bytes.isNull() || scoped_read_bytes->size() == 0) {
|
||||
return ExceptionOr<ConstPtr<ByteArray> >(Exception::IO);
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(Exception::IO);
|
||||
}
|
||||
|
||||
buffer.append(scoped_read_bytes->getData(), scoped_read_bytes->size());
|
||||
remaining_size -= scoped_read_bytes->size();
|
||||
}
|
||||
|
||||
return ExceptionOr<ConstPtr<ByteArray> >(
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(
|
||||
MakeConstPtr(new ByteArray(buffer.data(), buffer.size())));
|
||||
}
|
||||
|
||||
ExceptionOr<std::int32_t> readInt(Ptr<InputStream> reader) {
|
||||
ExceptionOr<ConstPtr<ByteArray> > read_bytes =
|
||||
ExceptionOr<ConstPtr<ByteArray>> read_bytes =
|
||||
readExactly(reader, sizeof(std::int32_t));
|
||||
if (!read_bytes.ok()) {
|
||||
if (Exception::IO == read_bytes.exception()) {
|
||||
@@ -70,7 +73,7 @@ ExceptionOr<std::int32_t> readInt(Ptr<InputStream> reader) {
|
||||
}
|
||||
}
|
||||
// Avoid leaks.
|
||||
ScopedPtr<ConstPtr<ByteArray> > scoped_read_bytes(read_bytes.result());
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_read_bytes(read_bytes.result());
|
||||
|
||||
return ExceptionOr<std::int32_t>(bytesToInt(scoped_read_bytes.get()));
|
||||
}
|
||||
@@ -82,10 +85,9 @@ Exception::Value writeInt(Ptr<OutputStream> writer, std::int32_t value) {
|
||||
} // namespace
|
||||
|
||||
// TODO(b/150763574): Move implementatiopn to header or .inc file.
|
||||
template <typename Platform>
|
||||
BaseEndpointChannel<Platform>::BaseEndpointChannel(const string& channel_name,
|
||||
Ptr<InputStream> reader,
|
||||
Ptr<OutputStream> writer)
|
||||
BaseEndpointChannel::BaseEndpointChannel(absl::string_view channel_name,
|
||||
Ptr<InputStream> reader,
|
||||
Ptr<OutputStream> writer)
|
||||
: last_read_timestamp_(-1),
|
||||
channel_name_(channel_name),
|
||||
system_clock_(Platform::createSystemClock()),
|
||||
@@ -100,8 +102,7 @@ BaseEndpointChannel<Platform>::BaseEndpointChannel(const string& channel_name,
|
||||
Platform::createConditionVariable(is_paused_lock_.get())),
|
||||
is_paused_(Platform::createAtomicBoolean(false)) {}
|
||||
|
||||
template <typename Platform>
|
||||
BaseEndpointChannel<Platform>::~BaseEndpointChannel() {
|
||||
BaseEndpointChannel::~BaseEndpointChannel() {
|
||||
// WARNING: Make sure to never access reader_ and writer_ from here.
|
||||
//
|
||||
// They're owned by the specialized *Socket classes that are in turn
|
||||
@@ -114,14 +115,13 @@ BaseEndpointChannel<Platform>::~BaseEndpointChannel() {
|
||||
// of this class).
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
ExceptionOr<ConstPtr<ByteArray> > BaseEndpointChannel<Platform>::read() {
|
||||
ExceptionOr<ConstPtr<ByteArray>> BaseEndpointChannel::read() {
|
||||
Synchronized s(reader_lock_.get());
|
||||
|
||||
ExceptionOr<std::int32_t> read_int = readInt(reader_);
|
||||
if (!read_int.ok()) {
|
||||
if (Exception::IO == read_int.exception()) {
|
||||
return ExceptionOr<ConstPtr<ByteArray> >(read_int.exception());
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(read_int.exception());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,11 +131,11 @@ ExceptionOr<ConstPtr<ByteArray> > BaseEndpointChannel<Platform>::read() {
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(Exception::IO);
|
||||
}
|
||||
|
||||
ExceptionOr<ConstPtr<ByteArray> > read_bytes =
|
||||
ExceptionOr<ConstPtr<ByteArray>> read_bytes =
|
||||
readExactly(reader_, read_int.result());
|
||||
if (!read_bytes.ok()) {
|
||||
if (Exception::IO == read_bytes.exception()) {
|
||||
return ExceptionOr<ConstPtr<ByteArray> >(read_bytes.exception());
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(read_bytes.exception());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ ExceptionOr<ConstPtr<ByteArray> > BaseEndpointChannel<Platform>::read() {
|
||||
// short-circuit out of here on error.
|
||||
read_bytes_result.destroy();
|
||||
if (decoded_bytes == nullptr) {
|
||||
return ExceptionOr<ConstPtr<ByteArray> >(
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(
|
||||
Exception::INVALID_PROTOCOL_BUFFER);
|
||||
}
|
||||
read_bytes_result = MakeConstPtr(
|
||||
@@ -162,16 +162,14 @@ ExceptionOr<ConstPtr<ByteArray> > BaseEndpointChannel<Platform>::read() {
|
||||
}
|
||||
|
||||
last_read_timestamp_ = system_clock_->elapsedRealtime();
|
||||
return ExceptionOr<ConstPtr<ByteArray> >(read_bytes_result);
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(read_bytes_result);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Exception::Value BaseEndpointChannel<Platform>::write(
|
||||
ConstPtr<ByteArray> data) {
|
||||
Exception::Value BaseEndpointChannel::write(ConstPtr<ByteArray> data) {
|
||||
Synchronized s(writer_lock_.get());
|
||||
|
||||
// Avoid leaks.
|
||||
ScopedPtr<ConstPtr<ByteArray> > scoped_data(data);
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_data(data);
|
||||
|
||||
if (isPaused()) {
|
||||
blockUntilUnpaused();
|
||||
@@ -191,7 +189,7 @@ Exception::Value BaseEndpointChannel<Platform>::write(
|
||||
data_to_write = scoped_data.release();
|
||||
}
|
||||
// Avoid leaks.
|
||||
ScopedPtr<ConstPtr<ByteArray> > scoped_data_to_write(data_to_write);
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_data_to_write(data_to_write);
|
||||
|
||||
Exception::Value write_exception = writeInt(
|
||||
writer_, static_cast<std::int32_t>(scoped_data_to_write->size()));
|
||||
@@ -218,8 +216,7 @@ Exception::Value BaseEndpointChannel<Platform>::write(
|
||||
return Exception::NONE;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseEndpointChannel<Platform>::close() {
|
||||
void BaseEndpointChannel::close() {
|
||||
// WARNING WARNING WARNING
|
||||
//
|
||||
// This block deviates from the corresponding Java code.
|
||||
@@ -246,8 +243,7 @@ void BaseEndpointChannel<Platform>::close() {
|
||||
// TODO(tracyzhou): Add logging.
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseEndpointChannel<Platform>::close(
|
||||
void BaseEndpointChannel::close(
|
||||
proto::connections::DisconnectionReason reason) {
|
||||
// WARNING WARNING WARNING
|
||||
//
|
||||
@@ -259,8 +255,7 @@ void BaseEndpointChannel<Platform>::close(
|
||||
// TODO(tracyzhou): Add logging.
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
string BaseEndpointChannel<Platform>::getType() {
|
||||
string BaseEndpointChannel::getType() {
|
||||
string subtype = isEncryptionEnabled() ? "ENCRYPTED_" : "";
|
||||
switch (getMedium()) {
|
||||
case proto::connections::Medium::BLUETOOTH:
|
||||
@@ -278,46 +273,32 @@ string BaseEndpointChannel<Platform>::getType() {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
string BaseEndpointChannel<Platform>::getName() {
|
||||
return channel_name_;
|
||||
}
|
||||
string BaseEndpointChannel::getName() { return channel_name_; }
|
||||
|
||||
template <typename Platform>
|
||||
void BaseEndpointChannel<Platform>::enableEncryption(
|
||||
void BaseEndpointChannel::enableEncryption(
|
||||
Ptr<securegcm::D2DConnectionContextV1> encryption_context) {
|
||||
assert(!encryption_context.isNull());
|
||||
encryption_context_->set(encryption_context);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool BaseEndpointChannel<Platform>::isPaused() {
|
||||
return is_paused_->get();
|
||||
}
|
||||
bool BaseEndpointChannel::isPaused() { return is_paused_->get(); }
|
||||
|
||||
template <typename Platform>
|
||||
void BaseEndpointChannel<Platform>::pause() {
|
||||
is_paused_->set(true);
|
||||
}
|
||||
void BaseEndpointChannel::pause() { is_paused_->set(true); }
|
||||
|
||||
template <typename Platform>
|
||||
void BaseEndpointChannel<Platform>::resume() {
|
||||
void BaseEndpointChannel::resume() {
|
||||
is_paused_->set(false);
|
||||
unblockPausedWriter();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
std::int64_t BaseEndpointChannel<Platform>::getLastReadTimestamp() {
|
||||
std::int64_t BaseEndpointChannel::getLastReadTimestamp() {
|
||||
return last_read_timestamp_;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool BaseEndpointChannel<Platform>::isEncryptionEnabled() {
|
||||
bool BaseEndpointChannel::isEncryptionEnabled() {
|
||||
return !encryption_context_->get().isNull();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseEndpointChannel<Platform>::unblockPausedWriter() {
|
||||
void BaseEndpointChannel::unblockPausedWriter() {
|
||||
Synchronized s(is_paused_lock_.get());
|
||||
|
||||
// Notify to tell the thread calling wait() to check again.
|
||||
@@ -329,8 +310,7 @@ void BaseEndpointChannel<Platform>::unblockPausedWriter() {
|
||||
is_paused_condition_variable_->notify();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BaseEndpointChannel<Platform>::blockUntilUnpaused() {
|
||||
void BaseEndpointChannel::blockUntilUnpaused() {
|
||||
Synchronized s(is_paused_lock_.get());
|
||||
|
||||
// For more on how this works, see
|
||||
|
||||
@@ -16,15 +16,15 @@
|
||||
#include "platform/ptr.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
#include "securegcm/d2d_connection_context_v1.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
template <typename Platform>
|
||||
class BaseEndpointChannel : public EndpointChannel {
|
||||
public:
|
||||
BaseEndpointChannel(const string& channel_name, Ptr<InputStream> reader,
|
||||
BaseEndpointChannel(absl::string_view channel_name, Ptr<InputStream> reader,
|
||||
Ptr<OutputStream> writer);
|
||||
~BaseEndpointChannel() override;
|
||||
|
||||
@@ -69,7 +69,7 @@ class BaseEndpointChannel : public EndpointChannel {
|
||||
|
||||
private:
|
||||
// Used to sanity check that our frame sizes are reasonable.
|
||||
static const std::int32_t kMaxAllowedReadBytes = 1048576; // 1MB
|
||||
static constexpr std::int32_t kMaxAllowedReadBytes = 1048576; // 1MB
|
||||
|
||||
bool isEncryptionEnabled();
|
||||
void unblockPausedWriter();
|
||||
@@ -107,6 +107,4 @@ class BaseEndpointChannel : public EndpointChannel {
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#include "core/internal/base_endpoint_channel.cc"
|
||||
|
||||
#endif // CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "core/internal/base_endpoint_channel.h"
|
||||
|
||||
#include "platform/impl/default/default_platform.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/pipe.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
#include "gmock/gmock.h"
|
||||
@@ -11,21 +11,7 @@ namespace nearby {
|
||||
namespace connections {
|
||||
namespace {
|
||||
|
||||
class TestPlatform : public DefaultPlatform {
|
||||
public:
|
||||
static SystemClock* createSystemClock() { return nullptr; }
|
||||
|
||||
static Ptr<AtomicBoolean> createAtomicBoolean(bool initial_value) {
|
||||
return Ptr<AtomicBoolean>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static Ptr<AtomicReference<T>> createAtomicReference(const T& initial_value) {
|
||||
return Ptr<AtomicReference<T>>();
|
||||
}
|
||||
};
|
||||
|
||||
class TestEndpointChannel : public BaseEndpointChannel<TestPlatform> {
|
||||
class TestEndpointChannel : public BaseEndpointChannel {
|
||||
public:
|
||||
explicit TestEndpointChannel(Ptr<InputStream> input_stream)
|
||||
: BaseEndpointChannel("channel", input_stream, Ptr<OutputStream>()) {}
|
||||
@@ -34,7 +20,7 @@ class TestEndpointChannel : public BaseEndpointChannel<TestPlatform> {
|
||||
MOCK_METHOD(void, closeImpl, (), (override));
|
||||
};
|
||||
|
||||
using SamplePipe = Pipe<TestPlatform>;
|
||||
using SamplePipe = Pipe;
|
||||
|
||||
TEST(BaseEndpointChannelTest, ReadAfterInputStreamClosed) {
|
||||
auto pipe = MakeRefCountedPtr(new SamplePipe());
|
||||
|
||||
@@ -48,12 +48,7 @@ class StartAdvertisingCallable : public Callable<Status::Value> {
|
||||
service_id_(service_id),
|
||||
local_endpoint_name_(local_endpoint_name),
|
||||
options_(options),
|
||||
// Convert the passed in connection_lifecycle_listener Ptr into a
|
||||
// reference counted one. The advertising session and any connected
|
||||
// endpoints need a handle to the same connection_lifecycle_listener, so
|
||||
// there is no clear model of who actually owns the listener.
|
||||
connection_lifecycle_listener_(
|
||||
MakeRefCountedPtr(&(*connection_lifecycle_listener))) {}
|
||||
connection_lifecycle_listener_(connection_lifecycle_listener) {}
|
||||
|
||||
ExceptionOr<Status::Value> call() override {
|
||||
// Ask the implementation to attempt to start advertising.
|
||||
@@ -675,8 +670,8 @@ const std::int64_t
|
||||
template <typename Platform>
|
||||
BasePCPHandler<Platform>::BasePCPHandler(
|
||||
Ptr<EndpointManager<Platform>> endpoint_manager,
|
||||
Ptr<EndpointChannelManager<Platform>> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform>> bandwidth_upgrade_manager)
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager)
|
||||
: endpoint_manager_(endpoint_manager),
|
||||
endpoint_channel_manager_(endpoint_channel_manager),
|
||||
bandwidth_upgrade_manager_(bandwidth_upgrade_manager),
|
||||
@@ -1137,6 +1132,14 @@ Exception::Value BasePCPHandler<Platform>::onIncomingConnection(
|
||||
return Exception::IO;
|
||||
}
|
||||
|
||||
// The ConnectionRequest frame has two fields that both contain the
|
||||
// EndpointInfo. The legacy field stores it as a string while the newer field
|
||||
// stores it as a byte array. We'll attempt to grab from the newer field, but
|
||||
// will accept the older string if it's all that exists.
|
||||
const std::string& endpoint_name = connection_request.has_endpoint_info()
|
||||
? connection_request.endpoint_info()
|
||||
: connection_request.endpoint_name();
|
||||
|
||||
// We've successfully connected to the device, and are now about to jump on to
|
||||
// the EncryptionRunner thread to start running our encryption protocol. We'll
|
||||
// mark ourselves as pending in case we get another call to requestConnection
|
||||
@@ -1146,7 +1149,7 @@ Exception::Value BasePCPHandler<Platform>::onIncomingConnection(
|
||||
.insert(std::make_pair(
|
||||
connection_request.endpoint_id(),
|
||||
PendingConnectionInfo::newIncomingPendingConnectionInfo(
|
||||
client_proxy, connection_request.endpoint_name(),
|
||||
client_proxy, endpoint_name,
|
||||
scoped_endpoint_channel.release(), connection_request.nonce(),
|
||||
start_time_millis, advertising_connection_lifecycle_listener_,
|
||||
OfflineFrames::connectionRequestMediumsToMediums(
|
||||
@@ -1378,7 +1381,7 @@ void BasePCPHandler<Platform>::evaluateConnectionResult(
|
||||
} else {
|
||||
pending_rejected_connection_close_alarms_.insert(std::make_pair(
|
||||
endpoint_id,
|
||||
MakePtr(new CancelableAlarm<Platform>(
|
||||
MakePtr(new CancelableAlarm(
|
||||
"BasePCPHandler.evaluateConnectionResult() delayed close",
|
||||
MakePtr(
|
||||
new base_pcp_handler::
|
||||
@@ -1407,7 +1410,7 @@ BasePCPHandler<Platform>::readConnectionRequestFrame(
|
||||
|
||||
// To avoid a device connecting but never sending their introductory frame, we
|
||||
// time out the connection after a certain amount of time.
|
||||
CancelableAlarm<Platform> timeout_alarm(
|
||||
CancelableAlarm timeout_alarm(
|
||||
"PCPHandler(" + this->getStrategy().getName() +
|
||||
").readConnectionRequestFrame",
|
||||
MakePtr(
|
||||
|
||||
@@ -69,10 +69,9 @@ class BasePCPHandler
|
||||
public EndpointManager<Platform>::IncomingOfflineFrameProcessor {
|
||||
public:
|
||||
// TODO(tracyzhou): Add SecureRandom.
|
||||
BasePCPHandler(
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager);
|
||||
BasePCPHandler(Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager);
|
||||
~BasePCPHandler() override;
|
||||
|
||||
// We have been asked by the client to start advertising. Once we successfully
|
||||
@@ -239,8 +238,8 @@ class BasePCPHandler
|
||||
virtual proto::connections::Medium getDefaultUpgradeMedium() = 0;
|
||||
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager_;
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager_;
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager_;
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager_;
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager_;
|
||||
|
||||
private:
|
||||
template <typename>
|
||||
@@ -473,7 +472,7 @@ class BasePCPHandler
|
||||
// reading the message (in which case, this alarm should be cancelled as it's
|
||||
// no longer needed), but this alarm is the fallback in case that doesn't
|
||||
// happen.
|
||||
typedef std::map<string, Ptr<CancelableAlarm<Platform> > >
|
||||
typedef std::map<string, Ptr<CancelableAlarm> >
|
||||
PendingRejectedConnectionCloseAlarmsMap;
|
||||
PendingRejectedConnectionCloseAlarmsMap
|
||||
pending_rejected_connection_close_alarms_;
|
||||
|
||||
@@ -6,42 +6,31 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<BLEEndpointChannel<Platform> >
|
||||
BLEEndpointChannel<Platform>::createOutgoing(
|
||||
Ptr<BLEEndpointChannel> BLEEndpointChannel::createOutgoing(
|
||||
Ptr<MediumManager<Platform> > medium_manager, const string& channel_name,
|
||||
Ptr<BLESocket> ble_socket) {
|
||||
return MakePtr(
|
||||
new BLEEndpointChannel<Platform>(channel_name, ble_socket));
|
||||
return MakePtr(new BLEEndpointChannel(channel_name, ble_socket));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<BLEEndpointChannel<Platform> >
|
||||
BLEEndpointChannel<Platform>::createIncoming(
|
||||
Ptr<BLEEndpointChannel> BLEEndpointChannel::createIncoming(
|
||||
Ptr<MediumManager<Platform> > medium_manager, const string& channel_name,
|
||||
Ptr<BLESocket> ble_socket) {
|
||||
return MakePtr(
|
||||
new BLEEndpointChannel<Platform>(channel_name, ble_socket));
|
||||
return MakePtr(new BLEEndpointChannel(channel_name, ble_socket));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
BLEEndpointChannel<Platform>::BLEEndpointChannel(
|
||||
const string& channel_name, Ptr<BLESocket> ble_socket)
|
||||
: BaseEndpointChannel<Platform>(channel_name,
|
||||
ble_socket->getInputStream(),
|
||||
ble_socket->getOutputStream()),
|
||||
BLEEndpointChannel::BLEEndpointChannel(const string& channel_name,
|
||||
Ptr<BLESocket> ble_socket)
|
||||
: BaseEndpointChannel(channel_name, ble_socket->getInputStream(),
|
||||
ble_socket->getOutputStream()),
|
||||
ble_socket_(ble_socket) {}
|
||||
|
||||
template <typename Platform>
|
||||
BLEEndpointChannel<Platform>::~BLEEndpointChannel() {}
|
||||
BLEEndpointChannel::~BLEEndpointChannel() {}
|
||||
|
||||
template <typename Platform>
|
||||
proto::connections::Medium BLEEndpointChannel<Platform>::getMedium() {
|
||||
proto::connections::Medium BLEEndpointChannel::getMedium() {
|
||||
return proto::connections::Medium::BLE;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BLEEndpointChannel<Platform>::closeImpl() {
|
||||
void BLEEndpointChannel::closeImpl() {
|
||||
Exception::Value exception = ble_socket_->close();
|
||||
if (exception != Exception::NONE) {
|
||||
if (exception == Exception::IO) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "core/internal/base_endpoint_channel.h"
|
||||
#include "core/internal/medium_manager.h"
|
||||
#include "platform/api/ble.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
@@ -12,13 +13,14 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
template <typename Platform>
|
||||
class BLEEndpointChannel : public BaseEndpointChannel<Platform> {
|
||||
class BLEEndpointChannel : public BaseEndpointChannel {
|
||||
public:
|
||||
static Ptr<BLEEndpointChannel<Platform> > createOutgoing(
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
|
||||
static Ptr<BLEEndpointChannel> createOutgoing(
|
||||
Ptr<MediumManager<Platform> > medium_manager, const string& channel_name,
|
||||
Ptr<BLESocket> ble_socket);
|
||||
static Ptr<BLEEndpointChannel<Platform> > createIncoming(
|
||||
static Ptr<BLEEndpointChannel> createIncoming(
|
||||
Ptr<MediumManager<Platform> > medium_manager, const string& channel_name,
|
||||
Ptr<BLESocket> ble_socket);
|
||||
|
||||
@@ -39,6 +41,4 @@ class BLEEndpointChannel : public BaseEndpointChannel<Platform> {
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#include "core/internal/ble_endpoint_channel.cc"
|
||||
|
||||
#endif // CORE_INTERNAL_BLE_ENDPOINT_CHANNEL_H_
|
||||
|
||||
@@ -6,42 +6,31 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<BluetoothEndpointChannel<Platform> >
|
||||
BluetoothEndpointChannel<Platform>::createOutgoing(
|
||||
Ptr<BluetoothEndpointChannel> BluetoothEndpointChannel::createOutgoing(
|
||||
Ptr<MediumManager<Platform> > medium_manager, const string& channel_name,
|
||||
Ptr<BluetoothSocket> bluetooth_socket) {
|
||||
return MakePtr(
|
||||
new BluetoothEndpointChannel<Platform>(channel_name, bluetooth_socket));
|
||||
return MakePtr(new BluetoothEndpointChannel(channel_name, bluetooth_socket));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<BluetoothEndpointChannel<Platform> >
|
||||
BluetoothEndpointChannel<Platform>::createIncoming(
|
||||
Ptr<BluetoothEndpointChannel> BluetoothEndpointChannel::createIncoming(
|
||||
Ptr<MediumManager<Platform> > medium_manager, const string& channel_name,
|
||||
Ptr<BluetoothSocket> bluetooth_socket) {
|
||||
return MakePtr(
|
||||
new BluetoothEndpointChannel<Platform>(channel_name, bluetooth_socket));
|
||||
return MakePtr(new BluetoothEndpointChannel(channel_name, bluetooth_socket));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
BluetoothEndpointChannel<Platform>::BluetoothEndpointChannel(
|
||||
BluetoothEndpointChannel::BluetoothEndpointChannel(
|
||||
const string& channel_name, Ptr<BluetoothSocket> bluetooth_socket)
|
||||
: BaseEndpointChannel<Platform>(channel_name,
|
||||
bluetooth_socket->getInputStream(),
|
||||
bluetooth_socket->getOutputStream()),
|
||||
: BaseEndpointChannel(channel_name, bluetooth_socket->getInputStream(),
|
||||
bluetooth_socket->getOutputStream()),
|
||||
bluetooth_socket_(bluetooth_socket) {}
|
||||
|
||||
template <typename Platform>
|
||||
BluetoothEndpointChannel<Platform>::~BluetoothEndpointChannel() {}
|
||||
BluetoothEndpointChannel::~BluetoothEndpointChannel() {}
|
||||
|
||||
template <typename Platform>
|
||||
proto::connections::Medium BluetoothEndpointChannel<Platform>::getMedium() {
|
||||
proto::connections::Medium BluetoothEndpointChannel::getMedium() {
|
||||
return proto::connections::Medium::BLUETOOTH;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void BluetoothEndpointChannel<Platform>::closeImpl() {
|
||||
void BluetoothEndpointChannel::closeImpl() {
|
||||
Exception::Value exception = bluetooth_socket_->close();
|
||||
if (exception != Exception::NONE) {
|
||||
if (exception == Exception::IO) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "core/internal/base_endpoint_channel.h"
|
||||
#include "core/internal/medium_manager.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
@@ -12,13 +13,14 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
template <typename Platform>
|
||||
class BluetoothEndpointChannel : public BaseEndpointChannel<Platform> {
|
||||
class BluetoothEndpointChannel : public BaseEndpointChannel {
|
||||
public:
|
||||
static Ptr<BluetoothEndpointChannel<Platform> > createOutgoing(
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
|
||||
static Ptr<BluetoothEndpointChannel> createOutgoing(
|
||||
Ptr<MediumManager<Platform> > medium_manager, const string& channel_name,
|
||||
Ptr<BluetoothSocket> bluetooth_socket);
|
||||
static Ptr<BluetoothEndpointChannel<Platform> > createIncoming(
|
||||
static Ptr<BluetoothEndpointChannel> createIncoming(
|
||||
Ptr<MediumManager<Platform> > medium_manager, const string& channel_name,
|
||||
Ptr<BluetoothSocket> bluetooth_socket);
|
||||
|
||||
@@ -40,6 +42,4 @@ class BluetoothEndpointChannel : public BaseEndpointChannel<Platform> {
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#include "core/internal/bluetooth_endpoint_channel.cc"
|
||||
|
||||
#endif // CORE_INTERNAL_BLUETOOTH_ENDPOINT_CHANNEL_H_
|
||||
|
||||
@@ -101,7 +101,7 @@ class ServerRunnable : public Runnable {
|
||||
encryption_result_listener_(encryption_result_listener) {}
|
||||
|
||||
void run() override {
|
||||
CancelableAlarm<Platform> timeout_alarm(
|
||||
CancelableAlarm timeout_alarm(
|
||||
"EncryptionRunner.startServer() timeout",
|
||||
MakePtr(new CancelableAlarmRunnable<Platform>(
|
||||
client_proxy_, endpoint_id_, endpoint_channel_)),
|
||||
@@ -112,7 +112,7 @@ class ServerRunnable : public Runnable {
|
||||
// Java code throws a HandshakeException.
|
||||
if (server == nullptr) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ class ServerRunnable : public Runnable {
|
||||
if (!client_init.ok()) {
|
||||
if (Exception::IO == client_init.exception()) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -138,7 +138,7 @@ class ServerRunnable : public Runnable {
|
||||
if (parse_result.alert_to_send != nullptr) {
|
||||
handleAlertException(parse_result);
|
||||
}
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ class ServerRunnable : public Runnable {
|
||||
// Java code throws a HandshakeException.
|
||||
if (server_init == nullptr) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ class ServerRunnable : public Runnable {
|
||||
if (Exception::NONE != write_exception) {
|
||||
if (Exception::IO == write_exception) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -174,7 +174,7 @@ class ServerRunnable : public Runnable {
|
||||
if (!client_finish.ok()) {
|
||||
if (Exception::IO == client_finish.exception()) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -189,7 +189,7 @@ class ServerRunnable : public Runnable {
|
||||
if (parse_result.alert_to_send != nullptr) {
|
||||
handleAlertException(parse_result);
|
||||
}
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ class ServerRunnable : public Runnable {
|
||||
MakePtr(server.release()),
|
||||
encryption_result_listener_.get())) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -213,8 +213,8 @@ class ServerRunnable : public Runnable {
|
||||
endpoint_id_.c_str());
|
||||
}
|
||||
|
||||
void handleHandshakeOrIOException(CancelableAlarm<Platform>& timeout_alarm) {
|
||||
timeout_alarm.cancel();
|
||||
void handleHandshakeOrIOException(CancelableAlarm* timeout_alarm) {
|
||||
timeout_alarm->cancel();
|
||||
encryption_result_listener_->onEncryptionFailure(endpoint_id_,
|
||||
endpoint_channel_);
|
||||
}
|
||||
@@ -258,7 +258,7 @@ class ClientRunnable : public Runnable {
|
||||
encryption_result_listener_(encryption_result_listener) {}
|
||||
|
||||
void run() override {
|
||||
CancelableAlarm<Platform> timeout_alarm(
|
||||
CancelableAlarm timeout_alarm(
|
||||
"EncryptionRunner.startClient() timeout",
|
||||
MakePtr(new CancelableAlarmRunnable<Platform>(
|
||||
client_proxy_, endpoint_id_, endpoint_channel_)),
|
||||
@@ -270,7 +270,7 @@ class ClientRunnable : public Runnable {
|
||||
// Java code throws a HandshakeException.
|
||||
if (client == nullptr) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -280,7 +280,7 @@ class ClientRunnable : public Runnable {
|
||||
// Java code throws a HandshakeException.
|
||||
if (client_init == nullptr) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -289,7 +289,7 @@ class ClientRunnable : public Runnable {
|
||||
if (Exception::NONE != write_init_exception) {
|
||||
if (Exception::IO == write_init_exception) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -303,7 +303,7 @@ class ClientRunnable : public Runnable {
|
||||
if (!server_init.ok()) {
|
||||
if (Exception::IO == server_init.exception()) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -319,7 +319,7 @@ class ClientRunnable : public Runnable {
|
||||
if (parse_result.alert_to_send != nullptr) {
|
||||
handleAlertException(parse_result);
|
||||
}
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -332,7 +332,7 @@ class ClientRunnable : public Runnable {
|
||||
// Java code throws a HandshakeException.
|
||||
if (client_finish == nullptr) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -342,7 +342,7 @@ class ClientRunnable : public Runnable {
|
||||
if (Exception::NONE != write_finish_exception) {
|
||||
if (Exception::IO == write_finish_exception) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -356,7 +356,7 @@ class ClientRunnable : public Runnable {
|
||||
MakePtr(client.release()),
|
||||
encryption_result_listener_.get())) {
|
||||
logException();
|
||||
handleHandshakeOrIOException(timeout_alarm);
|
||||
handleHandshakeOrIOException(&timeout_alarm);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -367,8 +367,8 @@ class ClientRunnable : public Runnable {
|
||||
endpoint_id_.c_str());
|
||||
}
|
||||
|
||||
void handleHandshakeOrIOException(CancelableAlarm<Platform>& timeout_alarm) {
|
||||
timeout_alarm.cancel();
|
||||
void handleHandshakeOrIOException(CancelableAlarm* timeout_alarm) {
|
||||
timeout_alarm->cancel();
|
||||
encryption_result_listener_->onEncryptionFailure(endpoint_id_,
|
||||
endpoint_channel_);
|
||||
}
|
||||
|
||||
@@ -2,21 +2,20 @@
|
||||
|
||||
#include "core/internal/ble_endpoint_channel.h"
|
||||
#include "core/internal/bluetooth_endpoint_channel.h"
|
||||
#include "core/internal/wifi_lan_endpoint_channel.h"
|
||||
#include "platform/synchronized.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
template <typename Platform>
|
||||
EndpointChannelManager<Platform>::EndpointChannelManager(
|
||||
EndpointChannelManager::EndpointChannelManager(
|
||||
Ptr<MediumManager<Platform> > medium_manager)
|
||||
: lock_(Platform::createLock()),
|
||||
medium_manager_(medium_manager),
|
||||
channel_state_(new ChannelState()) {}
|
||||
|
||||
template <typename Platform>
|
||||
EndpointChannelManager<Platform>::~EndpointChannelManager() {
|
||||
EndpointChannelManager::~EndpointChannelManager() {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
// TODO(tracyzhou): logger.atDebug().log("Initiating shutdown of
|
||||
@@ -26,40 +25,47 @@ EndpointChannelManager<Platform>::~EndpointChannelManager() {
|
||||
// down.");
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager<Platform>::createOutgoingBluetoothEndpointChannel(
|
||||
EndpointChannelManager::createOutgoingBluetoothEndpointChannel(
|
||||
const string& channel_name, Ptr<BluetoothSocket> bluetooth_socket) {
|
||||
return BluetoothEndpointChannel<Platform>::createOutgoing(
|
||||
medium_manager_, channel_name, bluetooth_socket);
|
||||
return BluetoothEndpointChannel::createOutgoing(medium_manager_, channel_name,
|
||||
bluetooth_socket);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager<Platform>::createIncomingBluetoothEndpointChannel(
|
||||
EndpointChannelManager::createIncomingBluetoothEndpointChannel(
|
||||
const string& channel_name, Ptr<BluetoothSocket> bluetooth_socket) {
|
||||
return BluetoothEndpointChannel<Platform>::createIncoming(
|
||||
medium_manager_, channel_name, bluetooth_socket);
|
||||
return BluetoothEndpointChannel::createIncoming(medium_manager_, channel_name,
|
||||
bluetooth_socket);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager<Platform>::createOutgoingBLEEndpointChannel(
|
||||
Ptr<EndpointChannel> EndpointChannelManager::createOutgoingBLEEndpointChannel(
|
||||
const string& channel_name, Ptr<BLESocket> ble_socket) {
|
||||
return BLEEndpointChannel<Platform>::createOutgoing(medium_manager_,
|
||||
channel_name, ble_socket);
|
||||
return BLEEndpointChannel::createOutgoing(medium_manager_, channel_name,
|
||||
ble_socket);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager<Platform>::createIncomingBLEEndpointChannel(
|
||||
Ptr<EndpointChannel> EndpointChannelManager::createIncomingBLEEndpointChannel(
|
||||
const string& channel_name, Ptr<BLESocket> ble_socket) {
|
||||
return BLEEndpointChannel<Platform>::createIncoming(medium_manager_,
|
||||
channel_name, ble_socket);
|
||||
return BLEEndpointChannel::createIncoming(medium_manager_, channel_name,
|
||||
ble_socket);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void EndpointChannelManager<Platform>::registerChannelForEndpoint(
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager::CreateOutgoingWifiLanEndpointChannel(
|
||||
const string& channel_name, Ptr<WifiLanSocket> wifi_lan_socket) {
|
||||
return WifiLanEndpointChannel::CreateOutgoing(
|
||||
medium_manager_, channel_name, wifi_lan_socket);
|
||||
}
|
||||
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager::CreateIncomingWifiLanEndpointChannel(
|
||||
const string& channel_name, Ptr<WifiLanSocket> wifi_lan_socket) {
|
||||
return WifiLanEndpointChannel::CreateIncoming(
|
||||
medium_manager_, channel_name, wifi_lan_socket);
|
||||
}
|
||||
|
||||
void EndpointChannelManager::registerChannelForEndpoint(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id,
|
||||
Ptr<EndpointChannel> endpoint_channel) {
|
||||
Synchronized s(lock_.get());
|
||||
@@ -74,9 +80,7 @@ void EndpointChannelManager<Platform>::registerChannelForEndpoint(
|
||||
}
|
||||
|
||||
#ifdef BANDWIDTH_UPGRADE_MANAGER_IMPLEMENTED
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager<Platform>::replaceChannelForEndpoint(
|
||||
Ptr<EndpointChannel> EndpointChannelManager::replaceChannelForEndpoint(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id,
|
||||
Ptr<EndpointChannel> endpoint_channel) {
|
||||
Synchronized s(lock_.get());
|
||||
@@ -96,8 +100,7 @@ EndpointChannelManager<Platform>::replaceChannelForEndpoint(
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename Platform>
|
||||
bool EndpointChannelManager<Platform>::encryptChannelForEndpoint(
|
||||
bool EndpointChannelManager::encryptChannelForEndpoint(
|
||||
const string& endpoint_id,
|
||||
Ptr<securegcm::D2DConnectionContextV1> encryption_context) {
|
||||
Synchronized s(lock_.get());
|
||||
@@ -125,16 +128,14 @@ bool EndpointChannelManager<Platform>::encryptChannelForEndpoint(
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel> EndpointChannelManager<Platform>::getChannelForEndpoint(
|
||||
Ptr<EndpointChannel> EndpointChannelManager::getChannelForEndpoint(
|
||||
const string& endpoint_id) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
return channel_state_->getChannelForEndpoint(endpoint_id);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void EndpointChannelManager<Platform>::setActiveEndpointChannel(
|
||||
void EndpointChannelManager::setActiveEndpointChannel(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& endpoint_id,
|
||||
Ptr<EndpointChannel> endpoint_channel) {
|
||||
#ifdef BANDWIDTH_UPGRADE_MANAGER_IMPLEMENTED
|
||||
@@ -155,8 +156,7 @@ void EndpointChannelManager<Platform>::setActiveEndpointChannel(
|
||||
channel_state_->updateChannelForEndpoint(endpoint_id, endpoint_channel));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void EndpointChannelManager<Platform>::encryptChannel(
|
||||
void EndpointChannelManager::encryptChannel(
|
||||
const string& endpoint_id, Ptr<EndpointChannel> endpoint_channel,
|
||||
Ptr<securegcm::D2DConnectionContextV1> encryption_context) {
|
||||
// TODO(tracyzhou): Add logging.
|
||||
@@ -165,8 +165,7 @@ void EndpointChannelManager<Platform>::encryptChannel(
|
||||
|
||||
///////////////////////////////// ChannelState /////////////////////////////////
|
||||
|
||||
template <typename Platform>
|
||||
EndpointChannelManager<Platform>::ChannelState::~ChannelState() {
|
||||
EndpointChannelManager::ChannelState::~ChannelState() {
|
||||
while (!endpoint_id_to_metadata_.empty()) {
|
||||
typename EndpointIdToMetadataMap::iterator it =
|
||||
endpoint_id_to_metadata_.begin();
|
||||
@@ -176,15 +175,13 @@ EndpointChannelManager<Platform>::ChannelState::~ChannelState() {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool EndpointChannelManager<Platform>::ChannelState::isEndpointEncrypted(
|
||||
bool EndpointChannelManager::ChannelState::isEndpointEncrypted(
|
||||
const string& endpoint_id) {
|
||||
return !getEncryptionContextForEndpoint(endpoint_id).isNull();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager<Platform>::ChannelState::updateChannelForEndpoint(
|
||||
EndpointChannelManager::ChannelState::updateChannelForEndpoint(
|
||||
const string& endpoint_id, Ptr<EndpointChannel> endpoint_channel) {
|
||||
Ptr<EndpointChannel> previous_endpoint_channel;
|
||||
Ptr<EndpointMetaData> endpoint_metadata;
|
||||
@@ -208,11 +205,10 @@ EndpointChannelManager<Platform>::ChannelState::updateChannelForEndpoint(
|
||||
return scoped_previous_endpoint_channel.release();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<securegcm::D2DConnectionContextV1> EndpointChannelManager<Platform>::
|
||||
ChannelState::updateEncryptionContextForEndpoint(
|
||||
const string& endpoint_id,
|
||||
Ptr<securegcm::D2DConnectionContextV1> encryption_context) {
|
||||
Ptr<securegcm::D2DConnectionContextV1>
|
||||
EndpointChannelManager::ChannelState::updateEncryptionContextForEndpoint(
|
||||
const string& endpoint_id,
|
||||
Ptr<securegcm::D2DConnectionContextV1> encryption_context) {
|
||||
Ptr<securegcm::D2DConnectionContextV1> previous_encryption_context;
|
||||
Ptr<EndpointMetaData> endpoint_metadata;
|
||||
|
||||
@@ -234,8 +230,7 @@ Ptr<securegcm::D2DConnectionContextV1> EndpointChannelManager<Platform>::
|
||||
return scoped_previous_encryption_context.release();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool EndpointChannelManager<Platform>::ChannelState::removeEndpoint(
|
||||
bool EndpointChannelManager::ChannelState::removeEndpoint(
|
||||
const string& endpoint_id, proto::connections::DisconnectionReason reason) {
|
||||
typename EndpointIdToMetadataMap::iterator it =
|
||||
endpoint_id_to_metadata_.find(endpoint_id);
|
||||
@@ -249,9 +244,8 @@ bool EndpointChannelManager<Platform>::ChannelState::removeEndpoint(
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<securegcm::D2DConnectionContextV1>
|
||||
EndpointChannelManager<Platform>::ChannelState::getEncryptionContextForEndpoint(
|
||||
EndpointChannelManager::ChannelState::getEncryptionContextForEndpoint(
|
||||
const string& endpoint_id) {
|
||||
typename EndpointIdToMetadataMap::iterator it =
|
||||
endpoint_id_to_metadata_.find(endpoint_id);
|
||||
@@ -262,9 +256,8 @@ EndpointChannelManager<Platform>::ChannelState::getEncryptionContextForEndpoint(
|
||||
return it->second->encryption_context;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<EndpointChannel>
|
||||
EndpointChannelManager<Platform>::ChannelState::getChannelForEndpoint(
|
||||
EndpointChannelManager::ChannelState::getChannelForEndpoint(
|
||||
const string& endpoint_id) {
|
||||
typename EndpointIdToMetadataMap::iterator it =
|
||||
endpoint_id_to_metadata_.find(endpoint_id);
|
||||
@@ -275,8 +268,7 @@ EndpointChannelManager<Platform>::ChannelState::getChannelForEndpoint(
|
||||
return it->second->endpoint_channel;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool EndpointChannelManager<Platform>::unregisterChannelForEndpoint(
|
||||
bool EndpointChannelManager::unregisterChannelForEndpoint(
|
||||
const string& endpoint_id) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include "platform/api/ble.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
#include "platform/api/lock.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "securegcm/d2d_connection_context_v1.h"
|
||||
@@ -22,10 +24,11 @@ namespace connections {
|
||||
//
|
||||
// The factory methods would be static, but for the fact that they need to use
|
||||
// the MediumManager.
|
||||
template <typename Platform>
|
||||
class EndpointChannelManager {
|
||||
public:
|
||||
explicit EndpointChannelManager(Ptr<MediumManager<Platform> > medium_manager);
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
|
||||
explicit EndpointChannelManager(Ptr<MediumManager<Platform>> medium_manager);
|
||||
~EndpointChannelManager();
|
||||
|
||||
Ptr<EndpointChannel> createOutgoingBluetoothEndpointChannel(
|
||||
@@ -38,6 +41,11 @@ class EndpointChannelManager {
|
||||
Ptr<EndpointChannel> createIncomingBLEEndpointChannel(
|
||||
const string& channel_name, Ptr<BLESocket> ble_socket);
|
||||
|
||||
Ptr<EndpointChannel> CreateOutgoingWifiLanEndpointChannel(
|
||||
const string& channel_name, Ptr<WifiLanSocket> wifi_lan_socket);
|
||||
Ptr<EndpointChannel> CreateIncomingWifiLanEndpointChannel(
|
||||
const string& channel_name, Ptr<WifiLanSocket> wifi_lan_socket);
|
||||
|
||||
// Registers the initial EndpointChannel to be associated with an endpoint;
|
||||
// if there already exists a previously-associated EndpointChannel, that will
|
||||
// be closed before continuing the registration.
|
||||
@@ -138,6 +146,4 @@ class EndpointChannelManager {
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#include "core/internal/endpoint_channel_manager.cc"
|
||||
|
||||
#endif // CORE_INTERNAL_ENDPOINT_CHANNEL_MANAGER_H_
|
||||
|
||||
@@ -466,7 +466,7 @@ const std::int32_t EndpointManager<Platform>::kMaxConcurrentEndpoints = 50;
|
||||
|
||||
template <typename Platform>
|
||||
EndpointManager<Platform>::EndpointManager(
|
||||
Ptr<EndpointChannelManager<Platform>> endpoint_channel_manager)
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager)
|
||||
: thread_utils_(Platform::createThreadUtils()),
|
||||
system_clock_(Platform::createSystemClock()),
|
||||
endpoint_channel_manager_(endpoint_channel_manager),
|
||||
|
||||
@@ -95,7 +95,7 @@ class EndpointManager {
|
||||
};
|
||||
|
||||
explicit EndpointManager(
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager);
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager);
|
||||
~EndpointManager();
|
||||
|
||||
// Invoked from the constructors of the various *Manager components that make
|
||||
@@ -211,7 +211,7 @@ class EndpointManager {
|
||||
ScopedPtr<Ptr<ThreadUtils> > thread_utils_;
|
||||
ScopedPtr<Ptr<SystemClock> > system_clock_;
|
||||
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager_;
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager_;
|
||||
|
||||
typedef std::map<V1Frame::FrameType, Ptr<IncomingOfflineFrameProcessor> >
|
||||
IncomingOfflineFrameProcessorsMap;
|
||||
|
||||
@@ -108,7 +108,7 @@ class OutgoingStreamInternalPayload : public InternalPayload {
|
||||
}
|
||||
|
||||
private:
|
||||
static const std::int64_t kChunkSize = 64 * 1024;
|
||||
static constexpr std::int64_t kChunkSize = 64 * 1024;
|
||||
};
|
||||
|
||||
template <typename Platform>
|
||||
@@ -191,7 +191,7 @@ class OutgoingFileInternalPayload : public InternalPayload {
|
||||
void close() override { payload_->asFile()->asInputFile()->close(); }
|
||||
|
||||
private:
|
||||
static const std::int64_t kChunkSize = 64 * 1024;
|
||||
static constexpr std::int64_t kChunkSize = 64 * 1024;
|
||||
};
|
||||
|
||||
class IncomingFileInternalPayload : public InternalPayload {
|
||||
@@ -277,14 +277,13 @@ Ptr<InternalPayload> InternalPayloadFactory<Platform>::createIncoming(
|
||||
|
||||
case PayloadTransferFrame::PayloadHeader::STREAM: {
|
||||
// pipe will be auto-destroyed when it is no longer referenced.
|
||||
auto pipe = MakeRefCountedPtr(new Pipe<Platform>());
|
||||
auto pipe = MakeRefCountedPtr(new Pipe());
|
||||
|
||||
return MakePtr(new IncomingStreamInternalPayload<Platform>(
|
||||
MakeConstPtr(new Payload(
|
||||
payload_id,
|
||||
MakeConstPtr(new Payload::Stream(
|
||||
Pipe<Platform>::createInputStream(pipe))))),
|
||||
Pipe<Platform>::createOutputStream(pipe)));
|
||||
MakeConstPtr(
|
||||
new Payload(payload_id, MakeConstPtr(new Payload::Stream(
|
||||
Pipe::createInputStream(pipe))))),
|
||||
Pipe::createOutputStream(pipe)));
|
||||
}
|
||||
|
||||
case PayloadTransferFrame::PayloadHeader::FILE: {
|
||||
|
||||
@@ -10,13 +10,15 @@ template <typename Platform>
|
||||
MediumManager<Platform>::MediumManager()
|
||||
: mediums_(new Mediums<Platform>()),
|
||||
bluetooth_classic_lock_(Platform::createLock()),
|
||||
ble_lock_(Platform::createLock()) {}
|
||||
ble_lock_(Platform::createLock()),
|
||||
wifi_lan_lock_(Platform::createLock()) {}
|
||||
|
||||
template <typename Platform>
|
||||
MediumManager<Platform>::~MediumManager() {
|
||||
// TODO(reznor): log.atDebug().log("Initiating shutdown of MediumManager.");
|
||||
Synchronized s1(bluetooth_classic_lock_.get());
|
||||
Synchronized s2(ble_lock_.get());
|
||||
Synchronized s3(wifi_lan_lock_.get());
|
||||
|
||||
mediums_.destroy();
|
||||
// TODO(reznor): log.atDebug().log("MediumManager has shut down.");
|
||||
@@ -356,6 +358,131 @@ Ptr<BLESocket> MediumManager<Platform>::connectToBlePeripheral(
|
||||
#endif
|
||||
}
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~ WIFILAN ~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
template <typename Platform>
|
||||
bool MediumManager<Platform>::IsWifiLanAvailable() {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
return mediums_->wifi_lan()->IsAvailable();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool MediumManager<Platform>::StartWifiLanAdvertising(
|
||||
absl::string_view service_id, absl::string_view service_info_name) {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
return mediums_->wifi_lan()->StartAdvertising(service_id, service_info_name);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void MediumManager<Platform>::StopWifiLanAdvertising(
|
||||
absl::string_view service_id) {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
mediums_->wifi_lan()->StopAdvertising(service_id);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
class DiscoveredServiceCallback : public mediums::DiscoveredServiceCallback {
|
||||
public:
|
||||
typedef typename MediumManager<Platform>::FoundWifiLanServiceProcessor
|
||||
FoundWifiLanServiceProcessor;
|
||||
|
||||
explicit DiscoveredServiceCallback(
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor)
|
||||
: found_wifi_lan_service_processor_(found_wifi_lan_service_processor) {}
|
||||
|
||||
void OnServiceDiscovered(Ptr<WifiLanService> wifi_lan_service) override {
|
||||
found_wifi_lan_service_processor_->OnFoundWifiLanService(wifi_lan_service);
|
||||
}
|
||||
|
||||
void OnServiceLost(Ptr<WifiLanService> wifi_lan_service) override {
|
||||
found_wifi_lan_service_processor_->OnLostWifiLanService(wifi_lan_service);
|
||||
}
|
||||
|
||||
private:
|
||||
ScopedPtr<Ptr<FoundWifiLanServiceProcessor> >
|
||||
found_wifi_lan_service_processor_;
|
||||
};
|
||||
|
||||
template <typename Platform>
|
||||
bool MediumManager<Platform>::StartWifiLanDiscovery(
|
||||
absl::string_view service_id,
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor) {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
return mediums_->wifi_lan()->StartDiscovery(
|
||||
service_id, MakePtr(new DiscoveredServiceCallback<Platform>(
|
||||
found_wifi_lan_service_processor)));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void MediumManager<Platform>::StopWifiLanDiscovery(
|
||||
absl::string_view service_id) {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
mediums_->wifi_lan()->StopDiscovery(service_id);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
class WifiLanAcceptedConnectionCallback
|
||||
: public mediums::WifiLan<Platform>::AcceptedConnectionCallback {
|
||||
public:
|
||||
typedef typename MediumManager<Platform>::IncomingWifiLanConnectionProcessor
|
||||
IncomingWifiLanConnectionProcessor;
|
||||
|
||||
explicit WifiLanAcceptedConnectionCallback(
|
||||
Ptr<IncomingWifiLanConnectionProcessor>
|
||||
incoming_wifi_lan_connection_processor)
|
||||
: incoming_wifi_lan_connection_processor_(
|
||||
incoming_wifi_lan_connection_processor) {}
|
||||
|
||||
void OnConnectionAccepted(Ptr<WifiLanSocket> socket,
|
||||
absl::string_view service_id) override {
|
||||
incoming_wifi_lan_connection_processor_->OnIncomingWifiLanConnection(
|
||||
socket);
|
||||
}
|
||||
|
||||
private:
|
||||
ScopedPtr<Ptr<IncomingWifiLanConnectionProcessor> >
|
||||
incoming_wifi_lan_connection_processor_;
|
||||
};
|
||||
|
||||
template <typename Platform>
|
||||
bool MediumManager<Platform>::IsListeningForIncomingWifiLanConnections(
|
||||
absl::string_view service_id) {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
return mediums_->wifi_lan()->IsAcceptingConnections(service_id);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool MediumManager<Platform>::StartListeningForIncomingWifiLanConnections(
|
||||
absl::string_view service_id, Ptr<IncomingWifiLanConnectionProcessor>
|
||||
incoming_wifi_lan_connection_processor) {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
return mediums_->wifi_lan()->StartAcceptingConnections(
|
||||
service_id, MakePtr(new WifiLanAcceptedConnectionCallback<Platform>(
|
||||
incoming_wifi_lan_connection_processor)));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void MediumManager<Platform>::StopListeningForIncomingWifiLanConnections(
|
||||
absl::string_view service_id) {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
mediums_->wifi_lan()->StopAcceptingConnections(service_id);
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<WifiLanSocket> MediumManager<Platform>::ConnectToWifiLanService(
|
||||
Ptr<WifiLanService> wifi_lan_service, absl::string_view service_id) {
|
||||
Synchronized s(wifi_lan_lock_.get());
|
||||
|
||||
return mediums_->wifi_lan()->Connect(wifi_lan_service, service_id);
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -122,6 +122,45 @@ class MediumManager {
|
||||
Ptr<BLESocket> connectToBlePeripheral(Ptr<BLE_PERIPHERAL> ble_peripheral,
|
||||
const string& service_id);
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~ WIFI-LAN ~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
bool IsWifiLanAvailable();
|
||||
|
||||
bool StartWifiLanAdvertising(absl::string_view service_id,
|
||||
absl::string_view wifi_lan_service_info_name);
|
||||
void StopWifiLanAdvertising(absl::string_view service_id);
|
||||
|
||||
class FoundWifiLanServiceProcessor {
|
||||
public:
|
||||
virtual ~FoundWifiLanServiceProcessor() {}
|
||||
|
||||
virtual void OnFoundWifiLanService(
|
||||
Ptr<WifiLanService> wifi_lan_service) = 0;
|
||||
virtual void OnLostWifiLanService(Ptr<WifiLanService> wifi_lan_service) = 0;
|
||||
};
|
||||
|
||||
bool StartWifiLanDiscovery(
|
||||
absl::string_view service_id,
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor);
|
||||
void StopWifiLanDiscovery(absl::string_view service_id);
|
||||
|
||||
class IncomingWifiLanConnectionProcessor {
|
||||
public:
|
||||
virtual ~IncomingWifiLanConnectionProcessor() {}
|
||||
|
||||
virtual void OnIncomingWifiLanConnection(
|
||||
Ptr<WifiLanSocket> wifi_lan_socket) = 0;
|
||||
};
|
||||
|
||||
bool IsListeningForIncomingWifiLanConnections(absl::string_view service_id);
|
||||
bool StartListeningForIncomingWifiLanConnections(
|
||||
absl::string_view service_id, Ptr<IncomingWifiLanConnectionProcessor>
|
||||
incoming_wifi_lan_connection_processor);
|
||||
void StopListeningForIncomingWifiLanConnections(absl::string_view service_id);
|
||||
|
||||
Ptr<WifiLanSocket> ConnectToWifiLanService(
|
||||
Ptr<WifiLanService> wifi_lan_service, absl::string_view service_id);
|
||||
|
||||
private:
|
||||
// The destructor for this needs to be manually invoked after the locks below
|
||||
// are acquired, so it cannot be a ScopedPtr.
|
||||
@@ -129,6 +168,7 @@ class MediumManager {
|
||||
|
||||
ScopedPtr<Ptr<Lock> > bluetooth_classic_lock_;
|
||||
ScopedPtr<Ptr<Lock> > ble_lock_;
|
||||
ScopedPtr<Ptr<Lock> > wifi_lan_lock_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -1,3 +1,23 @@
|
||||
cc_library(
|
||||
name = "utils",
|
||||
srcs = [
|
||||
"utils.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"utils.h",
|
||||
],
|
||||
visibility = [
|
||||
"//core/internal/mediums/webrtc:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
"//platform:types",
|
||||
"//platform:utils",
|
||||
"//platform/api",
|
||||
"//platform/port:string",
|
||||
"//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "mediums",
|
||||
srcs = [
|
||||
@@ -5,8 +25,6 @@ cc_library(
|
||||
"ble_advertisement_header.cc",
|
||||
"ble_packet.cc",
|
||||
"ble_peripheral.cc",
|
||||
"utils.cc",
|
||||
"utils.h",
|
||||
],
|
||||
hdrs = [
|
||||
"advertisement_read_result.cc",
|
||||
@@ -34,9 +52,12 @@ cc_library(
|
||||
"mediums.h",
|
||||
"uuid.cc",
|
||||
"uuid.h",
|
||||
"wifi_lan.cc",
|
||||
"wifi_lan.h",
|
||||
],
|
||||
visibility = ["//core/internal:__pkg__"],
|
||||
deps = [
|
||||
":utils",
|
||||
"//platform:logging",
|
||||
"//platform:types",
|
||||
"//platform:utils",
|
||||
@@ -53,7 +74,8 @@ cc_test(
|
||||
srcs = ["advertisement_read_result_test.cc"],
|
||||
deps = [
|
||||
":mediums",
|
||||
"//platform/impl/default",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//testing/base/public:gunit_main",
|
||||
"//absl/time",
|
||||
],
|
||||
@@ -65,6 +87,8 @@ cc_test(
|
||||
deps = [
|
||||
":mediums",
|
||||
"//platform:utils",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
)
|
||||
@@ -74,6 +98,8 @@ cc_test(
|
||||
srcs = ["ble_advertisement_test.cc"],
|
||||
deps = [
|
||||
":mediums",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
)
|
||||
@@ -83,6 +109,8 @@ cc_test(
|
||||
srcs = ["ble_packet_test.cc"],
|
||||
deps = [
|
||||
":mediums",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
)
|
||||
@@ -92,6 +120,8 @@ cc_test(
|
||||
srcs = ["bloom_filter_test.cc"],
|
||||
deps = [
|
||||
":mediums",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
)
|
||||
@@ -101,7 +131,8 @@ cc_test(
|
||||
srcs = ["lost_entity_tracker_test.cc"],
|
||||
deps = [
|
||||
":mediums",
|
||||
"//platform/impl/default",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -52,7 +52,7 @@ target_link_libraries(core_internal_mediums_test
|
||||
core_internal_mediums
|
||||
gtest
|
||||
gtest_main
|
||||
platform_impl_default
|
||||
platform_impl_g3
|
||||
platform_utils
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "core/internal/mediums/advertisement_read_result.h"
|
||||
|
||||
#include "platform/impl/default/default_platform.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/time/clock.h"
|
||||
#include "absl/time/time.h"
|
||||
@@ -10,23 +10,7 @@ namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
class SampleSystemClock : public SystemClock {
|
||||
public:
|
||||
SampleSystemClock() {}
|
||||
~SampleSystemClock() override {}
|
||||
|
||||
std::int64_t elapsedRealtime() override {
|
||||
return absl::ToUnixMillis(absl::Now());
|
||||
}
|
||||
};
|
||||
|
||||
class SamplePlatform {
|
||||
public:
|
||||
static Ptr<Lock> createLock() { return DefaultPlatform::createLock(); }
|
||||
static Ptr<SystemClock> createSystemClock() {
|
||||
return MakePtr(new SampleSystemClock());
|
||||
}
|
||||
};
|
||||
using TestPlatform = platform::ImplementationPlatform;
|
||||
|
||||
constexpr char kAdvertisementBytes[] = {0x0A, 0x0B, 0x0C};
|
||||
|
||||
@@ -39,16 +23,16 @@ const absl::Duration kAdvertisementMaxBackoffDuration =
|
||||
|
||||
template <>
|
||||
const std::int64_t AdvertisementReadResult<
|
||||
SamplePlatform>::kAdvertisementMaxBackoffDurationMillis =
|
||||
TestPlatform>::kAdvertisementMaxBackoffDurationMillis =
|
||||
ToInt64Milliseconds(kAdvertisementMaxBackoffDuration);
|
||||
template <>
|
||||
const std::int64_t
|
||||
AdvertisementReadResult<
|
||||
SamplePlatform>::kAdvertisementBaseBackoffDurationMillis =
|
||||
TestPlatform>::kAdvertisementBaseBackoffDurationMillis =
|
||||
ToInt64Milliseconds(kAdvertisementBaseBackoffDuration);
|
||||
|
||||
TEST(AdvertisementReadResultTest, AdvertisementExists) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
advertisement_read_result.recordLastReadStatus(/* is_success= */ true);
|
||||
|
||||
std::int32_t slot = 6;
|
||||
@@ -61,7 +45,7 @@ TEST(AdvertisementReadResultTest, AdvertisementExists) {
|
||||
}
|
||||
|
||||
TEST(AdvertisementReadResultTest, AdvertisementNonExistent) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
advertisement_read_result.recordLastReadStatus(/* is_success= */ true);
|
||||
|
||||
std::int32_t slot = 6;
|
||||
@@ -70,23 +54,23 @@ TEST(AdvertisementReadResultTest, AdvertisementNonExistent) {
|
||||
}
|
||||
|
||||
TEST(AdvertisementReadResultTest, EvaluateRetryStatusInitialized) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
|
||||
ASSERT_EQ(advertisement_read_result.evaluateRetryStatus(),
|
||||
AdvertisementReadResult<SamplePlatform>::RetryStatus::RETRY);
|
||||
AdvertisementReadResult<TestPlatform>::RetryStatus::RETRY);
|
||||
}
|
||||
|
||||
TEST(AdvertisementReadResultTest, EvaluateRetryStatusSuccess) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
advertisement_read_result.recordLastReadStatus(/* is_success= */ true);
|
||||
|
||||
ASSERT_EQ(advertisement_read_result.evaluateRetryStatus(),
|
||||
AdvertisementReadResult<
|
||||
SamplePlatform>::RetryStatus::PREVIOUSLY_SUCCEEDED);
|
||||
TestPlatform>::RetryStatus::PREVIOUSLY_SUCCEEDED);
|
||||
}
|
||||
|
||||
TEST(AdvertisementReadResultTest, EvaluateRetryStatusTooSoon) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
advertisement_read_result.recordLastReadStatus(/* is_success= */ false);
|
||||
|
||||
// Sleep for some time, but not long enough to warrant a retry.
|
||||
@@ -94,22 +78,22 @@ TEST(AdvertisementReadResultTest, EvaluateRetryStatusTooSoon) {
|
||||
absl::ToInt64Milliseconds(kAdvertisementBaseBackoffDuration) / 2));
|
||||
|
||||
ASSERT_EQ(advertisement_read_result.evaluateRetryStatus(),
|
||||
AdvertisementReadResult<SamplePlatform>::RetryStatus::TOO_SOON);
|
||||
AdvertisementReadResult<TestPlatform>::RetryStatus::TOO_SOON);
|
||||
}
|
||||
|
||||
TEST(AdvertisementReadResultTest, EvaluateRetryStatusRetry) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
advertisement_read_result.recordLastReadStatus(/* is_success= */ false);
|
||||
|
||||
// Sleep long enough to warrant a retry.
|
||||
absl::SleepFor(kAdvertisementBaseBackoffDuration);
|
||||
|
||||
ASSERT_EQ(advertisement_read_result.evaluateRetryStatus(),
|
||||
AdvertisementReadResult<SamplePlatform>::RetryStatus::RETRY);
|
||||
AdvertisementReadResult<TestPlatform>::RetryStatus::RETRY);
|
||||
}
|
||||
|
||||
TEST(AdvertisementReadResultTest, ReportStatusExponentialBackoff) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
advertisement_read_result.recordLastReadStatus(/* is_success= */ false);
|
||||
|
||||
// Record an additional failure so our backoff duration increases.
|
||||
@@ -120,11 +104,11 @@ TEST(AdvertisementReadResultTest, ReportStatusExponentialBackoff) {
|
||||
absl::SleepFor(kAdvertisementBaseBackoffDuration);
|
||||
|
||||
ASSERT_EQ(advertisement_read_result.evaluateRetryStatus(),
|
||||
AdvertisementReadResult<SamplePlatform>::RetryStatus::TOO_SOON);
|
||||
AdvertisementReadResult<TestPlatform>::RetryStatus::TOO_SOON);
|
||||
}
|
||||
|
||||
TEST(AdvertisementReadResultTest, ReportStatusExponentialBackoffMax) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
advertisement_read_result.recordLastReadStatus(/* is_success= */ false);
|
||||
|
||||
// Record an absurd amount of failures so we hit the maximum backoff duration.
|
||||
@@ -137,11 +121,11 @@ TEST(AdvertisementReadResultTest, ReportStatusExponentialBackoffMax) {
|
||||
absl::SleepFor(kAdvertisementMaxBackoffDuration);
|
||||
|
||||
ASSERT_EQ(advertisement_read_result.evaluateRetryStatus(),
|
||||
AdvertisementReadResult<SamplePlatform>::RetryStatus::RETRY);
|
||||
AdvertisementReadResult<TestPlatform>::RetryStatus::RETRY);
|
||||
}
|
||||
|
||||
TEST(AdvertisementReadResultTest, GetDurationSinceRead) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
AdvertisementReadResult<TestPlatform> advertisement_read_result;
|
||||
advertisement_read_result.recordLastReadStatus(/* is_success= */ true);
|
||||
|
||||
std::int64_t sleepTime = 420;
|
||||
|
||||
@@ -460,8 +460,8 @@ void BLEV2<Platform>::stopScanning() {
|
||||
|
||||
// TODO(b/112199086) Change to RecurringCancelableAlarm
|
||||
template <typename Platform>
|
||||
Ptr<CancelableAlarm<Platform>> BLEV2<Platform>::createOnLostAlarm() {
|
||||
return Ptr<CancelableAlarm<Platform>>();
|
||||
Ptr<CancelableAlarm> BLEV2<Platform>::createOnLostAlarm() {
|
||||
return Ptr<CancelableAlarm>();
|
||||
}
|
||||
|
||||
// Returns true if the device is currently accepting incoming BLE socket
|
||||
|
||||
@@ -166,7 +166,7 @@ class BLEV2 {
|
||||
struct ScanningInfo {
|
||||
ScanningInfo(const string& service_id,
|
||||
Ptr<ScanCallbackFacade> scan_callback_facade,
|
||||
Ptr<CancelableAlarm<Platform>> on_lost_alarm)
|
||||
Ptr<CancelableAlarm> on_lost_alarm)
|
||||
: service_id(service_id),
|
||||
scan_callback_facade(scan_callback_facade),
|
||||
on_lost_alarm(on_lost_alarm) {}
|
||||
@@ -177,7 +177,7 @@ class BLEV2 {
|
||||
const string service_id;
|
||||
ScopedPtr<Ptr<ScanCallbackFacade>> scan_callback_facade;
|
||||
// TODO(ahlee): Change to recurring cancelable alarm
|
||||
ScopedPtr<Ptr<CancelableAlarm<Platform>>> on_lost_alarm;
|
||||
ScopedPtr<Ptr<CancelableAlarm>> on_lost_alarm;
|
||||
};
|
||||
|
||||
struct AdvertisingInfo {
|
||||
@@ -236,7 +236,7 @@ class BLEV2 {
|
||||
Ptr<BLEPeripheralV2> ble_peripheral,
|
||||
ConstPtr<BLEAdvertisementData> advertisement_data);
|
||||
void processOnLostTimeout();
|
||||
Ptr<CancelableAlarm<Platform>> createOnLostAlarm();
|
||||
Ptr<CancelableAlarm> createOnLostAlarm();
|
||||
|
||||
bool isAdvertisementGattServerRunning();
|
||||
bool startAdvertisementGattServer(const string& service_id,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "core/internal/mediums/lost_entity_tracker.h"
|
||||
|
||||
#include "platform/impl/default/default_platform.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace location {
|
||||
@@ -9,6 +9,8 @@ namespace connections {
|
||||
namespace mediums {
|
||||
namespace {
|
||||
|
||||
using TestPlatform = platform::ImplementationPlatform;
|
||||
|
||||
struct TestEntity {
|
||||
int id;
|
||||
|
||||
@@ -18,7 +20,7 @@ struct TestEntity {
|
||||
};
|
||||
|
||||
TEST(LostEntityTracker, NoEntitiesLost) {
|
||||
LostEntityTracker<DefaultPlatform, TestEntity> lost_entity_tracker;
|
||||
LostEntityTracker<TestPlatform, TestEntity> lost_entity_tracker;
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_1(MakeConstPtr(new TestEntity(1)));
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_2(MakeConstPtr(new TestEntity(2)));
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_3(MakeConstPtr(new TestEntity(3)));
|
||||
@@ -41,7 +43,7 @@ TEST(LostEntityTracker, NoEntitiesLost) {
|
||||
}
|
||||
|
||||
TEST(LostEntityTracker, AllEntitiesLost) {
|
||||
LostEntityTracker<DefaultPlatform, TestEntity> lost_entity_tracker;
|
||||
LostEntityTracker<TestPlatform, TestEntity> lost_entity_tracker;
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_1(MakeConstPtr(new TestEntity(1)));
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_2(MakeConstPtr(new TestEntity(2)));
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_3(MakeConstPtr(new TestEntity(3)));
|
||||
@@ -55,7 +57,7 @@ TEST(LostEntityTracker, AllEntitiesLost) {
|
||||
ASSERT_TRUE(lost_entity_tracker.computeLostEntities().empty());
|
||||
|
||||
// Go through a round without rediscovering any entities.
|
||||
typename LostEntityTracker<DefaultPlatform, TestEntity>::EntitySet
|
||||
typename LostEntityTracker<TestPlatform, TestEntity>::EntitySet
|
||||
lost_entities = lost_entity_tracker.computeLostEntities();
|
||||
ASSERT_TRUE(lost_entities.find(entity_1.get()) != lost_entities.end());
|
||||
ASSERT_TRUE(lost_entities.find(entity_2.get()) != lost_entities.end());
|
||||
@@ -63,7 +65,7 @@ TEST(LostEntityTracker, AllEntitiesLost) {
|
||||
}
|
||||
|
||||
TEST(LostEntityTracker, SomeEntitiesLost) {
|
||||
LostEntityTracker<DefaultPlatform, TestEntity> lost_entity_tracker;
|
||||
LostEntityTracker<TestPlatform, TestEntity> lost_entity_tracker;
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_1(MakeConstPtr(new TestEntity(1)));
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_2(MakeConstPtr(new TestEntity(2)));
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_3(MakeConstPtr(new TestEntity(3)));
|
||||
@@ -80,7 +82,7 @@ TEST(LostEntityTracker, SomeEntitiesLost) {
|
||||
// was lost after the check.
|
||||
lost_entity_tracker.recordFoundEntity(entity_1.get());
|
||||
lost_entity_tracker.recordFoundEntity(entity_3.get());
|
||||
typename LostEntityTracker<DefaultPlatform, TestEntity>::EntitySet
|
||||
typename LostEntityTracker<TestPlatform, TestEntity>::EntitySet
|
||||
lost_entities = lost_entity_tracker.computeLostEntities();
|
||||
ASSERT_TRUE(lost_entities.find(entity_1.get()) == lost_entities.end());
|
||||
ASSERT_TRUE(lost_entities.find(entity_2.get()) != lost_entities.end());
|
||||
@@ -88,7 +90,7 @@ TEST(LostEntityTracker, SomeEntitiesLost) {
|
||||
}
|
||||
|
||||
TEST(LostEntityTracker, SameEntityMultipleCopies) {
|
||||
LostEntityTracker<DefaultPlatform, TestEntity> lost_entity_tracker;
|
||||
LostEntityTracker<TestPlatform, TestEntity> lost_entity_tracker;
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_1(MakeConstPtr(new TestEntity(1)));
|
||||
ScopedPtr<ConstPtr<TestEntity> > entity_1_copy(
|
||||
MakeConstPtr(new TestEntity(1)));
|
||||
@@ -107,7 +109,7 @@ TEST(LostEntityTracker, SameEntityMultipleCopies) {
|
||||
|
||||
// Go through a round without rediscovering any entities and verify that we
|
||||
// lost an entity equivalent to both copies of it.
|
||||
typename LostEntityTracker<DefaultPlatform, TestEntity>::EntitySet
|
||||
typename LostEntityTracker<TestPlatform, TestEntity>::EntitySet
|
||||
lost_entities = lost_entity_tracker.computeLostEntities();
|
||||
ASSERT_EQ(lost_entities.size(), 1);
|
||||
ASSERT_TRUE(lost_entities.find(entity_1.get()) != lost_entities.end());
|
||||
|
||||
@@ -10,7 +10,8 @@ Mediums<Platform>::Mediums()
|
||||
bluetooth_classic_(
|
||||
new BluetoothClassic<Platform>(bluetooth_radio_.get())),
|
||||
ble_(new BLE<Platform>(bluetooth_radio_.get())),
|
||||
ble_v2_(new mediums::BLEV2<Platform>(bluetooth_radio_.get())) {}
|
||||
ble_v2_(new mediums::BLEV2<Platform>(bluetooth_radio_.get())),
|
||||
wifi_lan_(new mediums::WifiLan<Platform>()) {}
|
||||
|
||||
template <typename Platform>
|
||||
Mediums<Platform>::~Mediums() {
|
||||
@@ -37,6 +38,11 @@ Ptr<mediums::BLEV2<Platform> > Mediums<Platform>::bleV2() const {
|
||||
return ble_v2_.get();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<mediums::WifiLan<Platform> > Mediums<Platform>::wifi_lan() const {
|
||||
return wifi_lan_.get();
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "core/internal/mediums/ble_v2.h"
|
||||
#include "core/internal/mediums/bluetooth_classic.h"
|
||||
#include "core/internal/mediums/bluetooth_radio.h"
|
||||
#include "core/internal/mediums/wifi_lan.h"
|
||||
#include "platform/ptr.h"
|
||||
|
||||
namespace location {
|
||||
@@ -27,6 +28,8 @@ class Mediums {
|
||||
Ptr<BLE<Platform> > ble() const;
|
||||
// Returns a handle to V2 of the Bluetooth Low Energy (BLE) medium.
|
||||
Ptr<mediums::BLEV2<Platform> > bleV2() const;
|
||||
// Returns a handle to the Wifi-Lan medium.
|
||||
Ptr<mediums::WifiLan<Platform> > wifi_lan() const;
|
||||
|
||||
private:
|
||||
// The order of declaration is critical for both construction and
|
||||
@@ -41,6 +44,7 @@ class Mediums {
|
||||
ScopedPtr<Ptr<BluetoothClassic<Platform> > > bluetooth_classic_;
|
||||
ScopedPtr<Ptr<BLE<Platform> > > ble_;
|
||||
ScopedPtr<Ptr<mediums::BLEV2<Platform> > > ble_v2_;
|
||||
ScopedPtr<Ptr<mediums::WifiLan<Platform> > > wifi_lan_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include "core/internal/mediums/utils.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
#include "platform/exception.h"
|
||||
#include "platform/prng.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
|
||||
namespace location {
|
||||
@@ -48,6 +50,26 @@ ConstPtr<ByteArray> Utils::legacySha256HashOnlyForPrinting(
|
||||
return Utils::sha256Hash(hash_utils, formatted_hex_byte_array.get(), length);
|
||||
}
|
||||
|
||||
ConstPtr<ByteArray> Utils::generateRandomBytes(size_t length) {
|
||||
Prng rng;
|
||||
std::string data;
|
||||
data.reserve(length);
|
||||
|
||||
// Adds 4 random bytes per iteration.
|
||||
while (length > 0) {
|
||||
std::uint32_t val = rng.nextUInt32();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
data += val & 0xFF;
|
||||
val >>= 8;
|
||||
length--;
|
||||
|
||||
if (!length) break;
|
||||
}
|
||||
}
|
||||
|
||||
return MakeConstPtr(new ByteArray(data));
|
||||
}
|
||||
|
||||
std::string Utils::bytesToPrintableHexString(ConstPtr<ByteArray> bytes) {
|
||||
std::string hex_string(
|
||||
absl::BytesToHexString(std::string(bytes->getData(), bytes->size())));
|
||||
|
||||
@@ -21,6 +21,8 @@ class Utils {
|
||||
static ConstPtr<ByteArray> legacySha256HashOnlyForPrinting(
|
||||
Ptr<HashUtils> hash_utils, ConstPtr<ByteArray> source, size_t length);
|
||||
|
||||
static ConstPtr<ByteArray> generateRandomBytes(size_t length);
|
||||
|
||||
private:
|
||||
static std::string bytesToPrintableHexString(ConstPtr<ByteArray> bytes);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
cc_library(
|
||||
name = "webrtc",
|
||||
hdrs = [
|
||||
"webrtc_socket.cc",
|
||||
"webrtc_socket.h",
|
||||
],
|
||||
deps = [
|
||||
"//platform:utils",
|
||||
"//platform/api",
|
||||
"//webrtc/api:libjingle_peerconnection_api",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "webrtc_test",
|
||||
srcs = ["webrtc_socket_test.cc"],
|
||||
deps = [
|
||||
":webrtc",
|
||||
"//platform:types",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3", # buildcleaner: keep
|
||||
"//testing/base/public:gunit_main",
|
||||
"//webrtc/api:libjingle_peerconnection_api",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "peer_id",
|
||||
srcs = ["peer_id.cc"],
|
||||
hdrs = ["peer_id.h"],
|
||||
deps = [
|
||||
"//core/internal/mediums:utils",
|
||||
"//platform:types",
|
||||
"//platform/api",
|
||||
"//platform/port:string",
|
||||
"//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "signaling_frames",
|
||||
srcs = ["signaling_frames.cc"],
|
||||
hdrs = ["signaling_frames.h"],
|
||||
deps = [
|
||||
":peer_id",
|
||||
"//platform:types",
|
||||
"//location/nearby/mediums/proto:web_rtc_signaling_frames_cc_proto",
|
||||
"//webrtc/api:libjingle_peerconnection_api",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "peer_id_test",
|
||||
srcs = ["peer_id_test.cc"],
|
||||
deps = [
|
||||
":peer_id",
|
||||
"//platform:types",
|
||||
"//platform/api",
|
||||
"//platform/impl/g3", # buildcleaner: keep
|
||||
"//testing/base/public:gunit_main",
|
||||
"//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "signaling_frames_test",
|
||||
srcs = ["signaling_frames_test.cc"],
|
||||
deps = [
|
||||
":peer_id",
|
||||
":signaling_frames",
|
||||
"//platform:types",
|
||||
"//platform/impl/g3", # buildcleaner: keep
|
||||
"//net/proto2/public:proto2",
|
||||
"//testing/base/public:gunit_main",
|
||||
"//webrtc/pc:peerconnection", # buildcleaner: keep
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "core/internal/mediums/webrtc/peer_id.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "core/internal/mediums/utils.h"
|
||||
#include "absl/strings/ascii.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
namespace {
|
||||
constexpr int kPeerIdLength = 64;
|
||||
|
||||
std::string BytesToStringUppercase(ConstPtr<ByteArray> bytes) {
|
||||
std::string hex_string(
|
||||
absl::BytesToHexString(std::string(bytes->getData(), bytes->size())));
|
||||
absl::AsciiStrToUpper(&hex_string);
|
||||
return hex_string;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ConstPtr<PeerId> PeerId::FromRandom(Ptr<HashUtils> hash_utils) {
|
||||
return FromSeed(Utils::generateRandomBytes(kPeerIdLength), hash_utils);
|
||||
}
|
||||
|
||||
ConstPtr<PeerId> PeerId::FromSeed(ConstPtr<ByteArray> seed,
|
||||
Ptr<HashUtils> hash_utils) {
|
||||
ScopedPtr<ConstPtr<ByteArray>> full_hash(
|
||||
Utils::sha256Hash(hash_utils, seed, kPeerIdLength));
|
||||
ScopedPtr<ConstPtr<ByteArray>> hashedSeed(
|
||||
MakeConstPtr(new ByteArray(full_hash->getData(), kPeerIdLength / 2)));
|
||||
return MakeConstPtr(new PeerId(BytesToStringUppercase(hashedSeed.get())));
|
||||
}
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_
|
||||
#define CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_
|
||||
|
||||
#include "platform/api/hash_utils.h"
|
||||
#include "platform/byte_array.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
// PeerId is used as an identifier to exchange SDP messages to establish WebRTC
|
||||
// p2p connection.
|
||||
class PeerId {
|
||||
public:
|
||||
explicit PeerId(const string& id) : id_(id) {}
|
||||
~PeerId() = default;
|
||||
|
||||
static ConstPtr<PeerId> FromRandom(Ptr<HashUtils> hash_utils);
|
||||
static ConstPtr<PeerId> FromSeed(ConstPtr<ByteArray> seed,
|
||||
Ptr<HashUtils> hash_utils);
|
||||
|
||||
const string& GetId() const { return id_; }
|
||||
|
||||
private:
|
||||
const string id_;
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_
|
||||
@@ -0,0 +1,76 @@
|
||||
#include "core/internal/mediums/webrtc/peer_id.h"
|
||||
|
||||
#include "platform/api/hash_utils.h"
|
||||
#include "platform/byte_array.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
namespace {
|
||||
|
||||
class MockHashUtils : public HashUtils {
|
||||
public:
|
||||
MOCK_METHOD(ConstPtr<ByteArray>, md5, (const std::string& input), (override));
|
||||
MOCK_METHOD(ConstPtr<ByteArray>, sha256, (const std::string& input),
|
||||
(override));
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(PeerIdTest, GenerateRandomPeerId) {
|
||||
// These are actual SHA-256 values for |seed| = "seed".
|
||||
std::string hashed_output =
|
||||
"19b25856e1c150ca834cffc8b59b23adbd0ec0389e58eb22b3b64768098d002b";
|
||||
std::string expected_peer_id =
|
||||
"19B25856E1C150CA834CFFC8B59B23ADBD0EC0389E58EB22B3B64768098D002B";
|
||||
|
||||
Ptr<testing::NiceMock<MockHashUtils>> mock_hash_utils(
|
||||
MakePtr(new MockHashUtils()));
|
||||
ON_CALL(*mock_hash_utils.get(), sha256(testing::_))
|
||||
.WillByDefault(testing::Return(
|
||||
MakeConstPtr(new ByteArray(absl::HexStringToBytes(hashed_output)))));
|
||||
EXPECT_CALL(*mock_hash_utils.get(), sha256(testing::_));
|
||||
|
||||
ConstPtr<PeerId> peer_id = PeerId::FromRandom(mock_hash_utils);
|
||||
ASSERT_EQ(64, peer_id->GetId().size());
|
||||
ASSERT_EQ(expected_peer_id, peer_id->GetId());
|
||||
}
|
||||
|
||||
TEST(PeerIdTest, GenerateFromSeed) {
|
||||
// Values calculated by running actual SHA-256 hash on |seed|.
|
||||
std::string seed = "sesdfed";
|
||||
std::string hashed_output =
|
||||
"19b25856e1c150ca834cffc8b59b23adbd0ec0389e58eb22b3b64768098d002b";
|
||||
std::string expected_peer_id =
|
||||
"19B25856E1C150CA834CFFC8B59B23ADBD0EC0389E58EB22B3B64768098D002B";
|
||||
|
||||
Ptr<testing::NiceMock<MockHashUtils>> mock_hash_utils(
|
||||
MakePtr(new MockHashUtils()));
|
||||
ON_CALL(*mock_hash_utils.get(), sha256(testing::Eq(seed)))
|
||||
.WillByDefault(testing::Return(
|
||||
MakeConstPtr(new ByteArray(absl::HexStringToBytes(hashed_output)))));
|
||||
EXPECT_CALL(*mock_hash_utils.get(), sha256(testing::Eq(seed)));
|
||||
|
||||
ConstPtr<PeerId> peer_id =
|
||||
PeerId::FromSeed(MakeConstPtr(new ByteArray(seed)), mock_hash_utils);
|
||||
|
||||
ASSERT_EQ(64, peer_id->GetId().size());
|
||||
ASSERT_EQ(expected_peer_id, peer_id->GetId());
|
||||
}
|
||||
|
||||
TEST(PeerIdTest, GetId) {
|
||||
const std::string id = "this_is_a_test";
|
||||
PeerId peer_id(id);
|
||||
ASSERT_EQ(id, peer_id.GetId());
|
||||
}
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,125 @@
|
||||
#include "core/internal/mediums/webrtc/signaling_frames.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
namespace webrtc_frames {
|
||||
using WebRtcSignalingFrame = location::nearby::mediums::WebRtcSignalingFrame;
|
||||
|
||||
namespace {
|
||||
|
||||
ConstPtr<ByteArray> FrameToByteArray(
|
||||
const WebRtcSignalingFrame& signaling_frame) {
|
||||
std::string message;
|
||||
signaling_frame.SerializeToString(&message);
|
||||
return MakeConstPtr(new ByteArray(message.c_str(), message.size()));
|
||||
}
|
||||
|
||||
void SetSenderId(ConstPtr<PeerId> sender_id, WebRtcSignalingFrame& frame) {
|
||||
frame.mutable_sender_id()->set_id(sender_id->GetId());
|
||||
}
|
||||
|
||||
ConstPtr<webrtc::IceCandidateInterface> DecodeIceCandidate(
|
||||
const location::nearby::mediums::IceCandidate& ice_candidate_proto) {
|
||||
webrtc::SdpParseError error;
|
||||
return ConstPtr<webrtc::IceCandidateInterface>(webrtc::CreateIceCandidate(
|
||||
ice_candidate_proto.sdp_mid(), ice_candidate_proto.sdp_m_line_index(),
|
||||
ice_candidate_proto.sdp(), &error));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ConstPtr<ByteArray> EncodeReadyForSignalingPoke(ConstPtr<PeerId> sender_id) {
|
||||
WebRtcSignalingFrame signaling_frame;
|
||||
signaling_frame.set_type(WebRtcSignalingFrame::READY_FOR_SIGNALING_POKE_TYPE);
|
||||
SetSenderId(sender_id, signaling_frame);
|
||||
signaling_frame.mutable_ready_for_signaling_poke();
|
||||
return FrameToByteArray(std::move(signaling_frame));
|
||||
}
|
||||
|
||||
ConstPtr<ByteArray> EncodeOffer(
|
||||
ConstPtr<PeerId> sender_id,
|
||||
const webrtc::SessionDescriptionInterface& offer) {
|
||||
WebRtcSignalingFrame signaling_frame;
|
||||
signaling_frame.set_type(WebRtcSignalingFrame::OFFER_TYPE);
|
||||
SetSenderId(sender_id, signaling_frame);
|
||||
std::string offer_str;
|
||||
offer.ToString(&offer_str);
|
||||
signaling_frame.mutable_offer()
|
||||
->mutable_session_description()
|
||||
->set_description(offer_str);
|
||||
return FrameToByteArray(std::move(signaling_frame));
|
||||
}
|
||||
|
||||
ConstPtr<ByteArray> EncodeAnswer(
|
||||
ConstPtr<PeerId> sender_id,
|
||||
const webrtc::SessionDescriptionInterface& answer) {
|
||||
WebRtcSignalingFrame signaling_frame;
|
||||
signaling_frame.set_type(WebRtcSignalingFrame::ANSWER_TYPE);
|
||||
SetSenderId(sender_id, signaling_frame);
|
||||
std::string answer_str;
|
||||
answer.ToString(&answer_str);
|
||||
signaling_frame.mutable_answer()
|
||||
->mutable_session_description()
|
||||
->set_description(answer_str);
|
||||
return FrameToByteArray(std::move(signaling_frame));
|
||||
}
|
||||
|
||||
ConstPtr<ByteArray> EncodeIceCandidates(
|
||||
ConstPtr<PeerId> sender_id,
|
||||
const std::vector<location::nearby::mediums::IceCandidate>&
|
||||
ice_candidates) {
|
||||
WebRtcSignalingFrame signaling_frame;
|
||||
signaling_frame.set_type(WebRtcSignalingFrame::ICE_CANDIDATES_TYPE);
|
||||
SetSenderId(sender_id, signaling_frame);
|
||||
for (const auto& ice_candidate : ice_candidates) {
|
||||
*signaling_frame.mutable_ice_candidates()->add_ice_candidates() =
|
||||
ice_candidate;
|
||||
}
|
||||
return FrameToByteArray(std::move(signaling_frame));
|
||||
}
|
||||
|
||||
Ptr<webrtc::SessionDescriptionInterface> DecodeOffer(
|
||||
const WebRtcSignalingFrame& frame) {
|
||||
return MakePtr(webrtc::CreateSessionDescription(
|
||||
webrtc::SdpType::kOffer,
|
||||
frame.offer().session_description().description())
|
||||
.release());
|
||||
}
|
||||
|
||||
Ptr<webrtc::SessionDescriptionInterface> DecodeAnswer(
|
||||
const WebRtcSignalingFrame& frame) {
|
||||
return MakePtr(webrtc::CreateSessionDescription(
|
||||
webrtc::SdpType::kAnswer,
|
||||
frame.answer().session_description().description())
|
||||
.release());
|
||||
}
|
||||
|
||||
std::vector<ConstPtr<webrtc::IceCandidateInterface>> DecodeIceCandidates(
|
||||
const WebRtcSignalingFrame& frame) {
|
||||
std::vector<ConstPtr<webrtc::IceCandidateInterface>> ice_candidates;
|
||||
for (const auto& candidate : frame.ice_candidates().ice_candidates()) {
|
||||
ice_candidates.push_back(DecodeIceCandidate(candidate));
|
||||
}
|
||||
return ice_candidates;
|
||||
}
|
||||
|
||||
location::nearby::mediums::IceCandidate EncodeIceCandidate(
|
||||
const webrtc::IceCandidateInterface& ice_candidate) {
|
||||
std::string sdp;
|
||||
ice_candidate.ToString(&sdp);
|
||||
location::nearby::mediums::IceCandidate ice_candidate_proto;
|
||||
ice_candidate_proto.set_sdp(sdp);
|
||||
ice_candidate_proto.set_sdp_mid(ice_candidate.sdp_mid());
|
||||
ice_candidate_proto.set_sdp_m_line_index(ice_candidate.sdp_mline_index());
|
||||
return ice_candidate_proto;
|
||||
}
|
||||
|
||||
} // namespace webrtc_frames
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_SIGNALING_FRAMES_H_
|
||||
#define CORE_INTERNAL_MEDIUMS_WEBRTC_SIGNALING_FRAMES_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "core/internal/mediums/webrtc/peer_id.h"
|
||||
#include "platform/byte_array.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "location/nearby/mediums/proto/web_rtc_signaling_frames.pb.h"
|
||||
#include "webrtc/api/peer_connection_interface.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
namespace webrtc_frames {
|
||||
|
||||
ConstPtr<ByteArray> EncodeReadyForSignalingPoke(ConstPtr<PeerId> sender_id);
|
||||
|
||||
ConstPtr<ByteArray> EncodeOffer(
|
||||
ConstPtr<PeerId> sender_id,
|
||||
const webrtc::SessionDescriptionInterface& offer);
|
||||
ConstPtr<ByteArray> EncodeAnswer(
|
||||
ConstPtr<PeerId> sender_id,
|
||||
const webrtc::SessionDescriptionInterface& answer);
|
||||
|
||||
ConstPtr<ByteArray> EncodeIceCandidates(
|
||||
ConstPtr<PeerId> sender_id,
|
||||
const std::vector<location::nearby::mediums::IceCandidate>& ice_candidates);
|
||||
location::nearby::mediums::IceCandidate EncodeIceCandidate(
|
||||
const webrtc::IceCandidateInterface& ice_candidate);
|
||||
|
||||
Ptr<webrtc::SessionDescriptionInterface> DecodeOffer(
|
||||
const location::nearby::mediums::WebRtcSignalingFrame& frame);
|
||||
Ptr<webrtc::SessionDescriptionInterface> DecodeAnswer(
|
||||
const location::nearby::mediums::WebRtcSignalingFrame& frame);
|
||||
|
||||
std::vector<ConstPtr<webrtc::IceCandidateInterface>> DecodeIceCandidates(
|
||||
const location::nearby::mediums::WebRtcSignalingFrame& frame);
|
||||
|
||||
} // namespace webrtc_frames
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_SIGNALING_FRAMES_H_
|
||||
@@ -0,0 +1,184 @@
|
||||
#include "core/internal/mediums/webrtc/signaling_frames.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "core/internal/mediums/webrtc/peer_id.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "net/proto2/public/text_format.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
namespace webrtc_frames {
|
||||
|
||||
namespace {
|
||||
|
||||
const char kSampleSdp[] =
|
||||
"v=0\r\no=- 7859371131 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 "
|
||||
"0\r\na=msid-semantic: WMS\r\n";
|
||||
|
||||
const char kIceCandidateSdp1[] =
|
||||
"a=candidate:1 1 UDP 2130706431 10.0.1.1 8998 typ host";
|
||||
const char kIceCandidateSdp2[] =
|
||||
"a=candidate:2 1 UDP 1694498815 192.0.2.3 45664 typ srflx raddr";
|
||||
|
||||
const char kIceSdpMid[] = "data";
|
||||
const int kIceSdpMLineIndex = 0;
|
||||
|
||||
const char kOfferProto[] = R"(
|
||||
sender_id { id: "abc" }
|
||||
type: OFFER_TYPE
|
||||
offer {
|
||||
session_description {
|
||||
description: "v=0\r\no=- 7859371131 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=msid-semantic: WMS\r\n"
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
const char kAnswerProto[] = R"(
|
||||
sender_id { id: "abc" }
|
||||
type: ANSWER_TYPE
|
||||
answer {
|
||||
session_description {
|
||||
description: "v=0\r\no=- 7859371131 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=msid-semantic: WMS\r\n"
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
const char kIceCandidatesProto[] = R"(
|
||||
sender_id { id: "abc" }
|
||||
type: ICE_CANDIDATES_TYPE
|
||||
ice_candidates {
|
||||
ice_candidates {
|
||||
sdp: "candidate:1 1 udp 2130706431 10.0.1.1 8998 typ host generation 0"
|
||||
sdp_mid: "data"
|
||||
sdp_m_line_index: 0
|
||||
}
|
||||
ice_candidates {
|
||||
sdp: "candidate:2 1 udp 1694498815 192.0.2.3 45664 typ srflx generation 0"
|
||||
sdp_mid: "data"
|
||||
sdp_m_line_index: 0
|
||||
}
|
||||
}
|
||||
)";
|
||||
} // namespace
|
||||
|
||||
TEST(SignalingFramesTest, SignalingPoke) {
|
||||
ConstPtr<PeerId> sender_id(new PeerId("abc"));
|
||||
ConstPtr<ByteArray> encoded_poke = EncodeReadyForSignalingPoke(sender_id);
|
||||
|
||||
location::nearby::mediums::WebRtcSignalingFrame frame;
|
||||
frame.ParseFromString(
|
||||
std::string(encoded_poke->getData(), encoded_poke->size()));
|
||||
|
||||
EXPECT_THAT(frame, testing::EqualsProto(R"(
|
||||
sender_id { id: "abc" }
|
||||
type: READY_FOR_SIGNALING_POKE_TYPE
|
||||
ready_for_signaling_poke {}
|
||||
)"));
|
||||
}
|
||||
|
||||
TEST(SignalingFramesTest, EncodeValidOffer) {
|
||||
ConstPtr<PeerId> sender_id(new PeerId("abc"));
|
||||
std::unique_ptr<webrtc::SessionDescriptionInterface> offer =
|
||||
webrtc::CreateSessionDescription(webrtc::SdpType::kOffer, kSampleSdp);
|
||||
ConstPtr<ByteArray> encoded_offer = EncodeOffer(sender_id, *offer);
|
||||
|
||||
location::nearby::mediums::WebRtcSignalingFrame frame;
|
||||
frame.ParseFromString(
|
||||
std::string(encoded_offer->getData(), encoded_offer->size()));
|
||||
|
||||
EXPECT_THAT(frame, testing::EqualsProto(kOfferProto));
|
||||
}
|
||||
|
||||
TEST(SignalingFramesTest, DecodeValidOffer) {
|
||||
location::nearby::mediums::WebRtcSignalingFrame frame;
|
||||
proto2::TextFormat::ParseFromString(kOfferProto, &frame);
|
||||
Ptr<webrtc::SessionDescriptionInterface> decoded_offer = DecodeOffer(frame);
|
||||
|
||||
EXPECT_EQ(webrtc::SdpType::kOffer, decoded_offer->GetType());
|
||||
std::string description;
|
||||
decoded_offer->ToString(&description);
|
||||
EXPECT_EQ(kSampleSdp, description);
|
||||
}
|
||||
|
||||
TEST(SignalingFramesTest, EncodeValidAnswer) {
|
||||
ConstPtr<PeerId> sender_id(new PeerId("abc"));
|
||||
std::unique_ptr<webrtc::SessionDescriptionInterface> answer =
|
||||
webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, kSampleSdp);
|
||||
ConstPtr<ByteArray> encoded_answer = EncodeAnswer(sender_id, *answer);
|
||||
|
||||
location::nearby::mediums::WebRtcSignalingFrame frame;
|
||||
frame.ParseFromString(
|
||||
std::string(encoded_answer->getData(), encoded_answer->size()));
|
||||
|
||||
EXPECT_THAT(frame, testing::EqualsProto(kAnswerProto));
|
||||
}
|
||||
|
||||
TEST(SignalingFramesTest, DecodeValidAnswer) {
|
||||
location::nearby::mediums::WebRtcSignalingFrame frame;
|
||||
proto2::TextFormat::ParseFromString(kAnswerProto, &frame);
|
||||
Ptr<webrtc::SessionDescriptionInterface> decoded_answer = DecodeAnswer(frame);
|
||||
|
||||
EXPECT_EQ(webrtc::SdpType::kAnswer, decoded_answer->GetType());
|
||||
std::string description;
|
||||
decoded_answer->ToString(&description);
|
||||
EXPECT_EQ(kSampleSdp, description);
|
||||
}
|
||||
|
||||
TEST(SignalingFramesTest, EncodeValidIceCandidates) {
|
||||
ConstPtr<PeerId> sender_id(new PeerId("abc"));
|
||||
webrtc::SdpParseError error;
|
||||
|
||||
std::vector<ConstPtr<webrtc::IceCandidateInterface>> ice_candidates;
|
||||
ice_candidates.emplace_back(webrtc::CreateIceCandidate(
|
||||
kIceSdpMid, kIceSdpMLineIndex, kIceCandidateSdp1, &error));
|
||||
ice_candidates.emplace_back(webrtc::CreateIceCandidate(
|
||||
kIceSdpMid, kIceSdpMLineIndex, kIceCandidateSdp2, &error));
|
||||
std::vector<location::nearby::mediums::IceCandidate> encoded_candidates_vec;
|
||||
for (const auto& ice_candidate : ice_candidates) {
|
||||
encoded_candidates_vec.push_back(EncodeIceCandidate(*ice_candidate.get()));
|
||||
}
|
||||
ConstPtr<ByteArray> encoded_candidates =
|
||||
EncodeIceCandidates(sender_id, encoded_candidates_vec);
|
||||
|
||||
location::nearby::mediums::WebRtcSignalingFrame frame;
|
||||
frame.ParseFromString(
|
||||
std::string(encoded_candidates->getData(), encoded_candidates->size()));
|
||||
|
||||
EXPECT_THAT(frame, testing::EqualsProto(kIceCandidatesProto));
|
||||
}
|
||||
|
||||
TEST(SignalingFramesTest, DecodeValidIceCandidates) {
|
||||
webrtc::SdpParseError error;
|
||||
|
||||
std::vector<ConstPtr<webrtc::IceCandidateInterface>> ice_candidates;
|
||||
ice_candidates.emplace_back(webrtc::CreateIceCandidate(
|
||||
kIceSdpMid, kIceSdpMLineIndex, kIceCandidateSdp1, &error));
|
||||
ice_candidates.emplace_back(webrtc::CreateIceCandidate(
|
||||
kIceSdpMid, kIceSdpMLineIndex, kIceCandidateSdp2, &error));
|
||||
std::vector<location::nearby::mediums::IceCandidate> encoded_candidates_vec;
|
||||
|
||||
location::nearby::mediums::WebRtcSignalingFrame frame;
|
||||
proto2::TextFormat::ParseFromString(kIceCandidatesProto, &frame);
|
||||
std::vector<ConstPtr<webrtc::IceCandidateInterface>> decoded_candidates =
|
||||
DecodeIceCandidates(frame);
|
||||
|
||||
ASSERT_EQ(2u, decoded_candidates.size());
|
||||
for (int i = 0; i < static_cast<int>(decoded_candidates.size()); i++) {
|
||||
EXPECT_TRUE(ice_candidates[i]->candidate().IsEquivalent(
|
||||
decoded_candidates[i]->candidate()));
|
||||
EXPECT_EQ(ice_candidates[i]->sdp_mid(), decoded_candidates[i]->sdp_mid());
|
||||
EXPECT_EQ(ice_candidates[i]->sdp_mline_index(),
|
||||
decoded_candidates[i]->sdp_mline_index());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace webrtc_frames
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,139 @@
|
||||
#include "core/internal/mediums/webrtc/webrtc_socket.h"
|
||||
|
||||
#include "platform/synchronized.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
// OutputStreamImpl
|
||||
template <typename Platform>
|
||||
Exception::Value WebRtcSocket<Platform>::OutputStreamImpl::write(
|
||||
ConstPtr<ByteArray> data) {
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_data(data);
|
||||
|
||||
if (scoped_data->size() > kMaxDataSize) {
|
||||
NEARBY_LOG(WARNING, "Sending data larger than 1MB");
|
||||
return Exception::IO;
|
||||
}
|
||||
|
||||
socket_->BlockUntilSufficientSpaceInBuffer(scoped_data->size());
|
||||
|
||||
if (socket_->IsClosed()) {
|
||||
NEARBY_LOG(WARNING, "Tried sending message while socket is closed");
|
||||
return Exception::IO;
|
||||
}
|
||||
|
||||
if (!socket_->SendMessage(scoped_data.release())) {
|
||||
return Exception::IO;
|
||||
}
|
||||
return Exception::NONE;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Exception::Value WebRtcSocket<Platform>::OutputStreamImpl::flush() {
|
||||
// Java implementation is empty.
|
||||
return Exception::NONE;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Exception::Value WebRtcSocket<Platform>::OutputStreamImpl::close() {
|
||||
socket_->close();
|
||||
return Exception::NONE;
|
||||
}
|
||||
|
||||
// WebRtcSocket
|
||||
template <typename Platform>
|
||||
WebRtcSocket<Platform>::WebRtcSocket(
|
||||
const string& name,
|
||||
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel)
|
||||
: name_(name),
|
||||
data_channel_(std::move(data_channel)),
|
||||
pipe_(MakeRefCountedPtr(new Pipe())),
|
||||
incoming_data_piped_input_stream_(Pipe::createInputStream(pipe_)),
|
||||
incoming_data_piped_output_stream_(Pipe::createOutputStream(pipe_)),
|
||||
output_stream_(MakePtr(new OutputStreamImpl(this))),
|
||||
closed_(Platform::createAtomicBoolean(false)),
|
||||
backpressure_lock_(Platform::createLock()),
|
||||
buffer_variable_(
|
||||
Platform::createConditionVariable(backpressure_lock_.get())) {}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<InputStream> WebRtcSocket<Platform>::getInputStream() {
|
||||
return incoming_data_piped_input_stream_.get();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<OutputStream> WebRtcSocket<Platform>::getOutputStream() {
|
||||
return output_stream_.get();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WebRtcSocket<Platform>::close() {
|
||||
if (IsClosed()) return;
|
||||
|
||||
closed_->set(true);
|
||||
incoming_data_piped_output_stream_->close();
|
||||
incoming_data_piped_input_stream_->close();
|
||||
data_channel_->Close();
|
||||
WakeUpWriter();
|
||||
if (!socket_closed_listener_.isNull()) {
|
||||
socket_closed_listener_->OnSocketClosed();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WebRtcSocket<Platform>::NotifyDataChannelMsgReceived(
|
||||
ConstPtr<ByteArray> message) {
|
||||
Exception::Value exception =
|
||||
incoming_data_piped_output_stream_->write(message);
|
||||
if (exception != Exception::NONE) close();
|
||||
|
||||
exception = incoming_data_piped_output_stream_->flush();
|
||||
if (exception != Exception::NONE) close();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WebRtcSocket<Platform>::NotifyDataChannelBufferedAmountChanged() {
|
||||
WakeUpWriter();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool WebRtcSocket<Platform>::SendMessage(ConstPtr<ByteArray> data) {
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_data(data);
|
||||
return data_channel_->Send(webrtc::DataBuffer(
|
||||
std::string(scoped_data->getData(), scoped_data->size())));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool WebRtcSocket<Platform>::IsClosed() {
|
||||
return closed_->get();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WebRtcSocket<Platform>::WakeUpWriter() {
|
||||
Synchronized s(backpressure_lock_.get());
|
||||
buffer_variable_->notify();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WebRtcSocket<Platform>::SetOnSocketClosedListener(
|
||||
Ptr<SocketClosedListener> listener) {
|
||||
socket_closed_listener_ = listener;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WebRtcSocket<Platform>::BlockUntilSufficientSpaceInBuffer(int length) {
|
||||
Synchronized s(backpressure_lock_.get());
|
||||
while (!IsClosed() &&
|
||||
(data_channel_->buffered_amount() + length > kMaxDataSize)) {
|
||||
// TODO(himanshujaju): Add wait with timeout.
|
||||
buffer_variable_->wait();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,104 @@
|
||||
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_H_
|
||||
#define CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_H_
|
||||
|
||||
#include "platform/api/atomic_boolean.h"
|
||||
#include "platform/api/input_stream.h"
|
||||
#include "platform/api/output_stream.h"
|
||||
#include "platform/api/socket.h"
|
||||
#include "platform/pipe.h"
|
||||
#include "webrtc/api/data_channel_interface.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
// Maximum data size: 1 MB
|
||||
constexpr int kMaxDataSize = 1 * 1024 * 1024;
|
||||
|
||||
// Defines the Socket implementation specific to WebRTC, which uses the WebRTC
|
||||
// data channel to send and receive messages.
|
||||
//
|
||||
// Messages are buffered here to prevent the data channel from overflowing,
|
||||
// which could lead to data loss.
|
||||
template <typename Platform>
|
||||
class WebRtcSocket : public Socket {
|
||||
public:
|
||||
WebRtcSocket(const string& name,
|
||||
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel);
|
||||
~WebRtcSocket() override = default;
|
||||
|
||||
WebRtcSocket(const WebRtcSocket& other) = delete;
|
||||
WebRtcSocket& operator=(const WebRtcSocket& other) = delete;
|
||||
|
||||
// Overrides for location::nearby::Socket:
|
||||
Ptr<InputStream> getInputStream() override;
|
||||
Ptr<OutputStream> getOutputStream() override;
|
||||
void close() override;
|
||||
|
||||
// Callback from WebRTC data channel when new message has been received from
|
||||
// the remote.
|
||||
void NotifyDataChannelMsgReceived(ConstPtr<ByteArray> message);
|
||||
|
||||
// Callback from WebRTC data channel that the buffered data amount has
|
||||
// changed.
|
||||
void NotifyDataChannelBufferedAmountChanged();
|
||||
|
||||
// Listener class the gets called when the socket is closed.
|
||||
class SocketClosedListener {
|
||||
public:
|
||||
virtual ~SocketClosedListener() = default;
|
||||
virtual void OnSocketClosed() = 0;
|
||||
};
|
||||
void SetOnSocketClosedListener(Ptr<SocketClosedListener> listener);
|
||||
|
||||
private:
|
||||
class OutputStreamImpl : public OutputStream {
|
||||
public:
|
||||
explicit OutputStreamImpl(WebRtcSocket<Platform>* const socket)
|
||||
: socket_(socket) {}
|
||||
~OutputStreamImpl() override = default;
|
||||
|
||||
OutputStreamImpl(const OutputStreamImpl& other) = delete;
|
||||
OutputStreamImpl& operator=(const OutputStreamImpl& other) = delete;
|
||||
|
||||
// OutputStream:
|
||||
Exception::Value write(ConstPtr<ByteArray> data) override;
|
||||
Exception::Value flush() override;
|
||||
Exception::Value close() override;
|
||||
|
||||
private:
|
||||
// |this| OutputStreamImpl is owned by |socket_|.
|
||||
WebRtcSocket<Platform>* const socket_;
|
||||
};
|
||||
|
||||
void WakeUpWriter();
|
||||
bool IsClosed();
|
||||
bool SendMessage(ConstPtr<ByteArray> data);
|
||||
void BlockUntilSufficientSpaceInBuffer(int length);
|
||||
|
||||
string name_;
|
||||
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel_;
|
||||
|
||||
Ptr<Pipe> pipe_;
|
||||
ScopedPtr<Ptr<InputStream>> incoming_data_piped_input_stream_;
|
||||
ScopedPtr<Ptr<OutputStream>> incoming_data_piped_output_stream_;
|
||||
|
||||
ScopedPtr<Ptr<OutputStream>> output_stream_;
|
||||
|
||||
ScopedPtr<Ptr<AtomicBoolean>> closed_;
|
||||
|
||||
Ptr<SocketClosedListener> socket_closed_listener_;
|
||||
|
||||
ScopedPtr<Ptr<Lock>> backpressure_lock_;
|
||||
ScopedPtr<Ptr<ConditionVariable>> buffer_variable_;
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#include "core/internal/mediums/webrtc/webrtc_socket.cc"
|
||||
|
||||
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_H_
|
||||
@@ -0,0 +1,155 @@
|
||||
#include "core/internal/mediums/webrtc/webrtc_socket.h"
|
||||
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/byte_array.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "webrtc/api/data_channel_interface.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
namespace {
|
||||
|
||||
using TestPlatform = platform::ImplementationPlatform;
|
||||
|
||||
const char kSocketName[] = "TestSocket";
|
||||
|
||||
class MockDataChannel
|
||||
: public rtc::RefCountedObject<webrtc::DataChannelInterface> {
|
||||
public:
|
||||
MOCK_METHOD(void, RegisterObserver, (webrtc::DataChannelObserver*));
|
||||
MOCK_METHOD(void, UnregisterObserver, ());
|
||||
|
||||
MOCK_METHOD(std::string, label, (), (const));
|
||||
|
||||
MOCK_METHOD(bool, reliable, (), (const));
|
||||
MOCK_METHOD(int, id, (), (const));
|
||||
MOCK_METHOD(DataState, state, (), (const));
|
||||
MOCK_METHOD(uint32_t, messages_sent, (), (const));
|
||||
MOCK_METHOD(uint64_t, bytes_sent, (), (const));
|
||||
MOCK_METHOD(uint32_t, messages_received, (), (const));
|
||||
MOCK_METHOD(uint64_t, bytes_received, (), (const));
|
||||
|
||||
MOCK_METHOD(uint64_t, buffered_amount, (), (const));
|
||||
|
||||
MOCK_METHOD(void, Close, ());
|
||||
|
||||
MOCK_METHOD(bool, Send, (const webrtc::DataBuffer&));
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
class MockSocketClosedListener
|
||||
: public WebRtcSocket<TestPlatform>::SocketClosedListener {
|
||||
public:
|
||||
MOCK_METHOD(void, OnSocketClosed, ());
|
||||
};
|
||||
|
||||
TEST(WebRtcSocketTest, ReadFromSocket) {
|
||||
ConstPtr<ByteArray> kMessage = MakeConstPtr(new ByteArray("Message"));
|
||||
rtc::scoped_refptr<MockDataChannel> mock_data_channel = new MockDataChannel();
|
||||
WebRtcSocket<TestPlatform> webrtc_socket(kSocketName, mock_data_channel);
|
||||
|
||||
webrtc_socket.NotifyDataChannelMsgReceived(kMessage);
|
||||
ExceptionOr<ConstPtr<ByteArray>> result =
|
||||
webrtc_socket.getInputStream()->read();
|
||||
EXPECT_TRUE(result.ok());
|
||||
EXPECT_EQ(result.result(), kMessage);
|
||||
}
|
||||
|
||||
TEST(WebRtcSocketTest, ReadMultipleMessages) {
|
||||
rtc::scoped_refptr<MockDataChannel> mock_data_channel = new MockDataChannel();
|
||||
WebRtcSocket<TestPlatform> webrtc_socket(kSocketName, mock_data_channel);
|
||||
|
||||
webrtc_socket.NotifyDataChannelMsgReceived(MakeConstPtr(new ByteArray("Me")));
|
||||
webrtc_socket.NotifyDataChannelMsgReceived(
|
||||
MakeConstPtr(new ByteArray("ssa")));
|
||||
webrtc_socket.NotifyDataChannelMsgReceived(MakeConstPtr(new ByteArray("ge")));
|
||||
ExceptionOr<ConstPtr<ByteArray>> result;
|
||||
|
||||
// This behaviour is different from the Java code
|
||||
result = webrtc_socket.getInputStream()->read();
|
||||
EXPECT_TRUE(result.ok());
|
||||
EXPECT_EQ(result.result()->asString(), "Me");
|
||||
|
||||
result = webrtc_socket.getInputStream()->read();
|
||||
EXPECT_TRUE(result.ok());
|
||||
EXPECT_EQ(result.result()->asString(), "ssa");
|
||||
|
||||
result = webrtc_socket.getInputStream()->read();
|
||||
EXPECT_TRUE(result.ok());
|
||||
EXPECT_EQ(result.result()->asString(), "ge");
|
||||
}
|
||||
|
||||
TEST(WebRtcSocketTest, WriteToSocket) {
|
||||
ConstPtr<ByteArray> kMessage = MakeConstPtr(new ByteArray("Message"));
|
||||
rtc::scoped_refptr<MockDataChannel> mock_data_channel = new MockDataChannel();
|
||||
WebRtcSocket<TestPlatform> webrtc_socket(kSocketName, mock_data_channel);
|
||||
|
||||
EXPECT_CALL(*mock_data_channel, Send(testing::_))
|
||||
.WillRepeatedly(testing::Return(true));
|
||||
EXPECT_EQ(webrtc_socket.getOutputStream()->write(kMessage), Exception::NONE);
|
||||
}
|
||||
|
||||
TEST(WebRtcSocketTest, SendDataBiggerThanMax) {
|
||||
ConstPtr<ByteArray> kMessage = MakeConstPtr(new ByteArray(kMaxDataSize + 1));
|
||||
rtc::scoped_refptr<MockDataChannel> mock_data_channel = new MockDataChannel();
|
||||
WebRtcSocket<TestPlatform> webrtc_socket(kSocketName, mock_data_channel);
|
||||
|
||||
EXPECT_CALL(*mock_data_channel, Send(testing::_)).Times(0);
|
||||
EXPECT_EQ(webrtc_socket.getOutputStream()->write(kMessage), Exception::IO);
|
||||
}
|
||||
|
||||
TEST(WebRtcSocketTest, WriteToDataChannelFails) {
|
||||
ConstPtr<ByteArray> kMessage = MakeConstPtr(new ByteArray("Message"));
|
||||
rtc::scoped_refptr<MockDataChannel> mock_data_channel = new MockDataChannel();
|
||||
WebRtcSocket<TestPlatform> webrtc_socket(kSocketName, mock_data_channel);
|
||||
|
||||
ON_CALL(*mock_data_channel, Send(testing::_))
|
||||
.WillByDefault(testing::Return(false));
|
||||
EXPECT_EQ(webrtc_socket.getOutputStream()->write(kMessage), Exception::IO);
|
||||
}
|
||||
|
||||
TEST(WebRtcSocketTest, Close) {
|
||||
rtc::scoped_refptr<MockDataChannel> mock_data_channel = new MockDataChannel();
|
||||
ScopedPtr<Ptr<MockSocketClosedListener>> mock_listener(
|
||||
MakePtr(new MockSocketClosedListener()));
|
||||
WebRtcSocket<TestPlatform> webrtc_socket(kSocketName, mock_data_channel);
|
||||
webrtc_socket.SetOnSocketClosedListener(mock_listener.get());
|
||||
|
||||
EXPECT_CALL(*mock_listener, OnSocketClosed());
|
||||
EXPECT_CALL(*mock_data_channel, Close());
|
||||
webrtc_socket.close();
|
||||
}
|
||||
|
||||
TEST(WebRtcSocketTest, WriteOnClosedChannel) {
|
||||
ConstPtr<ByteArray> kMessage = MakeConstPtr(new ByteArray("Message"));
|
||||
rtc::scoped_refptr<MockDataChannel> mock_data_channel = new MockDataChannel();
|
||||
WebRtcSocket<TestPlatform> webrtc_socket(kSocketName, mock_data_channel);
|
||||
webrtc_socket.close();
|
||||
|
||||
EXPECT_CALL(*mock_data_channel, Send(testing::_)).Times(0);
|
||||
EXPECT_EQ(webrtc_socket.getOutputStream()->write(kMessage), Exception::IO);
|
||||
}
|
||||
|
||||
TEST(WebRtcSocketTest, ReadFromClosedChannel) {
|
||||
ConstPtr<ByteArray> kMessage = MakeConstPtr(new ByteArray("Message"));
|
||||
rtc::scoped_refptr<MockDataChannel> mock_data_channel = new MockDataChannel();
|
||||
WebRtcSocket<TestPlatform> webrtc_socket(kSocketName, mock_data_channel);
|
||||
ON_CALL(*mock_data_channel, Send(testing::_))
|
||||
.WillByDefault(testing::Return(true));
|
||||
|
||||
webrtc_socket.getOutputStream()->write(kMessage);
|
||||
webrtc_socket.close();
|
||||
|
||||
EXPECT_EQ(webrtc_socket.getInputStream()->read().exception(), Exception::IO);
|
||||
}
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,213 @@
|
||||
#include "core/internal/mediums/wifi_lan.h"
|
||||
|
||||
#include "platform/synchronized.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
template <typename Platform>
|
||||
WifiLan<Platform>::WifiLan()
|
||||
: lock_(Platform::createLock()),
|
||||
wifi_lan_medium_(Platform::createWifiLanMedium()) {}
|
||||
|
||||
template <typename Platform>
|
||||
bool WifiLan<Platform>::IsAvailable() {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
return !wifi_lan_medium_.isNull();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool WifiLan<Platform>::StartAdvertising(
|
||||
absl::string_view service_id,
|
||||
absl::string_view wifi_lan_service_info_name) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
if (!IsAvailable()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO(b/149806065): Implements platform wifi-lan medium.
|
||||
// wifi_lan_medium_->StartAdvertising(service_id,
|
||||
// wifi_lan_service_info_name));
|
||||
|
||||
advertising_info_.service_id.assign(service_id.data());
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WifiLan<Platform>::StopAdvertising(absl::string_view service_id) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
if (!IsAdvertising()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(b/149806065): Implements platform wifi-lan medium.
|
||||
// wifi_lan_medium_->StopAdvertising(advertising_info_.service_id);
|
||||
// Reset our bundle of advertising state to mark that we're no longer
|
||||
// advertising.
|
||||
advertising_info_.service_id.clear();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool WifiLan<Platform>::IsAdvertising() {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
return !advertising_info_.service_id.empty();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool WifiLan<Platform>::StartDiscovery(
|
||||
absl::string_view service_id,
|
||||
Ptr<DiscoveredServiceCallback> discovered_service_callback) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
if (discovered_service_callback.isNull() || service_id.empty()) {
|
||||
// TODO(b/149806065): logger.atSevere().log("Refusing to start WifiLan
|
||||
// discovering because a null parameter was passed in.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsDiscovering(service_id)) {
|
||||
// TODO(b/149806065): logger.atSevere().log("Refusing to start WifiLan
|
||||
// discovering because we are already discovering.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsAvailable()) {
|
||||
// TODO(b/149806065): logger.atSevere().log("Can't start WifiLan discovering
|
||||
// because WifiLan isn't available.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Avoid leaks.
|
||||
ScopedPtr<Ptr<DiscoveredServiceCallbackBridge>>
|
||||
scoped_discovered_service_callback_bridge(
|
||||
new DiscoveredServiceCallbackBridge(discovered_service_callback));
|
||||
|
||||
// TODO(b/149806065): Implements platform wifi-lan medium.
|
||||
// A possible implementation is:
|
||||
// wifi_lan_medium_->StartDiscovery(
|
||||
// service_id, Ptr<DiscoveredServiceCallbackBridge>(
|
||||
// discovered_service_callback_bridge.release()));
|
||||
|
||||
discovering_info_.service_id.assign(service_id.data());
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WifiLan<Platform>::StopDiscovery(absl::string_view service_id) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
if (!IsDiscovering(service_id)) {
|
||||
// TODO(b/149806065): logger.atDebug().log("Can't turn off WifiLan
|
||||
// discovering because we never started discovering.");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(b/149806065): Implements platform wifi-lan medium.
|
||||
// wifi_lan_medium_->StopDiscovery(discovering_info_.service_id);
|
||||
// Reset our bundle of scanning state to mark that we're no longer scanning.
|
||||
discovering_info_.service_id.clear();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool WifiLan<Platform>::IsDiscovering(absl::string_view service_id) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
return !discovering_info_.service_id.empty();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool WifiLan<Platform>::StartAcceptingConnections(
|
||||
absl::string_view service_id,
|
||||
Ptr<AcceptedConnectionCallback> accepted_connection_callback) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
if (accepted_connection_callback.isNull() || service_id.empty()) {
|
||||
// TODO(b/149806065): logger.atSevere().log("Refusing to start accepting
|
||||
// WifiLan connections because a null parameter was passed in.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsAcceptingConnections(service_id)) {
|
||||
// TODO(b/149806065): logger.atSevere().log("Refusing to start accepting
|
||||
// WifiLan connections for %s because another WifiLan service socket is
|
||||
// already in-progress.", service_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsAvailable()) {
|
||||
// TODO(b/149806065): logger.atSevere().log("Can't start accepting WifiLan
|
||||
// connections for %s because WifiLan isn't available.", serviceId);
|
||||
return false;
|
||||
}
|
||||
|
||||
ScopedPtr<Ptr<WifiLanAcceptedConnectionCallback>>
|
||||
scoped_wifi_lan_accepted_connection_callback(
|
||||
new WifiLanAcceptedConnectionCallback(
|
||||
accepted_connection_callback));
|
||||
|
||||
// TODO(b/149806065): Implements platform wifi-lan medium.
|
||||
// A possible implementation is:
|
||||
// wifi_lan_medium_->StartAcceptingConnections(
|
||||
// service_id, Ptr<WifiLanAcceptedConnectionCallback>(
|
||||
// wifi_lan_accepted_connection_callback.release()));
|
||||
|
||||
accepting_connections_info_.service_id.assign(service_id.data());
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void WifiLan<Platform>::StopAcceptingConnections(absl::string_view service_id) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
if (!IsAcceptingConnections(service_id)) {
|
||||
// TODO(b/149806065): logger.atDebug().log("Can't stop accepting WifiLan
|
||||
// connections because it was never started.");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(b/149806065): Implements platform wifi-lan medium.);
|
||||
// A possible implementation is:
|
||||
// wifi_lan_medium_->StopAcceptingConnections(
|
||||
// accepting_connections_info_.service_id);
|
||||
|
||||
// Reset our bundle of accepting connections state to mark that we're no
|
||||
// longer accepting connections.
|
||||
accepting_connections_info_.service_id.clear();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool WifiLan<Platform>::IsAcceptingConnections(absl::string_view service_id) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
return !accepting_connections_info_.service_id.empty();
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
Ptr<WifiLanSocket> WifiLan<Platform>::Connect(
|
||||
Ptr<WifiLanService> wifi_lan_service, absl::string_view service_id) {
|
||||
Synchronized s(lock_.get());
|
||||
|
||||
if (wifi_lan_service.isNull() || service_id.empty()) {
|
||||
return Ptr<WifiLanSocket>();
|
||||
}
|
||||
|
||||
if (!IsAvailable()) {
|
||||
return Ptr<WifiLanSocket>();
|
||||
}
|
||||
|
||||
// TODO(b/149806065): Implements platform wifi-lan medium.
|
||||
// A possible implementation is:
|
||||
// return wifi_lan_medium_->Connect(wifi_lan_service, service_id);
|
||||
return Ptr<WifiLanSocket>();
|
||||
}
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,160 @@
|
||||
#ifndef CORE_INTERNAL_MEDIUMS_WIFI_LAN_H_
|
||||
#define CORE_INTERNAL_MEDIUMS_WIFI_LAN_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "platform/api/lock.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/byte_array.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
class DiscoveredServiceCallback {
|
||||
public:
|
||||
virtual ~DiscoveredServiceCallback() = default;
|
||||
|
||||
virtual void OnServiceDiscovered(Ptr<WifiLanService> wifi_lan_service) = 0;
|
||||
virtual void OnServiceLost(Ptr<WifiLanService> wifi_lan_service) = 0;
|
||||
};
|
||||
|
||||
template <typename Platform>
|
||||
class WifiLan {
|
||||
public:
|
||||
WifiLan();
|
||||
virtual ~WifiLan() = default;
|
||||
|
||||
bool IsAvailable();
|
||||
|
||||
bool StartAdvertising(absl::string_view service_id,
|
||||
absl::string_view wifi_lan_service_info_name);
|
||||
void StopAdvertising(absl::string_view service_id);
|
||||
bool IsAdvertising();
|
||||
|
||||
bool StartDiscovery(
|
||||
absl::string_view service_id,
|
||||
Ptr<DiscoveredServiceCallback> discovered_service_callback);
|
||||
void StopDiscovery(absl::string_view service_id);
|
||||
bool IsDiscovering(absl::string_view service_id);
|
||||
|
||||
class AcceptedConnectionCallback {
|
||||
public:
|
||||
virtual ~AcceptedConnectionCallback() = default;
|
||||
|
||||
virtual void OnConnectionAccepted(Ptr<WifiLanSocket> socket,
|
||||
absl::string_view service_id) = 0;
|
||||
};
|
||||
|
||||
bool StartAcceptingConnections(
|
||||
absl::string_view service_id,
|
||||
Ptr<AcceptedConnectionCallback> accepted_connection_callback);
|
||||
void StopAcceptingConnections(absl::string_view service_id);
|
||||
bool IsAcceptingConnections(absl::string_view service_id);
|
||||
|
||||
Ptr<WifiLanSocket> Connect(Ptr<WifiLanService> wifi_lan_service,
|
||||
absl::string_view service_id);
|
||||
|
||||
private:
|
||||
class DiscoveredServiceCallbackBridge
|
||||
: public WifiLanMedium::DiscoveredServiceCallback {
|
||||
public:
|
||||
explicit DiscoveredServiceCallbackBridge(
|
||||
Ptr<mediums::DiscoveredServiceCallback> discovered_service_callback)
|
||||
: discovered_service_callback_(discovered_service_callback) {}
|
||||
~DiscoveredServiceCallbackBridge() override = default;
|
||||
|
||||
void OnServiceDiscovered(Ptr<WifiLanService> wifi_lan_service) override {
|
||||
discovered_service_callback_->OnServiceDiscovered(wifi_lan_service);
|
||||
}
|
||||
void OnServiceLost(Ptr<WifiLanService> wifi_lan_service) override {
|
||||
discovered_service_callback_->OnServiceLost(wifi_lan_service);
|
||||
}
|
||||
|
||||
private:
|
||||
ScopedPtr<Ptr<mediums::DiscoveredServiceCallback>>
|
||||
discovered_service_callback_;
|
||||
};
|
||||
|
||||
class WifiLanAcceptedConnectionCallback
|
||||
: public WifiLanMedium::AcceptedConnectionCallback {
|
||||
public:
|
||||
explicit WifiLanAcceptedConnectionCallback(
|
||||
Ptr<WifiLan::AcceptedConnectionCallback> accepted_connection_callback)
|
||||
: accepted_connection_callback_(accepted_connection_callback) {}
|
||||
~WifiLanAcceptedConnectionCallback() override = default;
|
||||
|
||||
void OnConnectionAccepted(Ptr<WifiLanSocket> wifi_lan_socket,
|
||||
absl::string_view service_id) override {
|
||||
accepted_connection_callback_->OnConnectionAccepted(wifi_lan_socket,
|
||||
service_id);
|
||||
}
|
||||
|
||||
private:
|
||||
ScopedPtr<Ptr<WifiLan::AcceptedConnectionCallback>>
|
||||
accepted_connection_callback_;
|
||||
};
|
||||
|
||||
struct DiscoveringInfo {
|
||||
DiscoveringInfo() = default;
|
||||
explicit DiscoveringInfo(absl::string_view service_id)
|
||||
: service_id(service_id) {}
|
||||
~DiscoveringInfo() = default;
|
||||
|
||||
string service_id;
|
||||
};
|
||||
|
||||
struct AdvertisingInfo {
|
||||
AdvertisingInfo() = default;
|
||||
explicit AdvertisingInfo(absl::string_view service_id)
|
||||
: service_id(service_id) {}
|
||||
~AdvertisingInfo() = default;
|
||||
|
||||
string service_id;
|
||||
};
|
||||
|
||||
struct AcceptingConnectionsInfo {
|
||||
AcceptingConnectionsInfo() = default;
|
||||
explicit AcceptingConnectionsInfo(absl::string_view service_id)
|
||||
: service_id(service_id) {}
|
||||
~AcceptingConnectionsInfo() = default;
|
||||
|
||||
string service_id;
|
||||
};
|
||||
|
||||
// ------------ GENERAL ------------
|
||||
|
||||
ScopedPtr<Ptr<Lock>> lock_;
|
||||
|
||||
// ---------- CORE WIFILAN------------
|
||||
|
||||
// The underlying, per-platform implementation.
|
||||
ScopedPtr<Ptr<WifiLanMedium>> wifi_lan_medium_;
|
||||
|
||||
// ------------ DISCOVERY ------------
|
||||
|
||||
// discovering_info_ is not scoped because it's nullable.
|
||||
DiscoveringInfo discovering_info_;
|
||||
|
||||
// ------------ ADVERTISING ------------
|
||||
|
||||
// A bundle of state required to start/stop WifiLan service publishing.
|
||||
AdvertisingInfo advertising_info_;
|
||||
|
||||
// A bundle of state required to start/stop accepting WifiLan service
|
||||
/// connections.
|
||||
AcceptingConnectionsInfo accepting_connections_info_;
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#include "core/internal/mediums/wifi_lan.cc"
|
||||
|
||||
#endif // CORE_INTERNAL_MEDIUMS_WIFI_LAN_H_
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef CORE_INTERNAL_MESSAGE_LITE_H_
|
||||
#define CORE_INTERNAL_MESSAGE_LITE_H_
|
||||
|
||||
#include "google/protobuf/message_lite.h"
|
||||
|
||||
#endif // CORE_INTERNAL_MESSAGE_LITE_H_
|
||||
@@ -61,7 +61,8 @@ ExceptionOrOfflineFrame OfflineFrames::fromBytes(
|
||||
ConstPtr<ByteArray> offline_frame_bytes) {
|
||||
auto offline_frame = std::make_unique<OfflineFrame>();
|
||||
|
||||
if (!offline_frame->ParseFromString(offline_frame_bytes->asString())) {
|
||||
if (!offline_frame->ParseFromArray(offline_frame_bytes->getData(),
|
||||
offline_frame_bytes->size())) {
|
||||
return ExceptionOrOfflineFrame(Exception::INVALID_PROTOCOL_BUFFER);
|
||||
}
|
||||
|
||||
@@ -78,6 +79,7 @@ V1Frame::FrameType OfflineFrames::getFrameType(
|
||||
return V1Frame::UNKNOWN_FRAME_TYPE;
|
||||
}
|
||||
|
||||
// TODO(b/155752436): Use byte array endpoint_info instead of endpoint_name.
|
||||
ConstPtr<ByteArray> OfflineFrames::forConnectionRequest(
|
||||
const std::string &endpoint_id, const std::string &endpoint_name,
|
||||
std::int32_t nonce,
|
||||
@@ -85,6 +87,7 @@ ConstPtr<ByteArray> OfflineFrames::forConnectionRequest(
|
||||
auto connection_request = std::make_unique<ConnectionRequestFrame>();
|
||||
connection_request->set_endpoint_id(endpoint_id);
|
||||
connection_request->set_endpoint_name(endpoint_name);
|
||||
connection_request->set_endpoint_info(endpoint_name);
|
||||
connection_request->set_nonce(nonce);
|
||||
|
||||
for (std::vector<proto::connections::Medium>::const_iterator it =
|
||||
|
||||
@@ -11,11 +11,11 @@ OfflineServiceController<Platform>::OfflineServiceController()
|
||||
: ServiceController<Platform>(),
|
||||
medium_manager_(new MediumManager<Platform>()),
|
||||
endpoint_channel_manager_(
|
||||
new EndpointChannelManager<Platform>(medium_manager_.get())),
|
||||
new EndpointChannelManager(medium_manager_.get())),
|
||||
endpoint_manager_(
|
||||
new EndpointManager<Platform>(endpoint_channel_manager_.get())),
|
||||
payload_manager_(new PayloadManager<Platform>(endpoint_manager_.get())),
|
||||
bandwidth_upgrade_manager_(new BandwidthUpgradeManager<Platform>(
|
||||
bandwidth_upgrade_manager_(new BandwidthUpgradeManager(
|
||||
medium_manager_.get(), endpoint_channel_manager_.get(),
|
||||
endpoint_manager_.get())),
|
||||
pcp_manager_(new PCPManager<Platform>(
|
||||
|
||||
@@ -68,11 +68,10 @@ class OfflineServiceController : public ServiceController<Platform> {
|
||||
// on the destructors running (strictly) in the reverse order; a deviation
|
||||
// from that will lead to crashes at runtime.
|
||||
ScopedPtr<Ptr<MediumManager<Platform> > > medium_manager_;
|
||||
ScopedPtr<Ptr<EndpointChannelManager<Platform> > > endpoint_channel_manager_;
|
||||
ScopedPtr<Ptr<EndpointChannelManager>> endpoint_channel_manager_;
|
||||
ScopedPtr<Ptr<EndpointManager<Platform> > > endpoint_manager_;
|
||||
ScopedPtr<Ptr<PayloadManager<Platform> > > payload_manager_;
|
||||
ScopedPtr<Ptr<BandwidthUpgradeManager<Platform> > >
|
||||
bandwidth_upgrade_manager_;
|
||||
ScopedPtr<Ptr<BandwidthUpgradeManager>> bandwidth_upgrade_manager_;
|
||||
ScopedPtr<Ptr<PCPManager<Platform> > > pcp_manager_;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "core/internal/p2p_cluster_pcp_handler.h"
|
||||
|
||||
#include "platform/api/hash_utils.h"
|
||||
|
||||
namespace location {
|
||||
@@ -16,6 +15,11 @@ const BLEAdvertisement::Version::Value
|
||||
P2PClusterPCPHandler<Platform>::kBleAdvertisementVersion =
|
||||
BLEAdvertisement::Version::V1;
|
||||
|
||||
template <typename Platform>
|
||||
const WifiLanServiceInfo::Version
|
||||
P2PClusterPCPHandler<Platform>::kWifiLanServiceInfoVersion =
|
||||
WifiLanServiceInfo::Version::kV1;
|
||||
|
||||
template <typename Platform>
|
||||
ConstPtr<ByteArray> P2PClusterPCPHandler<Platform>::generateHash(
|
||||
const string& source, size_t size) {
|
||||
@@ -35,8 +39,8 @@ template <typename Platform>
|
||||
P2PClusterPCPHandler<Platform>::P2PClusterPCPHandler(
|
||||
Ptr<MediumManager<Platform>> medium_manager,
|
||||
Ptr<EndpointManager<Platform>> endpoint_manager,
|
||||
Ptr<EndpointChannelManager<Platform>> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform>> bandwidth_upgrade_manager)
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager)
|
||||
: BasePCPHandler<Platform>(endpoint_manager, endpoint_channel_manager,
|
||||
bandwidth_upgrade_manager),
|
||||
medium_manager_(medium_manager) {}
|
||||
@@ -58,6 +62,9 @@ template <typename Platform>
|
||||
std::vector<proto::connections::Medium>
|
||||
P2PClusterPCPHandler<Platform>::getConnectionMediumsByPriority() {
|
||||
std::vector<proto::connections::Medium> mediums;
|
||||
if (medium_manager_->IsWifiLanAvailable()) {
|
||||
mediums.push_back(proto::connections::WIFI_LAN);
|
||||
}
|
||||
if (medium_manager_->isBluetoothAvailable()) {
|
||||
mediums.push_back(proto::connections::BLUETOOTH);
|
||||
}
|
||||
@@ -81,6 +88,15 @@ P2PClusterPCPHandler<Platform>::startAdvertisingImpl(
|
||||
const AdvertisingOptions& options) {
|
||||
std::vector<proto::connections::Medium> mediums_started_successfully;
|
||||
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_wifi_lan_service_id_hash(
|
||||
generateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength));
|
||||
proto::connections::Medium wifi_lan_medium = StartWifiLanAdvertising(
|
||||
client_proxy, service_id, scoped_wifi_lan_service_id_hash.get(),
|
||||
local_endpoint_id, local_endpoint_name);
|
||||
if (proto::connections::UNKNOWN_MEDIUM != wifi_lan_medium) {
|
||||
mediums_started_successfully.push_back(wifi_lan_medium);
|
||||
}
|
||||
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_bluetooth_service_id_hash(
|
||||
generateHash(service_id, BluetoothDeviceName::kServiceIdHashLength));
|
||||
proto::connections::Medium bluetooth_medium = startBluetoothAdvertising(
|
||||
@@ -118,10 +134,14 @@ Status::Value P2PClusterPCPHandler<Platform>::stopAdvertisingImpl(
|
||||
Ptr<ClientProxy<Platform>> client_proxy) {
|
||||
medium_manager_->stopBleAdvertising(client_proxy->getAdvertisingServiceId());
|
||||
medium_manager_->turnOffBluetoothDiscoverability();
|
||||
medium_manager_->StopWifiLanAdvertising(
|
||||
client_proxy->getAdvertisingServiceId());
|
||||
medium_manager_->stopListeningForIncomingBleConnections(
|
||||
client_proxy->getAdvertisingServiceId());
|
||||
medium_manager_->stopListeningForIncomingBluetoothConnections(
|
||||
client_proxy->getAdvertisingServiceId());
|
||||
medium_manager_->StopListeningForIncomingWifiLanConnections(
|
||||
client_proxy->getAdvertisingServiceId());
|
||||
return Status::SUCCESS;
|
||||
}
|
||||
|
||||
@@ -132,6 +152,14 @@ P2PClusterPCPHandler<Platform>::startDiscoveryImpl(
|
||||
const DiscoveryOptions& options) {
|
||||
std::vector<proto::connections::Medium> mediums_started_successfully;
|
||||
|
||||
proto::connections::Medium wifi_lan_medium =
|
||||
StartWifiLanDiscovery(MakePtr(new FoundWifiLanServiceProcessor(
|
||||
self_, client_proxy, service_id)),
|
||||
client_proxy, service_id);
|
||||
if (proto::connections::UNKNOWN_MEDIUM != wifi_lan_medium) {
|
||||
mediums_started_successfully.push_back(wifi_lan_medium);
|
||||
}
|
||||
|
||||
proto::connections::Medium bluetooth_medium =
|
||||
startBluetoothDiscovery(MakePtr(new FoundBluetoothAdvertisementProcessor(
|
||||
self_, client_proxy, service_id)),
|
||||
@@ -170,6 +198,12 @@ typename BasePCPHandler<Platform>::ConnectImplResult
|
||||
P2PClusterPCPHandler<Platform>::connectImpl(
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<typename BasePCPHandler<Platform>::DiscoveredEndpoint> endpoint) {
|
||||
Ptr<WifiLanEndpoint> wifi_lan_endpoint =
|
||||
DowncastPtr<WifiLanEndpoint>(endpoint);
|
||||
if (!wifi_lan_endpoint.isNull()) {
|
||||
return WifiLanConnectImpl(client_proxy, wifi_lan_endpoint);
|
||||
}
|
||||
|
||||
Ptr<BluetoothEndpoint> bluetooth_endpoint =
|
||||
DowncastPtr<BluetoothEndpoint>(endpoint);
|
||||
if (!bluetooth_endpoint.isNull()) {
|
||||
@@ -295,6 +329,60 @@ void P2PClusterPCPHandler<Platform>::IncomingBleConnectionProcessor::
|
||||
proto::connections::Medium::BLE);
|
||||
}
|
||||
|
||||
//////////// P2PClusterPCPHandler::IncomingWifiLanConnectionProcessor /////////
|
||||
template <typename Platform>
|
||||
P2PClusterPCPHandler<Platform>::IncomingWifiLanConnectionProcessor::
|
||||
IncomingWifiLanConnectionProcessor(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
absl::string_view local_endpoint_name)
|
||||
: pcp_handler_(pcp_handler),
|
||||
client_proxy_(client_proxy),
|
||||
local_endpoint_name_(local_endpoint_name) {}
|
||||
|
||||
template <typename Platform>
|
||||
void P2PClusterPCPHandler<Platform>::IncomingWifiLanConnectionProcessor::
|
||||
OnIncomingWifiLanConnection(Ptr<WifiLanSocket> wifi_lan_socket) {
|
||||
pcp_handler_->runOnPCPHandlerThread(
|
||||
MakePtr(new OnIncomingWifiLanConnectionRunnable(
|
||||
pcp_handler_, client_proxy_, wifi_lan_socket)));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
P2PClusterPCPHandler<Platform>::IncomingWifiLanConnectionProcessor::
|
||||
OnIncomingWifiLanConnectionRunnable::OnIncomingWifiLanConnectionRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<WifiLanSocket> wifi_lan_socket)
|
||||
: pcp_handler_(pcp_handler),
|
||||
client_proxy_(client_proxy),
|
||||
wifi_lan_socket_(wifi_lan_socket) {}
|
||||
|
||||
template <typename Platform>
|
||||
void P2PClusterPCPHandler<Platform>::IncomingWifiLanConnectionProcessor::
|
||||
OnIncomingWifiLanConnectionRunnable::run() {
|
||||
string remote_service_name =
|
||||
wifi_lan_socket_->GetRemoteWifiLanService()->GetName();
|
||||
ScopedPtr<Ptr<EndpointChannel>> scoped_wifi_lan_endpoint_channel(
|
||||
pcp_handler_->endpoint_channel_manager_
|
||||
->CreateIncomingWifiLanEndpointChannel(remote_service_name,
|
||||
wifi_lan_socket_));
|
||||
if (!scoped_wifi_lan_endpoint_channel.isNull()) {
|
||||
// TODO(b/149806065): Add logging.
|
||||
} else {
|
||||
Exception::Value exception = wifi_lan_socket_->Close();
|
||||
wifi_lan_socket_.destroy();
|
||||
if (Exception::NONE != exception) {
|
||||
if (Exception::IO == exception) {
|
||||
// TODO(b/149806065): Add logging.
|
||||
}
|
||||
}
|
||||
}
|
||||
pcp_handler_->onIncomingConnection(client_proxy_, remote_service_name,
|
||||
scoped_wifi_lan_endpoint_channel.release(),
|
||||
proto::connections::Medium::WIFI_LAN);
|
||||
}
|
||||
|
||||
///////// P2PClusterPCPHandler::FoundBluetoothAdvertisementProcessor //////////
|
||||
template <typename Platform>
|
||||
P2PClusterPCPHandler<Platform>::FoundBluetoothAdvertisementProcessor::
|
||||
@@ -582,6 +670,137 @@ void P2PClusterPCPHandler<Platform>::FoundBleAdvertisementProcessor::
|
||||
}
|
||||
}
|
||||
|
||||
////////// P2PClusterPCPHandler::FoundWifiLanServiceProcessor ///////////
|
||||
template <typename Platform>
|
||||
P2PClusterPCPHandler<Platform>::FoundWifiLanServiceProcessor::
|
||||
FoundWifiLanServiceProcessor(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy, absl::string_view service_id)
|
||||
: pcp_handler_(pcp_handler),
|
||||
client_proxy_(client_proxy),
|
||||
service_id_(service_id),
|
||||
expected_service_id_hash_(generateHash(
|
||||
string(service_id), WifiLanServiceInfo::kServiceIdHashLength)) {}
|
||||
|
||||
template <typename Platform>
|
||||
void P2PClusterPCPHandler<Platform>::FoundWifiLanServiceProcessor::
|
||||
OnFoundWifiLanService(Ptr<WifiLanService> wifi_lan_service) {
|
||||
pcp_handler_->runOnPCPHandlerThread(MakePtr(new OnFoundWifiLanServiceRunnable(
|
||||
pcp_handler_, client_proxy_, self_, service_id_, wifi_lan_service)));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
void P2PClusterPCPHandler<Platform>::FoundWifiLanServiceProcessor::
|
||||
OnLostWifiLanService(Ptr<WifiLanService> wifi_lan_service) {
|
||||
pcp_handler_->runOnPCPHandlerThread(MakePtr(new OnLostWifiLanServiceRunnable(
|
||||
pcp_handler_, client_proxy_, self_, service_id_, wifi_lan_service)));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
bool P2PClusterPCPHandler<Platform>::FoundWifiLanServiceProcessor::
|
||||
IsRecognizedWifiLanEndpoint(Ptr<WifiLanServiceInfo> wifi_lan_service_info) {
|
||||
if (wifi_lan_service_info.isNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wifi_lan_service_info->GetPcp() != pcp_handler_->getPCP()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (*(wifi_lan_service_info->GetServiceIdHash()) !=
|
||||
*(expected_service_id_hash_.get())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
P2PClusterPCPHandler<Platform>::FoundWifiLanServiceProcessor::
|
||||
OnFoundWifiLanServiceRunnable::OnFoundWifiLanServiceRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor,
|
||||
absl::string_view service_id, Ptr<WifiLanService> wifi_lan_service)
|
||||
: pcp_handler_(pcp_handler),
|
||||
client_proxy_(client_proxy),
|
||||
found_wifi_lan_service_processor_(found_wifi_lan_service_processor),
|
||||
service_id_(service_id),
|
||||
wifi_lan_service_(wifi_lan_service),
|
||||
expected_service_id_hash_(generateHash(
|
||||
string(service_id), WifiLanServiceInfo::kServiceIdHashLength)) {}
|
||||
|
||||
template <typename Platform>
|
||||
void P2PClusterPCPHandler<Platform>::FoundWifiLanServiceProcessor::
|
||||
OnFoundWifiLanServiceRunnable::run() {
|
||||
// Make sure we are still discovering before proceeding.
|
||||
if (!client_proxy_->isDiscovering()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the WifiLan service name.
|
||||
ScopedPtr<Ptr<WifiLanServiceInfo>> wifi_lan_service_info(
|
||||
WifiLanServiceInfo::FromString(wifi_lan_service_->GetName()));
|
||||
|
||||
// Make sure the WifiLan service name points to a valid endpoint we're
|
||||
// discovering.
|
||||
if (!found_wifi_lan_service_processor_->IsRecognizedWifiLanEndpoint(
|
||||
wifi_lan_service_info.get())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Report the discovered endpoint to the client.
|
||||
pcp_handler_->onEndpointFound(
|
||||
client_proxy_,
|
||||
MakePtr(new WifiLanEndpoint(
|
||||
wifi_lan_service_.release(),
|
||||
wifi_lan_service_info->GetEndpointId(),
|
||||
wifi_lan_service_info->GetEndpointName(), service_id_)));
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
P2PClusterPCPHandler<Platform>::FoundWifiLanServiceProcessor::
|
||||
OnLostWifiLanServiceRunnable::OnLostWifiLanServiceRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor,
|
||||
absl::string_view service_id, Ptr<WifiLanService> wifi_lan_service)
|
||||
: pcp_handler_(pcp_handler),
|
||||
client_proxy_(client_proxy),
|
||||
found_wifi_lan_service_processor_(found_wifi_lan_service_processor),
|
||||
service_id_(service_id),
|
||||
wifi_lan_service_(wifi_lan_service.operator->()) {}
|
||||
|
||||
template <typename Platform>
|
||||
void P2PClusterPCPHandler<Platform>::FoundWifiLanServiceProcessor::
|
||||
OnLostWifiLanServiceRunnable::run() {
|
||||
// Make sure we are still discovering before proceeding.
|
||||
if (!client_proxy_->isDiscovering()) {
|
||||
// TODO(b/149806065): Add logging.
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the WifiLan service name.
|
||||
ScopedPtr<Ptr<WifiLanServiceInfo>> wifi_lan_service_info(
|
||||
WifiLanServiceInfo::FromString(wifi_lan_service_->GetName()));
|
||||
|
||||
// Make sure the WifiLan service name points to a valid endpoint we're
|
||||
// discovering.
|
||||
if (!found_wifi_lan_service_processor_->IsRecognizedWifiLanEndpoint(
|
||||
wifi_lan_service_info.get())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Report the endpoint as lost to the client.
|
||||
// TODO(b/149806065): Add logging.
|
||||
pcp_handler_->onEndpointLost(
|
||||
client_proxy_,
|
||||
MakePtr(new WifiLanEndpoint(
|
||||
Ptr<WifiLanService>(wifi_lan_service_.release()),
|
||||
wifi_lan_service_info->GetEndpointId(),
|
||||
wifi_lan_service_info->GetEndpointName(), service_id_)));
|
||||
}
|
||||
|
||||
//////////////////// END IMPLEMENTATIONS FOR NESTED CLASSES ////////////////////
|
||||
|
||||
template <typename Platform>
|
||||
@@ -710,6 +929,65 @@ proto::connections::Medium P2PClusterPCPHandler<Platform>::startBleDiscovery(
|
||||
return proto::connections::BLE;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
proto::connections::Medium
|
||||
P2PClusterPCPHandler<Platform>::StartWifiLanAdvertising(
|
||||
Ptr<ClientProxy<Platform>> client_proxy, absl::string_view service_id,
|
||||
ConstPtr<ByteArray> service_id_hash, absl::string_view local_endpoint_id,
|
||||
absl::string_view local_endpoint_name) {
|
||||
// Start listening for connections before advertising in case a connection
|
||||
// request comes in very quickly.
|
||||
if (!medium_manager_->IsListeningForIncomingWifiLanConnections(service_id)) {
|
||||
if (!medium_manager_->StartListeningForIncomingWifiLanConnections(
|
||||
service_id, MakePtr(new IncomingWifiLanConnectionProcessor(
|
||||
self_, client_proxy, local_endpoint_name)))) {
|
||||
// TODO(b/149806065): logger.atWarning().log("In
|
||||
// StartWifiLanAdvertising(%s), client %d failed to start listening for
|
||||
// incoming WifiLan connections to ServiceId %s", local_endpoint_name,
|
||||
// clientProxy.getClientId(), service_id);
|
||||
return proto::connections::UNKNOWN_MEDIUM;
|
||||
}
|
||||
|
||||
// TODO(b/149806065): Add logging.
|
||||
}
|
||||
|
||||
// Generate a WifiLanServiceInfo.
|
||||
const string wifi_lan_service_info =
|
||||
WifiLanServiceInfo::AsString(kWifiLanServiceInfoVersion,
|
||||
getPCP(),
|
||||
local_endpoint_id,
|
||||
service_id_hash);
|
||||
if (wifi_lan_service_info.empty()) {
|
||||
// TODO(b/149806065): Add logging.
|
||||
return proto::connections::UNKNOWN_MEDIUM;
|
||||
} else {
|
||||
// TODO(b/149806065): Add logging.
|
||||
}
|
||||
|
||||
// TODO(b/149806065): Add logging
|
||||
|
||||
if (!medium_manager_->StartWifiLanAdvertising(
|
||||
service_id, wifi_lan_service_info)) {
|
||||
// TODO(b/149806065): Add logging
|
||||
medium_manager_->StopWifiLanAdvertising(service_id);
|
||||
return proto::connections::UNKNOWN_MEDIUM;
|
||||
}
|
||||
return proto::connections::WIFI_LAN;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
proto::connections::Medium
|
||||
P2PClusterPCPHandler<Platform>::StartWifiLanDiscovery(
|
||||
Ptr<FoundWifiLanServiceProcessor> processor,
|
||||
Ptr<ClientProxy<Platform> > client_proxy, absl::string_view service_id) {
|
||||
if (!medium_manager_->StartWifiLanDiscovery(service_id, processor)) {
|
||||
// TODO(b/149806065): Add logging.
|
||||
return proto::connections::UNKNOWN_MEDIUM;
|
||||
}
|
||||
|
||||
return proto::connections::WIFI_LAN;
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
typename BasePCPHandler<Platform>::ConnectImplResult
|
||||
P2PClusterPCPHandler<Platform>::bluetoothConnectImpl(
|
||||
@@ -783,6 +1061,38 @@ string P2PClusterPCPHandler<Platform>::getBlePeripheralId(
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename Platform>
|
||||
typename BasePCPHandler<Platform>::ConnectImplResult
|
||||
P2PClusterPCPHandler<Platform>::WifiLanConnectImpl(
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<WifiLanEndpoint> wifi_lan_endpoint) {
|
||||
Ptr<WifiLanService> remote_wifi_lan_service =
|
||||
wifi_lan_endpoint->GetWifiLanService();
|
||||
|
||||
Ptr<WifiLanSocket> wifi_lan_socket = medium_manager_->ConnectToWifiLanService(
|
||||
remote_wifi_lan_service, wifi_lan_endpoint->getServiceId());
|
||||
|
||||
if (wifi_lan_socket.isNull()) {
|
||||
return typename BasePCPHandler<Platform>::ConnectImplResult(
|
||||
proto::connections::Medium::WIFI_LAN, Status::BLUETOOTH_ERROR);
|
||||
}
|
||||
|
||||
ScopedPtr<Ptr<EndpointChannel>> scoped_wifi_lan_endpoint_channel(
|
||||
this->endpoint_channel_manager_->CreateOutgoingWifiLanEndpointChannel(
|
||||
wifi_lan_endpoint->getEndpointId(), wifi_lan_socket));
|
||||
|
||||
if (scoped_wifi_lan_endpoint_channel.isNull()) {
|
||||
wifi_lan_socket->Close();
|
||||
wifi_lan_socket.destroy(); // Avoid leaks.
|
||||
return typename BasePCPHandler<Platform>::ConnectImplResult(
|
||||
proto::connections::Medium::WIFI_LAN, Status::ERROR);
|
||||
}
|
||||
|
||||
// TODO(b/149806065): Add logging.
|
||||
return typename BasePCPHandler<Platform>::ConnectImplResult(
|
||||
scoped_wifi_lan_endpoint_channel.release());
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "core/internal/endpoint_manager.h"
|
||||
#include "core/internal/medium_manager.h"
|
||||
#include "core/internal/pcp.h"
|
||||
#include "core/internal/wifi_lan_service_info.h"
|
||||
#include "core/options.h"
|
||||
#include "core/strategy.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
@@ -35,11 +36,10 @@ namespace connections {
|
||||
template <typename Platform>
|
||||
class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
public:
|
||||
P2PClusterPCPHandler(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager);
|
||||
P2PClusterPCPHandler(Ptr<MediumManager<Platform>> medium_manager,
|
||||
Ptr<EndpointManager<Platform>> endpoint_manager,
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager);
|
||||
~P2PClusterPCPHandler() override;
|
||||
|
||||
Strategy getStrategy() override;
|
||||
@@ -52,27 +52,29 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
|
||||
// @PCPHandlerThread
|
||||
Ptr<typename BasePCPHandler<Platform>::StartOperationResult>
|
||||
startAdvertisingImpl(Ptr<ClientProxy<Platform> > client_proxy,
|
||||
startAdvertisingImpl(Ptr<ClientProxy<Platform>> client_proxy,
|
||||
const string& service_id,
|
||||
const string& local_endpoint_id,
|
||||
const string& local_endpoint_name,
|
||||
const AdvertisingOptions& options) override;
|
||||
|
||||
// @PCPHandlerThread
|
||||
Status::Value stopAdvertisingImpl(
|
||||
Ptr<ClientProxy<Platform> > client_proxy) override;
|
||||
Ptr<ClientProxy<Platform>> client_proxy) override;
|
||||
|
||||
// @PCPHandlerThread
|
||||
Ptr<typename BasePCPHandler<Platform>::StartOperationResult>
|
||||
startDiscoveryImpl(Ptr<ClientProxy<Platform> > client_proxy,
|
||||
startDiscoveryImpl(Ptr<ClientProxy<Platform>> client_proxy,
|
||||
const string& service_id,
|
||||
const DiscoveryOptions& options) override;
|
||||
|
||||
// @PCPHandlerThread
|
||||
Status::Value stopDiscoveryImpl(
|
||||
Ptr<ClientProxy<Platform> > client_proxy) override;
|
||||
Ptr<ClientProxy<Platform>> client_proxy) override;
|
||||
|
||||
// @PCPHandlerThread
|
||||
typename BasePCPHandler<Platform>::ConnectImplResult connectImpl(
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<typename BasePCPHandler<Platform>::DiscoveredEndpoint> endpoint)
|
||||
override;
|
||||
|
||||
@@ -82,16 +84,20 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
template <typename>
|
||||
friend class IncomingBleConnectionProcessor;
|
||||
template <typename>
|
||||
friend class IncomingWifiLanConnectionProcessor;
|
||||
template <typename>
|
||||
friend class FoundBluetoothAdvertisementProcessor;
|
||||
template <typename>
|
||||
friend class FoundBleAdvertisementProcessor;
|
||||
template <typename>
|
||||
friend class FoundWifiLanServiceProcessor;
|
||||
|
||||
class IncomingBluetoothConnectionProcessor
|
||||
: public MediumManager<Platform>::IncomingBluetoothConnectionProcessor {
|
||||
public:
|
||||
IncomingBluetoothConnectionProcessor(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
const string& local_endpoint_name);
|
||||
|
||||
void onIncomingBluetoothConnection(
|
||||
@@ -101,20 +107,20 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
class OnIncomingBluetoothConnectionRunnable : public Runnable {
|
||||
public:
|
||||
OnIncomingBluetoothConnectionRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<BluetoothSocket> bluetooth_socket);
|
||||
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<BluetoothSocket> bluetooth_socket_;
|
||||
};
|
||||
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
const string local_endpoint_name_;
|
||||
};
|
||||
|
||||
@@ -122,8 +128,8 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
: public MediumManager<Platform>::IncomingBleConnectionProcessor {
|
||||
public:
|
||||
IncomingBleConnectionProcessor(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
const string& local_endpoint_name);
|
||||
|
||||
void onIncomingBleConnection(Ptr<BLESocket> ble_socket,
|
||||
@@ -133,19 +139,51 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
class OnIncomingBleConnectionRunnable : public Runnable {
|
||||
public:
|
||||
OnIncomingBleConnectionRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy, Ptr<BLESocket> ble_socket);
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy, Ptr<BLESocket> ble_socket);
|
||||
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<BLESocket> ble_socket_;
|
||||
};
|
||||
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
const string local_endpoint_name_;
|
||||
};
|
||||
|
||||
class IncomingWifiLanConnectionProcessor
|
||||
: public MediumManager<Platform>::IncomingWifiLanConnectionProcessor {
|
||||
public:
|
||||
IncomingWifiLanConnectionProcessor(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
absl::string_view local_endpoint_name);
|
||||
|
||||
void OnIncomingWifiLanConnection(
|
||||
Ptr<WifiLanSocket> wifi_lan_socket) override;
|
||||
|
||||
private:
|
||||
class OnIncomingWifiLanConnectionRunnable : public Runnable {
|
||||
public:
|
||||
OnIncomingWifiLanConnectionRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<WifiLanSocket> wifi_lan_socket);
|
||||
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<WifiLanSocket> wifi_lan_socket_;
|
||||
};
|
||||
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
const string local_endpoint_name_;
|
||||
};
|
||||
|
||||
@@ -153,8 +191,8 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
: public MediumManager<Platform>::FoundBluetoothDeviceProcessor {
|
||||
public:
|
||||
FoundBluetoothAdvertisementProcessor(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& service_id);
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy, const string& service_id);
|
||||
|
||||
void onFoundBluetoothDevice(Ptr<BluetoothDevice> bluetooth_device) override;
|
||||
void onLostBluetoothDevice(Ptr<BluetoothDevice> bluetooth_device) override;
|
||||
@@ -163,8 +201,8 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
class OnFoundBluetoothDeviceRunnable : public Runnable {
|
||||
public:
|
||||
OnFoundBluetoothDeviceRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<FoundBluetoothAdvertisementProcessor>
|
||||
found_bluetooth_advertisement_processor,
|
||||
const string& service_id, Ptr<BluetoothDevice> bluetooth_device);
|
||||
@@ -172,19 +210,19 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<FoundBluetoothAdvertisementProcessor>
|
||||
found_bluetooth_advertisement_processor_;
|
||||
const string service_id_;
|
||||
ScopedPtr<Ptr<BluetoothDevice> > bluetooth_device_;
|
||||
ScopedPtr<Ptr<BluetoothDevice>> bluetooth_device_;
|
||||
};
|
||||
|
||||
class OnLostBluetoothDeviceRunnable : public Runnable {
|
||||
public:
|
||||
OnLostBluetoothDeviceRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<FoundBluetoothAdvertisementProcessor>
|
||||
found_bluetooth_advertisement_processor,
|
||||
const string& service_id, Ptr<BluetoothDevice> bluetooth_device);
|
||||
@@ -192,22 +230,22 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<FoundBluetoothAdvertisementProcessor>
|
||||
found_bluetooth_advertisement_processor_;
|
||||
const string service_id_;
|
||||
ScopedPtr<Ptr<BluetoothDevice> > bluetooth_device_;
|
||||
ScopedPtr<Ptr<BluetoothDevice>> bluetooth_device_;
|
||||
};
|
||||
|
||||
bool isRecognizedBluetoothEndpoint(
|
||||
const string& found_bluetooth_device_name,
|
||||
Ptr<BluetoothDeviceName> bluetooth_device_name);
|
||||
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
const string service_id_;
|
||||
ScopedPtr<ConstPtr<ByteArray> > expected_service_id_hash_;
|
||||
ScopedPtr<ConstPtr<ByteArray>> expected_service_id_hash_;
|
||||
std::shared_ptr<FoundBluetoothAdvertisementProcessor> self_{this,
|
||||
[](void*) {}};
|
||||
};
|
||||
@@ -216,8 +254,8 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
: public MediumManager<Platform>::FoundBlePeripheralProcessor {
|
||||
public:
|
||||
FoundBleAdvertisementProcessor(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy);
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy);
|
||||
|
||||
void onFoundBlePeripheral(Ptr<BLE_PERIPHERAL> ble_peripheral,
|
||||
const string& service_id,
|
||||
@@ -229,8 +267,8 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
class OnFoundBlePeripheralRunnable : public Runnable {
|
||||
public:
|
||||
OnFoundBlePeripheralRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<FoundBleAdvertisementProcessor> found_ble_advertisement_processor,
|
||||
const string& service_id, Ptr<BLE_PERIPHERAL> ble_peripheral,
|
||||
ConstPtr<ByteArray> advertisement_bytes);
|
||||
@@ -238,31 +276,31 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<FoundBleAdvertisementProcessor> found_ble_advertisement_processor_;
|
||||
const string service_id_;
|
||||
ScopedPtr<Ptr<BLE_PERIPHERAL> > ble_peripheral_;
|
||||
ScopedPtr<ConstPtr<ByteArray> > advertisement_bytes_;
|
||||
ScopedPtr<ConstPtr<ByteArray> > expected_service_id_hash_;
|
||||
ScopedPtr<Ptr<BLE_PERIPHERAL>> ble_peripheral_;
|
||||
ScopedPtr<ConstPtr<ByteArray>> advertisement_bytes_;
|
||||
ScopedPtr<ConstPtr<ByteArray>> expected_service_id_hash_;
|
||||
};
|
||||
|
||||
class OnLostBlePeripheralRunnable : public Runnable {
|
||||
public:
|
||||
OnLostBlePeripheralRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler,
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<FoundBleAdvertisementProcessor> found_ble_advertisement_processor,
|
||||
const string& service_id, Ptr<BLE_PERIPHERAL> ble_peripheral);
|
||||
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<FoundBleAdvertisementProcessor> found_ble_advertisement_processor_;
|
||||
const string service_id_;
|
||||
ScopedPtr<Ptr<BLE_PERIPHERAL> > ble_peripheral_;
|
||||
ScopedPtr<Ptr<BLE_PERIPHERAL>> ble_peripheral_;
|
||||
};
|
||||
|
||||
// Holds the state required to re-create a BLEEndpoint we see on a
|
||||
@@ -278,14 +316,74 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
const string endpoint_name;
|
||||
};
|
||||
|
||||
Ptr<P2PClusterPCPHandler<Platform> > pcp_handler_;
|
||||
Ptr<ClientProxy<Platform> > client_proxy_;
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
|
||||
// Maps a BLEPeripheral to its corresponding BLEEndpointState.
|
||||
typedef std::map<string, BLEEndpointState> FoundBLEEndpointsMap;
|
||||
FoundBLEEndpointsMap found_ble_endpoints_;
|
||||
std::shared_ptr<FoundBleAdvertisementProcessor> self_{this, [](void*) {}};
|
||||
};
|
||||
|
||||
class FoundWifiLanServiceProcessor
|
||||
: public MediumManager<Platform>::FoundWifiLanServiceProcessor {
|
||||
public:
|
||||
FoundWifiLanServiceProcessor(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
absl::string_view service_id);
|
||||
|
||||
void OnFoundWifiLanService(Ptr<WifiLanService> wifi_lan_service) override;
|
||||
void OnLostWifiLanService(Ptr<WifiLanService> wifi_lan_service) override;
|
||||
|
||||
private:
|
||||
class OnFoundWifiLanServiceRunnable : public Runnable {
|
||||
public:
|
||||
OnFoundWifiLanServiceRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor,
|
||||
absl::string_view service_id, Ptr<WifiLanService> wifi_lan_service);
|
||||
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor_;
|
||||
const string service_id_;
|
||||
ScopedPtr<Ptr<WifiLanService>> wifi_lan_service_;
|
||||
ScopedPtr<ConstPtr<ByteArray>> expected_service_id_hash_;
|
||||
};
|
||||
|
||||
class OnLostWifiLanServiceRunnable : public Runnable {
|
||||
public:
|
||||
OnLostWifiLanServiceRunnable(
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor,
|
||||
absl::string_view service_id, Ptr<WifiLanService> wifi_lan_service);
|
||||
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
Ptr<FoundWifiLanServiceProcessor> found_wifi_lan_service_processor_;
|
||||
const string service_id_;
|
||||
ScopedPtr<Ptr<WifiLanService>> wifi_lan_service_;
|
||||
};
|
||||
|
||||
bool IsRecognizedWifiLanEndpoint(
|
||||
Ptr<WifiLanServiceInfo> wifi_lan_service_info);
|
||||
|
||||
Ptr<P2PClusterPCPHandler<Platform>> pcp_handler_;
|
||||
Ptr<ClientProxy<Platform>> client_proxy_;
|
||||
const string service_id_;
|
||||
ScopedPtr<ConstPtr<ByteArray>> expected_service_id_hash_;
|
||||
std::shared_ptr<FoundWifiLanServiceProcessor> self_{this, [](void*) {}};
|
||||
};
|
||||
|
||||
class BluetoothEndpoint
|
||||
: public BasePCPHandler<Platform>::DiscoveredEndpoint {
|
||||
public:
|
||||
@@ -310,7 +408,7 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
|
||||
friend class FoundBluetoothAdvertisementProcessor;
|
||||
|
||||
ScopedPtr<Ptr<BluetoothDevice> > bluetooth_device_;
|
||||
ScopedPtr<Ptr<BluetoothDevice>> bluetooth_device_;
|
||||
const string endpoint_id_;
|
||||
const string endpoint_name_;
|
||||
const string service_id_;
|
||||
@@ -336,7 +434,35 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
|
||||
friend class FoundBleAdvertisementProcessor;
|
||||
|
||||
ScopedPtr<Ptr<BLE_PERIPHERAL> > ble_peripheral_;
|
||||
ScopedPtr<Ptr<BLE_PERIPHERAL>> ble_peripheral_;
|
||||
const string endpoint_id_;
|
||||
const string endpoint_name_;
|
||||
const string service_id_;
|
||||
};
|
||||
|
||||
class WifiLanEndpoint : public BasePCPHandler<Platform>::DiscoveredEndpoint {
|
||||
public:
|
||||
Ptr<WifiLanService> GetWifiLanService() { return wifi_lan_service_.get(); }
|
||||
string getEndpointId() override { return endpoint_id_; }
|
||||
string getEndpointName() override { return endpoint_name_; }
|
||||
string getServiceId() override { return service_id_; }
|
||||
proto::connections::Medium getMedium() override {
|
||||
return proto::connections::Medium::WIFI_LAN;
|
||||
}
|
||||
|
||||
private:
|
||||
WifiLanEndpoint(Ptr<WifiLanService> wifi_lan_service,
|
||||
absl::string_view endpoint_id,
|
||||
absl::string_view endpoint_name,
|
||||
absl::string_view service_id)
|
||||
: wifi_lan_service_(wifi_lan_service),
|
||||
endpoint_id_(endpoint_id),
|
||||
endpoint_name_(endpoint_name),
|
||||
service_id_(service_id) {}
|
||||
|
||||
friend class FoundWifiLanServiceProcessor;
|
||||
|
||||
ScopedPtr<Ptr<WifiLanService>> wifi_lan_service_;
|
||||
const string endpoint_id_;
|
||||
const string endpoint_name_;
|
||||
const string service_id_;
|
||||
@@ -344,32 +470,44 @@ class P2PClusterPCPHandler : public BasePCPHandler<Platform> {
|
||||
|
||||
static const BluetoothDeviceName::Version::Value kBluetoothDeviceNameVersion;
|
||||
static const BLEAdvertisement::Version::Value kBleAdvertisementVersion;
|
||||
static const WifiLanServiceInfo::Version kWifiLanServiceInfoVersion;
|
||||
|
||||
static ConstPtr<ByteArray> generateHash(const string& source, size_t size);
|
||||
static string getBlePeripheralId(Ptr<BLE_PERIPHERAL> ble_peripheral);
|
||||
|
||||
proto::connections::Medium startBluetoothAdvertising(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& service_id,
|
||||
Ptr<ClientProxy<Platform>> client_proxy, const string& service_id,
|
||||
ConstPtr<ByteArray> service_id_hash, const string& local_endpoint_id,
|
||||
const string& local_endpoint_name);
|
||||
proto::connections::Medium startBluetoothDiscovery(
|
||||
Ptr<FoundBluetoothAdvertisementProcessor> processor,
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& service_id);
|
||||
Ptr<ClientProxy<Platform>> client_proxy, const string& service_id);
|
||||
typename BasePCPHandler<Platform>::ConnectImplResult bluetoothConnectImpl(
|
||||
Ptr<ClientProxy<Platform> > client_proxy,
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<BluetoothEndpoint> bluetooth_endpoint);
|
||||
|
||||
proto::connections::Medium startBleAdvertising(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& service_id,
|
||||
Ptr<ClientProxy<Platform>> client_proxy, const string& service_id,
|
||||
ConstPtr<ByteArray> service_id_hash, const string& local_endpoint_id,
|
||||
const string& local_endpoint_name);
|
||||
proto::connections::Medium startBleDiscovery(
|
||||
Ptr<FoundBleAdvertisementProcessor> processor,
|
||||
Ptr<ClientProxy<Platform> > client_proxy, const string& service_id);
|
||||
Ptr<ClientProxy<Platform>> client_proxy, const string& service_id);
|
||||
typename BasePCPHandler<Platform>::ConnectImplResult bleConnectImpl(
|
||||
Ptr<ClientProxy<Platform> > client_proxy, Ptr<BLEEndpoint> ble_endpoint);
|
||||
Ptr<ClientProxy<Platform>> client_proxy, Ptr<BLEEndpoint> ble_endpoint);
|
||||
|
||||
Ptr<MediumManager<Platform> > medium_manager_;
|
||||
proto::connections::Medium StartWifiLanAdvertising(
|
||||
Ptr<ClientProxy<Platform>> client_proxy, absl::string_view service_id,
|
||||
ConstPtr<ByteArray> service_id_hash, absl::string_view local_endpoint_id,
|
||||
absl::string_view local_endpoint_name);
|
||||
proto::connections::Medium StartWifiLanDiscovery(
|
||||
Ptr<FoundWifiLanServiceProcessor> processor,
|
||||
Ptr<ClientProxy<Platform>> client_proxy, absl::string_view service_id);
|
||||
typename BasePCPHandler<Platform>::ConnectImplResult WifiLanConnectImpl(
|
||||
Ptr<ClientProxy<Platform>> client_proxy,
|
||||
Ptr<WifiLanEndpoint> wifi_lan_endpoint);
|
||||
|
||||
Ptr<MediumManager<Platform>> medium_manager_;
|
||||
std::shared_ptr<P2PClusterPCPHandler> self_{this, [](void*) {}};
|
||||
};
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ template <typename Platform>
|
||||
P2PPointToPointPCPHandler<Platform>::P2PPointToPointPCPHandler(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager)
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager)
|
||||
: P2PStarPCPHandler<Platform>(medium_manager, endpoint_manager,
|
||||
endpoint_channel_manager,
|
||||
bandwidth_upgrade_manager),
|
||||
|
||||
@@ -27,8 +27,8 @@ class P2PPointToPointPCPHandler : public P2PStarPCPHandler<Platform> {
|
||||
P2PPointToPointPCPHandler(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager);
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager);
|
||||
|
||||
Strategy getStrategy() override;
|
||||
PCP::Value getPCP() override;
|
||||
|
||||
@@ -10,8 +10,8 @@ template <typename Platform>
|
||||
P2PStarPCPHandler<Platform>::P2PStarPCPHandler(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager)
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager)
|
||||
: P2PClusterPCPHandler<Platform>(medium_manager, endpoint_manager,
|
||||
endpoint_channel_manager,
|
||||
bandwidth_upgrade_manager),
|
||||
|
||||
@@ -27,11 +27,10 @@ namespace connections {
|
||||
template <typename Platform>
|
||||
class P2PStarPCPHandler : public P2PClusterPCPHandler<Platform> {
|
||||
public:
|
||||
P2PStarPCPHandler(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager);
|
||||
P2PStarPCPHandler(Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager);
|
||||
~P2PStarPCPHandler() override;
|
||||
|
||||
Strategy getStrategy() override;
|
||||
|
||||
@@ -11,9 +11,9 @@ namespace connections {
|
||||
template <typename Platform>
|
||||
PCPManager<Platform>::PCPManager(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager)
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager)
|
||||
: pcp_handlers_(), current_pcp_handler_() {
|
||||
pcp_handlers_[PCP::P2P_CLUSTER] = MakePtr(new P2PClusterPCPHandler<Platform>(
|
||||
medium_manager, endpoint_manager, endpoint_channel_manager,
|
||||
|
||||
@@ -29,9 +29,9 @@ template <typename Platform>
|
||||
class PCPManager {
|
||||
public:
|
||||
PCPManager(Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager,
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager,
|
||||
Ptr<EndpointManager<Platform> > endpoint_manager,
|
||||
Ptr<BandwidthUpgradeManager<Platform> > bandwidth_upgrade_manager);
|
||||
Ptr<BandwidthUpgradeManager> bandwidth_upgrade_manager);
|
||||
~PCPManager();
|
||||
|
||||
Status::Value startAdvertising(
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "core/internal/wifi_lan_endpoint_channel.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
Ptr<WifiLanEndpointChannel>
|
||||
WifiLanEndpointChannel::CreateOutgoing(
|
||||
Ptr<MediumManager<Platform>> medium_manager,
|
||||
absl::string_view channel_name, Ptr<WifiLanSocket> wifi_lan_socket) {
|
||||
return MakePtr(
|
||||
new WifiLanEndpointChannel(channel_name, wifi_lan_socket));
|
||||
}
|
||||
|
||||
Ptr<WifiLanEndpointChannel>
|
||||
WifiLanEndpointChannel::CreateIncoming(
|
||||
Ptr<MediumManager<Platform>> medium_manager,
|
||||
absl::string_view channel_name, Ptr<WifiLanSocket> wifi_lan_socket) {
|
||||
return MakePtr(
|
||||
new WifiLanEndpointChannel(channel_name, wifi_lan_socket));
|
||||
}
|
||||
|
||||
WifiLanEndpointChannel::WifiLanEndpointChannel(
|
||||
absl::string_view channel_name, Ptr<WifiLanSocket> wifi_lan_socket)
|
||||
: BaseEndpointChannel(channel_name,
|
||||
wifi_lan_socket->GetInputStream(),
|
||||
wifi_lan_socket->GetOutputStream()),
|
||||
wifi_lan_socket_(wifi_lan_socket) {}
|
||||
|
||||
WifiLanEndpointChannel::~WifiLanEndpointChannel() {}
|
||||
|
||||
proto::connections::Medium WifiLanEndpointChannel::getMedium() {
|
||||
return proto::connections::Medium::WIFI_LAN;
|
||||
}
|
||||
|
||||
void WifiLanEndpointChannel::closeImpl() {
|
||||
Exception::Value exception = wifi_lan_socket_->Close();
|
||||
if (exception != Exception::NONE) {
|
||||
if (exception == Exception::IO) {
|
||||
// TODO(b/149806065): Add logging.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_H_
|
||||
#define CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_H_
|
||||
|
||||
#include "core/internal/base_endpoint_channel.h"
|
||||
#include "core/internal/medium_manager.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
class WifiLanEndpointChannel : public BaseEndpointChannel {
|
||||
public:
|
||||
using Platform = platform::ImplementationPlatform;
|
||||
|
||||
static Ptr<WifiLanEndpointChannel> CreateOutgoing(
|
||||
Ptr<MediumManager<Platform>> medium_manager,
|
||||
absl::string_view channel_name, Ptr<WifiLanSocket> wifi_lan_socket);
|
||||
static Ptr<WifiLanEndpointChannel> CreateIncoming(
|
||||
Ptr<MediumManager<Platform>> medium_manager,
|
||||
absl::string_view channel_name, Ptr<WifiLanSocket> wifi_lan_socket);
|
||||
|
||||
~WifiLanEndpointChannel() override;
|
||||
|
||||
proto::connections::Medium getMedium() override;
|
||||
|
||||
protected:
|
||||
void closeImpl() override;
|
||||
|
||||
private:
|
||||
WifiLanEndpointChannel(absl::string_view channel_name,
|
||||
Ptr<WifiLanSocket> wifi_lan_socket);
|
||||
|
||||
ScopedPtr<Ptr<WifiLanSocket> > wifi_lan_socket_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_H_
|
||||
@@ -18,8 +18,8 @@ class OnIncomingWifiConnectionRunnable : public Runnable {
|
||||
template <typename Platform>
|
||||
WifiLanUpgradeHandler<Platform>::WifiLanUpgradeHandler(
|
||||
Ptr<MediumManager<Platform> > medium_manager,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager)
|
||||
: BaseBandwidthUpgradeHandler<Platform>(endpoint_channel_manager),
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager)
|
||||
: BaseBandwidthUpgradeHandler(endpoint_channel_manager),
|
||||
medium_manager_(medium_manager) {}
|
||||
|
||||
template <typename Platform>
|
||||
|
||||
@@ -23,36 +23,35 @@ class OnIncomingWifiConnectionRunnable;
|
||||
|
||||
// Manages the WIFI_LAN-specific methods needed to upgrade an EndpointChannel
|
||||
template <typename Platform>
|
||||
class WifiLanUpgradeHandler : public BaseBandwidthUpgradeHandler<Platform> {
|
||||
class WifiLanUpgradeHandler : public BaseBandwidthUpgradeHandler {
|
||||
// TODO(ahlee): Uncomment when WIFI_LAN plumbing is done.
|
||||
// public MediumManager<Platform>::IncomingWifiConnectionProcessor {
|
||||
public:
|
||||
WifiLanUpgradeHandler(
|
||||
Ptr<MediumManager<Platform> > medium_manager_,
|
||||
Ptr<EndpointChannelManager<Platform> > endpoint_channel_manager);
|
||||
~WifiLanUpgradeHandler();
|
||||
WifiLanUpgradeHandler(Ptr<MediumManager<Platform> > medium_manager_,
|
||||
Ptr<EndpointChannelManager> endpoint_channel_manager);
|
||||
~WifiLanUpgradeHandler() override;
|
||||
|
||||
void onIncomingWifiConnection(Ptr<Socket> socket);
|
||||
|
||||
protected:
|
||||
// @BandwidthUpgradeHandlerThread
|
||||
ConstPtr<ByteArray> initializeUpgradedMediumForEndpoint(
|
||||
const string& endpoint_id);
|
||||
const string& endpoint_id) override;
|
||||
// @BandwidthUpgradeHandlerThread
|
||||
Ptr<EndpointChannel> createUpgradedEndpointChannel(
|
||||
const string& endpoint_id,
|
||||
ConstPtr<BandwidthUpgradeNegotiationFrame::UpgradePathInfo>
|
||||
upgrade_path_info);
|
||||
upgrade_path_info) override;
|
||||
// TODO(ahlee): Change the java counterparts of these methods to private.
|
||||
proto::connections::Medium getUpgradeMedium();
|
||||
proto::connections::Medium getUpgradeMedium() override;
|
||||
// @BandwidthUpgradeHandlerThread
|
||||
void revertImpl();
|
||||
void revertImpl() override;
|
||||
|
||||
private:
|
||||
class IncomingWifiLanSocketConnection
|
||||
: public BaseBandwidthUpgradeHandler<Platform>::IncomingSocketConnection {
|
||||
: public BaseBandwidthUpgradeHandler::IncomingSocketConnection {
|
||||
public:
|
||||
IncomingWifiLanSocketConnection(Ptr<Socket> socket)
|
||||
explicit IncomingWifiLanSocketConnection(Ptr<Socket> socket)
|
||||
: new_endpoint_channel_(Ptr<EndpointChannel>()),
|
||||
// TODO(ahlee): Uncomment when plumbing for WIFI_LAN is done.
|
||||
// new_endpoint_channel_(getEndpointChannelManager()
|
||||
@@ -61,15 +60,15 @@ class WifiLanUpgradeHandler : public BaseBandwidthUpgradeHandler<Platform> {
|
||||
// TODO(ahlee): This is only used for logging which is not currently
|
||||
// implemented. If we want to match the Java code in the future, we'll need
|
||||
// to add toString() to socket.h.
|
||||
string socketToString() { return string(); }
|
||||
void closeSocket() {
|
||||
string socketToString() override { return string(); }
|
||||
void closeSocket() override {
|
||||
// Ignore the potential Exception returned by close(), as a counterpart
|
||||
// to Java's closeQuietly().
|
||||
wifi_socket_->close();
|
||||
}
|
||||
// TODO(ahlee): Double check that the ownership of this is correct when
|
||||
// this is fully implemented.
|
||||
Ptr<EndpointChannel> getEndpointChannel() {
|
||||
Ptr<EndpointChannel> getEndpointChannel() override {
|
||||
return new_endpoint_channel_.release();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user