From 94ef1b532b1ddfd9ad65782159fed91b7e188e1c Mon Sep 17 00:00:00 2001 From: suetfei Date: Wed, 27 Oct 2021 16:47:13 -0700 Subject: [PATCH] [Nearby Connection] Move peer_id.* & webrtc_socket_wrapper.* up one directory. PiperOrigin-RevId: 406011497 --- cpp/core/internal/base_pcp_handler.cc | 4 +- cpp/core/internal/base_pcp_handler.h | 10 ++--- cpp/core/internal/mediums/BUILD | 20 +++++++--- cpp/core/internal/mediums/webrtc.cc | 32 ++++++++-------- cpp/core/internal/mediums/webrtc.h | 37 ++++++++++--------- cpp/core/internal/mediums/webrtc/BUILD | 31 ++++++++++++---- .../mediums/webrtc/connection_flow.cc | 2 +- .../internal/mediums/webrtc/connection_flow.h | 2 +- .../mediums/webrtc/connection_flow_test.cc | 2 +- .../mediums/webrtc/data_channel_listener.h | 2 +- .../mediums/webrtc/signaling_frames.cc | 10 ++--- .../mediums/webrtc/signaling_frames.h | 10 ++--- .../mediums/webrtc/signaling_frames_test.cc | 10 ++--- .../{webrtc/peer_id.cc => webrtc_peer_id.cc} | 10 ++--- .../{webrtc/peer_id.h => webrtc_peer_id.h} | 16 ++++---- ...peer_id_test.cc => webrtc_peer_id_test.cc} | 14 +++---- .../{webrtc => }/webrtc_socket_wrapper.h | 4 +- cpp/core/internal/mediums/webrtc_test.cc | 28 +++++++------- cpp/core/internal/p2p_cluster_pcp_handler.cc | 2 +- cpp/core/internal/p2p_cluster_pcp_handler.h | 1 - cpp/core/internal/webrtc_bwu_handler.cc | 6 +-- cpp/core/internal/webrtc_bwu_handler.h | 2 +- cpp/core/internal/webrtc_endpoint_channel.h | 2 +- 23 files changed, 142 insertions(+), 115 deletions(-) rename cpp/core/internal/mediums/{webrtc/peer_id.cc => webrtc_peer_id.cc} (83%) rename cpp/core/internal/mediums/{webrtc/peer_id.h => webrtc_peer_id.h} (74%) rename cpp/core/internal/mediums/{webrtc/peer_id_test.cc => webrtc_peer_id_test.cc} (81%) rename cpp/core/internal/mediums/{webrtc => }/webrtc_socket_wrapper.h (92%) diff --git a/cpp/core/internal/base_pcp_handler.cc b/cpp/core/internal/base_pcp_handler.cc index 430caf40..8e3bf3e9 100644 --- a/cpp/core/internal/base_pcp_handler.cc +++ b/cpp/core/internal/base_pcp_handler.cc @@ -659,12 +659,12 @@ BasePcpHandler::GetDiscoveredEndpoints( return result; } -mediums::PeerId BasePcpHandler::CreatePeerIdFromAdvertisement( +mediums::WebrtcPeerId BasePcpHandler::CreatePeerIdFromAdvertisement( const std::string& service_id, const std::string& endpoint_id, const ByteArray& endpoint_info) { std::string seed = absl::StrCat(service_id, endpoint_id, std::string(endpoint_info)); - return mediums::PeerId::FromSeed(ByteArray(std::move(seed))); + return mediums::WebrtcPeerId::FromSeed(ByteArray(std::move(seed))); } bool BasePcpHandler::HasOutgoingConnections(ClientProxy* client) const { diff --git a/cpp/core/internal/base_pcp_handler.h b/cpp/core/internal/base_pcp_handler.h index ed18a5bf..1b075667 100644 --- a/cpp/core/internal/base_pcp_handler.h +++ b/cpp/core/internal/base_pcp_handler.h @@ -215,11 +215,11 @@ class BasePcpHandler : public PcpHandler, }; struct WebRtcEndpoint : public DiscoveredEndpoint { - WebRtcEndpoint(DiscoveredEndpoint endpoint, mediums::PeerId peer_id) + WebRtcEndpoint(DiscoveredEndpoint endpoint, mediums::WebrtcPeerId peer_id) : DiscoveredEndpoint(std::move(endpoint)), peer_id(std::move(peer_id)) {} - mediums::PeerId peer_id; + mediums::WebrtcPeerId peer_id; }; struct ConnectImplResult { @@ -296,9 +296,9 @@ class BasePcpHandler : public PcpHandler, std::vector GetDiscoveredEndpoints( const proto::connections::Medium medium); - mediums::PeerId CreatePeerIdFromAdvertisement(const string& service_id, - const string& endpoint_id, - const ByteArray& endpoint_info); + mediums::WebrtcPeerId CreatePeerIdFromAdvertisement( + const string& service_id, const string& endpoint_id, + const ByteArray& endpoint_info); SingleThreadExecutor* GetPcpHandlerThread() ABSL_LOCK_RETURNED(serial_executor_) { diff --git a/cpp/core/internal/mediums/BUILD b/cpp/core/internal/mediums/BUILD index 3f3bc386..8eb15e40 100644 --- a/cpp/core/internal/mediums/BUILD +++ b/cpp/core/internal/mediums/BUILD @@ -54,6 +54,7 @@ cc_library( "//core:core_types", "//core/internal/mediums/ble_v2", "//core/internal/mediums/webrtc", + "//core/internal/mediums/webrtc:data_types", "//platform/base", "//platform/base:cancellation_flag", "//platform/public:comm", @@ -63,14 +64,20 @@ cc_library( "//proto/mediums:web_rtc_signaling_frames_cc_proto", "//smhasher:libmurmur3", "//webrtc/api:libjingle_peerconnection_api", - "//webrtc/api:scoped_refptr", ], ) cc_library( name = "utils", - srcs = ["utils.cc"], - hdrs = ["utils.h"], + srcs = [ + "utils.cc", + "webrtc_peer_id.cc", + ], + hdrs = [ + "utils.h", + "webrtc_peer_id.h", + "webrtc_socket_wrapper.h", + ], compatible_with = ["//buildenv/target:non_prod"], visibility = [ "//core/internal:__pkg__", @@ -78,6 +85,8 @@ cc_library( "//core/internal/mediums/webrtc:__pkg__", ], deps = [ + "//absl/strings", + "//core/internal/mediums/webrtc:data_types", "//platform/base", "//platform/public:types", "//proto/connections:offline_wire_formats_portable_proto", @@ -100,6 +109,7 @@ cc_test( shard_count = 16, deps = [ ":mediums", + ":utils", "//testing/base/public:gunit_main", "//absl/strings", "//absl/time", @@ -115,6 +125,7 @@ cc_test( name = "core_internal_mediums_webrtc_test", size = "small", srcs = [ + "webrtc_peer_id_test.cc", "webrtc_test.cc", ], shard_count = 16, @@ -124,9 +135,8 @@ cc_test( ], deps = [ ":mediums", + ":utils", "//testing/base/public:gunit_main", - "//absl/strings", - "//core/internal/mediums/webrtc", "//platform/base", "//platform/base:test_util", "//platform/impl/g3", # build_cleaner: keep diff --git a/cpp/core/internal/mediums/webrtc.cc b/cpp/core/internal/mediums/webrtc.cc index 0eba6799..1edcf639 100644 --- a/cpp/core/internal/mediums/webrtc.cc +++ b/cpp/core/internal/mediums/webrtc.cc @@ -22,7 +22,7 @@ #include "absl/time/time.h" #include "core/internal/mediums/webrtc/session_description_wrapper.h" #include "core/internal/mediums/webrtc/signaling_frames.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" #include "platform/base/byte_array.h" #include "platform/base/listeners.h" #include "platform/public/cancelable_alarm.h" @@ -80,7 +80,7 @@ bool WebRtc::IsAcceptingConnectionsLocked(const std::string& service_id) { } bool WebRtc::StartAcceptingConnections(const std::string& service_id, - const PeerId& self_peer_id, + const WebrtcPeerId& self_peer_id, const LocationHint& location_hint, AcceptedConnectionCallback callback) { MutexLock lock(&mutex_); @@ -193,7 +193,7 @@ void WebRtc::StopAcceptingConnections(const std::string& service_id) { } WebRtcSocketWrapper WebRtc::Connect(const std::string& service_id, - const PeerId& remote_peer_id, + const WebrtcPeerId& remote_peer_id, const LocationHint& location_hint, CancellationFlag* cancellation_flag) { for (int attempts_count = 0; attempts_count < kConnectAttemptsLimit; @@ -208,10 +208,10 @@ WebRtcSocketWrapper WebRtc::Connect(const std::string& service_id, } WebRtcSocketWrapper WebRtc::AttemptToConnect( - const std::string& service_id, const PeerId& remote_peer_id, + const std::string& service_id, const WebrtcPeerId& remote_peer_id, const LocationHint& location_hint, CancellationFlag* cancellation_flag) { ConnectionRequestInfo info = ConnectionRequestInfo(); - info.self_peer_id = PeerId::FromRandom(); + info.self_peer_id = WebrtcPeerId::FromRandom(); Future socket_future = info.socket_future; { @@ -324,7 +324,7 @@ WebRtcSocketWrapper WebRtc::AttemptToConnect( } void WebRtc::ProcessLocalIceCandidate( - const std::string& service_id, const PeerId& remote_peer_id, + const std::string& service_id, const WebrtcPeerId& remote_peer_id, const ::location::nearby::mediums::IceCandidate ice_candidate) { MutexLock lock(&mutex_); @@ -422,7 +422,7 @@ void WebRtc::ProcessTachyonInboxMessage(const std::string& service_id, NEARBY_LOG(WARNING, "Invalid WebRTC frame: Sender ID is missing."); return; } - PeerId remote_peer_id = PeerId(frame.sender_id().id()); + WebrtcPeerId remote_peer_id = WebrtcPeerId(frame.sender_id().id()); // Depending on the message type, we'll respond as appropriate. if (requesting_connections_info_.contains(remote_peer_id.GetId())) { @@ -462,7 +462,7 @@ void WebRtc::ProcessTachyonInboxMessage(const std::string& service_id, } void WebRtc::SendOffer(const std::string& service_id, - const PeerId& remote_peer_id) { + const WebrtcPeerId& remote_peer_id) { std::unique_ptr connection_flow = CreateConnectionFlow(service_id, remote_peer_id); if (!connection_flow) { @@ -507,7 +507,7 @@ void WebRtc::SendOffer(const std::string& service_id, NEARBY_LOG(INFO, "Sent offer to %s.", remote_peer_id.GetId().c_str()); } -void WebRtc::ReceiveOffer(const PeerId& remote_peer_id, +void WebRtc::ReceiveOffer(const WebrtcPeerId& remote_peer_id, SessionDescriptionWrapper offer) { const auto& entry = connection_flows_.find(remote_peer_id.GetId()); if (entry == connection_flows_.end()) { @@ -522,7 +522,7 @@ void WebRtc::ReceiveOffer(const PeerId& remote_peer_id, } } -void WebRtc::SendAnswer(const PeerId& remote_peer_id) { +void WebRtc::SendAnswer(const WebrtcPeerId& remote_peer_id) { const auto& entry = connection_flows_.find(remote_peer_id.GetId()); if (entry == connection_flows_.end()) { NEARBY_LOG(INFO, @@ -574,7 +574,7 @@ void WebRtc::SendAnswer(const PeerId& remote_peer_id) { NEARBY_LOG(INFO, "Sent answer to %s.", remote_peer_id.GetId().c_str()); } -void WebRtc::ReceiveAnswer(const PeerId& remote_peer_id, +void WebRtc::ReceiveAnswer(const WebrtcPeerId& remote_peer_id, SessionDescriptionWrapper answer) { const auto& entry = connection_flows_.find(remote_peer_id.GetId()); if (entry == connection_flows_.end()) { @@ -590,7 +590,7 @@ void WebRtc::ReceiveAnswer(const PeerId& remote_peer_id, } void WebRtc::ReceiveIceCandidates( - const PeerId& remote_peer_id, + const WebrtcPeerId& remote_peer_id, std::vector> ice_candidates) { const auto& entry = connection_flows_.find(remote_peer_id.GetId()); @@ -643,7 +643,7 @@ void WebRtc::RestartTachyonReceiveMessages(const std::string& service_id) { } void WebRtc::ProcessDataChannelOpen(const std::string& service_id, - const PeerId& remote_peer_id, + const WebrtcPeerId& remote_peer_id, WebRtcSocketWrapper socket_wrapper) { MutexLock lock(&mutex_); @@ -671,7 +671,7 @@ void WebRtc::ProcessDataChannelOpen(const std::string& service_id, service_id.c_str()); } -void WebRtc::ProcessDataChannelClosed(const PeerId& remote_peer_id) { +void WebRtc::ProcessDataChannelClosed(const WebrtcPeerId& remote_peer_id) { MutexLock lock(&mutex_); NEARBY_LOG(INFO, "Data channel has closed, removing connection flow for peer %s.", @@ -681,7 +681,7 @@ void WebRtc::ProcessDataChannelClosed(const PeerId& remote_peer_id) { } std::unique_ptr WebRtc::CreateConnectionFlow( - const std::string& service_id, const PeerId& remote_peer_id) { + const std::string& service_id, const WebrtcPeerId& remote_peer_id) { RemoveConnectionFlow(remote_peer_id); return ConnectionFlow::Create( @@ -719,7 +719,7 @@ std::unique_ptr WebRtc::CreateConnectionFlow( medium_); } -void WebRtc::RemoveConnectionFlow(const PeerId& remote_peer_id) { +void WebRtc::RemoveConnectionFlow(const WebrtcPeerId& remote_peer_id) { if (!connection_flows_.erase(remote_peer_id.GetId())) { return; } diff --git a/cpp/core/internal/mediums/webrtc.h b/cpp/core/internal/mediums/webrtc.h index 77f14294..b5fb959a 100644 --- a/cpp/core/internal/mediums/webrtc.h +++ b/cpp/core/internal/mediums/webrtc.h @@ -25,9 +25,9 @@ #include "core/internal/mediums/webrtc/connection_flow.h" #include "core/internal/mediums/webrtc/data_channel_listener.h" #include "core/internal/mediums/webrtc/local_ice_candidate_listener.h" -#include "core/internal/mediums/webrtc/peer_id.h" #include "core/internal/mediums/webrtc/webrtc_socket.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_peer_id.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" #include "platform/base/byte_array.h" #include "platform/base/cancellation_flag.h" #include "platform/base/listeners.h" @@ -77,7 +77,7 @@ class WebRtc { // boolean value indicating if the device has started accepting connections. // Runs on @MainThread. bool StartAcceptingConnections(const std::string& service_id, - const PeerId& self_peer_id, + const WebrtcPeerId& self_peer_id, const LocationHint& location_hint, AcceptedConnectionCallback callback) ABSL_LOCKS_EXCLUDED(mutex_); @@ -91,7 +91,7 @@ class WebRtc { // with internal retry for maximum attempts of kConnectAttemptsLimit. // Runs on @MainThread. WebRtcSocketWrapper Connect(const std::string& service_id, - const PeerId& peer_id, + const WebrtcPeerId& peer_id, const LocationHint& location_hint, CancellationFlag* cancellation_flag) ABSL_LOCKS_EXCLUDED(mutex_); @@ -109,7 +109,7 @@ class WebRtc { struct AcceptingConnectionsInfo { // The self_peer_id is generated from the BT/WiFi advertisements and allows // the scanner to message us over Tachyon. - PeerId self_peer_id; + WebrtcPeerId self_peer_id; // The registered callback. When there's an incoming connection, this // callback is notified. @@ -133,7 +133,7 @@ class WebRtc { struct ConnectionRequestInfo { // The self_peer_id is randomly generated and allows the advertiser to // message us over Tachyon. - PeerId self_peer_id; + WebrtcPeerId self_peer_id; // Allows us to communicate with the Tachyon web server. std::unique_ptr signaling_messenger; @@ -147,7 +147,7 @@ class WebRtc { // |peer_id|. // Runs on @MainThread. WebRtcSocketWrapper AttemptToConnect(const std::string& service_id, - const PeerId& peer_id, + const WebrtcPeerId& peer_id, const LocationHint& location_hint, CancellationFlag* cancellation_flag) ABSL_LOCKS_EXCLUDED(mutex_); @@ -170,55 +170,56 @@ class WebRtc { ABSL_LOCKS_EXCLUDED(mutex_); // Runs on |single_thread_executor_|. - void SendOffer(const std::string& service_id, const PeerId& remote_peer_id) + void SendOffer(const std::string& service_id, + const WebrtcPeerId& remote_peer_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. - void ReceiveOffer(const PeerId& remote_peer_id, + void ReceiveOffer(const WebrtcPeerId& remote_peer_id, SessionDescriptionWrapper offer) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. - void SendAnswer(const PeerId& remote_peer_id) + void SendAnswer(const WebrtcPeerId& remote_peer_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. - void ReceiveAnswer(const PeerId& remote_peer_id, + void ReceiveAnswer(const WebrtcPeerId& remote_peer_id, SessionDescriptionWrapper answer) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. void ReceiveIceCandidates( - const PeerId& remote_peer_id, + const WebrtcPeerId& remote_peer_id, std::vector> ice_candidates) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. std::unique_ptr CreateConnectionFlow( - const std::string& service_id, const PeerId& remote_peer_id) + const std::string& service_id, const WebrtcPeerId& remote_peer_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. std::unique_ptr GetConnectionFlow( - const PeerId& remote_peer_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); + const WebrtcPeerId& remote_peer_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. - void RemoveConnectionFlow(const PeerId& remote_peer_id) + void RemoveConnectionFlow(const WebrtcPeerId& remote_peer_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. void ProcessDataChannelOpen(const std::string& service_id, - const PeerId& remote_peer_id, + const WebrtcPeerId& remote_peer_id, WebRtcSocketWrapper socket_wrapper) ABSL_LOCKS_EXCLUDED(mutex_); // Runs on |single_thread_executor_|. - void ProcessDataChannelClosed(const PeerId& remote_peer_id) + void ProcessDataChannelClosed(const WebrtcPeerId& remote_peer_id) ABSL_LOCKS_EXCLUDED(mutex_); // Runs on |single_thread_executor_|. void ProcessLocalIceCandidate( - const std::string& service_id, const PeerId& remote_peer_id, + const std::string& service_id, const WebrtcPeerId& remote_peer_id, const ::location::nearby::mediums::IceCandidate ice_candidate) ABSL_LOCKS_EXCLUDED(mutex_); diff --git a/cpp/core/internal/mediums/webrtc/BUILD b/cpp/core/internal/mediums/webrtc/BUILD index acfbd89a..9e13e8b8 100644 --- a/cpp/core/internal/mediums/webrtc/BUILD +++ b/cpp/core/internal/mediums/webrtc/BUILD @@ -17,19 +17,14 @@ cc_library( name = "webrtc", srcs = [ "connection_flow.cc", - "peer_id.cc", "signaling_frames.cc", - "webrtc_socket.cc", ], hdrs = [ "connection_flow.h", "data_channel_listener.h", "local_ice_candidate_listener.h", - "peer_id.h", "session_description_wrapper.h", "signaling_frames.h", - "webrtc_socket.h", - "webrtc_socket_wrapper.h", ], compatible_with = ["//buildenv/target:non_prod"], copts = ["-DCORE_ADAPTER_DLL"], @@ -37,8 +32,8 @@ cc_library( "//core/internal:__subpackages__", ], deps = [ + ":data_types", "//absl/memory", - "//absl/strings", "//absl/time", "//core:core_types", "//core/internal/mediums:utils", @@ -52,12 +47,32 @@ cc_library( ], ) +cc_library( + name = "data_types", + srcs = [ + "webrtc_socket.cc", + ], + hdrs = [ + "webrtc_socket.h", + ], + compatible_with = ["//buildenv/target:non_prod"], + copts = ["-DCORE_ADAPTER_DLL"], + visibility = [ + "//core/internal:__subpackages__", + ], + deps = [ + "//core:core_types", + "//platform/base", + "//platform/public:types", + "//webrtc/api:libjingle_peerconnection_api", + ], +) + cc_test( name = "webrtc_test", timeout = "short", srcs = [ "connection_flow_test.cc", - "peer_id_test.cc", "signaling_frames_test.cc", "webrtc_socket_test.cc", ], @@ -66,10 +81,12 @@ cc_test( "requires-net:external", ], deps = [ + ":data_types", ":webrtc", "//net/proto2/compat/public:proto2", "//testing/base/public:gunit_main", "//absl/time", + "//core/internal/mediums:utils", "//platform/base", "//platform/base:test_util", "//platform/impl/g3", # buildcleaner: keep diff --git a/cpp/core/internal/mediums/webrtc/connection_flow.cc b/cpp/core/internal/mediums/webrtc/connection_flow.cc index ad24b6a0..29066453 100644 --- a/cpp/core/internal/mediums/webrtc/connection_flow.cc +++ b/cpp/core/internal/mediums/webrtc/connection_flow.cc @@ -21,7 +21,7 @@ #include "absl/time/time.h" #include "core/internal/mediums/webrtc/session_description_wrapper.h" #include "core/internal/mediums/webrtc/webrtc_socket.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" #include "platform/public/logging.h" #include "platform/public/mutex_lock.h" #include "platform/public/webrtc.h" diff --git a/cpp/core/internal/mediums/webrtc/connection_flow.h b/cpp/core/internal/mediums/webrtc/connection_flow.h index 7ac24869..474a1829 100644 --- a/cpp/core/internal/mediums/webrtc/connection_flow.h +++ b/cpp/core/internal/mediums/webrtc/connection_flow.h @@ -20,7 +20,7 @@ #include "core/internal/mediums/webrtc/data_channel_listener.h" #include "core/internal/mediums/webrtc/local_ice_candidate_listener.h" #include "core/internal/mediums/webrtc/session_description_wrapper.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" #include "platform/base/runnable.h" #include "platform/public/count_down_latch.h" #include "platform/public/single_thread_executor.h" diff --git a/cpp/core/internal/mediums/webrtc/connection_flow_test.cc b/cpp/core/internal/mediums/webrtc/connection_flow_test.cc index c46f8bd2..dfc98d71 100644 --- a/cpp/core/internal/mediums/webrtc/connection_flow_test.cc +++ b/cpp/core/internal/mediums/webrtc/connection_flow_test.cc @@ -21,7 +21,7 @@ #include "gtest/gtest.h" #include "absl/time/time.h" #include "core/internal/mediums/webrtc/session_description_wrapper.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" #include "platform/base/byte_array.h" #include "platform/base/medium_environment.h" #include "platform/public/count_down_latch.h" diff --git a/cpp/core/internal/mediums/webrtc/data_channel_listener.h b/cpp/core/internal/mediums/webrtc/data_channel_listener.h index 9e2c71a9..13e6bb5a 100644 --- a/cpp/core/internal/mediums/webrtc/data_channel_listener.h +++ b/cpp/core/internal/mediums/webrtc/data_channel_listener.h @@ -15,7 +15,7 @@ #ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_DATA_CHANNEL_LISTENER_H_ #define CORE_INTERNAL_MEDIUMS_WEBRTC_DATA_CHANNEL_LISTENER_H_ -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" #include "core/listeners.h" #include "platform/base/byte_array.h" diff --git a/cpp/core/internal/mediums/webrtc/signaling_frames.cc b/cpp/core/internal/mediums/webrtc/signaling_frames.cc index 284ce55b..033c2fac 100644 --- a/cpp/core/internal/mediums/webrtc/signaling_frames.cc +++ b/cpp/core/internal/mediums/webrtc/signaling_frames.cc @@ -29,7 +29,7 @@ ByteArray FrameToByteArray(const WebRtcSignalingFrame& signaling_frame) { return ByteArray(message.c_str(), message.size()); } -void SetSenderId(const PeerId& sender_id, WebRtcSignalingFrame& frame) { +void SetSenderId(const WebrtcPeerId& sender_id, WebRtcSignalingFrame& frame) { frame.mutable_sender_id()->set_id(sender_id.GetId()); } @@ -44,7 +44,7 @@ std::unique_ptr DecodeIceCandidate( } // namespace -ByteArray EncodeReadyForSignalingPoke(const PeerId& sender_id) { +ByteArray EncodeReadyForSignalingPoke(const WebrtcPeerId& sender_id) { WebRtcSignalingFrame signaling_frame; signaling_frame.set_type(WebRtcSignalingFrame::READY_FOR_SIGNALING_POKE_TYPE); SetSenderId(sender_id, signaling_frame); @@ -53,7 +53,7 @@ ByteArray EncodeReadyForSignalingPoke(const PeerId& sender_id) { return FrameToByteArray(std::move(signaling_frame)); } -ByteArray EncodeOffer(const PeerId& sender_id, +ByteArray EncodeOffer(const WebrtcPeerId& sender_id, const webrtc::SessionDescriptionInterface& offer) { WebRtcSignalingFrame signaling_frame; signaling_frame.set_type(WebRtcSignalingFrame::OFFER_TYPE); @@ -66,7 +66,7 @@ ByteArray EncodeOffer(const PeerId& sender_id, return FrameToByteArray(std::move(signaling_frame)); } -ByteArray EncodeAnswer(const PeerId& sender_id, +ByteArray EncodeAnswer(const WebrtcPeerId& sender_id, const webrtc::SessionDescriptionInterface& answer) { WebRtcSignalingFrame signaling_frame; signaling_frame.set_type(WebRtcSignalingFrame::ANSWER_TYPE); @@ -80,7 +80,7 @@ ByteArray EncodeAnswer(const PeerId& sender_id, } ByteArray EncodeIceCandidates( - const PeerId& sender_id, + const WebrtcPeerId& sender_id, const std::vector& ice_candidates) { WebRtcSignalingFrame signaling_frame; diff --git a/cpp/core/internal/mediums/webrtc/signaling_frames.h b/cpp/core/internal/mediums/webrtc/signaling_frames.h index ce57ccd1..8be6a97b 100644 --- a/cpp/core/internal/mediums/webrtc/signaling_frames.h +++ b/cpp/core/internal/mediums/webrtc/signaling_frames.h @@ -17,7 +17,7 @@ #include -#include "core/internal/mediums/webrtc/peer_id.h" +#include "core/internal/mediums/webrtc_peer_id.h" #include "platform/base/byte_array.h" #include "proto/mediums/web_rtc_signaling_frames.pb.h" #include "webrtc/api/peer_connection_interface.h" @@ -28,15 +28,15 @@ namespace connections { namespace mediums { namespace webrtc_frames { -ByteArray EncodeReadyForSignalingPoke(const PeerId& sender_id); +ByteArray EncodeReadyForSignalingPoke(const WebrtcPeerId& sender_id); -ByteArray EncodeOffer(const PeerId& sender_id, +ByteArray EncodeOffer(const WebrtcPeerId& sender_id, const webrtc::SessionDescriptionInterface& offer); -ByteArray EncodeAnswer(const PeerId& sender_id, +ByteArray EncodeAnswer(const WebrtcPeerId& sender_id, const webrtc::SessionDescriptionInterface& answer); ByteArray EncodeIceCandidates( - const PeerId& sender_id, + const WebrtcPeerId& sender_id, const std::vector& ice_candidates); location::nearby::mediums::IceCandidate EncodeIceCandidate( const webrtc::IceCandidateInterface& ice_candidate); diff --git a/cpp/core/internal/mediums/webrtc/signaling_frames_test.cc b/cpp/core/internal/mediums/webrtc/signaling_frames_test.cc index cf2a4ab6..4105c7b7 100644 --- a/cpp/core/internal/mediums/webrtc/signaling_frames_test.cc +++ b/cpp/core/internal/mediums/webrtc/signaling_frames_test.cc @@ -19,7 +19,7 @@ #include "google/protobuf/text_format.h" #include "gmock/gmock.h" #include "gtest/gtest.h" -#include "core/internal/mediums/webrtc/peer_id.h" +#include "core/internal/mediums/webrtc_peer_id.h" namespace location { namespace nearby { @@ -80,7 +80,7 @@ const char kIceCandidatesProto[] = R"( } // namespace TEST(SignalingFramesTest, SignalingPoke) { - PeerId sender_id("abc"); + WebrtcPeerId sender_id("abc"); ByteArray encoded_poke = EncodeReadyForSignalingPoke(sender_id); location::nearby::mediums::WebRtcSignalingFrame frame; @@ -94,7 +94,7 @@ TEST(SignalingFramesTest, SignalingPoke) { } TEST(SignalingFramesTest, EncodeValidOffer) { - PeerId sender_id("abc"); + WebrtcPeerId sender_id("abc"); std::unique_ptr offer = webrtc::CreateSessionDescription(webrtc::SdpType::kOffer, kSampleSdp); ByteArray encoded_offer = EncodeOffer(sender_id, *offer); @@ -119,7 +119,7 @@ TEST(SignaingFramesTest, DecodeValidOffer) { } TEST(SignalingFramesTest, EncodeValidAnswer) { - PeerId sender_id("abc"); + WebrtcPeerId sender_id("abc"); std::unique_ptr answer( webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, kSampleSdp)); ByteArray encoded_answer = EncodeAnswer(sender_id, *answer); @@ -144,7 +144,7 @@ TEST(SignalingFramesTest, DecodeValidAnswer) { } TEST(SignalingFramesTest, EncodeValidIceCandidates) { - PeerId sender_id("abc"); + WebrtcPeerId sender_id("abc"); webrtc::SdpParseError error; std::vector> ice_candidates; diff --git a/cpp/core/internal/mediums/webrtc/peer_id.cc b/cpp/core/internal/mediums/webrtc_peer_id.cc similarity index 83% rename from cpp/core/internal/mediums/webrtc/peer_id.cc rename to cpp/core/internal/mediums/webrtc_peer_id.cc index 96109cc9..9360357c 100644 --- a/cpp/core/internal/mediums/webrtc/peer_id.cc +++ b/cpp/core/internal/mediums/webrtc_peer_id.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "core/internal/mediums/webrtc/peer_id.h" +#include "core/internal/mediums/webrtc_peer_id.h" #include @@ -36,17 +36,17 @@ std::string BytesToStringUppercase(const ByteArray& bytes) { } } // namespace -PeerId PeerId::FromRandom() { +WebrtcPeerId WebrtcPeerId::FromRandom() { return FromSeed(Utils::GenerateRandomBytes(kPeerIdLength)); } -PeerId PeerId::FromSeed(const ByteArray& seed) { +WebrtcPeerId WebrtcPeerId::FromSeed(const ByteArray& seed) { ByteArray full_hash(Utils::Sha256Hash(seed, kPeerIdLength)); ByteArray hashed_seed(full_hash.data(), kPeerIdLength / 2); - return PeerId(BytesToStringUppercase(hashed_seed)); + return WebrtcPeerId(BytesToStringUppercase(hashed_seed)); } -bool PeerId::IsValid() const { return !id_.empty(); } +bool WebrtcPeerId::IsValid() const { return !id_.empty(); } } // namespace mediums } // namespace connections diff --git a/cpp/core/internal/mediums/webrtc/peer_id.h b/cpp/core/internal/mediums/webrtc_peer_id.h similarity index 74% rename from cpp/core/internal/mediums/webrtc/peer_id.h rename to cpp/core/internal/mediums/webrtc_peer_id.h index a6df2e92..d2331b5a 100644 --- a/cpp/core/internal/mediums/webrtc/peer_id.h +++ b/cpp/core/internal/mediums/webrtc_peer_id.h @@ -25,16 +25,16 @@ namespace nearby { namespace connections { namespace mediums { -// PeerId is used as an identifier to exchange SDP messages to establish WebRTC -// p2p connection. An empty PeerId is considered to be invalid. -class PeerId { +// WebrtcPeerId is used as an identifier to exchange SDP messages to establish +// WebRTC p2p connection. An empty WebrtcPeerId is considered to be invalid. +class WebrtcPeerId { public: - PeerId() = default; - explicit PeerId(const std::string& id) : id_(id) {} - ~PeerId() = default; + WebrtcPeerId() = default; + explicit WebrtcPeerId(const std::string& id) : id_(id) {} + ~WebrtcPeerId() = default; - static PeerId FromRandom(); - static PeerId FromSeed(const ByteArray& seed); + static WebrtcPeerId FromRandom(); + static WebrtcPeerId FromSeed(const ByteArray& seed); bool IsValid() const; diff --git a/cpp/core/internal/mediums/webrtc/peer_id_test.cc b/cpp/core/internal/mediums/webrtc_peer_id_test.cc similarity index 81% rename from cpp/core/internal/mediums/webrtc/peer_id_test.cc rename to cpp/core/internal/mediums/webrtc_peer_id_test.cc index 68ea725a..612d213c 100644 --- a/cpp/core/internal/mediums/webrtc/peer_id_test.cc +++ b/cpp/core/internal/mediums/webrtc_peer_id_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "core/internal/mediums/webrtc/peer_id.h" +#include "core/internal/mediums/webrtc_peer_id.h" #include @@ -26,27 +26,27 @@ namespace nearby { namespace connections { namespace mediums { -TEST(PeerIdTest, GenerateRandomPeerId) { - PeerId peer_id = PeerId::FromRandom(); +TEST(WebrtcPeerIdTest, GenerateRandomPeerId) { + WebrtcPeerId peer_id = WebrtcPeerId::FromRandom(); EXPECT_EQ(64, peer_id.GetId().size()); } -TEST(PeerIdTest, GenerateFromSeed) { +TEST(WebrtcPeerIdTest, GenerateFromSeed) { // Values calculated by running actual SHA-256 hash on |seed|. std::string seed = "seed"; std::string expected_peer_id = "19B25856E1C150CA834CFFC8B59B23ADBD0EC0389E58EB22B3B64768098D002B"; ByteArray seed_bytes(seed); - PeerId peer_id = PeerId::FromSeed(seed_bytes); + WebrtcPeerId peer_id = WebrtcPeerId::FromSeed(seed_bytes); EXPECT_EQ(64, peer_id.GetId().size()); EXPECT_EQ(expected_peer_id, peer_id.GetId()); } -TEST(PeerIdTest, GetId) { +TEST(WebrtcPeerIdTest, GetId) { const std::string id = "this_is_a_test"; - PeerId peer_id(id); + WebrtcPeerId peer_id(id); EXPECT_EQ(id, peer_id.GetId()); } diff --git a/cpp/core/internal/mediums/webrtc/webrtc_socket_wrapper.h b/cpp/core/internal/mediums/webrtc_socket_wrapper.h similarity index 92% rename from cpp/core/internal/mediums/webrtc/webrtc_socket_wrapper.h rename to cpp/core/internal/mediums/webrtc_socket_wrapper.h index 4da6151d..bc467113 100644 --- a/cpp/core/internal/mediums/webrtc/webrtc_socket_wrapper.h +++ b/cpp/core/internal/mediums/webrtc_socket_wrapper.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_WRAPPER_H_ -#define CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_WRAPPER_H_ +#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_WRAPPER_H_ +#define CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_WRAPPER_H_ #include diff --git a/cpp/core/internal/mediums/webrtc_test.cc b/cpp/core/internal/mediums/webrtc_test.cc index 4fa77cc0..863e6ef7 100644 --- a/cpp/core/internal/mediums/webrtc_test.cc +++ b/cpp/core/internal/mediums/webrtc_test.cc @@ -16,7 +16,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" #include "platform/base/listeners.h" #include "platform/base/medium_environment.h" #include "platform/public/mutex_lock.h" @@ -57,7 +57,7 @@ TEST_P(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) { env_.SetFeatureFlags(feature_flags); WebRtc receiver, sender; WebRtcSocketWrapper receiver_socket, sender_socket; - const PeerId self_id("self_id"); + const WebrtcPeerId self_id("self_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; Future connected; @@ -95,7 +95,7 @@ TEST_P(WebRtcTest, CanCancelConnect) { env_.SetFeatureFlags(feature_flags); WebRtc receiver, sender; WebRtcSocketWrapper receiver_socket, sender_socket; - const PeerId self_id("self_id"); + const WebrtcPeerId self_id("self_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; Future connected; @@ -149,7 +149,7 @@ TEST_F(WebRtcTest, StartAcceptingConnectionTwice) { env_.Start({.webrtc_enabled = true}); testing::StrictMock mock_accepted_callback_; WebRtc webrtc; - PeerId self_id("peer_id"); + WebrtcPeerId self_id("peer_id"); const std::string service_id("NearbySharing"); LocationHint location_hint{}; @@ -170,7 +170,7 @@ TEST_F(WebRtcTest, StartAcceptingConnectionTwice) { TEST_F(WebRtcTest, Connect_NoPeer) { env_.Start({.webrtc_enabled = true}); WebRtc webrtc; - PeerId peer_id("peer_id"); + WebrtcPeerId peer_id("peer_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; @@ -191,7 +191,7 @@ TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) { env_.Start({.webrtc_enabled = true}); testing::StrictMock mock_accepted_callback_; WebRtc webrtc; - PeerId self_id("peer_id"); + WebrtcPeerId self_id("peer_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; @@ -201,7 +201,7 @@ TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) { {mock_accepted_callback_.AsStdFunction()})); CancellationFlag flag; WebRtcSocketWrapper wrapper = webrtc.Connect( - service_id, PeerId("random_peer_id"), location_hint, &flag); + service_id, WebrtcPeerId("random_peer_id"), location_hint, &flag); EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id)); EXPECT_FALSE(wrapper.IsValid()); EXPECT_FALSE(webrtc.StartAcceptingConnections( @@ -216,7 +216,7 @@ TEST_F(WebRtcTest, StartAndStopAcceptingConnections) { env_.Start({.webrtc_enabled = true}); testing::StrictMock mock_accepted_callback_; WebRtc webrtc; - PeerId self_id("peer_id"); + WebrtcPeerId self_id("peer_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; @@ -236,7 +236,7 @@ TEST_F(WebRtcTest, ConnectTwice) { env_.Start({.webrtc_enabled = true}); WebRtc receiver, sender, device_c; WebRtcSocketWrapper receiver_socket, sender_socket; - const PeerId self_id("self_id"), other_id("other_id"); + const WebrtcPeerId self_id("self_id"), other_id("other_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; Future connected; @@ -284,7 +284,7 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) { env_.Start({.webrtc_enabled = true}); WebRtc receiver, sender; WebRtcSocketWrapper receiver_socket, sender_socket; - const PeerId self_id("self_id"); + const WebrtcPeerId self_id("self_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; Future connected; @@ -315,7 +315,7 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) { env_.Start({.webrtc_enabled = true}); WebRtc receiver, sender; WebRtcSocketWrapper receiver_socket, sender_socket; - const PeerId self_id("self_id"); + const WebrtcPeerId self_id("self_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; Future connected; @@ -354,13 +354,13 @@ TEST_F(WebRtcTest, Connect_NullPeerConnection) { WebRtc webrtc; const std::string service_id("NearbySharing"); - PeerId self_id("peer_id"); + WebrtcPeerId self_id("peer_id"); LocationHint location_hint; ASSERT_TRUE(webrtc.IsAvailable()); CancellationFlag flag; WebRtcSocketWrapper wrapper = webrtc.Connect( - service_id, PeerId("random_peer_id"), location_hint, &flag); + service_id, WebrtcPeerId("random_peer_id"), location_hint, &flag); EXPECT_FALSE(wrapper.IsValid()); env_.Stop(); } @@ -371,7 +371,7 @@ TEST_F(WebRtcTest, ContinueAcceptingConnectionsOnComplete) { env_.Start({.webrtc_enabled = true}); testing::StrictMock mock_accepted_callback_; WebRtc webrtc; - PeerId self_id("peer_id"); + WebrtcPeerId self_id("peer_id"); const std::string service_id("NearbySharing"); LocationHint location_hint; diff --git a/cpp/core/internal/p2p_cluster_pcp_handler.cc b/cpp/core/internal/p2p_cluster_pcp_handler.cc index 4f63c27d..7f174ce1 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.cc +++ b/cpp/core/internal/p2p_cluster_pcp_handler.cc @@ -22,7 +22,7 @@ #include "core/internal/bluetooth_endpoint_channel.h" #include "core/internal/bwu_manager.h" #include "core/internal/mediums/utils.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" #include "core/internal/webrtc_endpoint_channel.h" #include "core/internal/wifi_lan_endpoint_channel.h" #include "core/internal/wifi_lan_endpoint_channel_v2.h" diff --git a/cpp/core/internal/p2p_cluster_pcp_handler.h b/cpp/core/internal/p2p_cluster_pcp_handler.h index ac296483..4257d7ce 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.h +++ b/cpp/core/internal/p2p_cluster_pcp_handler.h @@ -29,7 +29,6 @@ #include "core/internal/mediums/bluetooth_classic.h" #include "core/internal/mediums/mediums.h" #include "core/internal/mediums/webrtc.h" -#include "core/internal/mediums/webrtc/peer_id.h" #include "core/internal/pcp.h" #include "core/internal/wifi_lan_service_info.h" #include "core/options.h" diff --git a/cpp/core/internal/webrtc_bwu_handler.cc b/cpp/core/internal/webrtc_bwu_handler.cc index d36f1e9e..c3b536de 100644 --- a/cpp/core/internal/webrtc_bwu_handler.cc +++ b/cpp/core/internal/webrtc_bwu_handler.cc @@ -19,7 +19,7 @@ #include "absl/functional/bind_front.h" #include "core/internal/client_proxy.h" #include "core/internal/mediums/utils.h" -#include "core/internal/mediums/webrtc/peer_id.h" +#include "core/internal/mediums/webrtc_peer_id.h" #include "core/internal/offline_frames.h" #include "core/internal/webrtc_endpoint_channel.h" @@ -74,7 +74,7 @@ ByteArray WebrtcBwuHandler::InitializeUpgradedMediumForEndpoint( LocationHint location_hint = Utils::BuildLocationHint(webrtc_.GetDefaultCountryCode()); - mediums::PeerId self_id{mediums::PeerId::FromRandom()}; + mediums::WebrtcPeerId self_id{mediums::WebrtcPeerId::FromRandom()}; if (!webrtc_.IsAcceptingConnections(service_id)) { if (!webrtc_.StartAcceptingConnections( upgrade_service_id, self_id, location_hint, @@ -110,7 +110,7 @@ WebrtcBwuHandler::CreateUpgradedEndpointChannel( const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) { const UpgradePathInfo::WebRtcCredentials& web_rtc_credentials = upgrade_path_info.web_rtc_credentials(); - mediums::PeerId peer_id(web_rtc_credentials.peer_id()); + mediums::WebrtcPeerId peer_id(web_rtc_credentials.peer_id()); LocationHint location_hint; location_hint.set_format(LocationStandard::UNKNOWN); diff --git a/cpp/core/internal/webrtc_bwu_handler.h b/cpp/core/internal/webrtc_bwu_handler.h index 169c4d97..6587b643 100644 --- a/cpp/core/internal/webrtc_bwu_handler.h +++ b/cpp/core/internal/webrtc_bwu_handler.h @@ -19,7 +19,7 @@ #include "core/internal/client_proxy.h" #include "core/internal/endpoint_channel_manager.h" #include "core/internal/mediums/mediums.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" namespace location { namespace nearby { diff --git a/cpp/core/internal/webrtc_endpoint_channel.h b/cpp/core/internal/webrtc_endpoint_channel.h index 48d8d37f..5515ab6e 100644 --- a/cpp/core/internal/webrtc_endpoint_channel.h +++ b/cpp/core/internal/webrtc_endpoint_channel.h @@ -16,7 +16,7 @@ #define CORE_INTERNAL_WEBRTC_ENDPOINT_CHANNEL_H_ #include "core/internal/base_endpoint_channel.h" -#include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" +#include "core/internal/mediums/webrtc_socket_wrapper.h" namespace location { namespace nearby {