mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
nearbyconnections : Replace WifiLanV2 to WifiLan.
PiperOrigin-RevId: 406033177
This commit is contained in:
committed by
Copybara-Service
parent
94ef1b532b
commit
acf516189b
@@ -61,7 +61,6 @@ cc_library(
|
||||
"webrtc.h",
|
||||
"wifi.h",
|
||||
"wifi_lan.h",
|
||||
"wifi_lan_v2.h",
|
||||
],
|
||||
compatible_with = ["//buildenv/target:non_prod"],
|
||||
visibility = [
|
||||
|
||||
@@ -41,7 +41,6 @@
|
||||
#include "platform/api/webrtc.h"
|
||||
#include "platform/api/wifi.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/api/wifi_lan_v2.h"
|
||||
#include "platform/base/payload_id.h"
|
||||
|
||||
namespace location {
|
||||
@@ -102,7 +101,6 @@ class ImplementationPlatform {
|
||||
static std::unique_ptr<ServerSyncMedium> CreateServerSyncMedium();
|
||||
static std::unique_ptr<WifiMedium> CreateWifiMedium();
|
||||
static std::unique_ptr<WifiLanMedium> CreateWifiLanMedium();
|
||||
static std::unique_ptr<WifiLanMediumV2> CreateWifiLanMediumV2();
|
||||
static std::unique_ptr<WebRtcMedium> CreateWebRtcMedium();
|
||||
};
|
||||
|
||||
|
||||
+74
-56
@@ -17,8 +17,6 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "platform/base/cancellation_flag.h"
|
||||
#include "platform/base/input_stream.h"
|
||||
#include "platform/base/listeners.h"
|
||||
@@ -29,19 +27,6 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
|
||||
// Opaque wrapper over a WifiLan service which contains |NsdServiceInfo|.
|
||||
class WifiLanService {
|
||||
public:
|
||||
virtual ~WifiLanService() = default;
|
||||
|
||||
// Returns the |NsdServiceInfo| which contains the packed string of
|
||||
// |WifiLanServiceInfo| and the endpoint info with named key in a TXTRecord
|
||||
// map.
|
||||
// The details refer to
|
||||
// https://developer.android.com/reference/android/net/nsd/NsdServiceInfo.html.
|
||||
virtual NsdServiceInfo GetServiceInfo() const = 0;
|
||||
};
|
||||
|
||||
class WifiLanSocket {
|
||||
public:
|
||||
virtual ~WifiLanSocket() = default;
|
||||
@@ -62,10 +47,28 @@ class WifiLanSocket {
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
virtual Exception Close() = 0;
|
||||
};
|
||||
|
||||
// Returns valid WifiLanService pointer if there is a connection, and
|
||||
// nullptr otherwise.
|
||||
virtual WifiLanService* GetRemoteWifiLanService() = 0;
|
||||
class WifiLanServerSocket {
|
||||
public:
|
||||
virtual ~WifiLanServerSocket() = default;
|
||||
|
||||
// Returns ip address.
|
||||
virtual std::string GetIPAddress() const = 0;
|
||||
|
||||
// Returns port.
|
||||
virtual int GetPort() const = 0;
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
// On success, returns connected socket, ready to exchange data.
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be closed.
|
||||
virtual std::unique_ptr<WifiLanSocket> Accept() = 0;
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
virtual Exception Close() = 0;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the WifiLan medium.
|
||||
@@ -73,59 +76,74 @@ class WifiLanMedium {
|
||||
public:
|
||||
virtual ~WifiLanMedium() = default;
|
||||
|
||||
virtual bool StartAdvertising(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info) = 0;
|
||||
virtual bool StopAdvertising(const std::string& service_id) = 0;
|
||||
// Starts WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service is now advertising.
|
||||
// On error if the service cannot start to advertise or the service type in
|
||||
// NsdServiceInfo has been passed previously which StopAdvertising is not
|
||||
// been called.
|
||||
virtual bool StartAdvertising(const NsdServiceInfo& nsd_service_info) = 0;
|
||||
|
||||
// Stops WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service stops advertising.
|
||||
// On error if the service cannot stop advertising or the service type in
|
||||
// NsdServiceInfo cannot be found.
|
||||
virtual bool StopAdvertising(const NsdServiceInfo& nsd_service_info) = 0;
|
||||
|
||||
// Callback that is invoked when a discovered service is found or lost.
|
||||
struct DiscoveredServiceCallback {
|
||||
std::function<void(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id)>
|
||||
service_discovered_cb =
|
||||
DefaultCallback<WifiLanService&, const std::string&>();
|
||||
std::function<void(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id)>
|
||||
service_lost_cb =
|
||||
DefaultCallback<WifiLanService&, const std::string&>();
|
||||
std::function<void(NsdServiceInfo service_info)> service_discovered_cb =
|
||||
DefaultCallback<NsdServiceInfo>();
|
||||
std::function<void(NsdServiceInfo service_info)> service_lost_cb =
|
||||
DefaultCallback<NsdServiceInfo>();
|
||||
};
|
||||
|
||||
// Returns true once the WifiLan discovery has been initiated.
|
||||
virtual bool StartDiscovery(const std::string& service_id,
|
||||
// Starts the discovery of nearby WifiLan services.
|
||||
//
|
||||
// service_type - mDNS service type.
|
||||
// callback - the instance of DiscoveredServiceCallback.
|
||||
// Returns true once the WifiLan discovery has been initiated. The
|
||||
// service_type is associated with callback.
|
||||
virtual bool StartDiscovery(const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) = 0;
|
||||
|
||||
// Returns true once WifiLan discovery for service_id is well and truly
|
||||
// stopped; after this returns, there must be no more invocations of the
|
||||
// DiscoveredServiceCallback passed in to StartDiscovery() for service_id.
|
||||
virtual bool StopDiscovery(const std::string& service_id) = 0;
|
||||
|
||||
// Callback that is invoked when a new connection is accepted.
|
||||
struct AcceptedConnectionCallback {
|
||||
std::function<void(WifiLanSocket& socket, const std::string& service_id)>
|
||||
accepted_cb = DefaultCallback<WifiLanSocket&, const std::string&>();
|
||||
};
|
||||
|
||||
// Returns true once WifiLan socket connection requests to service_id can be
|
||||
// accepted.
|
||||
virtual bool StartAcceptingConnections(
|
||||
const std::string& service_id, AcceptedConnectionCallback callback) = 0;
|
||||
virtual bool StopAcceptingConnections(const std::string& service_id) = 0;
|
||||
// Stops the discovery of nearby WifiLan services.
|
||||
//
|
||||
// service_type - The one assigned in StartDiscovery.
|
||||
// On success if the service_type is matched to the callback and will be
|
||||
// removed from the list. If list is empty then stops the WifiLan
|
||||
// discovery service.
|
||||
// On error if the service_type is not existed, then return immediately.
|
||||
virtual bool StopDiscovery(const std::string& service_type) = 0;
|
||||
|
||||
// Connects to a WifiLan service.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
virtual std::unique_ptr<WifiLanSocket> Connect(
|
||||
WifiLanService& wifi_lan_service, const std::string& service_id,
|
||||
virtual std::unique_ptr<WifiLanSocket> ConnectToService(
|
||||
const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag) = 0;
|
||||
|
||||
virtual WifiLanService* GetRemoteService(const std::string& ip_address,
|
||||
int port) = 0;
|
||||
// Connects to a WifiLan service by ip address and port.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
virtual std::unique_ptr<WifiLanSocket> ConnectToService(
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) = 0;
|
||||
|
||||
// Gets ip address + port for remote services on the network to identify and
|
||||
// connect to this service.
|
||||
// Listens for incoming connection.
|
||||
//
|
||||
// Credential is for the currently-hosted Wifi ServerSocket (if any).
|
||||
virtual std::pair<std::string, int> GetCredentials(
|
||||
const std::string& service_id) = 0;
|
||||
// port - A port number.
|
||||
// 0 : use a random port.
|
||||
// 1~65536 : open a server socket on that exact port.
|
||||
// On success, returns a new WifiLanServerSocket.
|
||||
// On error, returns nullptr.
|
||||
virtual std::unique_ptr<WifiLanServerSocket> ListenForService(
|
||||
int port = 0) = 0;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -1,153 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef PLATFORM_API_WIFI_LAN__V2_H_
|
||||
#define PLATFORM_API_WIFI_LAN__V2_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "platform/base/cancellation_flag.h"
|
||||
#include "platform/base/input_stream.h"
|
||||
#include "platform/base/listeners.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/output_stream.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
|
||||
class WifiLanSocketV2 {
|
||||
public:
|
||||
virtual ~WifiLanSocketV2() = default;
|
||||
|
||||
// Returns the InputStream of the WifiLanSocket.
|
||||
// On error, returned stream will report Exception::kIo on any operation.
|
||||
//
|
||||
// The returned object is not owned by the caller, and can be invalidated once
|
||||
// the WifiLanSocket object is destroyed.
|
||||
virtual InputStream& GetInputStream() = 0;
|
||||
|
||||
// Returns the OutputStream of the WifiLanSocket.
|
||||
// On error, returned stream will report Exception::kIo on any operation.
|
||||
//
|
||||
// The returned object is not owned by the caller, and can be invalidated once
|
||||
// the WifiLanSocket object is destroyed.
|
||||
virtual OutputStream& GetOutputStream() = 0;
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
virtual Exception Close() = 0;
|
||||
};
|
||||
|
||||
class WifiLanServerSocketV2 {
|
||||
public:
|
||||
virtual ~WifiLanServerSocketV2() = default;
|
||||
|
||||
// Returns ip address.
|
||||
virtual std::string GetIPAddress() const = 0;
|
||||
|
||||
// Returns port.
|
||||
virtual int GetPort() const = 0;
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
// On success, returns connected socket, ready to exchange data.
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be closed.
|
||||
virtual std::unique_ptr<WifiLanSocketV2> Accept() = 0;
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
virtual Exception Close() = 0;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the WifiLan medium.
|
||||
class WifiLanMediumV2 {
|
||||
public:
|
||||
virtual ~WifiLanMediumV2() = default;
|
||||
|
||||
// Starts WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service is now advertising.
|
||||
// On error if the service cannot start to advertise or the service type in
|
||||
// NsdServiceInfo has been passed previously which StopAdvertising is not
|
||||
// been called.
|
||||
virtual bool StartAdvertising(const NsdServiceInfo& nsd_service_info) = 0;
|
||||
|
||||
// Stops WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service stops advertising.
|
||||
// On error if the service cannot stop advertising or the service type in
|
||||
// NsdServiceInfo cannot be found.
|
||||
virtual bool StopAdvertising(const NsdServiceInfo& nsd_service_info) = 0;
|
||||
|
||||
// Callback that is invoked when a discovered service is found or lost.
|
||||
struct DiscoveredServiceCallback {
|
||||
std::function<void(NsdServiceInfo service_info)> service_discovered_cb =
|
||||
DefaultCallback<NsdServiceInfo>();
|
||||
std::function<void(NsdServiceInfo service_info)> service_lost_cb =
|
||||
DefaultCallback<NsdServiceInfo>();
|
||||
};
|
||||
|
||||
// Starts the discovery of nearby WifiLan services.
|
||||
//
|
||||
// service_type - mDNS service type.
|
||||
// callback - the instance of DiscoveredServiceCallback.
|
||||
// Returns true once the WifiLan discovery has been initiated. The
|
||||
// service_type is associated with callback.
|
||||
virtual bool StartDiscovery(const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) = 0;
|
||||
|
||||
// Stops the discovery of nearby WifiLan services.
|
||||
//
|
||||
// service_type - The one assigned in StartDiscovery.
|
||||
// On success if the service_type is matched to the callback and will be
|
||||
// removed from the list. If list is empty then stops the WifiLan
|
||||
// discovery service.
|
||||
// On error if the service_type is not existed, then return immediately.
|
||||
virtual bool StopDiscovery(const std::string& service_type) = 0;
|
||||
|
||||
// Connects to a WifiLan service.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
virtual std::unique_ptr<WifiLanSocketV2> ConnectToService(
|
||||
const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag) = 0;
|
||||
|
||||
// Connects to a WifiLan service by ip address and port.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
virtual std::unique_ptr<WifiLanSocketV2> ConnectToService(
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) = 0;
|
||||
|
||||
// Listens for incoming connection.
|
||||
//
|
||||
// port - A port number.
|
||||
// 0 : use a random port.
|
||||
// 1~65536 : open a server socket on that exact port.
|
||||
// On success, returns a new WifiLanServerSocket.
|
||||
// On error, returns nullptr.
|
||||
virtual std::unique_ptr<WifiLanServerSocketV2> ListenForService(
|
||||
int port = 0) = 0;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_API_WIFI_LAN__V2_H_
|
||||
@@ -62,7 +62,6 @@ void MediumEnvironment::Reset() {
|
||||
webrtc_signaling_message_callback_.clear();
|
||||
webrtc_signaling_complete_callback_.clear();
|
||||
wifi_lan_mediums_.clear();
|
||||
wifi_lan_mediums_v2_.clear();
|
||||
use_valid_peer_connection_ = true;
|
||||
peer_connection_latency_ = absl::ZeroDuration();
|
||||
});
|
||||
@@ -228,35 +227,7 @@ void MediumEnvironment::OnBlePeripheralStateChanged(
|
||||
}
|
||||
|
||||
void MediumEnvironment::OnWifiLanServiceStateChanged(
|
||||
WifiLanMediumContext& info, api::WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
NEARBY_LOGS(INFO) << "G3 OnWifiLanServiceStateChanged [wifi_lan_service impl="
|
||||
<< &wifi_lan_service << "]; context=" << &info
|
||||
<< "; service_id=" << service_id
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (!enable_notifications_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[&info, enabled, &wifi_lan_service, service_id]() {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 [Run] OnWifiLanServiceStateChanged [wifi_lan_service impl="
|
||||
<< &wifi_lan_service << "]; context=" << &info
|
||||
<< "; service_id=" << service_id << "; enabled=" << enabled;
|
||||
auto service_id_context = info.services.find(service_id);
|
||||
if (service_id_context == info.services.end()) return;
|
||||
|
||||
if (enabled) {
|
||||
service_id_context->second.discovery_callback.service_discovered_cb(
|
||||
wifi_lan_service, service_id);
|
||||
} else {
|
||||
service_id_context->second.discovery_callback.service_lost_cb(
|
||||
wifi_lan_service, service_id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::OnWifiLanServiceV2StateChanged(
|
||||
WifiLanMediumV2Context& info, const NsdServiceInfo& service_info,
|
||||
WifiLanMediumContext& info, const NsdServiceInfo& service_info,
|
||||
bool enabled) {
|
||||
if (!enabled_) return;
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
@@ -583,101 +554,6 @@ absl::Duration MediumEnvironment::GetPeerConnectionLatency() {
|
||||
return peer_connection_latency_;
|
||||
}
|
||||
|
||||
void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
wifi_lan_mediums_.insert({&medium, WifiLanMediumContext{}});
|
||||
NEARBY_LOGS(INFO) << "Registered: medium=" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForAdvertising(
|
||||
api::WifiLanMedium& medium, api::WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, &wifi_lan_service, service_id, enabled]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateWifiLanMediumForAdvertising failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.wifi_lan_service = &wifi_lan_service;
|
||||
auto service_id_context = context.services.find(service_id);
|
||||
if (service_id_context == context.services.end()) {
|
||||
WifiLanServiceIdContext id_context{
|
||||
.advertising = enabled,
|
||||
};
|
||||
context.services.emplace(service_id, std::move(id_context));
|
||||
} else {
|
||||
service_id_context->second.advertising = enabled;
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "Update WifiLan medium for advertising: this="
|
||||
<< this << "; medium=" << &medium
|
||||
<< "; service_id=" << service_id
|
||||
<< "; wifi_lan_service=" << &wifi_lan_service
|
||||
<< ", service_info_name="
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceName()
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
if (local_medium == &medium) continue;
|
||||
OnWifiLanServiceStateChanged(info, wifi_lan_service, service_id,
|
||||
enabled);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForDiscovery(
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanDiscoveredServiceCallback callback, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, service_id, callback = std::move(callback), enabled]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateWifiLanMediumForDiscovery failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
auto service_id_context = context.services.find(service_id);
|
||||
if (service_id_context == context.services.end()) {
|
||||
WifiLanServiceIdContext id_context{
|
||||
.discovery_callback = std::move(callback),
|
||||
};
|
||||
context.services.emplace(service_id, std::move(id_context));
|
||||
} else {
|
||||
service_id_context->second.discovery_callback = std::move(callback);
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "Update WifiLan medium for discovery: this="
|
||||
<< this << "; medium=" << &medium
|
||||
<< "; service_id=" << service_id
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
if (local_medium == &medium) continue;
|
||||
// Search advertising mediums and send notification.
|
||||
for (auto& service_id_context : info.services) {
|
||||
auto& service_id = service_id_context.first;
|
||||
auto& id_context = service_id_context.second;
|
||||
if (id_context.advertising && enabled) {
|
||||
OnWifiLanServiceStateChanged(context, *(info.wifi_lan_service),
|
||||
service_id, enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
std::string MediumEnvironment::GetFakeIPAddress() const {
|
||||
std::string ip_address;
|
||||
ip_address.resize(4);
|
||||
@@ -696,99 +572,16 @@ int MediumEnvironment::GetFakePort() const {
|
||||
return port;
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForAcceptedConnection(
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback callback) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, service_id, callback = std::move(callback)]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Update WifiLan medium failed. There is no medium registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
auto service_id_context = context.services.find(service_id);
|
||||
if (service_id_context == context.services.end()) {
|
||||
WifiLanServiceIdContext id_context{
|
||||
.accepted_connection_callback = std::move(callback),
|
||||
};
|
||||
context.services.emplace(service_id, std::move(id_context));
|
||||
} else {
|
||||
service_id_context->second.accepted_connection_callback =
|
||||
std::move(callback);
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Update WifiLan medium for accepted callback: this=" << this
|
||||
<< "; medium=" << &medium << "; service_id=" << service_id;
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UnregisterWifiLanMedium(api::WifiLanMedium& medium) {
|
||||
void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
auto item = wifi_lan_mediums_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO) << "Unregistered WifiLan medium";
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::CallWifiLanAcceptedConnectionCallback(
|
||||
api::WifiLanMedium& medium, api::WifiLanSocket& socket,
|
||||
const std::string& service_id) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, &socket, service_id]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Call AcceptedConnectionCallback failed.. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& info = item->second;
|
||||
auto service_id_context = info.services.find(service_id);
|
||||
if (service_id_context != info.services.end()) {
|
||||
service_id_context->second.accepted_connection_callback.accepted_cb(
|
||||
socket, service_id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
api::WifiLanService* MediumEnvironment::GetWifiLanService(
|
||||
const std::string& ip_address, int port) {
|
||||
api::WifiLanService* remote_wifi_lan_service = nullptr;
|
||||
CountDownLatch latch(1);
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &remote_wifi_lan_service, &ip_address, port, &latch]() {
|
||||
for (auto& item : wifi_lan_mediums_) {
|
||||
auto* wifi_lan_service = item.second.wifi_lan_service;
|
||||
if (!wifi_lan_service) continue;
|
||||
std::string remote_ip_address =
|
||||
remote_wifi_lan_service->GetServiceInfo().GetIPAddress();
|
||||
int remote_port = remote_wifi_lan_service->GetServiceInfo().GetPort();
|
||||
if (remote_ip_address == ip_address && remote_port == port) {
|
||||
remote_wifi_lan_service = wifi_lan_service;
|
||||
break;
|
||||
}
|
||||
}
|
||||
latch.CountDown();
|
||||
});
|
||||
latch.Await();
|
||||
return remote_wifi_lan_service;
|
||||
}
|
||||
|
||||
void MediumEnvironment::RegisterWifiLanMediumV2(api::WifiLanMediumV2& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
wifi_lan_mediums_v2_.insert({&medium, WifiLanMediumV2Context{}});
|
||||
wifi_lan_mediums_.insert({&medium, WifiLanMediumContext{}});
|
||||
NEARBY_LOG(INFO, "Registered: medium=%p", &medium);
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumV2ForAdvertising(
|
||||
api::WifiLanMediumV2& medium, const NsdServiceInfo& service_info,
|
||||
void MediumEnvironment::UpdateWifiLanMediumForAdvertising(
|
||||
api::WifiLanMedium& medium, const NsdServiceInfo& service_info,
|
||||
bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_info = service_info,
|
||||
@@ -799,7 +592,7 @@ void MediumEnvironment::UpdateWifiLanMediumV2ForAdvertising(
|
||||
<< "; service_name=" << service_info.GetServiceName()
|
||||
<< "; service_type=" << service_type
|
||||
<< ", enabled=" << enabled;
|
||||
for (auto& medium_info : wifi_lan_mediums_v2_) {
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium but update
|
||||
@@ -812,19 +605,19 @@ void MediumEnvironment::UpdateWifiLanMediumV2ForAdvertising(
|
||||
}
|
||||
continue;
|
||||
}
|
||||
OnWifiLanServiceV2StateChanged(info, service_info, enabled);
|
||||
OnWifiLanServiceStateChanged(info, service_info, enabled);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumV2ForDiscovery(
|
||||
api::WifiLanMediumV2& medium, WifiLanDiscoveredServiceV2Callback callback,
|
||||
void MediumEnvironment::UpdateWifiLanMediumForDiscovery(
|
||||
api::WifiLanMedium& medium, WifiLanDiscoveredServiceCallback callback,
|
||||
const std::string& service_type, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, callback = std::move(callback),
|
||||
service_type, enabled]() {
|
||||
auto item = wifi_lan_mediums_v2_.find(&medium);
|
||||
if (item == wifi_lan_mediums_v2_.end()) {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateWifiLanMediumForDiscovery failed. There is no medium "
|
||||
"registered.";
|
||||
@@ -836,7 +629,7 @@ void MediumEnvironment::UpdateWifiLanMediumV2ForDiscovery(
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : wifi_lan_mediums_v2_) {
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
@@ -844,25 +637,24 @@ void MediumEnvironment::UpdateWifiLanMediumV2ForDiscovery(
|
||||
// Search advertising services and send notification.
|
||||
for (auto& advertising_service : info.advertising_services) {
|
||||
auto& service_info = advertising_service.second;
|
||||
OnWifiLanServiceV2StateChanged(context, service_info, /*enabled=*/true);
|
||||
OnWifiLanServiceStateChanged(context, service_info, /*enabled=*/true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UnregisterWifiLanMediumV2(
|
||||
api::WifiLanMediumV2& medium) {
|
||||
void MediumEnvironment::UnregisterWifiLanMedium(api::WifiLanMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
auto item = wifi_lan_mediums_v2_.extract(&medium);
|
||||
auto item = wifi_lan_mediums_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO) << "Unregistered WifiLan medium";
|
||||
});
|
||||
}
|
||||
|
||||
api::WifiLanMediumV2* MediumEnvironment::GetWifiLanV2Medium(
|
||||
api::WifiLanMedium* MediumEnvironment::GetWifiLanMedium(
|
||||
const std::string& ip_address, int port) {
|
||||
for (auto& medium_info : wifi_lan_mediums_v2_) {
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto* medium_found = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
for (auto& advertising_service : info.advertising_services) {
|
||||
|
||||
@@ -23,9 +23,8 @@
|
||||
#include "platform/api/ble.h"
|
||||
#include "platform/api/bluetooth_adapter.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/api/wifi_lan_v2.h"
|
||||
#include "platform/api/webrtc.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "platform/base/feature_flags.h"
|
||||
#include "platform/base/listeners.h"
|
||||
@@ -63,10 +62,6 @@ class MediumEnvironment {
|
||||
api::WebRtcSignalingMessenger::OnSignalingCompleteCallback;
|
||||
using WifiLanDiscoveredServiceCallback =
|
||||
api::WifiLanMedium::DiscoveredServiceCallback;
|
||||
using WifiLanAcceptedConnectionCallback =
|
||||
api::WifiLanMedium::AcceptedConnectionCallback;
|
||||
using WifiLanDiscoveredServiceV2Callback =
|
||||
api::WifiLanMediumV2::DiscoveredServiceCallback;
|
||||
|
||||
MediumEnvironment(const MediumEnvironment&) = delete;
|
||||
MediumEnvironment& operator=(const MediumEnvironment&) = delete;
|
||||
@@ -213,60 +208,16 @@ class MediumEnvironment {
|
||||
// Updates advertising info to indicate the current medium is exposing
|
||||
// advertising event.
|
||||
void UpdateWifiLanMediumForAdvertising(api::WifiLanMedium& medium,
|
||||
api::WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info,
|
||||
bool enabled);
|
||||
|
||||
// Updates discovery callback info to allow for dispatch of discovery events.
|
||||
//
|
||||
// Invokes callback asynchronously when any changes happen to discoverable
|
||||
// devices, or if the defice is turned off, whether or not it is discoverable,
|
||||
// if it was ever reported as discoverable.
|
||||
//
|
||||
// This should be called when discoverable state changes.
|
||||
// with user-specified callback when discovery is enabled, and with default
|
||||
// (empty) callback otherwise.
|
||||
void UpdateWifiLanMediumForDiscovery(
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanDiscoveredServiceCallback callback, bool enabled);
|
||||
|
||||
// Updates Accepted connection callback info to allow for dispatch of
|
||||
// advertising events.
|
||||
void UpdateWifiLanMediumForAcceptedConnection(
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback callback);
|
||||
|
||||
// Removes medium-related info. This should correspond to device power off.
|
||||
void UnregisterWifiLanMedium(api::WifiLanMedium& medium);
|
||||
|
||||
// Call back when advertising has created the server socket and is ready for
|
||||
// connect.
|
||||
void CallWifiLanAcceptedConnectionCallback(api::WifiLanMedium& medium,
|
||||
api::WifiLanSocket& socket,
|
||||
const std::string& service_id);
|
||||
|
||||
// Returns WifiLan service matching IP address and port, or nullptr.
|
||||
api::WifiLanService* GetWifiLanService(const std::string& ip_address,
|
||||
int port);
|
||||
|
||||
// Adds medium-related info to allow for discovery/advertising to work.
|
||||
// This provides acccess to this medium from other mediums, when protocol
|
||||
// expects they should communicate.
|
||||
void RegisterWifiLanMediumV2(api::WifiLanMediumV2& medium);
|
||||
|
||||
// Updates advertising info to indicate the current medium is exposing
|
||||
// advertising event.
|
||||
void UpdateWifiLanMediumV2ForAdvertising(
|
||||
api::WifiLanMediumV2& medium, const NsdServiceInfo& nsd_service_info,
|
||||
bool enabled);
|
||||
|
||||
// Updates discovery callback info to allow for dispatch of discovery events.
|
||||
//
|
||||
// This should be called when discoverable state changes.
|
||||
// with user-specified callback when discovery is enabled, and with default
|
||||
// (empty) callback otherwise.
|
||||
void UpdateWifiLanMediumV2ForDiscovery(
|
||||
api::WifiLanMediumV2& medium, WifiLanDiscoveredServiceV2Callback callback,
|
||||
api::WifiLanMedium& medium, WifiLanDiscoveredServiceCallback callback,
|
||||
const std::string& service_type, bool enabled);
|
||||
|
||||
// Gets Fake IP address for WifiLan medium.
|
||||
@@ -276,12 +227,11 @@ class MediumEnvironment {
|
||||
int GetFakePort() const;
|
||||
|
||||
// Removes medium-related info. This should correspond to device power off.
|
||||
void UnregisterWifiLanMediumV2(api::WifiLanMediumV2& medium);
|
||||
void UnregisterWifiLanMedium(api::WifiLanMedium& medium);
|
||||
|
||||
// Returns WifiLan medium whose advertising service matching IP address and
|
||||
// port, or nullptr.
|
||||
api::WifiLanMediumV2* GetWifiLanV2Medium(const std::string& ip_address,
|
||||
int port);
|
||||
api::WifiLanMedium* GetWifiLanMedium(const std::string& ip_address, int port);
|
||||
|
||||
void SetFeatureFlags(const FeatureFlags::Flags& flags);
|
||||
|
||||
@@ -301,26 +251,14 @@ class MediumEnvironment {
|
||||
bool fast_advertisement = false;
|
||||
};
|
||||
|
||||
struct WifiLanServiceIdContext {
|
||||
WifiLanDiscoveredServiceCallback discovery_callback;
|
||||
WifiLanAcceptedConnectionCallback accepted_connection_callback;
|
||||
bool advertising = false;
|
||||
};
|
||||
|
||||
struct WifiLanMediumContext {
|
||||
api::WifiLanService* wifi_lan_service = nullptr;
|
||||
absl::flat_hash_map<std::string, WifiLanServiceIdContext> services;
|
||||
};
|
||||
|
||||
struct WifiLanMediumV2Context {
|
||||
// advertising service type vs NsdServiceInfo map.
|
||||
absl::flat_hash_map<std::string, NsdServiceInfo> advertising_services;
|
||||
// discovered service type vs callback map.
|
||||
absl::flat_hash_map<std::string, WifiLanDiscoveredServiceV2Callback>
|
||||
absl::flat_hash_map<std::string, WifiLanDiscoveredServiceCallback>
|
||||
discovered_callbacks;
|
||||
// discovered service vs service type map.
|
||||
absl::flat_hash_map<std::string, NsdServiceInfo>
|
||||
discovered_services;
|
||||
absl::flat_hash_map<std::string, NsdServiceInfo> discovered_services;
|
||||
};
|
||||
|
||||
// This is a singleton object, for which destructor will never be called.
|
||||
@@ -342,14 +280,9 @@ class MediumEnvironment {
|
||||
bool fast_advertisement, bool enabled);
|
||||
|
||||
void OnWifiLanServiceStateChanged(WifiLanMediumContext& info,
|
||||
api::WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id,
|
||||
const NsdServiceInfo& service_info,
|
||||
bool enabled);
|
||||
|
||||
void OnWifiLanServiceV2StateChanged(WifiLanMediumV2Context& info,
|
||||
const NsdServiceInfo& service_info,
|
||||
bool enabled);
|
||||
|
||||
void RunOnMediumEnvironmentThread(std::function<void()> runnable);
|
||||
|
||||
std::atomic_bool enabled_ = true;
|
||||
@@ -378,9 +311,6 @@ class MediumEnvironment {
|
||||
absl::flat_hash_map<api::WifiLanMedium*, WifiLanMediumContext>
|
||||
wifi_lan_mediums_;
|
||||
|
||||
absl::flat_hash_map<api::WifiLanMediumV2*, WifiLanMediumV2Context>
|
||||
wifi_lan_mediums_v2_;
|
||||
|
||||
bool use_valid_peer_connection_ = true;
|
||||
absl::Duration peer_connection_latency_ = absl::ZeroDuration();
|
||||
};
|
||||
|
||||
@@ -57,7 +57,6 @@ cc_library(
|
||||
"bluetooth_classic.cc",
|
||||
"webrtc.cc",
|
||||
"wifi_lan.cc",
|
||||
"wifi_lan_v2.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"ble.h",
|
||||
@@ -65,7 +64,6 @@ cc_library(
|
||||
"bluetooth_classic.h",
|
||||
"webrtc.h",
|
||||
"wifi_lan.h",
|
||||
"wifi_lan_v2.h",
|
||||
],
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
|
||||
@@ -49,7 +49,6 @@
|
||||
#include "platform/impl/g3/single_thread_executor.h"
|
||||
#include "platform/impl/g3/webrtc.h"
|
||||
#include "platform/impl/g3/wifi_lan.h"
|
||||
#include "platform/impl/g3/wifi_lan_v2.h"
|
||||
#include "platform/impl/shared/file.h"
|
||||
|
||||
namespace location {
|
||||
@@ -147,11 +146,6 @@ std::unique_ptr<WifiLanMedium> ImplementationPlatform::CreateWifiLanMedium() {
|
||||
return absl::make_unique<g3::WifiLanMedium>();
|
||||
}
|
||||
|
||||
std::unique_ptr<WifiLanMediumV2>
|
||||
ImplementationPlatform::CreateWifiLanMediumV2() {
|
||||
return absl::make_unique<g3::WifiLanMediumV2>();
|
||||
}
|
||||
|
||||
std::unique_ptr<WebRtcMedium> ImplementationPlatform::CreateWebRtcMedium() {
|
||||
if (MediumEnvironment::Instance().GetEnvironmentConfig().webrtc_enabled) {
|
||||
return absl::make_unique<g3::WebRtcMedium>();
|
||||
|
||||
+157
-207
@@ -19,13 +19,14 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/base/cancellation_flag_listener.h"
|
||||
#include "platform/base/logging.h"
|
||||
#include "platform/base/medium_environment.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/prng.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -73,20 +74,13 @@ Exception WifiLanSocket::Close() {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
WifiLanService* WifiLanSocket::GetRemoteWifiLanService() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return wifi_lan_service_;
|
||||
}
|
||||
|
||||
void WifiLanSocket::DoClose() {
|
||||
if (!closed_) {
|
||||
remote_socket_ = nullptr;
|
||||
output_->GetOutputStream().Close();
|
||||
output_->GetInputStream().Close();
|
||||
if (IsConnectedLocked()) {
|
||||
input_->GetOutputStream().Close();
|
||||
input_->GetInputStream().Close();
|
||||
}
|
||||
input_->GetOutputStream().Close();
|
||||
input_->GetInputStream().Close();
|
||||
closed_ = true;
|
||||
}
|
||||
}
|
||||
@@ -103,19 +97,32 @@ OutputStream& WifiLanSocket::GetLocalOutputStream() {
|
||||
return output_->GetOutputStream();
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept(
|
||||
WifiLanService* wifi_lan_service) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (closed_) return {};
|
||||
while (pending_sockets_.empty()) {
|
||||
cond_.Wait(&mutex_);
|
||||
if (closed_) break;
|
||||
std::string WifiLanServerSocket::GetName(const std::string& ip_address,
|
||||
int port) {
|
||||
std::string dot_delimited_string;
|
||||
if (!ip_address.empty()) {
|
||||
for (auto byte : ip_address) {
|
||||
if (!dot_delimited_string.empty())
|
||||
absl::StrAppend(&dot_delimited_string, ".");
|
||||
absl::StrAppend(&dot_delimited_string, absl::StrFormat("%d", byte));
|
||||
}
|
||||
}
|
||||
std::string out = absl::StrCat(dot_delimited_string, ":", port);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
while (!closed_ && pending_sockets_.empty()) {
|
||||
cond_.Wait(&mutex_);
|
||||
}
|
||||
// whether or not we were running in the wait loop, return early if closed.
|
||||
if (closed_) return {};
|
||||
auto* remote_socket =
|
||||
pending_sockets_.extract(pending_sockets_.begin()).value();
|
||||
CHECK(remote_socket);
|
||||
auto local_socket = std::make_unique<WifiLanSocket>(wifi_lan_service);
|
||||
|
||||
auto local_socket = std::make_unique<WifiLanSocket>();
|
||||
local_socket->Connect(*remote_socket);
|
||||
remote_socket->Connect(*local_socket);
|
||||
cond_.SignalAll();
|
||||
@@ -126,12 +133,12 @@ bool WifiLanServerSocket::Connect(WifiLanSocket& socket) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (closed_) return false;
|
||||
if (socket.IsConnected()) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"Failed to connect to WifiLan server socket: already connected");
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "Failed to connect to WifiLan server socket: already connected";
|
||||
return true; // already connected.
|
||||
}
|
||||
// add client socket to the pending list
|
||||
pending_sockets_.emplace(&socket);
|
||||
pending_sockets_.insert(&socket);
|
||||
cond_.SignalAll();
|
||||
while (!socket.IsConnected()) {
|
||||
cond_.Wait(&mutex_);
|
||||
@@ -173,247 +180,190 @@ Exception WifiLanServerSocket::DoClose() {
|
||||
}
|
||||
|
||||
WifiLanMedium::WifiLanMedium() {
|
||||
wifi_lan_service_.SetMedium(this);
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.RegisterWifiLanMedium(*this);
|
||||
}
|
||||
|
||||
WifiLanMedium::~WifiLanMedium() {
|
||||
wifi_lan_service_.SetMedium(nullptr);
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UnregisterWifiLanMedium(*this);
|
||||
|
||||
StopAdvertising(advertising_info_.service_id);
|
||||
StopDiscovery(discovering_info_.service_id);
|
||||
|
||||
NEARBY_LOG(INFO, "WifiLanMedium dtor advertising_accept_thread_running_ = %d",
|
||||
acceptance_thread_running_.load());
|
||||
// If acceptance thread is still running, wait to finish.
|
||||
if (acceptance_thread_running_) {
|
||||
while (acceptance_thread_running_) {
|
||||
shared::CountDownLatch latch(1);
|
||||
close_accept_loops_runner_.Execute([&latch]() { latch.CountDown(); });
|
||||
latch.Await();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartAdvertising(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StartAdvertising: service_id=%s, nsd_service_info=%p, "
|
||||
"service_info_name=%s",
|
||||
service_id.c_str(), &nsd_service_info,
|
||||
nsd_service_info.GetServiceName().c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
NsdServiceInfo local_nsd_service_info{nsd_service_info};
|
||||
SetWifiLanService(nsd_service_info);
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, wifi_lan_service_, service_id,
|
||||
true);
|
||||
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (server_socket_ != nullptr) server_socket_.release();
|
||||
server_socket_ = std::make_unique<WifiLanServerSocket>();
|
||||
|
||||
acceptance_thread_running_.exchange(true);
|
||||
accept_loops_runner_.Execute([&env, this, service_id]() mutable {
|
||||
if (!accept_loops_runner_.InShutdown()) {
|
||||
while (true) {
|
||||
auto client_socket = server_socket_->Accept(&wifi_lan_service_);
|
||||
if (client_socket == nullptr) break;
|
||||
env.CallWifiLanAcceptedConnectionCallback(
|
||||
*this, *(client_socket.release()), service_id);
|
||||
}
|
||||
}
|
||||
acceptance_thread_running_.exchange(false);
|
||||
});
|
||||
advertising_info_.service_id = service_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StopAdvertising: service_id=%s",
|
||||
service_id.c_str());
|
||||
bool WifiLanMedium::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
std::string service_type = nsd_service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StartAdvertising: nsd_service_info="
|
||||
<< &nsd_service_info
|
||||
<< ", service_name=" << nsd_service_info.GetServiceName()
|
||||
<< ", service_type=" << service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (advertising_info_.Empty()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopAdvertising: Can't stop advertising because "
|
||||
"we never started advertising.");
|
||||
if (advertising_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StartAdvertising: Can't start advertising because "
|
||||
"service_type="
|
||||
<< service_type << ", has started already.";
|
||||
return false;
|
||||
}
|
||||
advertising_info_.Clear();
|
||||
}
|
||||
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, wifi_lan_service_, service_id,
|
||||
false);
|
||||
accept_loops_runner_.Shutdown();
|
||||
if (server_socket_ == nullptr) {
|
||||
NEARBY_LOGS(ERROR) << "G3 WifiLan StopAdvertising: failed to find WifiLan "
|
||||
"Server socket: service_id="
|
||||
<< service_id;
|
||||
// Fall through for server socket not found.
|
||||
return true;
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, nsd_service_info,
|
||||
/*enabled=*/true);
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
advertising_info_.Add(service_type);
|
||||
}
|
||||
|
||||
if (!server_socket_->Close().Ok()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopAdvertising: Failed to close WifiLan server "
|
||||
"socket for %s.",
|
||||
service_id.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartDiscovery(const std::string& service_id,
|
||||
DiscoveredServiceCallback callback) {
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StartDiscovery: service_id=%s",
|
||||
service_id.c_str());
|
||||
bool WifiLanMedium::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
std::string service_type = nsd_service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StopAdvertising: nsd_service_info="
|
||||
<< &nsd_service_info
|
||||
<< ", service_name=" << nsd_service_info.GetServiceName()
|
||||
<< ", service_type=" << service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (!advertising_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StopAdvertising: Can't stop advertising because "
|
||||
"we never started advertising for service_type="
|
||||
<< service_type;
|
||||
return false;
|
||||
}
|
||||
advertising_info_.Remove(service_type);
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_id, std::move(callback),
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, nsd_service_info,
|
||||
/*enabled=*/false);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartDiscovery(const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StartDiscovery: service_type="
|
||||
<< service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (discovering_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StartDiscovery: Can't start discovery because "
|
||||
"service_type="
|
||||
<< service_type << " has started already.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, std::move(callback), service_type,
|
||||
true);
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
discovering_info_.service_id = service_id;
|
||||
discovering_info_.Add(service_type);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StopDiscovery(const std::string& service_id) {
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StopDiscovery: service_id=%s",
|
||||
service_id.c_str());
|
||||
bool WifiLanMedium::StopDiscovery(const std::string& service_type) {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StopDiscovery: service_type="
|
||||
<< service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (discovering_info_.Empty()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopDiscovery: Can't stop discovering because we "
|
||||
"never started discovering.");
|
||||
if (!discovering_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StopDiscovery: Can't stop discovering because we "
|
||||
"never started discovering.";
|
||||
return false;
|
||||
}
|
||||
discovering_info_.Clear();
|
||||
discovering_info_.Remove(service_type);
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, {}, service_type, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
|
||||
const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
std::string service_type = remote_service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan ConnectToService [self]: medium=" << this
|
||||
<< ", service_type=" << service_type;
|
||||
return ConnectToService(remote_service_info.GetIPAddress(),
|
||||
remote_service_info.GetPort(), cancellation_flag);
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
std::string socket_name = WifiLanServerSocket::GetName(ip_address, port);
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan ConnectToService [self]: medium=" << this
|
||||
<< ", ip address + port=" << socket_name;
|
||||
// First, find an instance of remote medium, that exposed this service.
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
auto* remote_medium =
|
||||
static_cast<WifiLanMedium*>(env.GetWifiLanMedium(ip_address, port));
|
||||
if (!remote_medium) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_id, {}, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartAcceptingConnections(
|
||||
const std::string& service_id, AcceptedConnectionCallback callback) {
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StartAcceptingConnections: service_id=%s",
|
||||
service_id.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_id, callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) {
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StopAcceptingConnections: service_id=%s",
|
||||
service_id.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_id, {});
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
|
||||
api::WifiLanService& remote_wifi_lan_service, const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan Connect: medium=%p, wifi_lan_service=%p, "
|
||||
"service_info_name=%s, service_id=%s",
|
||||
this, &wifi_lan_service_,
|
||||
remote_wifi_lan_service.GetServiceInfo().GetServiceName().c_str(),
|
||||
service_id.c_str());
|
||||
// First, find an instance of remote medium, that exposed this service.
|
||||
auto* remote_medium =
|
||||
static_cast<WifiLanService&>(remote_wifi_lan_service).GetMedium();
|
||||
|
||||
if (!remote_medium) return {}; // Can't find medium. Bail out.
|
||||
|
||||
WifiLanServerSocket* remote_server_socket = nullptr;
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan Connect [peer]: remote_wifi_lan_service=%p, "
|
||||
"remote_service_info_name=%s, service_id=%s",
|
||||
&remote_wifi_lan_service,
|
||||
remote_wifi_lan_service.GetServiceInfo().GetServiceName().c_str(),
|
||||
service_id.c_str());
|
||||
WifiLanServerSocket* server_socket = nullptr;
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan ConnectToService [peer]: medium="
|
||||
<< remote_medium
|
||||
<< ", remote ip address + port=" << socket_name;
|
||||
// Then, find our server socket context in this medium.
|
||||
{
|
||||
absl::MutexLock medium_lock(&remote_medium->mutex_);
|
||||
remote_server_socket = remote_medium->server_socket_.get();
|
||||
if (remote_server_socket == nullptr) {
|
||||
auto item = remote_medium->server_sockets_.find(socket_name);
|
||||
server_socket = item != server_sockets_.end() ? item->second : nullptr;
|
||||
if (server_socket == nullptr) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "G3 WifiLan Connect: Failed to find remote WifiLan Server socket: "
|
||||
"service_id="
|
||||
<< service_id;
|
||||
// Fall through for server socket not found.
|
||||
<< "G3 WifiLan Failed to find WifiLan Server socket: socket_name="
|
||||
<< socket_name;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if (cancellation_flag->Cancelled()) {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan Connect: Has been cancelled: "
|
||||
"service_id="
|
||||
<< service_id;
|
||||
NEARBY_LOGS(ERROR) << "G3 WifiLan Connect: Has been cancelled: socket_name="
|
||||
<< socket_name;
|
||||
return {};
|
||||
}
|
||||
|
||||
CancellationFlagListener listener(cancellation_flag, [this]() {
|
||||
CancellationFlagListener listener(cancellation_flag, [&server_socket]() {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan Cancel Connect.";
|
||||
if (server_socket_ != nullptr) server_socket_->Close();
|
||||
if (server_socket != nullptr) {
|
||||
server_socket->Close();
|
||||
}
|
||||
});
|
||||
|
||||
WifiLanService wifi_lan_service =
|
||||
static_cast<WifiLanService&>(remote_wifi_lan_service);
|
||||
auto socket = std::make_unique<WifiLanSocket>(&wifi_lan_service);
|
||||
auto socket = std::make_unique<WifiLanSocket>();
|
||||
// Finally, Request to connect to this socket.
|
||||
if (!remote_server_socket->Connect(*socket)) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"G3 WifiLan Connect: Failed to connect to existing WifiLan "
|
||||
"Server socket: service_id=%s",
|
||||
service_id.c_str());
|
||||
if (!server_socket->Connect(*socket)) {
|
||||
NEARBY_LOGS(ERROR) << "G3 WifiLan Failed to connect to existing WifiLan "
|
||||
"Server socket: name="
|
||||
<< socket_name;
|
||||
return {};
|
||||
}
|
||||
|
||||
NEARBY_LOG(INFO, "G3 WifiLan Connect: connected: socket=%p", socket.get());
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan ConnectToService: connected: socket="
|
||||
<< socket.get();
|
||||
return socket;
|
||||
}
|
||||
|
||||
api::WifiLanService* WifiLanMedium::GetRemoteService(
|
||||
const std::string& ip_address, int port) {
|
||||
std::unique_ptr<api::WifiLanServerSocket> WifiLanMedium::ListenForService(
|
||||
int port) {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
return env.GetWifiLanService(ip_address, port);
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan GetCredential: service_id=" << service_id;
|
||||
return std::make_pair(wifi_lan_service_.GetServiceInfo().GetIPAddress(),
|
||||
wifi_lan_service_.GetServiceInfo().GetPort());
|
||||
}
|
||||
|
||||
void WifiLanMedium::SetWifiLanService(const NsdServiceInfo& nsd_service_info) {
|
||||
NsdServiceInfo local_nsd_service_info{nsd_service_info};
|
||||
auto credential = GetFakeCredentials();
|
||||
local_nsd_service_info.SetIPAddress(credential.first);
|
||||
local_nsd_service_info.SetPort(credential.second);
|
||||
wifi_lan_service_.SetServiceInfo(local_nsd_service_info);
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetFakeCredentials() const {
|
||||
std::string ip_address;
|
||||
ip_address.resize(4);
|
||||
uint32_t raw_ip_addr = Prng().NextUint32();
|
||||
uint16_t port = Prng().NextUint32();
|
||||
ip_address[0] = static_cast<char>(raw_ip_addr >> 24);
|
||||
ip_address[1] = static_cast<char>(raw_ip_addr >> 16);
|
||||
ip_address[2] = static_cast<char>(raw_ip_addr >> 8);
|
||||
ip_address[3] = static_cast<char>(raw_ip_addr >> 0);
|
||||
|
||||
return std::make_pair(ip_address, port);
|
||||
auto server_socket = std::make_unique<WifiLanServerSocket>();
|
||||
server_socket->SetIPAddress(env.GetFakeIPAddress());
|
||||
server_socket->SetPort(port == 0 ? env.GetFakePort() : port);
|
||||
std::string socket_name = WifiLanServerSocket::GetName(
|
||||
server_socket->GetIPAddress(), server_socket->GetPort());
|
||||
server_socket->SetCloseNotifier([this, socket_name]() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
server_sockets_.erase(socket_name);
|
||||
});
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan Adding server socket: medium=" << this
|
||||
<< ", socket_name=" << socket_name;
|
||||
absl::MutexLock lock(&mutex_);
|
||||
server_sockets_.insert({socket_name, server_socket.get()});
|
||||
return server_socket;
|
||||
}
|
||||
|
||||
} // namespace g3
|
||||
|
||||
+111
-89
@@ -36,34 +36,9 @@ namespace g3 {
|
||||
|
||||
class WifiLanMedium;
|
||||
|
||||
// Opaque wrapper over a WifiLan service which contains |NsdServiceInfo|.
|
||||
class WifiLanService : public api::WifiLanService {
|
||||
public:
|
||||
WifiLanService() = default;
|
||||
explicit WifiLanService(NsdServiceInfo nsd_service_info)
|
||||
: nsd_service_info_(std::move(nsd_service_info)) {}
|
||||
~WifiLanService() override = default;
|
||||
|
||||
NsdServiceInfo GetServiceInfo() const override { return nsd_service_info_; }
|
||||
|
||||
void SetServiceInfo(NsdServiceInfo nsd_service_info) {
|
||||
nsd_service_info_ = std::move(nsd_service_info);
|
||||
}
|
||||
|
||||
WifiLanMedium* GetMedium() { return medium_; }
|
||||
|
||||
void SetMedium(WifiLanMedium* medium) { medium_ = medium; }
|
||||
|
||||
private:
|
||||
NsdServiceInfo nsd_service_info_;
|
||||
WifiLanMedium* medium_ = nullptr;
|
||||
};
|
||||
|
||||
class WifiLanSocket : public api::WifiLanSocket {
|
||||
public:
|
||||
WifiLanSocket() = default;
|
||||
explicit WifiLanSocket(WifiLanService* wifi_lan_service)
|
||||
: wifi_lan_service_(wifi_lan_service) {}
|
||||
~WifiLanSocket() override;
|
||||
|
||||
// Connect to another WifiLanSocket, to form a functional low-level channel.
|
||||
@@ -89,11 +64,6 @@ class WifiLanSocket : public api::WifiLanSocket {
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns valid WifiLanService pointer if there is a connection, and
|
||||
// nullptr otherwise.
|
||||
WifiLanService* GetRemoteWifiLanService() override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
void DoClose() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
@@ -116,14 +86,39 @@ class WifiLanSocket : public api::WifiLanSocket {
|
||||
std::shared_ptr<Pipe> output_{new Pipe};
|
||||
std::shared_ptr<Pipe> input_;
|
||||
mutable absl::Mutex mutex_;
|
||||
WifiLanService* wifi_lan_service_;
|
||||
WifiLanSocket* remote_socket_ ABSL_GUARDED_BY(mutex_) = nullptr;
|
||||
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
};
|
||||
|
||||
class WifiLanServerSocket {
|
||||
class WifiLanServerSocket : public api::WifiLanServerSocket {
|
||||
public:
|
||||
~WifiLanServerSocket();
|
||||
static std::string GetName(const std::string& ip_address, int port);
|
||||
|
||||
~WifiLanServerSocket() override;
|
||||
|
||||
// Gets ip address.
|
||||
std::string GetIPAddress() const override ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return ip_address_;
|
||||
}
|
||||
|
||||
// Sets the ip address.
|
||||
void SetIPAddress(const std::string& ip_address) ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
ip_address_ = ip_address;
|
||||
}
|
||||
|
||||
// Gets the port.
|
||||
int GetPort() const override ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return port_;
|
||||
}
|
||||
|
||||
// Sets the port.
|
||||
void SetPort(int port) ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
port_ = port;
|
||||
}
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
@@ -135,7 +130,7 @@ class WifiLanServerSocket {
|
||||
// Called by the server side of a connection.
|
||||
// Returns WifiLanSocket to the server side.
|
||||
// If not null, returned socket is connected to its remote (client-side) peer.
|
||||
std::unique_ptr<api::WifiLanSocket> Accept(WifiLanService* service)
|
||||
std::unique_ptr<api::WifiLanSocket> Accept() override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Blocks until either:
|
||||
@@ -149,18 +144,20 @@ class WifiLanServerSocket {
|
||||
|
||||
// Called by the server side of a connection before passing ownership of
|
||||
// WifiLanServerSocker to user, to track validity of a pointer to this
|
||||
// server socket,
|
||||
// server socket.
|
||||
void SetCloseNotifier(std::function<void()> notifier)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
// Calls close_notifier if it was previously set, and marks socket as closed.
|
||||
Exception Close() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
Exception DoClose() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
absl::Mutex mutex_;
|
||||
mutable absl::Mutex mutex_;
|
||||
std::string ip_address_ ABSL_GUARDED_BY(mutex_);
|
||||
int port_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::CondVar cond_;
|
||||
absl::flat_hash_set<WifiLanSocket*> pending_sockets_ ABSL_GUARDED_BY(mutex_);
|
||||
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
|
||||
@@ -173,81 +170,106 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
WifiLanMedium();
|
||||
~WifiLanMedium() override;
|
||||
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAdvertising(const std::string& service_id) override
|
||||
// Starts WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service is now advertising.
|
||||
// On error if the service cannot start to advertise or the service type in
|
||||
// NsdServiceInfo has been passed previously which StopAdvertising is not
|
||||
// been called.
|
||||
bool StartAdvertising(const NsdServiceInfo& nsd_service_info) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns true once the WifiLan discovery has been initiated.
|
||||
bool StartDiscovery(const std::string& service_id,
|
||||
// Stops WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service stops advertising.
|
||||
// On error if the service cannot stop advertising or the service type in
|
||||
// NsdServiceInfo cannot be found.
|
||||
bool StopAdvertising(const NsdServiceInfo& nsd_service_info) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Starts the discovery of nearby WifiLan services.
|
||||
//
|
||||
// Returns true once the WifiLan discovery has been initiated. The
|
||||
// service_type is associated with callback.
|
||||
bool StartDiscovery(const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns true once WifiLan discovery for service_id is well and truly
|
||||
// stopped; after this returns, there must be no more invocations of the
|
||||
// DiscoveredServiceCallback passed in to StartDiscovery() for service_id.
|
||||
bool StopDiscovery(const std::string& service_id) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns true once WifiLan socket connection requests to service_id can be
|
||||
// accepted.
|
||||
bool StartAcceptingConnections(const std::string& service_id,
|
||||
AcceptedConnectionCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAcceptingConnections(const std::string& service_id) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Connects to existing remote WifiLan service.
|
||||
// Stops the discovery of nearby WifiLan services.
|
||||
//
|
||||
// service_type - The one assigend in StartDiscovery.
|
||||
// On success if service_type is matched to the callback and will be removed
|
||||
// from the list. If list is empty then stops the WifiLan discovery
|
||||
// service.
|
||||
// On error if the service_type is not existed, then return immediately.
|
||||
bool StopDiscovery(const std::string& service_type) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Connects to a WifiLan service.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::WifiLanSocket> Connect(
|
||||
api::WifiLanService& remote_wifi_lan_service,
|
||||
const std::string& service_id,
|
||||
std::unique_ptr<api::WifiLanSocket> ConnectToService(
|
||||
const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
api::WifiLanService* GetRemoteService(const std::string& ip_address,
|
||||
int port) override;
|
||||
// Connects to a WifiLan service by ip address and port.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::WifiLanSocket> ConnectToService(
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
std::pair<std::string, int> GetCredentials(
|
||||
const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
// Listens for incoming connection.
|
||||
//
|
||||
// port - A port number.
|
||||
// 0 : use a random port.
|
||||
// 1~65536 : open a server socket on that exact port.
|
||||
// On success, returns a new WifiLanServerSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::WifiLanServerSocket> ListenForService(int port) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
static constexpr int kMaxConcurrentAcceptLoops = 5;
|
||||
|
||||
struct AdvertisingInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
bool Empty() const { return service_types.empty(); }
|
||||
void Clear() { service_types.clear(); }
|
||||
void Add(const std::string& service_type) {
|
||||
service_types.insert(service_type);
|
||||
}
|
||||
void Remove(const std::string& service_type) {
|
||||
service_types.erase(service_type);
|
||||
}
|
||||
bool Existed(const std::string& service_type) const {
|
||||
return service_types.contains(service_type);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_types;
|
||||
};
|
||||
|
||||
struct DiscoveringInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
bool Empty() const { return service_types.empty(); }
|
||||
void Clear() { service_types.clear(); }
|
||||
void Add(const std::string& service_type) {
|
||||
service_types.insert(service_type);
|
||||
}
|
||||
void Remove(const std::string& service_type) {
|
||||
service_types.erase(service_type);
|
||||
}
|
||||
bool Existed(const std::string& service_type) const {
|
||||
return service_types.contains(service_type);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_types;
|
||||
};
|
||||
|
||||
void SetWifiLanService(const NsdServiceInfo& nsd_service_info);
|
||||
std::pair<std::string, int> GetFakeCredentials() const;
|
||||
|
||||
absl::Mutex mutex_;
|
||||
WifiLanService wifi_lan_service_;
|
||||
|
||||
// A thread pool dedicated to running all the accept loops from
|
||||
// StartAdvertising().
|
||||
MultiThreadExecutor accept_loops_runner_{kMaxConcurrentAcceptLoops};
|
||||
std::atomic_bool acceptance_thread_running_ = false;
|
||||
|
||||
// A thread pool dedicated to wait to complete the accept_loops_runner_.
|
||||
MultiThreadExecutor close_accept_loops_runner_{kMaxConcurrentAcceptLoops};
|
||||
|
||||
// A server socket is established when start advertising.
|
||||
std::unique_ptr<WifiLanServerSocket> server_socket_;
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
DiscoveringInfo discovering_info_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_map<std::string, WifiLanServerSocket*> server_sockets_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
|
||||
@@ -1,371 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "platform/impl/g3/wifi_lan_v2.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "platform/api/wifi_lan_v2.h"
|
||||
#include "platform/base/cancellation_flag_listener.h"
|
||||
#include "platform/base/logging.h"
|
||||
#include "platform/base/medium_environment.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
|
||||
WifiLanSocketV2::~WifiLanSocketV2() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
DoClose();
|
||||
}
|
||||
|
||||
void WifiLanSocketV2::Connect(WifiLanSocketV2& other) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
remote_socket_ = &other;
|
||||
input_ = other.output_;
|
||||
}
|
||||
|
||||
InputStream& WifiLanSocketV2::GetInputStream() {
|
||||
auto* remote_socket = GetRemoteSocket();
|
||||
CHECK(remote_socket != nullptr);
|
||||
return remote_socket->GetLocalInputStream();
|
||||
}
|
||||
|
||||
OutputStream& WifiLanSocketV2::GetOutputStream() {
|
||||
return GetLocalOutputStream();
|
||||
}
|
||||
|
||||
WifiLanSocketV2* WifiLanSocketV2::GetRemoteSocket() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return remote_socket_;
|
||||
}
|
||||
|
||||
bool WifiLanSocketV2::IsConnected() const {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return IsConnectedLocked();
|
||||
}
|
||||
|
||||
bool WifiLanSocketV2::IsClosed() const {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return closed_;
|
||||
}
|
||||
|
||||
Exception WifiLanSocketV2::Close() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
DoClose();
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
void WifiLanSocketV2::DoClose() {
|
||||
if (!closed_) {
|
||||
remote_socket_ = nullptr;
|
||||
output_->GetOutputStream().Close();
|
||||
output_->GetInputStream().Close();
|
||||
input_->GetOutputStream().Close();
|
||||
input_->GetInputStream().Close();
|
||||
closed_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiLanSocketV2::IsConnectedLocked() const { return input_ != nullptr; }
|
||||
|
||||
InputStream& WifiLanSocketV2::GetLocalInputStream() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return output_->GetInputStream();
|
||||
}
|
||||
|
||||
OutputStream& WifiLanSocketV2::GetLocalOutputStream() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return output_->GetOutputStream();
|
||||
}
|
||||
|
||||
std::string WifiLanServerSocketV2::GetName(const std::string& ip_address,
|
||||
int port) {
|
||||
std::string dot_delimited_string;
|
||||
if (!ip_address.empty()) {
|
||||
for (auto byte : ip_address) {
|
||||
if (!dot_delimited_string.empty())
|
||||
absl::StrAppend(&dot_delimited_string, ".");
|
||||
absl::StrAppend(&dot_delimited_string, absl::StrFormat("%d", byte));
|
||||
}
|
||||
}
|
||||
std::string out = absl::StrCat(dot_delimited_string, ":", port);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocketV2> WifiLanServerSocketV2::Accept() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
while (!closed_ && pending_sockets_.empty()) {
|
||||
cond_.Wait(&mutex_);
|
||||
}
|
||||
// whether or not we were running in the wait loop, return early if closed.
|
||||
if (closed_) return {};
|
||||
auto* remote_socket =
|
||||
pending_sockets_.extract(pending_sockets_.begin()).value();
|
||||
CHECK(remote_socket);
|
||||
|
||||
auto local_socket = std::make_unique<WifiLanSocketV2>();
|
||||
local_socket->Connect(*remote_socket);
|
||||
remote_socket->Connect(*local_socket);
|
||||
cond_.SignalAll();
|
||||
return local_socket;
|
||||
}
|
||||
|
||||
bool WifiLanServerSocketV2::Connect(WifiLanSocketV2& socket) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (closed_) return false;
|
||||
if (socket.IsConnected()) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "Failed to connect to WifiLan server socket: already connected";
|
||||
return true; // already connected.
|
||||
}
|
||||
// add client socket to the pending list
|
||||
pending_sockets_.insert(&socket);
|
||||
cond_.SignalAll();
|
||||
while (!socket.IsConnected()) {
|
||||
cond_.Wait(&mutex_);
|
||||
if (closed_) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void WifiLanServerSocketV2::SetCloseNotifier(std::function<void()> notifier) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
close_notifier_ = std::move(notifier);
|
||||
}
|
||||
|
||||
WifiLanServerSocketV2::~WifiLanServerSocketV2() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
DoClose();
|
||||
}
|
||||
|
||||
Exception WifiLanServerSocketV2::Close() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return DoClose();
|
||||
}
|
||||
|
||||
Exception WifiLanServerSocketV2::DoClose() {
|
||||
bool should_notify = !closed_;
|
||||
closed_ = true;
|
||||
if (should_notify) {
|
||||
cond_.SignalAll();
|
||||
if (close_notifier_) {
|
||||
auto notifier = std::move(close_notifier_);
|
||||
mutex_.Unlock();
|
||||
// Notifier may contain calls to public API, and may cause deadlock, if
|
||||
// mutex_ is held during the call.
|
||||
notifier();
|
||||
mutex_.Lock();
|
||||
}
|
||||
}
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
WifiLanMediumV2::WifiLanMediumV2() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.RegisterWifiLanMediumV2(*this);
|
||||
}
|
||||
|
||||
WifiLanMediumV2::~WifiLanMediumV2() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UnregisterWifiLanMediumV2(*this);
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
std::string service_type = nsd_service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StartAdvertising: nsd_service_info="
|
||||
<< &nsd_service_info
|
||||
<< ", service_name=" << nsd_service_info.GetServiceName()
|
||||
<< ", service_type=" << service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (advertising_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StartAdvertising: Can't start advertising because "
|
||||
"service_type="
|
||||
<< service_type << ", has started already.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumV2ForAdvertising(*this, nsd_service_info,
|
||||
/*enabled=*/true);
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
advertising_info_.Add(service_type);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
std::string service_type = nsd_service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StopAdvertising: nsd_service_info="
|
||||
<< &nsd_service_info
|
||||
<< ", service_name=" << nsd_service_info.GetServiceName()
|
||||
<< ", service_type=" << service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (!advertising_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StopAdvertising: Can't stop advertising because "
|
||||
"we never started advertising for service_type="
|
||||
<< service_type;
|
||||
return false;
|
||||
}
|
||||
advertising_info_.Remove(service_type);
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumV2ForAdvertising(*this, nsd_service_info,
|
||||
/*enabled=*/false);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StartDiscovery(const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StartDiscovery: service_type="
|
||||
<< service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (discovering_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StartDiscovery: Can't start discovery because "
|
||||
"service_type="
|
||||
<< service_type << " has started already.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumV2ForDiscovery(*this, std::move(callback),
|
||||
service_type, true);
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
discovering_info_.Add(service_type);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StopDiscovery(const std::string& service_type) {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StopDiscovery: service_type="
|
||||
<< service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (!discovering_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StopDiscovery: Can't stop discovering because we "
|
||||
"never started discovering.";
|
||||
return false;
|
||||
}
|
||||
discovering_info_.Remove(service_type);
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumV2ForDiscovery(*this, {}, service_type, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocketV2> WifiLanMediumV2::ConnectToService(
|
||||
const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
std::string service_type = remote_service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan ConnectToService [self]: medium=" << this
|
||||
<< ", service_type=" << service_type;
|
||||
return ConnectToService(remote_service_info.GetIPAddress(),
|
||||
remote_service_info.GetPort(), cancellation_flag);
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocketV2> WifiLanMediumV2::ConnectToService(
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
std::string socket_name = WifiLanServerSocketV2::GetName(ip_address, port);
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan ConnectToService [self]: medium=" << this
|
||||
<< ", ip address + port=" << socket_name;
|
||||
// First, find an instance of remote medium, that exposed this service.
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
auto* remote_medium =
|
||||
static_cast<WifiLanMediumV2*>(env.GetWifiLanV2Medium(ip_address, port));
|
||||
if (!remote_medium) {
|
||||
return {};
|
||||
}
|
||||
|
||||
WifiLanServerSocketV2* server_socket = nullptr;
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan ConnectToService [peer]: medium="
|
||||
<< remote_medium
|
||||
<< ", remote ip address + port=" << socket_name;
|
||||
// Then, find our server socket context in this medium.
|
||||
{
|
||||
absl::MutexLock medium_lock(&remote_medium->mutex_);
|
||||
auto item = remote_medium->server_sockets_.find(socket_name);
|
||||
server_socket = item != server_sockets_.end() ? item->second : nullptr;
|
||||
if (server_socket == nullptr) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "G3 WifiLan Failed to find WifiLan Server socket: socket_name="
|
||||
<< socket_name;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if (cancellation_flag->Cancelled()) {
|
||||
NEARBY_LOGS(ERROR) << "G3 WifiLan Connect: Has been cancelled: socket_name="
|
||||
<< socket_name;
|
||||
return {};
|
||||
}
|
||||
|
||||
CancellationFlagListener listener(cancellation_flag, [&server_socket]() {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan Cancel Connect.";
|
||||
if (server_socket != nullptr) {
|
||||
server_socket->Close();
|
||||
}
|
||||
});
|
||||
|
||||
auto socket = std::make_unique<WifiLanSocketV2>();
|
||||
// Finally, Request to connect to this socket.
|
||||
if (!server_socket->Connect(*socket)) {
|
||||
NEARBY_LOGS(ERROR) << "G3 WifiLan Failed to connect to existing WifiLan "
|
||||
"Server socket: name="
|
||||
<< socket_name;
|
||||
return {};
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan ConnectToService: connected: socket="
|
||||
<< socket.get();
|
||||
return socket;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanServerSocketV2> WifiLanMediumV2::ListenForService(
|
||||
int port) {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
auto server_socket = std::make_unique<WifiLanServerSocketV2>();
|
||||
server_socket->SetIPAddress(env.GetFakeIPAddress());
|
||||
server_socket->SetPort(port == 0 ? env.GetFakePort() : port);
|
||||
std::string socket_name = WifiLanServerSocketV2::GetName(
|
||||
server_socket->GetIPAddress(), server_socket->GetPort());
|
||||
server_socket->SetCloseNotifier([this, socket_name]() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
server_sockets_.erase(socket_name);
|
||||
});
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan Adding server socket: medium=" << this
|
||||
<< ", socket_name=" << socket_name;
|
||||
absl::MutexLock lock(&mutex_);
|
||||
server_sockets_.insert({socket_name, server_socket.get()});
|
||||
return server_socket;
|
||||
}
|
||||
|
||||
} // namespace g3
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,280 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef PLATFORM_IMPL_G3_WIFI_LAN_V2_H_
|
||||
#define PLATFORM_IMPL_G3_WIFI_LAN_V2_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "platform/api/wifi_lan_v2.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "platform/base/input_stream.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/output_stream.h"
|
||||
#include "platform/impl/g3/multi_thread_executor.h"
|
||||
#include "platform/impl/g3/pipe.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
|
||||
class WifiLanMediumV2;
|
||||
|
||||
class WifiLanSocketV2 : public api::WifiLanSocketV2 {
|
||||
public:
|
||||
WifiLanSocketV2() = default;
|
||||
~WifiLanSocketV2() override;
|
||||
|
||||
// Connect to another WifiLanSocket, to form a functional low-level channel.
|
||||
// from this point on, and until Close is called, connection exists.
|
||||
void Connect(WifiLanSocketV2& other) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns the InputStream of this connected WifiLanSocket.
|
||||
InputStream& GetInputStream() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns the OutputStream of this connected WifiLanSocket.
|
||||
// This stream is for local side to write.
|
||||
OutputStream& GetOutputStream() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns address of a remote WifiLanSocket or nullptr.
|
||||
WifiLanSocketV2* GetRemoteSocket() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns true if connection exists to the (possibly closed) remote socket.
|
||||
bool IsConnected() const ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns true if socket is closed.
|
||||
bool IsClosed() const ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
void DoClose() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Returns true if connection exists to the (possibly closed) remote socket.
|
||||
bool IsConnectedLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Returns InputStream of our side of a connection.
|
||||
// This is what the remote side is supposed to read from.
|
||||
// This is a helper for GetInputStream() method.
|
||||
InputStream& GetLocalInputStream() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns OutputStream of our side of a connection.
|
||||
// This is what the local size is supposed to write to.
|
||||
// This is a helper for GetOutputStream() method.
|
||||
OutputStream& GetLocalOutputStream() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Output pipe is initialized by constructor, it remains always valid, until
|
||||
// it is closed. it represents output part of a local socket. Input part of a
|
||||
// local socket comes from the peer socket, after connection.
|
||||
std::shared_ptr<Pipe> output_{new Pipe};
|
||||
std::shared_ptr<Pipe> input_;
|
||||
mutable absl::Mutex mutex_;
|
||||
WifiLanSocketV2* remote_socket_ ABSL_GUARDED_BY(mutex_) = nullptr;
|
||||
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
};
|
||||
|
||||
class WifiLanServerSocketV2 : public api::WifiLanServerSocketV2 {
|
||||
public:
|
||||
static std::string GetName(const std::string& ip_address, int port);
|
||||
|
||||
~WifiLanServerSocketV2() override;
|
||||
|
||||
// Gets ip address.
|
||||
std::string GetIPAddress() const override ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return ip_address_;
|
||||
}
|
||||
|
||||
// Sets the ip address.
|
||||
void SetIPAddress(const std::string& ip_address) ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
ip_address_ = ip_address;
|
||||
}
|
||||
|
||||
// Gets the port.
|
||||
int GetPort() const override ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return port_;
|
||||
}
|
||||
|
||||
// Sets the port.
|
||||
void SetPort(int port) ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
port_ = port;
|
||||
}
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
// On success, returns connected socket, ready to exchange data.
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be closed.
|
||||
//
|
||||
// Called by the server side of a connection.
|
||||
// Returns WifiLanSocket to the server side.
|
||||
// If not null, returned socket is connected to its remote (client-side) peer.
|
||||
std::unique_ptr<api::WifiLanSocketV2> Accept() override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Blocks until either:
|
||||
// - connection is available, or
|
||||
// - server socket is closed, or
|
||||
// - error happens.
|
||||
//
|
||||
// Called by the client side of a connection.
|
||||
// Returns true, if socket is successfully connected.
|
||||
bool Connect(WifiLanSocketV2& socket) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Called by the server side of a connection before passing ownership of
|
||||
// WifiLanServerSocker to user, to track validity of a pointer to this
|
||||
// server socket.
|
||||
void SetCloseNotifier(std::function<void()> notifier)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
// Calls close_notifier if it was previously set, and marks socket as closed.
|
||||
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
Exception DoClose() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
mutable absl::Mutex mutex_;
|
||||
std::string ip_address_ ABSL_GUARDED_BY(mutex_);
|
||||
int port_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::CondVar cond_;
|
||||
absl::flat_hash_set<WifiLanSocketV2*> pending_sockets_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
|
||||
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the WifiLan medium.
|
||||
class WifiLanMediumV2 : public api::WifiLanMediumV2 {
|
||||
public:
|
||||
WifiLanMediumV2();
|
||||
~WifiLanMediumV2() override;
|
||||
|
||||
// Starts WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service is now advertising.
|
||||
// On error if the service cannot start to advertise or the service type in
|
||||
// NsdServiceInfo has been passed previously which StopAdvertising is not
|
||||
// been called.
|
||||
bool StartAdvertising(const NsdServiceInfo& nsd_service_info) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Stops WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service stops advertising.
|
||||
// On error if the service cannot stop advertising or the service type in
|
||||
// NsdServiceInfo cannot be found.
|
||||
bool StopAdvertising(const NsdServiceInfo& nsd_service_info) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Starts the discovery of nearby WifiLan services.
|
||||
//
|
||||
// Returns true once the WifiLan discovery has been initiated. The
|
||||
// service_type is associated with callback.
|
||||
bool StartDiscovery(const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Stops the discovery of nearby WifiLan services.
|
||||
//
|
||||
// service_type - The one assigend in StartDiscovery.
|
||||
// On success if service_type is matched to the callback and will be removed
|
||||
// from the list. If list is empty then stops the WifiLan discovery
|
||||
// service.
|
||||
// On error if the service_type is not existed, then return immediately.
|
||||
bool StopDiscovery(const std::string& service_type) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Connects to a WifiLan service.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::WifiLanSocketV2> ConnectToService(
|
||||
const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag) override;
|
||||
|
||||
// Connects to a WifiLan service by ip address and port.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::WifiLanSocketV2> ConnectToService(
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) override;
|
||||
|
||||
// Listens for incoming connection.
|
||||
//
|
||||
// port - A port number.
|
||||
// 0 : use a random port.
|
||||
// 1~65536 : open a server socket on that exact port.
|
||||
// On success, returns a new WifiLanServerSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::WifiLanServerSocketV2> ListenForService(
|
||||
int port = 0) override;
|
||||
|
||||
private:
|
||||
struct AdvertisingInfo {
|
||||
bool Empty() const { return service_types.empty(); }
|
||||
void Clear() { service_types.clear(); }
|
||||
void Add(const std::string& service_type) {
|
||||
service_types.insert(service_type);
|
||||
}
|
||||
void Remove(const std::string& service_type) {
|
||||
service_types.erase(service_type);
|
||||
}
|
||||
bool Existed(const std::string& service_type) const {
|
||||
return service_types.contains(service_type);
|
||||
}
|
||||
|
||||
absl::flat_hash_set<std::string> service_types;
|
||||
};
|
||||
struct DiscoveringInfo {
|
||||
bool Empty() const { return service_types.empty(); }
|
||||
void Clear() { service_types.clear(); }
|
||||
void Add(const std::string& service_type) {
|
||||
service_types.insert(service_type);
|
||||
}
|
||||
void Remove(const std::string& service_type) {
|
||||
service_types.erase(service_type);
|
||||
}
|
||||
bool Existed(const std::string& service_type) const {
|
||||
return service_types.contains(service_type);
|
||||
}
|
||||
|
||||
absl::flat_hash_set<std::string> service_types;
|
||||
};
|
||||
|
||||
absl::Mutex mutex_;
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
DiscoveringInfo discovering_info_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_map<std::string, WifiLanServerSocketV2*> server_sockets_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_IMPL_G3_WIFI_LAN_V2_H_
|
||||
@@ -64,7 +64,6 @@ cc_library(
|
||||
"thread_pool.h",
|
||||
"webrtc.h",
|
||||
"wifi.h",
|
||||
"wifi_lan.h",
|
||||
],
|
||||
compatible_with = ["//buildenv/target:non_prod"],
|
||||
visibility = ["//visibility:private"],
|
||||
@@ -105,9 +104,6 @@ cc_library(
|
||||
"system_clock.cc",
|
||||
"thread_pool.cc",
|
||||
"utils.cc",
|
||||
"wifi_lan_medium.cc",
|
||||
"wifi_lan_nsd.cc",
|
||||
"wifi_lan_socket.cc",
|
||||
],
|
||||
compatible_with = ["//buildenv/target:non_prod"],
|
||||
copts = ["-Ithird_party/nearby_connections/cpp/platform/impl/windows/generated"],
|
||||
|
||||
@@ -34,7 +34,6 @@
|
||||
#include "platform/impl/windows/submittable_executor.h"
|
||||
#include "platform/impl/windows/webrtc.h"
|
||||
#include "platform/impl/windows/wifi.h"
|
||||
#include "platform/impl/windows/wifi_lan.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -141,13 +140,7 @@ std::unique_ptr<WifiMedium> ImplementationPlatform::CreateWifiMedium() {
|
||||
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
std::unique_ptr<WifiLanMedium> ImplementationPlatform::CreateWifiLanMedium() {
|
||||
return absl::make_unique<windows::WifiLanMedium>();
|
||||
}
|
||||
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
std::unique_ptr<WifiLanMediumV2>
|
||||
ImplementationPlatform::CreateWifiLanMediumV2() {
|
||||
return std::unique_ptr<WifiLanMediumV2>();
|
||||
return std::unique_ptr<WifiLanMedium>();
|
||||
}
|
||||
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
|
||||
@@ -78,7 +78,6 @@ cc_library(
|
||||
"bluetooth_classic.cc",
|
||||
"file.cc",
|
||||
"wifi_lan.cc",
|
||||
"wifi_lan_v2.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"ble.h",
|
||||
@@ -86,7 +85,6 @@ cc_library(
|
||||
"bluetooth_classic.h",
|
||||
"webrtc.h",
|
||||
"wifi_lan.h",
|
||||
"wifi_lan_v2.h",
|
||||
],
|
||||
compatible_with = ["//buildenv/target:non_prod"],
|
||||
copts = ["-DCORE_ADAPTER_DLL"],
|
||||
@@ -150,7 +148,6 @@ cc_test(
|
||||
"scheduled_executor_test.cc",
|
||||
"single_thread_executor_test.cc",
|
||||
"wifi_lan_test.cc",
|
||||
"wifi_lan_test_v2.cc",
|
||||
],
|
||||
copts = ["-DCORE_ADAPTER_DLL"],
|
||||
shard_count = 16,
|
||||
|
||||
+99
-115
@@ -14,148 +14,132 @@
|
||||
|
||||
#include "platform/public/wifi_lan.h"
|
||||
|
||||
#include "platform/public/logging.h"
|
||||
#include "platform/public/mutex_lock.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
bool WifiLanMedium::StartAdvertising(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info) {
|
||||
return impl_->StartAdvertising(service_id, nsd_service_info);
|
||||
bool WifiLanMedium::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
return impl_->StartAdvertising(nsd_service_info);
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
||||
return impl_->StopAdvertising(service_id);
|
||||
bool WifiLanMedium::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
return impl_->StopAdvertising(nsd_service_info);
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartDiscovery(const std::string& service_id,
|
||||
const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
discovered_service_callback_ = std::move(callback);
|
||||
services_.clear();
|
||||
if (discovery_callbacks_.contains(service_type)) {
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery already start with service_type="
|
||||
<< service_type << "; impl=" << &GetImpl();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return impl_->StartDiscovery(
|
||||
service_id,
|
||||
{
|
||||
.service_discovered_cb =
|
||||
[this](api::WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
auto pair = services_.emplace(
|
||||
&wifi_lan_service,
|
||||
absl::make_unique<ServiceDiscoveryInfo>());
|
||||
auto& context = *pair.first->second;
|
||||
if (!pair.second) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Discovering (again) service=%p, impl=%p, "
|
||||
"service_info_name=%s",
|
||||
&context.wifi_lan_service, &wifi_lan_service,
|
||||
wifi_lan_service.GetServiceInfo()
|
||||
.GetServiceName()
|
||||
.c_str());
|
||||
return;
|
||||
} else {
|
||||
context.wifi_lan_service = WifiLanService(&wifi_lan_service);
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Discovering wifi_lan_service=%p, service_info_name=%s",
|
||||
&wifi_lan_service,
|
||||
wifi_lan_service.GetServiceInfo()
|
||||
.GetServiceName()
|
||||
.c_str());
|
||||
}
|
||||
discovered_service_callback_.service_discovered_cb(
|
||||
context.wifi_lan_service, service_id);
|
||||
},
|
||||
.service_lost_cb =
|
||||
[this](api::WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (services_.empty()) return;
|
||||
auto context = services_.find(&wifi_lan_service);
|
||||
if (context == services_.end()) return;
|
||||
NEARBY_LOG(INFO, "Removing wifi_lan_service=%p, impl=%p",
|
||||
&(context->second->wifi_lan_service),
|
||||
&wifi_lan_service);
|
||||
discovered_service_callback_.service_lost_cb(
|
||||
context->second->wifi_lan_service, service_id);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StopDiscovery(const std::string& service_id) {
|
||||
api::WifiLanMedium::DiscoveredServiceCallback api_callback = {
|
||||
.service_discovered_cb =
|
||||
[this](NsdServiceInfo service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto pair = discovery_services_.insert(service_type);
|
||||
if (!pair.second) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Discovering (again) service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_name=" << service_info.GetServiceName();
|
||||
return;
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Adding service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_name=" << service_info.GetServiceName();
|
||||
// Callback service found.
|
||||
const auto& it = discovery_callbacks_.find(service_type);
|
||||
if (it != discovery_callbacks_.end()) {
|
||||
std::string service_id = it->second->service_id;
|
||||
DiscoveredServiceCallback medium_callback =
|
||||
it->second->medium_callback;
|
||||
medium_callback.service_discovered_cb(service_info, service_id);
|
||||
} else {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "There is no callback found for service_type="
|
||||
<< service_type;
|
||||
}
|
||||
},
|
||||
.service_lost_cb =
|
||||
[this](NsdServiceInfo service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto item = discovery_services_.extract(service_type);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Removing service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_info_name=" << service_info.GetServiceName();
|
||||
// Callback service lost.
|
||||
const auto& it = discovery_callbacks_.find(service_type);
|
||||
if (it != discovery_callbacks_.end()) {
|
||||
std::string service_id = it->second->service_id;
|
||||
DiscoveredServiceCallback medium_callback =
|
||||
it->second->medium_callback;
|
||||
medium_callback.service_lost_cb(service_info, service_id);
|
||||
}
|
||||
},
|
||||
};
|
||||
{
|
||||
// Insert callback to the map first no matter it succeeds or not.
|
||||
MutexLock lock(&mutex_);
|
||||
discovered_service_callback_ = {};
|
||||
services_.clear();
|
||||
NEARBY_LOG(INFO, "WifiLan Discovery disabled: impl=%p", &GetImpl());
|
||||
auto pair = discovery_callbacks_.insert(
|
||||
{service_type, absl::make_unique<DiscoveryCallbackInfo>()});
|
||||
auto& context = *pair.first->second;
|
||||
context.medium_callback = std::move(callback);
|
||||
context.service_id = service_id;
|
||||
}
|
||||
return impl_->StopDiscovery(service_id);
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartAcceptingConnections(
|
||||
const std::string& service_id, AcceptedConnectionCallback callback) {
|
||||
{
|
||||
bool success = impl_->StartDiscovery(service_type, std::move(api_callback));
|
||||
if (!success) {
|
||||
// If failed, then revert back the insertion.
|
||||
MutexLock lock(&mutex_);
|
||||
accepted_connection_callback_ = std::move(callback);
|
||||
discovery_callbacks_.erase(service_type);
|
||||
}
|
||||
return impl_->StartAcceptingConnections(
|
||||
service_id,
|
||||
{
|
||||
.accepted_cb =
|
||||
[this](api::WifiLanSocket& socket,
|
||||
const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
auto pair = sockets_.emplace(
|
||||
&socket, absl::make_unique<AcceptedConnectionInfo>());
|
||||
auto& context = *pair.first->second;
|
||||
if (!pair.second) {
|
||||
NEARBY_LOG(INFO, "Accepting (again) socket=%p, impl=%p",
|
||||
&context.socket, &socket);
|
||||
} else {
|
||||
context.socket = WifiLanSocket(&socket);
|
||||
NEARBY_LOG(INFO, "Accepting socket=%p, impl=%p",
|
||||
&context.socket, &socket);
|
||||
}
|
||||
accepted_connection_callback_.accepted_cb(context.socket,
|
||||
service_id);
|
||||
},
|
||||
});
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery started for service_type="
|
||||
<< service_type << ", impl=" << &GetImpl()
|
||||
<< ", success=" << success;
|
||||
return success;
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
accepted_connection_callback_ = {};
|
||||
sockets_.clear();
|
||||
NEARBY_LOG(INFO, "WifiLan accepted connection disabled: impl=%p",
|
||||
&GetImpl());
|
||||
bool WifiLanMedium::StopDiscovery(const std::string& service_type) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (!discovery_callbacks_.contains(service_type)) {
|
||||
return false;
|
||||
}
|
||||
return impl_->StopAcceptingConnections(service_id);
|
||||
discovery_callbacks_.erase(service_type);
|
||||
if (discovery_services_.contains(service_type)) {
|
||||
discovery_services_.erase(service_type);
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery disabled for service_type="
|
||||
<< service_type << ", impl=" << &GetImpl();
|
||||
return impl_->StopDiscovery(service_type);
|
||||
}
|
||||
|
||||
WifiLanSocket WifiLanMedium::Connect(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"WifiLanMedium::Connect: service=%p [impl=%p, service_info_name=%s]",
|
||||
&wifi_lan_service, &wifi_lan_service.GetImpl(),
|
||||
wifi_lan_service.GetServiceInfo().GetServiceName().c_str());
|
||||
return WifiLanSocket(impl_->Connect(wifi_lan_service.GetImpl(), service_id,
|
||||
cancellation_flag));
|
||||
WifiLanSocket WifiLanMedium::ConnectToService(
|
||||
const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
NEARBY_LOGS(INFO) << "WifiLanMedium::ConnectToService: remote_service_name="
|
||||
<< remote_service_info.GetServiceName();
|
||||
return WifiLanSocket(
|
||||
impl_->ConnectToService(remote_service_info, cancellation_flag));
|
||||
}
|
||||
|
||||
WifiLanService WifiLanMedium::GetRemoteService(const std::string& ip_address,
|
||||
int port) {
|
||||
return WifiLanService(impl_->GetRemoteService(ip_address, port));
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
return impl_->GetCredentials(service_id);
|
||||
WifiLanSocket WifiLanMedium::ConnectToService(
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
NEARBY_LOGS(INFO) << "WifiLanMedium::ConnectToService: ip address="
|
||||
<< ip_address << ", port=" << port;
|
||||
return WifiLanSocket(
|
||||
impl_->ConnectToService(ip_address, port, cancellation_flag));
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -23,37 +23,20 @@
|
||||
#include "platform/base/input_stream.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/output_stream.h"
|
||||
#include "platform/public/logging.h"
|
||||
#include "platform/public/mutex.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
// Opaque wrapper over a WifiLan service which contains |NsdServiceInfo|.
|
||||
class WifiLanService final {
|
||||
public:
|
||||
WifiLanService() = default;
|
||||
WifiLanService(const WifiLanService&) = default;
|
||||
WifiLanService& operator=(const WifiLanService&) = default;
|
||||
explicit WifiLanService(api::WifiLanService* service) : impl_(service) {}
|
||||
~WifiLanService() = default;
|
||||
|
||||
NsdServiceInfo GetServiceInfo() const { return impl_->GetServiceInfo(); }
|
||||
api::WifiLanService& GetImpl() { return *impl_; }
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
private:
|
||||
api::WifiLanService* impl_;
|
||||
};
|
||||
|
||||
class WifiLanSocket final {
|
||||
public:
|
||||
WifiLanSocket() = default;
|
||||
WifiLanSocket(const WifiLanSocket&) = default;
|
||||
WifiLanSocket& operator=(const WifiLanSocket&) = default;
|
||||
explicit WifiLanSocket(api::WifiLanSocket* socket) : impl_(socket) {}
|
||||
explicit WifiLanSocket(std::unique_ptr<api::WifiLanSocket> socket)
|
||||
: impl_(socket.release()) {}
|
||||
~WifiLanSocket() = default;
|
||||
explicit WifiLanSocket(std::unique_ptr<api::WifiLanSocket> socket)
|
||||
: impl_(std::move(socket)) {}
|
||||
|
||||
// Returns the InputStream of the WifiLanSocket.
|
||||
// On error, returned stream will report Exception::kIo on any operation.
|
||||
@@ -72,10 +55,6 @@ class WifiLanSocket final {
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() { return impl_->Close(); }
|
||||
|
||||
WifiLanService GetRemoteWifiLanService() {
|
||||
return WifiLanService(impl_->GetRemoteWifiLanService());
|
||||
}
|
||||
|
||||
// Returns true if a socket is usable. If this method returns false,
|
||||
// it is not safe to call any other method.
|
||||
// NOTE(socket validity):
|
||||
@@ -98,84 +77,126 @@ class WifiLanSocket final {
|
||||
std::shared_ptr<api::WifiLanSocket> impl_;
|
||||
};
|
||||
|
||||
class WifiLanServerSocket final {
|
||||
public:
|
||||
WifiLanServerSocket() = default;
|
||||
WifiLanServerSocket(const WifiLanServerSocket&) = default;
|
||||
WifiLanServerSocket& operator=(const WifiLanServerSocket&) = default;
|
||||
~WifiLanServerSocket() = default;
|
||||
explicit WifiLanServerSocket(std::unique_ptr<api::WifiLanServerSocket> socket)
|
||||
: impl_(std::move(socket)) {}
|
||||
|
||||
// Returns ip address.
|
||||
std::string GetIPAddress() { return impl_->GetIPAddress(); }
|
||||
|
||||
// Returns port.
|
||||
int GetPort() { return impl_->GetPort(); }
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
// On success, returns connected socket, ready to exchange data.
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be closed.
|
||||
WifiLanSocket Accept() {
|
||||
std::unique_ptr<api::WifiLanSocket> socket = impl_->Accept();
|
||||
if (!socket) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "WifiLanServerSocket Accept() failed on server socket: " << this;
|
||||
}
|
||||
return WifiLanSocket(std::move(socket));
|
||||
}
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() {
|
||||
NEARBY_LOGS(INFO) << "WifiLanServerSocket Closing:: " << this;
|
||||
return impl_->Close();
|
||||
}
|
||||
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
api::WifiLanServerSocket& GetImpl() { return *impl_; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<api::WifiLanServerSocket> impl_;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the WifiLan medium.
|
||||
class WifiLanMedium final {
|
||||
class WifiLanMedium {
|
||||
public:
|
||||
using Platform = api::ImplementationPlatform;
|
||||
|
||||
struct DiscoveredServiceCallback {
|
||||
std::function<void(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id)>
|
||||
std::function<void(NsdServiceInfo service_info,
|
||||
const std::string& service_type)>
|
||||
service_discovered_cb =
|
||||
DefaultCallback<WifiLanService&, const std::string&>();
|
||||
std::function<void(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id)>
|
||||
service_lost_cb =
|
||||
DefaultCallback<WifiLanService&, const std::string&>();
|
||||
DefaultCallback<NsdServiceInfo, const std::string&>();
|
||||
std::function<void(NsdServiceInfo service_info,
|
||||
const std::string& service_type)>
|
||||
service_lost_cb = DefaultCallback<NsdServiceInfo, const std::string&>();
|
||||
};
|
||||
|
||||
struct ServiceDiscoveryInfo {
|
||||
WifiLanService wifi_lan_service;
|
||||
};
|
||||
|
||||
struct AcceptedConnectionCallback {
|
||||
std::function<void(WifiLanSocket socket, const std::string& service_id)>
|
||||
accepted_cb = DefaultCallback<WifiLanSocket, const std::string&>();
|
||||
};
|
||||
|
||||
struct AcceptedConnectionInfo {
|
||||
WifiLanSocket socket;
|
||||
struct DiscoveryCallbackInfo {
|
||||
std::string service_id;
|
||||
DiscoveredServiceCallback medium_callback;
|
||||
};
|
||||
|
||||
WifiLanMedium() : impl_(Platform::CreateWifiLanMedium()) {}
|
||||
~WifiLanMedium() = default;
|
||||
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info);
|
||||
bool StopAdvertising(const std::string& service_id);
|
||||
// Starts WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service is now advertising.
|
||||
// On error if the service cannot start to advertise or the nsd_type in
|
||||
// NsdServiceInfo has been passed previously which StopAdvertising is not
|
||||
// been called.
|
||||
bool StartAdvertising(const NsdServiceInfo& nsd_service_info);
|
||||
|
||||
// Stops WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service stops advertising.
|
||||
// On error if the service cannot stop advertising or the nsd_type in
|
||||
// NsdServiceInfo cannot be found.
|
||||
bool StopAdvertising(const NsdServiceInfo& nsd_service_info);
|
||||
|
||||
// Returns true once the WifiLan discovery has been initiated.
|
||||
bool StartDiscovery(const std::string& service_id,
|
||||
const std::string& service_type,
|
||||
DiscoveredServiceCallback callback);
|
||||
|
||||
// Returns true once WifiLan discovery for service_id is well and truly
|
||||
// stopped; after this returns, there must be no more invocations of the
|
||||
// DiscoveredServiceCallback passed in to StartDiscovery() for service_id.
|
||||
bool StopDiscovery(const std::string& service_id);
|
||||
// Returns true once service_type is associated to existing callback. If the
|
||||
// callback is the last found then WifiLan discovery will be stopped.
|
||||
bool StopDiscovery(const std::string& service_type);
|
||||
|
||||
// Returns true once WifiLan socket connection requests to service_id can be
|
||||
// accepted.
|
||||
bool StartAcceptingConnections(const std::string& service_id,
|
||||
AcceptedConnectionCallback callback);
|
||||
bool StopAcceptingConnections(const std::string& service_id);
|
||||
// Returns a new WifiLanSocket.
|
||||
// On Success, WifiLanSocket::IsValid() returns true.
|
||||
WifiLanSocket ConnectToService(const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag);
|
||||
|
||||
// Returns a new WifiLanSocket. On Success, WifiLanSocket::IsValid()
|
||||
// returns true.
|
||||
WifiLanSocket Connect(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag);
|
||||
// Returns a new WifiLanSocket by ip address and port.
|
||||
// On Success, WifiLanSocket::IsValid()returns true.
|
||||
WifiLanSocket ConnectToService(const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag);
|
||||
|
||||
// Returns a new WifiLanServerSocket.
|
||||
// On Success, WifiLanServerSocket::IsValid() returns true.
|
||||
WifiLanServerSocket ListenForService(int port = 0) {
|
||||
return WifiLanServerSocket(impl_->ListenForService(port));
|
||||
}
|
||||
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
api::WifiLanMedium& GetImpl() { return *impl_; }
|
||||
|
||||
WifiLanService GetRemoteService(const std::string& ip_address, int port);
|
||||
|
||||
std::pair<std::string, int> GetCredentials(const std::string& service_id);
|
||||
|
||||
private:
|
||||
Mutex mutex_;
|
||||
std::unique_ptr<api::WifiLanMedium> impl_;
|
||||
absl::flat_hash_map<api::WifiLanService*,
|
||||
std::unique_ptr<ServiceDiscoveryInfo>>
|
||||
services_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_map<api::WifiLanSocket*,
|
||||
std::unique_ptr<AcceptedConnectionInfo>>
|
||||
sockets_ ABSL_GUARDED_BY(mutex_);
|
||||
DiscoveredServiceCallback discovered_service_callback_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
AcceptedConnectionCallback accepted_connection_callback_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_map<std::string, std::unique_ptr<DiscoveryCallbackInfo>>
|
||||
discovery_callbacks_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_set<std::string> discovery_services_ ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -38,7 +38,9 @@ constexpr FeatureFlags kTestCases[] = {
|
||||
},
|
||||
};
|
||||
|
||||
constexpr absl::string_view kServiceID{"com.google.location.nearby.apps.test"};
|
||||
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
|
||||
constexpr absl::string_view kServiceId{"service_id"};
|
||||
constexpr absl::string_view kServiceType{"_service.tcp_"};
|
||||
constexpr absl::string_view kServiceInfoName{"Simulated service info name"};
|
||||
constexpr absl::string_view kEndpointName{"Simulated endpoint name"};
|
||||
constexpr absl::string_view kEndpointInfoKey{"n"};
|
||||
@@ -46,71 +48,81 @@ constexpr absl::string_view kEndpointInfoKey{"n"};
|
||||
class WifiLanMediumTest : public ::testing::TestWithParam<FeatureFlags> {
|
||||
protected:
|
||||
using DiscoveredServiceCallback = WifiLanMedium::DiscoveredServiceCallback;
|
||||
using AcceptedConnectionCallback = WifiLanMedium::AcceptedConnectionCallback;
|
||||
|
||||
WifiLanMediumTest() { env_.Stop(); }
|
||||
|
||||
MediumEnvironment& env_{MediumEnvironment::Instance()};
|
||||
};
|
||||
|
||||
TEST_P(WifiLanMediumTest, CanStartAcceptingConnectionsAndConnect) {
|
||||
TEST_P(WifiLanMediumTest, CanConnectToService) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_a;
|
||||
WifiLanMedium wifi_b;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch found_latch(1);
|
||||
CountDownLatch accepted_latch(1);
|
||||
CancellationFlag flag;
|
||||
WifiLanMedium wifi_lan_a;
|
||||
WifiLanMedium wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
WifiLanService* discovered_service = nullptr;
|
||||
wifi_a.StartDiscovery(
|
||||
service_id,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&found_latch, &discovered_service](
|
||||
WifiLanService& service, const std::string& service_id) {
|
||||
NEARBY_LOG(INFO, "Service discovered: %s, %p",
|
||||
service.GetServiceInfo().GetServiceName().c_str(),
|
||||
&service);
|
||||
discovered_service = &service;
|
||||
found_latch.CountDown();
|
||||
},
|
||||
});
|
||||
WifiLanServerSocket server_socket = wifi_lan_b.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_b.StartAdvertising(service_id, nsd_service_info);
|
||||
wifi_b.StartAcceptingConnections(
|
||||
service_id,
|
||||
AcceptedConnectionCallback{
|
||||
.accepted_cb = [&accepted_latch](WifiLanSocket socket,
|
||||
const std::string& service_id) {
|
||||
NEARBY_LOG(INFO, "Connection accepted: socket=%p, service_id=%s",
|
||||
&socket, service_id.c_str());
|
||||
accepted_latch.CountDown();
|
||||
}});
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
nsd_service_info.SetIPAddress(server_socket.GetIPAddress());
|
||||
nsd_service_info.SetPort(server_socket.GetPort());
|
||||
wifi_lan_b.StartAdvertising(nsd_service_info);
|
||||
|
||||
NsdServiceInfo discovered_service_info;
|
||||
wifi_lan_a.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch, &discovered_service_info](
|
||||
NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_service_info = service_info;
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
EXPECT_TRUE(discovered_latch.Await(absl::Milliseconds(1000)).result());
|
||||
WifiLanSocket socket_a;
|
||||
WifiLanSocket socket_b;
|
||||
EXPECT_FALSE(socket_a.IsValid());
|
||||
EXPECT_FALSE(socket_b.IsValid());
|
||||
{
|
||||
CancellationFlag flag;
|
||||
SingleThreadExecutor server_executor;
|
||||
SingleThreadExecutor client_executor;
|
||||
client_executor.Execute(
|
||||
[&wifi_a, &socket_a, discovered_service, &service_id, &flag]() {
|
||||
socket_a = wifi_a.Connect(*discovered_service, service_id, &flag);
|
||||
});
|
||||
client_executor.Execute([&wifi_lan_a, &socket_a,
|
||||
discovered_service_info = discovered_service_info,
|
||||
service_type, &server_socket, &flag]() {
|
||||
socket_a = wifi_lan_a.ConnectToService(discovered_service_info, &flag);
|
||||
if (!socket_a.IsValid()) {
|
||||
server_socket.Close();
|
||||
}
|
||||
});
|
||||
server_executor.Execute([&socket_b, &server_socket]() {
|
||||
socket_b = server_socket.Accept();
|
||||
if (!socket_b.IsValid()) {
|
||||
server_socket.Close();
|
||||
}
|
||||
});
|
||||
}
|
||||
EXPECT_TRUE(accepted_latch.Await(absl::Milliseconds(1000)).result());
|
||||
EXPECT_TRUE(socket_a.IsValid());
|
||||
wifi_b.StopAcceptingConnections(service_id);
|
||||
wifi_b.StopAdvertising(service_id);
|
||||
wifi_a.StopDiscovery(service_id);
|
||||
EXPECT_TRUE(socket_b.IsValid());
|
||||
server_socket.Close();
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
@@ -118,68 +130,77 @@ TEST_P(WifiLanMediumTest, CanCancelConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_a;
|
||||
WifiLanMedium wifi_b;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch found_latch(1);
|
||||
CountDownLatch accepted_latch(1);
|
||||
CancellationFlag flag(true);
|
||||
WifiLanMedium wifi_lan_a;
|
||||
WifiLanMedium wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
WifiLanService* discovered_service = nullptr;
|
||||
wifi_a.StartDiscovery(
|
||||
service_id,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&found_latch, &discovered_service](
|
||||
WifiLanService& service, const std::string& service_id) {
|
||||
NEARBY_LOG(INFO, "Service discovered: %s, %p",
|
||||
service.GetServiceInfo().GetServiceName().c_str(),
|
||||
&service);
|
||||
discovered_service = &service;
|
||||
found_latch.CountDown();
|
||||
},
|
||||
});
|
||||
WifiLanServerSocket server_socket = wifi_lan_b.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_b.StartAdvertising(service_id, nsd_service_info);
|
||||
wifi_b.StartAcceptingConnections(
|
||||
service_id,
|
||||
AcceptedConnectionCallback{
|
||||
.accepted_cb = [&accepted_latch](WifiLanSocket socket,
|
||||
const std::string& service_id) {
|
||||
NEARBY_LOG(INFO, "Connection accepted: socket=%p, service_id=%s",
|
||||
&socket, service_id.c_str());
|
||||
accepted_latch.CountDown();
|
||||
}});
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
nsd_service_info.SetIPAddress(server_socket.GetIPAddress());
|
||||
nsd_service_info.SetPort(server_socket.GetPort());
|
||||
wifi_lan_b.StartAdvertising(nsd_service_info);
|
||||
|
||||
NsdServiceInfo discovered_service_info;
|
||||
wifi_lan_a.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch, &discovered_service_info](
|
||||
NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_service_info = service_info;
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
EXPECT_TRUE(discovered_latch.Await(absl::Milliseconds(1000)).result());
|
||||
WifiLanSocket socket_a;
|
||||
WifiLanSocket socket_b;
|
||||
EXPECT_FALSE(socket_a.IsValid());
|
||||
EXPECT_FALSE(socket_b.IsValid());
|
||||
{
|
||||
CancellationFlag flag(true);
|
||||
SingleThreadExecutor server_executor;
|
||||
SingleThreadExecutor client_executor;
|
||||
client_executor.Execute(
|
||||
[&wifi_a, &socket_a, discovered_service, &service_id, &flag]() {
|
||||
socket_a = wifi_a.Connect(*discovered_service, service_id, &flag);
|
||||
});
|
||||
client_executor.Execute([&wifi_lan_a, &socket_a,
|
||||
discovered_service_info = discovered_service_info,
|
||||
service_type, &server_socket, &flag]() {
|
||||
socket_a = wifi_lan_a.ConnectToService(discovered_service_info, &flag);
|
||||
if (!socket_a.IsValid()) {
|
||||
server_socket.Close();
|
||||
}
|
||||
});
|
||||
server_executor.Execute([&socket_b, &server_socket]() {
|
||||
socket_b = server_socket.Accept();
|
||||
if (!socket_b.IsValid()) {
|
||||
server_socket.Close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(accepted_latch.Await(absl::Milliseconds(1000)).result());
|
||||
EXPECT_TRUE(socket_a.IsValid());
|
||||
EXPECT_TRUE(socket_b.IsValid());
|
||||
} else {
|
||||
EXPECT_FALSE(accepted_latch.Await(absl::Milliseconds(1000)).result());
|
||||
EXPECT_FALSE(socket_a.IsValid());
|
||||
EXPECT_FALSE(socket_b.IsValid());
|
||||
}
|
||||
|
||||
wifi_b.StopAcceptingConnections(service_id);
|
||||
wifi_b.StopAdvertising(service_id);
|
||||
wifi_a.StopDiscovery(service_id);
|
||||
server_socket.Close();
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
@@ -188,117 +209,179 @@ INSTANTIATE_TEST_SUITE_P(ParametrisedWifiLanMediumTest, WifiLanMediumTest,
|
||||
|
||||
TEST_F(WifiLanMediumTest, ConstructorDestructorWorks) {
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_a;
|
||||
WifiLanMedium wifi_b;
|
||||
WifiLanMedium wifi_lan_a;
|
||||
WifiLanMedium wifi_lan_b;
|
||||
|
||||
// Make sure we can create functional mediums.
|
||||
ASSERT_TRUE(wifi_a.IsValid());
|
||||
ASSERT_TRUE(wifi_b.IsValid());
|
||||
ASSERT_TRUE(wifi_lan_a.IsValid());
|
||||
ASSERT_TRUE(wifi_lan_b.IsValid());
|
||||
|
||||
// Make sure we can create 2 distinct mediums.
|
||||
EXPECT_NE(&wifi_a.GetImpl(), &wifi_b.GetImpl());
|
||||
EXPECT_NE(&wifi_lan_a.GetImpl(), &wifi_lan_b.GetImpl());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumTest, CanStartAdvertising) {
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_a;
|
||||
WifiLanMedium wifi_b;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch found_latch(1);
|
||||
WifiLanMedium wifi_lan_a;
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
WifiLanServerSocket server_socket = wifi_lan_a.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_a.StartAdvertising(service_id, nsd_service_info);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
EXPECT_TRUE(wifi_b.StartDiscovery(
|
||||
service_id, DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&found_latch](WifiLanService& service,
|
||||
const std::string& service_id) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
EXPECT_TRUE(wifi_a.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(wifi_b.StopDiscovery(service_id));
|
||||
TEST_F(WifiLanMediumTest, CanStartMultipleAdvertising) {
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_lan_a;
|
||||
std::string service_type_1(kServiceType);
|
||||
std::string service_type_2("_service_1.tcp_");
|
||||
std::string service_info_name_1(kServiceInfoName);
|
||||
std::string service_info_name_2(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
WifiLanServerSocket server_socket = wifi_lan_a.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info_1;
|
||||
nsd_service_info_1.SetServiceName(service_info_name_1);
|
||||
nsd_service_info_1.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info_1.SetServiceType(service_type_1);
|
||||
|
||||
NsdServiceInfo nsd_service_info_2;
|
||||
nsd_service_info_2.SetServiceName(service_info_name_2);
|
||||
nsd_service_info_2.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info_2.SetServiceType(service_type_2);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_2));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info_2));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumTest, CanStartDiscovery) {
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_a;
|
||||
WifiLanMedium wifi_b;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch found_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
WifiLanMedium wifi_lan_a;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
|
||||
wifi_a.StartDiscovery(service_id,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&found_latch](WifiLanService& service,
|
||||
absl::string_view service_id) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](WifiLanService& service,
|
||||
absl::string_view service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
EXPECT_TRUE(wifi_b.StartAdvertising(service_id, nsd_service_info));
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
EXPECT_TRUE(wifi_b.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(lost_latch.Await(absl::Milliseconds(1000)).result());
|
||||
EXPECT_TRUE(wifi_a.StopDiscovery(service_id));
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id, service_type,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumTest, CanStopDiscovery) {
|
||||
TEST_F(WifiLanMediumTest, CanStartMultipleDiscovery) {
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_a;
|
||||
WifiLanMedium wifi_b;
|
||||
std::string service_id(kServiceID);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
CountDownLatch found_latch(1);
|
||||
WifiLanMedium wifi_lan_a;
|
||||
std::string service_id_1(kServiceId);
|
||||
std::string service_id_2("service_id_2");
|
||||
std::string service_type_1(kServiceType);
|
||||
std::string service_type_2("_service_1.tcp_");
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id_1, service_type_1,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id_2, service_type_2,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type_2));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumTest, CanAdvertiseThatOtherMediumDiscover) {
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_lan_a;
|
||||
WifiLanMedium wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
wifi_a.StartDiscovery(service_id,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&found_latch](WifiLanService& service,
|
||||
absl::string_view service_id) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](WifiLanService& service,
|
||||
absl::string_view service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
wifi_lan_b.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
WifiLanServerSocket server_socket = wifi_lan_a.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_b.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
EXPECT_TRUE(wifi_b.StartAdvertising(service_id, nsd_service_info));
|
||||
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
|
||||
EXPECT_TRUE(wifi_a.StopDiscovery(service_id));
|
||||
EXPECT_TRUE(wifi_b.StopAdvertising(service_id));
|
||||
EXPECT_FALSE(lost_latch.Await(absl::Milliseconds(1000)).result());
|
||||
TEST_F(WifiLanMediumTest, CanDiscoverThatOtherMediumAdvertise) {
|
||||
env_.Start();
|
||||
WifiLanMedium wifi_lan_a;
|
||||
WifiLanMedium wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
wifi_lan_a.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
WifiLanServerSocket server_socket = wifi_lan_a.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
EXPECT_TRUE(wifi_lan_b.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_b.StopAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,389 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "platform/base/medium_environment.h"
|
||||
#include "platform/public/count_down_latch.h"
|
||||
#include "platform/public/logging.h"
|
||||
#include "platform/public/wifi_lan_v2.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace {
|
||||
|
||||
using FeatureFlags = FeatureFlags::Flags;
|
||||
|
||||
constexpr FeatureFlags kTestCases[] = {
|
||||
FeatureFlags{
|
||||
.enable_cancellation_flag = true,
|
||||
},
|
||||
FeatureFlags{
|
||||
.enable_cancellation_flag = false,
|
||||
},
|
||||
};
|
||||
|
||||
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
|
||||
constexpr absl::string_view kServiceId{"service_id"};
|
||||
constexpr absl::string_view kServiceType{"_service.tcp_"};
|
||||
constexpr absl::string_view kServiceInfoName{"Simulated service info name"};
|
||||
constexpr absl::string_view kEndpointName{"Simulated endpoint name"};
|
||||
constexpr absl::string_view kEndpointInfoKey{"n"};
|
||||
|
||||
class WifiLanMediumV2Test : public ::testing::TestWithParam<FeatureFlags> {
|
||||
protected:
|
||||
using DiscoveredServiceCallback = WifiLanMediumV2::DiscoveredServiceCallback;
|
||||
|
||||
WifiLanMediumV2Test() { env_.Stop(); }
|
||||
|
||||
MediumEnvironment& env_{MediumEnvironment::Instance()};
|
||||
};
|
||||
|
||||
TEST_P(WifiLanMediumV2Test, CanConnectToService) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
WifiLanMediumV2 wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
WifiLanServerSocketV2 server_socket = wifi_lan_b.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
nsd_service_info.SetIPAddress(server_socket.GetIPAddress());
|
||||
nsd_service_info.SetPort(server_socket.GetPort());
|
||||
wifi_lan_b.StartAdvertising(nsd_service_info);
|
||||
|
||||
NsdServiceInfo discovered_service_info;
|
||||
wifi_lan_a.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch, &discovered_service_info](
|
||||
NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_service_info = service_info;
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
EXPECT_TRUE(discovered_latch.Await(absl::Milliseconds(1000)).result());
|
||||
WifiLanSocketV2 socket_a;
|
||||
WifiLanSocketV2 socket_b;
|
||||
EXPECT_FALSE(socket_a.IsValid());
|
||||
EXPECT_FALSE(socket_b.IsValid());
|
||||
{
|
||||
CancellationFlag flag;
|
||||
SingleThreadExecutor server_executor;
|
||||
SingleThreadExecutor client_executor;
|
||||
client_executor.Execute([&wifi_lan_a, &socket_a,
|
||||
discovered_service_info = discovered_service_info,
|
||||
service_type, &server_socket, &flag]() {
|
||||
socket_a = wifi_lan_a.ConnectToService(discovered_service_info, &flag);
|
||||
if (!socket_a.IsValid()) {
|
||||
server_socket.Close();
|
||||
}
|
||||
});
|
||||
server_executor.Execute([&socket_b, &server_socket]() {
|
||||
socket_b = server_socket.Accept();
|
||||
if (!socket_b.IsValid()) {
|
||||
server_socket.Close();
|
||||
}
|
||||
});
|
||||
}
|
||||
EXPECT_TRUE(socket_a.IsValid());
|
||||
EXPECT_TRUE(socket_b.IsValid());
|
||||
server_socket.Close();
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_P(WifiLanMediumV2Test, CanCancelConnect) {
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
WifiLanMediumV2 wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
WifiLanServerSocketV2 server_socket = wifi_lan_b.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
nsd_service_info.SetIPAddress(server_socket.GetIPAddress());
|
||||
nsd_service_info.SetPort(server_socket.GetPort());
|
||||
wifi_lan_b.StartAdvertising(nsd_service_info);
|
||||
|
||||
NsdServiceInfo discovered_service_info;
|
||||
wifi_lan_a.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch, &discovered_service_info](
|
||||
NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_service_info = service_info;
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
EXPECT_TRUE(discovered_latch.Await(absl::Milliseconds(1000)).result());
|
||||
WifiLanSocketV2 socket_a;
|
||||
WifiLanSocketV2 socket_b;
|
||||
EXPECT_FALSE(socket_a.IsValid());
|
||||
EXPECT_FALSE(socket_b.IsValid());
|
||||
{
|
||||
CancellationFlag flag(true);
|
||||
SingleThreadExecutor server_executor;
|
||||
SingleThreadExecutor client_executor;
|
||||
client_executor.Execute([&wifi_lan_a, &socket_a,
|
||||
discovered_service_info = discovered_service_info,
|
||||
service_type, &server_socket, &flag]() {
|
||||
socket_a = wifi_lan_a.ConnectToService(discovered_service_info, &flag);
|
||||
if (!socket_a.IsValid()) {
|
||||
server_socket.Close();
|
||||
}
|
||||
});
|
||||
server_executor.Execute([&socket_b, &server_socket]() {
|
||||
socket_b = server_socket.Accept();
|
||||
if (!socket_b.IsValid()) {
|
||||
server_socket.Close();
|
||||
}
|
||||
});
|
||||
}
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(socket_a.IsValid());
|
||||
EXPECT_TRUE(socket_b.IsValid());
|
||||
} else {
|
||||
EXPECT_FALSE(socket_a.IsValid());
|
||||
EXPECT_FALSE(socket_b.IsValid());
|
||||
}
|
||||
server_socket.Close();
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(ParametrisedWifiLanMediumTest, WifiLanMediumV2Test,
|
||||
::testing::ValuesIn(kTestCases));
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, ConstructorDestructorWorks) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
WifiLanMediumV2 wifi_lan_b;
|
||||
|
||||
// Make sure we can create functional mediums.
|
||||
ASSERT_TRUE(wifi_lan_a.IsValid());
|
||||
ASSERT_TRUE(wifi_lan_b.IsValid());
|
||||
|
||||
// Make sure we can create 2 distinct mediums.
|
||||
EXPECT_NE(&wifi_lan_a.GetImpl(), &wifi_lan_b.GetImpl());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanStartAdvertising) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
WifiLanServerSocketV2 server_socket = wifi_lan_a.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanStartMultipleAdvertising) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
std::string service_type_1(kServiceType);
|
||||
std::string service_type_2("_service_1.tcp_");
|
||||
std::string service_info_name_1(kServiceInfoName);
|
||||
std::string service_info_name_2(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
WifiLanServerSocketV2 server_socket = wifi_lan_a.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info_1;
|
||||
nsd_service_info_1.SetServiceName(service_info_name_1);
|
||||
nsd_service_info_1.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info_1.SetServiceType(service_type_1);
|
||||
|
||||
NsdServiceInfo nsd_service_info_2;
|
||||
nsd_service_info_2.SetServiceName(service_info_name_2);
|
||||
nsd_service_info_2.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info_2.SetServiceType(service_type_2);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_2));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info_2));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanStartDiscovery) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id, service_type,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanStartMultipleDiscovery) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
std::string service_id_1(kServiceId);
|
||||
std::string service_id_2("service_id_2");
|
||||
std::string service_type_1(kServiceType);
|
||||
std::string service_type_2("_service_1.tcp_");
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id_1, service_type_1,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id_2, service_type_2,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type_2));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanAdvertiseThatOtherMediumDiscover) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
WifiLanMediumV2 wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
wifi_lan_b.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
WifiLanServerSocketV2 server_socket = wifi_lan_a.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_b.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanDiscoverThatOtherMediumAdvertise) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
WifiLanMediumV2 wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
wifi_lan_a.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
WifiLanServerSocketV2 server_socket = wifi_lan_a.ListenForService();
|
||||
EXPECT_TRUE(server_socket.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
EXPECT_TRUE(wifi_lan_b.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_b.StopAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,146 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "platform/public/wifi_lan_v2.h"
|
||||
|
||||
#include "platform/public/mutex_lock.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
bool WifiLanMediumV2::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
return impl_->StartAdvertising(nsd_service_info);
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
return impl_->StopAdvertising(nsd_service_info);
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StartDiscovery(const std::string& service_id,
|
||||
const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
if (discovery_callbacks_.contains(service_type)) {
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery already start with service_type="
|
||||
<< service_type << "; impl=" << &GetImpl();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
api::WifiLanMediumV2::DiscoveredServiceCallback api_callback = {
|
||||
.service_discovered_cb =
|
||||
[this](NsdServiceInfo service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto pair = discovery_services_.insert(service_type);
|
||||
if (!pair.second) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Discovering (again) service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_name=" << service_info.GetServiceName();
|
||||
return;
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Adding service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_name=" << service_info.GetServiceName();
|
||||
// Callback service found.
|
||||
const auto& it = discovery_callbacks_.find(service_type);
|
||||
if (it != discovery_callbacks_.end()) {
|
||||
std::string service_id = it->second->service_id;
|
||||
DiscoveredServiceCallback medium_callback =
|
||||
it->second->medium_callback;
|
||||
medium_callback.service_discovered_cb(service_info, service_id);
|
||||
} else {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "There is no callback found for service_type="
|
||||
<< service_type;
|
||||
}
|
||||
},
|
||||
.service_lost_cb =
|
||||
[this](NsdServiceInfo service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto item = discovery_services_.extract(service_type);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Removing service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_info_name=" << service_info.GetServiceName();
|
||||
// Callback service lost.
|
||||
const auto& it = discovery_callbacks_.find(service_type);
|
||||
if (it != discovery_callbacks_.end()) {
|
||||
std::string service_id = it->second->service_id;
|
||||
DiscoveredServiceCallback medium_callback =
|
||||
it->second->medium_callback;
|
||||
medium_callback.service_lost_cb(service_info, service_id);
|
||||
}
|
||||
},
|
||||
};
|
||||
{
|
||||
// Insert callback to the map first no matter it succeeds or not.
|
||||
MutexLock lock(&mutex_);
|
||||
auto pair = discovery_callbacks_.insert(
|
||||
{service_type, absl::make_unique<DiscoveryCallbackInfo>()});
|
||||
auto& context = *pair.first->second;
|
||||
context.medium_callback = std::move(callback);
|
||||
context.service_id = service_id;
|
||||
}
|
||||
|
||||
bool success = impl_->StartDiscovery(service_type, std::move(api_callback));
|
||||
if (!success) {
|
||||
// If failed, then revert back the insertion.
|
||||
MutexLock lock(&mutex_);
|
||||
discovery_callbacks_.erase(service_type);
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery started for service_type="
|
||||
<< service_type << ", impl=" << &GetImpl()
|
||||
<< ", success=" << success;
|
||||
return success;
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StopDiscovery(const std::string& service_type) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (!discovery_callbacks_.contains(service_type)) {
|
||||
return false;
|
||||
}
|
||||
discovery_callbacks_.erase(service_type);
|
||||
if (discovery_services_.contains(service_type)) {
|
||||
discovery_services_.erase(service_type);
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery disabled for service_type="
|
||||
<< service_type << ", impl=" << &GetImpl();
|
||||
return impl_->StopDiscovery(service_type);
|
||||
}
|
||||
|
||||
WifiLanSocketV2 WifiLanMediumV2::ConnectToService(
|
||||
const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
NEARBY_LOGS(INFO) << "WifiLanMedium::ConnectToService: remote_service_name="
|
||||
<< remote_service_info.GetServiceName();
|
||||
return WifiLanSocketV2(
|
||||
impl_->ConnectToService(remote_service_info, cancellation_flag));
|
||||
}
|
||||
|
||||
WifiLanSocketV2 WifiLanMediumV2::ConnectToService(
|
||||
const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
NEARBY_LOGS(INFO) << "WifiLanMedium::ConnectToService: ip address="
|
||||
<< ip_address << ", port=" << port;
|
||||
return WifiLanSocketV2(
|
||||
impl_->ConnectToService(ip_address, port, cancellation_flag));
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,206 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef PLATFORM_PUBLIC_WIFI_LAN_V2_H_
|
||||
#define PLATFORM_PUBLIC_WIFI_LAN_V2_H_
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/api/wifi_lan_v2.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "platform/base/cancellation_flag.h"
|
||||
#include "platform/base/input_stream.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/output_stream.h"
|
||||
#include "platform/public/logging.h"
|
||||
#include "platform/public/mutex.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
class WifiLanSocketV2 final {
|
||||
public:
|
||||
WifiLanSocketV2() = default;
|
||||
WifiLanSocketV2(const WifiLanSocketV2&) = default;
|
||||
WifiLanSocketV2& operator=(const WifiLanSocketV2&) = default;
|
||||
~WifiLanSocketV2() = default;
|
||||
explicit WifiLanSocketV2(std::unique_ptr<api::WifiLanSocketV2> socket)
|
||||
: impl_(std::move(socket)) {}
|
||||
|
||||
// Returns the InputStream of the WifiLanSocket.
|
||||
// On error, returned stream will report Exception::kIo on any operation.
|
||||
//
|
||||
// The returned object is not owned by the caller, and can be invalidated once
|
||||
// the WifiLanSocket object is destroyed.
|
||||
InputStream& GetInputStream() { return impl_->GetInputStream(); }
|
||||
|
||||
// Returns the OutputStream of the WifiLanSocket.
|
||||
// On error, returned stream will report Exception::kIo on any operation.
|
||||
//
|
||||
// The returned object is not owned by the caller, and can be invalidated once
|
||||
// the WifiLanSocket object is destroyed.
|
||||
OutputStream& GetOutputStream() { return impl_->GetOutputStream(); }
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() { return impl_->Close(); }
|
||||
|
||||
// Returns true if a socket is usable. If this method returns false,
|
||||
// it is not safe to call any other method.
|
||||
// NOTE(socket validity):
|
||||
// Socket created by a default public constructor is not valid, because
|
||||
// it is missing platform implementation.
|
||||
// The only way to obtain a valid socket is through connection, such as
|
||||
// an object returned by WifiLanMedium::Connect
|
||||
// These methods may also return an invalid socket if connection failed for
|
||||
// any reason.
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
// Returns reference to platform implementation.
|
||||
// This is used to communicate with platform code, and for debugging purposes.
|
||||
// Returned reference will remain valid for while WifiLanSocket object is
|
||||
// itself valid. Typically WifiLanSocket lifetime matches duration of the
|
||||
// connection, and is controlled by end user, since they hold the instance.
|
||||
api::WifiLanSocketV2& GetImpl() { return *impl_; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<api::WifiLanSocketV2> impl_;
|
||||
};
|
||||
|
||||
class WifiLanServerSocketV2 final {
|
||||
public:
|
||||
WifiLanServerSocketV2() = default;
|
||||
WifiLanServerSocketV2(const WifiLanServerSocketV2&) = default;
|
||||
WifiLanServerSocketV2& operator=(const WifiLanServerSocketV2&) = default;
|
||||
~WifiLanServerSocketV2() = default;
|
||||
explicit WifiLanServerSocketV2(
|
||||
std::unique_ptr<api::WifiLanServerSocketV2> socket)
|
||||
: impl_(std::move(socket)) {}
|
||||
|
||||
// Returns ip address.
|
||||
std::string GetIPAddress() { return impl_->GetIPAddress(); }
|
||||
|
||||
// Returns port.
|
||||
int GetPort() { return impl_->GetPort(); }
|
||||
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
// On success, returns connected socket, ready to exchange data.
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be closed.
|
||||
WifiLanSocketV2 Accept() {
|
||||
std::unique_ptr<api::WifiLanSocketV2> socket = impl_->Accept();
|
||||
if (!socket) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "WifiLanServerSocket Accept() failed on server socket: " << this;
|
||||
}
|
||||
return WifiLanSocketV2(std::move(socket));
|
||||
}
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() {
|
||||
NEARBY_LOGS(INFO) << "WifiLanServerSocket Closing:: " << this;
|
||||
return impl_->Close();
|
||||
}
|
||||
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
api::WifiLanServerSocketV2& GetImpl() { return *impl_; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<api::WifiLanServerSocketV2> impl_;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the WifiLan medium.
|
||||
class WifiLanMediumV2 {
|
||||
public:
|
||||
using Platform = api::ImplementationPlatform;
|
||||
|
||||
struct DiscoveredServiceCallback {
|
||||
std::function<void(NsdServiceInfo service_info,
|
||||
const std::string& service_type)>
|
||||
service_discovered_cb =
|
||||
DefaultCallback<NsdServiceInfo, const std::string&>();
|
||||
std::function<void(NsdServiceInfo service_info,
|
||||
const std::string& service_type)>
|
||||
service_lost_cb = DefaultCallback<NsdServiceInfo, const std::string&>();
|
||||
};
|
||||
|
||||
struct DiscoveryCallbackInfo {
|
||||
std::string service_id;
|
||||
DiscoveredServiceCallback medium_callback;
|
||||
};
|
||||
|
||||
WifiLanMediumV2() : impl_(Platform::CreateWifiLanMediumV2()) {}
|
||||
~WifiLanMediumV2() = default;
|
||||
|
||||
// Starts WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service is now advertising.
|
||||
// On error if the service cannot start to advertise or the nsd_type in
|
||||
// NsdServiceInfo has been passed previously which StopAdvertising is not
|
||||
// been called.
|
||||
bool StartAdvertising(const NsdServiceInfo& nsd_service_info);
|
||||
|
||||
// Stops WifiLan advertising.
|
||||
//
|
||||
// nsd_service_info - NsdServiceInfo data that's advertised through mDNS
|
||||
// service.
|
||||
// On success if the service stops advertising.
|
||||
// On error if the service cannot stop advertising or the nsd_type in
|
||||
// NsdServiceInfo cannot be found.
|
||||
bool StopAdvertising(const NsdServiceInfo& nsd_service_info);
|
||||
|
||||
// Returns true once the WifiLan discovery has been initiated.
|
||||
bool StartDiscovery(const std::string& service_id,
|
||||
const std::string& service_type,
|
||||
DiscoveredServiceCallback callback);
|
||||
|
||||
// Returns true once service_type is associated to existing callback. If the
|
||||
// callback is the last found then WifiLan discovery will be stopped.
|
||||
bool StopDiscovery(const std::string& service_type);
|
||||
|
||||
// Returns a new WifiLanSocket.
|
||||
// On Success, WifiLanSocket::IsValid() returns true.
|
||||
WifiLanSocketV2 ConnectToService(const NsdServiceInfo& remote_service_info,
|
||||
CancellationFlag* cancellation_flag);
|
||||
|
||||
// Returns a new WifiLanSocket by ip address and port.
|
||||
// On Success, WifiLanSocket::IsValid()returns true.
|
||||
WifiLanSocketV2 ConnectToService(const std::string& ip_address, int port,
|
||||
CancellationFlag* cancellation_flag);
|
||||
|
||||
// Returns a new WifiLanServerSocket.
|
||||
// On Success, WifiLanServerSocket::IsValid() returns true.
|
||||
WifiLanServerSocketV2 ListenForService(int port = 0) {
|
||||
return WifiLanServerSocketV2(impl_->ListenForService(port));
|
||||
}
|
||||
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
api::WifiLanMediumV2& GetImpl() { return *impl_; }
|
||||
|
||||
private:
|
||||
Mutex mutex_;
|
||||
std::unique_ptr<api::WifiLanMediumV2> impl_;
|
||||
absl::flat_hash_map<std::string, std::unique_ptr<DiscoveryCallbackInfo>>
|
||||
discovery_callbacks_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_set<std::string> discovery_services_ ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_PUBLIC_WIFI_LAN_V2_H_
|
||||
Reference in New Issue
Block a user