mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 15:16:12 -04:00
Merge branch 'master' into release
Change-Id: Ieb7f2b392864096e9cd783ab89f32442bc57abe7
This commit is contained in:
@@ -27,7 +27,8 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
|
||||
// Opaque wrapper over a WifiLan service which contains encoded service name.
|
||||
// Opaque wrapper over a WifiLan service which contains packed
|
||||
// |WifiLanServiceInfo| string name.
|
||||
class WifiLanService {
|
||||
public:
|
||||
virtual ~WifiLanService() = default;
|
||||
@@ -71,10 +72,8 @@ class WifiLanMedium {
|
||||
const std::string& wifi_lan_service_info_name) = 0;
|
||||
virtual bool StopAdvertising(const std::string& service_id) = 0;
|
||||
|
||||
// Callback that is invoked when a discovered service is found or lost.
|
||||
struct DiscoveredServiceCallback {
|
||||
// The WifiLanService* is not owned by callbacks.
|
||||
// It is passed to give access to its non-const methods.
|
||||
// It is guaranteed to be valid for the duration of call.
|
||||
std::function<void(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id)>
|
||||
service_discovered_cb =
|
||||
|
||||
@@ -36,9 +36,10 @@ MediumEnvironment& MediumEnvironment::Instance() {
|
||||
return *env;
|
||||
}
|
||||
|
||||
void MediumEnvironment::Start() {
|
||||
void MediumEnvironment::Start(EnvironmentConfig config) {
|
||||
if (!enabled_.exchange(true)) {
|
||||
NEARBY_LOG(INFO, "MediumEnvironment::Start()");
|
||||
config_ = std::move(config);
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
@@ -79,6 +80,10 @@ void MediumEnvironment::Sync(bool enable_notifications) {
|
||||
NEARBY_LOG(INFO, "MediumEnvironment::Sync(): done [count=%d]", count);
|
||||
}
|
||||
|
||||
const EnvironmentConfig& MediumEnvironment::GetEnvironmentConfig() {
|
||||
return config_;
|
||||
}
|
||||
|
||||
void MediumEnvironment::OnBluetoothAdapterChangedState(
|
||||
api::BluetoothAdapter& adapter, api::BluetoothDevice& adapter_device,
|
||||
std::string name, bool enabled, api::BluetoothAdapter::ScanMode mode) {
|
||||
@@ -88,7 +93,8 @@ void MediumEnvironment::OnBluetoothAdapterChangedState(
|
||||
NEARBY_LOG(INFO,
|
||||
"[adapter=%p, device=%p] update: name=%s, enabled=%d, mode=%d",
|
||||
&adapter, &adapter_device, name.c_str(), enabled, mode);
|
||||
for (auto& [medium, info] : bluetooth_mediums_) {
|
||||
for (auto& medium_info : bluetooth_mediums_) {
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to medium that owns this adapter.
|
||||
if (info.adapter == &adapter) continue;
|
||||
NEARBY_LOG(INFO, "[adapter=%p, device=%p] notify: adapter=%p", &adapter,
|
||||
@@ -167,19 +173,22 @@ void MediumEnvironment::OnWifiLanServiceStateChanged(
|
||||
const std::string& service_id, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 OnWifiLanServiceStateChanged [service impl=%p]; context=%p, "
|
||||
"notify=%d",
|
||||
&info, &service, enable_notifications_.load());
|
||||
"G3 OnWifiLanServiceStateChanged [service impl=%p]; context=%p; "
|
||||
"service_id=%s; notify=%d",
|
||||
&service, &info, service_id.c_str(), enable_notifications_.load());
|
||||
if (!enable_notifications_) return;
|
||||
if (enabled) {
|
||||
RunOnMediumEnvironmentThread([&info, &service, service_id]() {
|
||||
info.discovery_callback.service_discovered_cb(service, service_id);
|
||||
});
|
||||
} else {
|
||||
RunOnMediumEnvironmentThread([&info, &service, service_id]() {
|
||||
info.discovery_callback.service_lost_cb(service, service_id);
|
||||
});
|
||||
}
|
||||
RunOnMediumEnvironmentThread([&info, enabled, &service, service_id]() {
|
||||
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(
|
||||
service, service_id);
|
||||
} else {
|
||||
service_id_context->second.discovery_callback.service_lost_cb(service,
|
||||
service_id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::RunOnMediumEnvironmentThread(
|
||||
@@ -202,7 +211,9 @@ void MediumEnvironment::RegisterBluetoothMedium(
|
||||
auto* owned_adapter = context.adapter;
|
||||
NEARBY_LOG(INFO, "Registered: medium=%p; adapter=%p", &medium,
|
||||
owned_adapter);
|
||||
for (auto& [adapter, device] : bluetooth_adapters_) {
|
||||
for (auto& adapter_device : bluetooth_adapters_) {
|
||||
auto& adapter = adapter_device.first;
|
||||
auto& device = adapter_device.second;
|
||||
if (adapter == nullptr) continue;
|
||||
OnBluetoothDeviceStateChanged(context, *device, adapter->GetName(),
|
||||
adapter->GetScanMode(),
|
||||
@@ -226,7 +237,9 @@ void MediumEnvironment::UpdateBluetoothMedium(
|
||||
"Updated: this=%p; medium=%p; adapter=%p; name=%s; enabled=%d; mode=%d",
|
||||
this, &medium, owned_adapter, owned_adapter->GetName().c_str(),
|
||||
owned_adapter->IsEnabled(), owned_adapter->GetScanMode());
|
||||
for (auto& [adapter, device] : bluetooth_adapters_) {
|
||||
for (auto& adapter_device : bluetooth_adapters_) {
|
||||
auto& adapter = adapter_device.first;
|
||||
auto& device = adapter_device.second;
|
||||
if (adapter == nullptr) continue;
|
||||
OnBluetoothDeviceStateChanged(context, *device, adapter->GetName(),
|
||||
adapter->GetScanMode(),
|
||||
@@ -285,13 +298,10 @@ void MediumEnvironment::SendWebRtcSignalingMessage(absl::string_view peer_id,
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium,
|
||||
api::WifiLanService& service) {
|
||||
void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &service]() {
|
||||
wifi_lan_mediums_.insert({&medium, WifiLanMediumContext{
|
||||
.service = &service,
|
||||
}});
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
wifi_lan_mediums_.insert({&medium, WifiLanMediumContext{}});
|
||||
NEARBY_LOG(INFO, "Registered: medium=%p", &medium);
|
||||
});
|
||||
}
|
||||
@@ -304,18 +314,31 @@ void MediumEnvironment::UpdateWifiLanMediumForAdvertising(
|
||||
enabled]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Update WifiLan medium failed. There is no medium registered.");
|
||||
NEARBY_LOG(INFO,
|
||||
"UpdateWifiLanMediumForAdvertising failed. There is no medium "
|
||||
"registered.");
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.advertising = enabled;
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Update WifiLan medium for advertising: this=%p; medium=%p; name=%s; "
|
||||
"enabled=%d; advertising=%d",
|
||||
this, &medium, service.GetName().c_str(), enabled, context.advertising);
|
||||
for (auto& [local_medium, info] : wifi_lan_mediums_) {
|
||||
context.wifi_lan_service = &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_LOG(INFO,
|
||||
"Update WifiLan medium for advertising: this=%p; medium=%p; "
|
||||
"service_id=%s; name=%s; "
|
||||
"enabled=%d",
|
||||
this, &medium, service_id.c_str(), service.GetName().c_str(),
|
||||
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, service, service_id, enabled);
|
||||
@@ -324,45 +347,56 @@ void MediumEnvironment::UpdateWifiLanMediumForAdvertising(
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForDiscovery(
|
||||
api::WifiLanMedium& medium, api::WifiLanService& service,
|
||||
const std::string& service_id, WifiLanDiscoveredServiceCallback callback,
|
||||
bool enabled) {
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanDiscoveredServiceCallback callback, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &service, service_id,
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_id,
|
||||
callback = std::move(callback), enabled]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Update WifiLan medium failed. There is no medium registered.");
|
||||
NEARBY_LOG(INFO,
|
||||
"UpdateWifiLanMediumForDiscovery failed. There is no medium "
|
||||
"registered.");
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.discovery_callback = std::move(callback);
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Update WifiLan medium for discovery: this=%p; medium=%p; name=%s; "
|
||||
"enabled=%d; advertising=%d",
|
||||
this, &medium, service.GetName().c_str(), enabled, context.advertising);
|
||||
for (auto& [local_medium, info] : wifi_lan_mediums_) {
|
||||
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_LOG(INFO,
|
||||
"Update WifiLan medium for discovery: this=%p; medium=%p; "
|
||||
"service_id=%s; enabled=%d; ",
|
||||
this, &medium, service_id.c_str(), 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.
|
||||
if (info.advertising && enabled) {
|
||||
OnWifiLanServiceStateChanged(context, *(info.service), service_id,
|
||||
enabled);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForAcceptedConnection(
|
||||
api::WifiLanMedium& medium, api::WifiLanService& service,
|
||||
const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback accepted_connection_callback) {
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback callback) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &service, service_id,
|
||||
accepted_connection_callback =
|
||||
std::move(accepted_connection_callback)]() {
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_id,
|
||||
callback = std::move(callback)]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOG(
|
||||
@@ -370,12 +404,20 @@ void MediumEnvironment::UpdateWifiLanMediumForAcceptedConnection(
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.accepted_connection_callback =
|
||||
std::move(accepted_connection_callback);
|
||||
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_LOG(INFO,
|
||||
"Update WifiLan medium for accepted callback: this=%p; "
|
||||
"medium=%p; name=%s; ",
|
||||
this, &medium, service.GetName().c_str());
|
||||
"medium=%p; service_id=%s; ",
|
||||
this, &medium, service_id.c_str());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -401,7 +443,11 @@ void MediumEnvironment::CallWifiLanAcceptedConnectionCallback(
|
||||
return;
|
||||
}
|
||||
auto& info = item->second;
|
||||
info.accepted_connection_callback.accepted_cb(socket, service_id);
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,15 @@
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
// Environment config that can control availability of certain mediums for
|
||||
// testing.
|
||||
struct EnvironmentConfig {
|
||||
// Control whether WEB_RTC medium is enabled in the environment.
|
||||
// This is currently set to false, due to http://b/139734036 that would lead
|
||||
// to flaky tests.
|
||||
bool webrtc_enabled = false;
|
||||
};
|
||||
|
||||
// MediumEnvironment is a simulated environment which allows multiple instances
|
||||
// of simulated HW devices to "work" together as if they are physical.
|
||||
// For each medium type it provides necessary methods to implement
|
||||
@@ -44,6 +53,7 @@ class MediumEnvironment {
|
||||
api::WifiLanMedium::DiscoveredServiceCallback;
|
||||
using WifiLanAcceptedConnectionCallback =
|
||||
api::WifiLanMedium::AcceptedConnectionCallback;
|
||||
|
||||
MediumEnvironment(const MediumEnvironment&) = delete;
|
||||
MediumEnvironment& operator=(const MediumEnvironment&) = delete;
|
||||
|
||||
@@ -56,7 +66,7 @@ class MediumEnvironment {
|
||||
// tests that are already using it and relying on it being ON.
|
||||
|
||||
// Enables Medium environment.
|
||||
void Start();
|
||||
void Start(EnvironmentConfig config = EnvironmentConfig());
|
||||
// Disables Medium environment.
|
||||
void Stop();
|
||||
|
||||
@@ -107,6 +117,8 @@ class MediumEnvironment {
|
||||
// Removes medium-related info. This should correspond to device power off.
|
||||
void UnregisterBluetoothMedium(api::BluetoothClassicMedium& medium);
|
||||
|
||||
const EnvironmentConfig& GetEnvironmentConfig();
|
||||
|
||||
// Registers |callback| to receive messages sent to device with id |self_id|.
|
||||
void RegisterWebRtcSignalingMessenger(absl::string_view self_id,
|
||||
OnSignalingMessageCallback callback);
|
||||
@@ -121,8 +133,7 @@ class MediumEnvironment {
|
||||
// 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 RegisterWifiLanMedium(api::WifiLanMedium& medium,
|
||||
api::WifiLanService& service);
|
||||
void RegisterWifiLanMedium(api::WifiLanMedium& medium);
|
||||
|
||||
// Updates advertising info to indicate the current medium is exposing
|
||||
// advertising event.
|
||||
@@ -140,16 +151,14 @@ class MediumEnvironment {
|
||||
// with user-specified callback when discovery is enabled, and with default
|
||||
// (empty) callback otherwise.
|
||||
void UpdateWifiLanMediumForDiscovery(
|
||||
api::WifiLanMedium& medium, api::WifiLanService& service,
|
||||
const std::string& service_id,
|
||||
WifiLanDiscoveredServiceCallback discovery_callback, bool enabled);
|
||||
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, api::WifiLanService& service,
|
||||
const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback accepted_connection_callback);
|
||||
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);
|
||||
@@ -168,13 +177,17 @@ class MediumEnvironment {
|
||||
absl::flat_hash_map<api::BluetoothDevice*, std::string> devices;
|
||||
};
|
||||
|
||||
struct WifiLanMediumContext {
|
||||
struct WifiLanServiceIdContext {
|
||||
WifiLanDiscoveredServiceCallback discovery_callback;
|
||||
WifiLanAcceptedConnectionCallback accepted_connection_callback;
|
||||
api::WifiLanService* service = nullptr;
|
||||
bool advertising = false;
|
||||
};
|
||||
|
||||
struct WifiLanMediumContext {
|
||||
api::WifiLanService* wifi_lan_service = nullptr;
|
||||
absl::flat_hash_map<std::string, WifiLanServiceIdContext> services;
|
||||
};
|
||||
|
||||
// This is a singleton object, for which destructor will never be called.
|
||||
// Constructor will be invoked once from Instance() static method.
|
||||
// Object is create in-place (with a placement new) to guarantee that
|
||||
@@ -199,6 +212,7 @@ class MediumEnvironment {
|
||||
std::atomic_int job_count_ = 0;
|
||||
std::atomic_bool enable_notifications_ = false;
|
||||
SingleThreadExecutor executor_;
|
||||
EnvironmentConfig config_;
|
||||
|
||||
// The following data members are accessed in the context of a private
|
||||
// executor_ thread.
|
||||
|
||||
@@ -119,6 +119,7 @@ cc_library(
|
||||
"//platform_v2/api:comm",
|
||||
"//platform_v2/api:platform",
|
||||
"//platform_v2/api:types",
|
||||
"//platform_v2/base:test_util",
|
||||
"//platform_v2/impl/shared:file",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/memory",
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "platform_v2/api/submittable_executor.h"
|
||||
#include "platform_v2/api/webrtc.h"
|
||||
#include "platform_v2/api/wifi.h"
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
#include "platform_v2/impl/g3/atomic_boolean.h"
|
||||
#include "platform_v2/impl/g3/atomic_reference.h"
|
||||
#include "platform_v2/impl/g3/bluetooth_adapter.h"
|
||||
@@ -147,7 +148,11 @@ std::unique_ptr<WifiLanMedium> ImplementationPlatform::CreateWifiLanMedium() {
|
||||
}
|
||||
|
||||
std::unique_ptr<WebRtcMedium> ImplementationPlatform::CreateWebRtcMedium() {
|
||||
return absl::make_unique<g3::WebRtcMedium>();
|
||||
if (MediumEnvironment::Instance().GetEnvironmentConfig().webrtc_enabled) {
|
||||
return absl::make_unique<g3::WebRtcMedium>();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Mutex> ImplementationPlatform::CreateMutex(Mutex::Mode mode) {
|
||||
|
||||
@@ -170,7 +170,7 @@ Exception WifiLanServerSocket::DoClose() {
|
||||
WifiLanMedium::WifiLanMedium() {
|
||||
service_.SetMedium(this);
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.RegisterWifiLanMedium(*this, service_);
|
||||
env.RegisterWifiLanMedium(*this);
|
||||
}
|
||||
|
||||
WifiLanMedium::~WifiLanMedium() {
|
||||
@@ -181,7 +181,6 @@ WifiLanMedium::~WifiLanMedium() {
|
||||
StopAdvertising(advertising_info_.service_id);
|
||||
StopDiscovery(discovering_info_.service_id);
|
||||
|
||||
accept_loops_runner_.Shutdown();
|
||||
NEARBY_LOG(INFO,
|
||||
"WifiLanMedium dtor advertising_accept_thread_running_ = %d",
|
||||
acceptance_thread_running_.load());
|
||||
@@ -195,12 +194,11 @@ WifiLanMedium::~WifiLanMedium() {
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartAdvertising(
|
||||
const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name) {
|
||||
bool WifiLanMedium::StartAdvertising(const std::string& service_id,
|
||||
const std::string& service_info_name) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StartAdvertising: service_id=%s, service_name=%s",
|
||||
service_id.c_str(), wifi_lan_service_info_name.c_str());
|
||||
"G3 WifiLan StartAdvertising: service_id=%s, service_info_name=%s",
|
||||
service_id.c_str(), service_info_name.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, service_, service_id, true);
|
||||
|
||||
@@ -230,8 +228,9 @@ bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (advertising_info_.Empty()) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Can't stop advertising because we never started advertising.");
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopAdvertising: Can't stop advertising because "
|
||||
"we never started advertising.");
|
||||
return false;
|
||||
}
|
||||
advertising_info_.Clear();
|
||||
@@ -241,14 +240,17 @@ bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, service_, service_id, false);
|
||||
accept_loops_runner_.Shutdown();
|
||||
if (server_socket_ == nullptr) {
|
||||
NEARBY_LOG(ERROR, "Failed to find WifiLan Server socket: service_id=%s",
|
||||
service_id.c_str());
|
||||
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;
|
||||
}
|
||||
|
||||
if (!server_socket_->Close().Ok()) {
|
||||
NEARBY_LOG(INFO, "Failed to close WifiLan server socket for %s.",
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopAdvertising: Failed to close WifiLan server "
|
||||
"socket for %s.",
|
||||
service_id.c_str());
|
||||
return false;
|
||||
}
|
||||
@@ -261,8 +263,8 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_id,
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StartDiscovery: service_id=%s",
|
||||
service_id.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_, service_id,
|
||||
std::move(callback), true);
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_id, std::move(callback),
|
||||
true);
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
discovering_info_.service_id = service_id;
|
||||
@@ -276,15 +278,16 @@ bool WifiLanMedium::StopDiscovery(const std::string& service_id) {
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (discovering_info_.Empty()) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Can't stop discovering because we never started discovering.");
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopDiscovery: Can't stop discovering because we "
|
||||
"never started discovering.");
|
||||
return false;
|
||||
}
|
||||
discovering_info_.Clear();
|
||||
}
|
||||
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_, service_id, {}, false);
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_id, {}, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -293,8 +296,7 @@ bool WifiLanMedium::StartAcceptingConnections(
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StartAcceptingConnections: service_id=%s",
|
||||
service_id.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_, service_id,
|
||||
callback);
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_id, callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -302,7 +304,7 @@ 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_, service_id, {});
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_id, {});
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -315,28 +317,31 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
|
||||
|
||||
if (!medium) return {}; // Can't find medium. Bail out.
|
||||
|
||||
WifiLanServerSocket* server_socket = nullptr;
|
||||
WifiLanServerSocket* remote_server_socket = nullptr;
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan Connect [peer]: medium=%p, service=%p, service_id=%s",
|
||||
medium, &remote_service, service_id.c_str());
|
||||
// Then, find our server socket context in this medium.
|
||||
{
|
||||
absl::MutexLock medium_lock(&medium->mutex_);
|
||||
server_socket = medium->server_socket_.get();
|
||||
if (server_socket == nullptr) {
|
||||
NEARBY_LOG(ERROR, "Failed to find WifiLan Server socket: service_id=%s",
|
||||
remote_server_socket = medium->server_socket_.get();
|
||||
if (remote_server_socket == nullptr) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"G3 WifiLan Connect: Failed to find WifiLan Server socket: "
|
||||
"service_id=%s",
|
||||
service_id.c_str());
|
||||
// Fall through for server socket not found.
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
auto socket = std::make_unique<WifiLanSocket>();
|
||||
// Finally, Request to connect to this socket.
|
||||
if (!server_socket->Connect(*socket)) {
|
||||
NEARBY_LOG(
|
||||
ERROR,
|
||||
"Failed to connect to existing WifiLan Server socket: service_id=%s",
|
||||
service_id.c_str());
|
||||
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());
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
@@ -34,21 +34,24 @@ namespace g3 {
|
||||
|
||||
class WifiLanMedium;
|
||||
|
||||
// Opaque wrapper over a WifiLan service which contains encoded WifiLan service
|
||||
// info name.
|
||||
// Opaque wrapper over a WifiLan service which contains packed
|
||||
// |WifiLanServiceInfo| string name.
|
||||
class WifiLanService : public api::WifiLanService {
|
||||
public:
|
||||
explicit WifiLanService(std::string name) : name_(std::move(name)) {}
|
||||
explicit WifiLanService(std::string service_info_name)
|
||||
: service_info_name_(std::move(service_info_name)) {}
|
||||
~WifiLanService() override = default;
|
||||
|
||||
void SetName(std::string name) { name_ = std::move(name); }
|
||||
std::string GetName() const override { return name_; }
|
||||
void SetName(std::string service_info_name) {
|
||||
service_info_name_ = std::move(service_info_name);
|
||||
}
|
||||
std::string GetName() const override { return service_info_name_; }
|
||||
|
||||
void SetMedium(WifiLanMedium* medium) { medium_ = medium; }
|
||||
WifiLanMedium* GetMedium() { return medium_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string service_info_name_;
|
||||
WifiLanMedium* medium_ = nullptr;
|
||||
};
|
||||
|
||||
@@ -165,7 +168,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
~WifiLanMedium() override;
|
||||
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name) override
|
||||
const std::string& service_info_name) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAdvertising(const std::string& service_id) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -215,7 +218,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
};
|
||||
|
||||
absl::Mutex mutex_;
|
||||
WifiLanService service_{"wifi_lan_service_info_name"};
|
||||
WifiLanService service_{"unknown G3 WifiLan service"};
|
||||
|
||||
// A thread pool dedicated to running all the accept loops from
|
||||
// StartAdvertising().
|
||||
@@ -225,8 +228,6 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
// A thread pool dedicated to wait to complete the accept_loops_runner_.
|
||||
MultiThreadExecutor close_accept_loops_runner_{kMaxConcurrentAcceptLoops};
|
||||
|
||||
// TODO(edwinwu): Extend it to hashmap to accept multiple sockets for multiple
|
||||
// entrance.
|
||||
// A server socket is established when start advertising.
|
||||
std::unique_ptr<WifiLanServerSocket> server_socket_;
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
@@ -22,8 +22,8 @@ namespace nearby {
|
||||
|
||||
bool WifiLanMedium::StartAdvertising(
|
||||
const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name) {
|
||||
return impl_->StartAdvertising(service_id, wifi_lan_service_info_name);
|
||||
const std::string& service_info_name) {
|
||||
return impl_->StartAdvertising(service_id, service_info_name);
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
||||
@@ -48,13 +48,18 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_id,
|
||||
&service, absl::make_unique<ServiceDiscoveryInfo>());
|
||||
auto& context = *pair.first->second;
|
||||
if (!pair.second) {
|
||||
NEARBY_LOG(INFO, "Adding (again) service=%p, impl=%p",
|
||||
&context.service, &service);
|
||||
NEARBY_LOG(INFO,
|
||||
"Discovering (again) service=%p, impl=%p, "
|
||||
"service_info_name=%s",
|
||||
&context.service, &service,
|
||||
service.GetName().c_str());
|
||||
return;
|
||||
}
|
||||
context.service = WifiLanService(&service);
|
||||
NEARBY_LOG(INFO, "Adding service=%p, impl=%p", &context.service,
|
||||
&service);
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Discovering service=%p, impl=%p, service_info_name=%s",
|
||||
&context.service, &service, service.GetName().c_str());
|
||||
discovered_service_callback_.service_discovered_cb(
|
||||
context.service, service_id);
|
||||
},
|
||||
@@ -100,12 +105,13 @@ bool WifiLanMedium::StartAcceptingConnections(
|
||||
&socket, absl::make_unique<AcceptedConnectionInfo>());
|
||||
auto& context = *pair.first->second;
|
||||
if (!pair.second) {
|
||||
NEARBY_LOG(INFO, "Adding (again) socket=%p, impl=%p",
|
||||
NEARBY_LOG(INFO, "Accepting (again) socket=%p, impl=%p",
|
||||
&context.socket, &socket);
|
||||
context.socket = WifiLanSocket(&socket);
|
||||
} else {
|
||||
NEARBY_LOG(INFO, "Accepting socket=%p, impl=%p",
|
||||
&context.socket, &socket);
|
||||
}
|
||||
NEARBY_LOG(INFO, "Adding socket=%p, impl=%p", &context.socket,
|
||||
&socket);
|
||||
accepted_connection_callback_.accepted_cb(context.socket,
|
||||
service_id);
|
||||
},
|
||||
|
||||
@@ -26,7 +26,8 @@
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
// Opaque wrapper over a WifiLan service which contains encoded service name.
|
||||
// Opaque wrapper over a WifiLan service which contains packed
|
||||
// |WifiLanServiceInfo| string name.
|
||||
class WifiLanService final {
|
||||
public:
|
||||
WifiLanService() = default;
|
||||
@@ -127,7 +128,7 @@ class WifiLanMedium final {
|
||||
~WifiLanMedium() = default;
|
||||
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name);
|
||||
const std::string& service_info_name);
|
||||
bool StopAdvertising(const std::string& service_id);
|
||||
|
||||
// Returns true once the WifiLan discovery has been initiated.
|
||||
|
||||
Reference in New Issue
Block a user