mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
nearbyconnections : Adds accessor of ServiceType for NsdServiceInfo.
PiperOrigin-RevId: 404003948
This commit is contained in:
committed by
Copybara-Service
parent
b6ea14f741
commit
296274bc48
@@ -47,6 +47,7 @@ cc_library(
|
||||
"//absl/functional:bind_front",
|
||||
"//absl/numeric:int128",
|
||||
"//absl/strings",
|
||||
"//absl/strings:str_format",
|
||||
"//absl/time",
|
||||
"//core:core_types",
|
||||
"//core/internal/mediums/ble_v2",
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "core/internal/mediums/utils.h"
|
||||
#include "platform/public/logging.h"
|
||||
#include "platform/public/mutex_lock.h"
|
||||
|
||||
@@ -34,7 +36,7 @@ bool WifiLan::IsAvailable() const {
|
||||
bool WifiLan::IsAvailableLocked() const { return medium_.IsValid(); }
|
||||
|
||||
bool WifiLan::StartAdvertising(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info) {
|
||||
NsdServiceInfo& nsd_service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!nsd_service_info.IsValid()) {
|
||||
@@ -56,18 +58,19 @@ bool WifiLan::StartAdvertising(const std::string& service_id,
|
||||
return false;
|
||||
}
|
||||
|
||||
nsd_service_info.SetServiceType(GenerateServiceType(service_id));
|
||||
if (!medium_.StartAdvertising(service_id, nsd_service_info)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Failed to turn on WifiLan advertising with wifi_lan_service="
|
||||
<< &nsd_service_info
|
||||
<< ", service_info_name=" << nsd_service_info.GetServiceInfoName()
|
||||
<< ", service_info_name=" << nsd_service_info.GetServiceName()
|
||||
<< ", service_id=" << service_id;
|
||||
return false;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with wifi_lan_service="
|
||||
<< &nsd_service_info << ", service_info_name="
|
||||
<< nsd_service_info.GetServiceInfoName()
|
||||
<< nsd_service_info.GetServiceName()
|
||||
<< ", service_id=" << service_id;
|
||||
advertising_info_.Add(service_id);
|
||||
return true;
|
||||
@@ -234,7 +237,7 @@ WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service,
|
||||
MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << "WifiLan::Connect: wifi_lan_service="
|
||||
<< &wifi_lan_service << ", service_info_name="
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceName()
|
||||
<< ", service_id=" << service_id;
|
||||
// Socket to return. To allow for NRVO to work, it has to be a single object.
|
||||
WifiLanSocket socket;
|
||||
@@ -273,10 +276,23 @@ WifiLanService WifiLan::GetRemoteWifiLanService(const std::string& ip_address,
|
||||
return medium_.GetRemoteService(ip_address, port);
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLan::GetServiceAddress(
|
||||
std::pair<std::string, int> WifiLan::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
return medium_.GetServiceAddress(service_id);
|
||||
return medium_.GetCredentials(service_id);
|
||||
}
|
||||
|
||||
std::string WifiLan::GenerateServiceType(const std::string& service_id) {
|
||||
std::string service_id_hash_string;
|
||||
|
||||
const ByteArray service_id_hash = Utils::Sha256Hash(
|
||||
service_id, NsdServiceInfo::kTypeFromServiceIdHashLength);
|
||||
for (auto byte : std::string(service_id_hash)) {
|
||||
absl::StrAppend(&service_id_hash_string, absl::StrFormat("%02X", byte));
|
||||
}
|
||||
|
||||
return absl::StrFormat(NsdServiceInfo::kNsdTypeFormat,
|
||||
service_id_hash_string);
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -42,7 +42,7 @@ class WifiLan {
|
||||
// then enables WifiLan advertising.
|
||||
// Returns true, if name is successfully set, and false otherwise.
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const NsdServiceInfo& nsd_service_info)
|
||||
NsdServiceInfo& nsd_service_info)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables WifiLan advertising, and restores service info name to
|
||||
@@ -89,7 +89,7 @@ class WifiLan {
|
||||
WifiLanService GetRemoteWifiLanService(const std::string& ip_address,
|
||||
int port) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
std::pair<std::string, int> GetServiceAddress(const std::string& service_id)
|
||||
std::pair<std::string, int> GetCredentials(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
@@ -150,6 +150,9 @@ class WifiLan {
|
||||
bool IsAcceptingConnectionsLocked(const std::string& service_id)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Generates mDNS type.
|
||||
std::string GenerateServiceType(const std::string& service_id);
|
||||
|
||||
mutable Mutex mutex_;
|
||||
WifiLanMedium medium_ ABSL_GUARDED_BY(mutex_);
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
@@ -69,7 +69,7 @@ TEST_P(WifiLanTest, CanStartAcceptingConnectionsAndConnect) {
|
||||
CountDownLatch accept_latch(1);
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_lan_a.StartAdvertising(service_id, nsd_service_info);
|
||||
@@ -120,7 +120,7 @@ TEST_P(WifiLanTest, CanCancelConnect) {
|
||||
CountDownLatch accept_latch(1);
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_lan_a.StartAdvertising(service_id, nsd_service_info);
|
||||
@@ -197,7 +197,7 @@ TEST_F(WifiLanTest, CanStartAdvertising) {
|
||||
});
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id, nsd_service_info));
|
||||
@@ -218,7 +218,7 @@ TEST_F(WifiLanTest, CanStartDiscovery) {
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
wifi_lan_b.StartAdvertising(service_id, nsd_service_info);
|
||||
|
||||
@@ -578,7 +578,7 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
|
||||
if (!client->IsDiscovering()) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "Skipping discovery of NsdServiceInfo "
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceName()
|
||||
<< " because we are no longer discovering.";
|
||||
return;
|
||||
}
|
||||
@@ -593,7 +593,7 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler(
|
||||
// Report the discovered endpoint to the client.
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Found NsdServiceInfo "
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceName()
|
||||
<< " (with endpoint_id=" << service_info.GetEndpointId()
|
||||
<< "and endpoint_info="
|
||||
<< absl::BytesToHexString(service_info.GetEndpointInfo().data())
|
||||
@@ -618,7 +618,7 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
|
||||
NsdServiceInfo nsd_service_info = wifi_lan_service.GetServiceInfo();
|
||||
NEARBY_LOG(INFO,
|
||||
"WifiLan: [LOST, SCHED] wifi_lan_service=%p, service_info_name=%s",
|
||||
&wifi_lan_service, nsd_service_info.GetServiceInfoName().c_str());
|
||||
&wifi_lan_service, nsd_service_info.GetServiceName().c_str());
|
||||
RunOnPcpHandlerThread(
|
||||
"p2p-wifi-service-lost",
|
||||
[this, client, service_id, nsd_service_info]()
|
||||
@@ -626,7 +626,7 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
|
||||
// Make sure we are still discovering before proceeding.
|
||||
if (!client->IsDiscovering()) {
|
||||
NEARBY_LOGS(WARNING) << "Ignoring lost NsdServiceInfo "
|
||||
<< nsd_service_info.GetServiceInfoName()
|
||||
<< nsd_service_info.GetServiceName()
|
||||
<< " because we are no longer "
|
||||
"discovering.";
|
||||
return;
|
||||
@@ -641,8 +641,7 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler(
|
||||
|
||||
// Report the lost endpoint to the client.
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Lost NsdServiceInfo "
|
||||
<< nsd_service_info.GetServiceInfoName()
|
||||
<< "Lost NsdServiceInfo " << nsd_service_info.GetServiceName()
|
||||
<< " (with endpoint_id=" << service_info.GetEndpointId()
|
||||
<< " and endpoint_info="
|
||||
<< absl::BytesToHexString(service_info.GetEndpointInfo().data())
|
||||
@@ -1234,7 +1233,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
std::string remote_service_info_name =
|
||||
socket.GetRemoteWifiLanService()
|
||||
.GetServiceInfo()
|
||||
.GetServiceInfoName();
|
||||
.GetServiceName();
|
||||
auto channel =
|
||||
absl::make_unique<WifiLanEndpointChannel>(
|
||||
remote_service_info_name, socket);
|
||||
@@ -1288,7 +1287,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "In StartWifiLanAdvertising(%s), client="
|
||||
<< client->GetClientId() << " generated WifiLanServiceInfo "
|
||||
<< nsd_service_info.GetServiceInfoName()
|
||||
<< nsd_service_info.GetServiceName()
|
||||
<< " with service_id=" << service_id;
|
||||
|
||||
if (!wifi_lan_medium_.StartAdvertising(service_id, nsd_service_info)) {
|
||||
@@ -1296,7 +1295,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
<< absl::BytesToHexString(local_endpoint_info.data())
|
||||
<< "), client=" << client->GetClientId()
|
||||
<< " couldn't advertise with WifiLanServiceInfo "
|
||||
<< nsd_service_info.GetServiceInfoName();
|
||||
<< nsd_service_info.GetServiceName();
|
||||
wifi_lan_medium_.StopAcceptingConnections(service_id);
|
||||
return proto::connections::UNKNOWN_MEDIUM;
|
||||
}
|
||||
@@ -1304,7 +1303,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising(
|
||||
<< absl::BytesToHexString(local_endpoint_info.data())
|
||||
<< "), client=" << client->GetClientId()
|
||||
<< " advertised with WifiLanServiceInfo "
|
||||
<< nsd_service_info.GetServiceInfoName();
|
||||
<< nsd_service_info.GetServiceName();
|
||||
return proto::connections::WIFI_LAN;
|
||||
}
|
||||
|
||||
@@ -1339,7 +1338,7 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl(
|
||||
if (!wifi_lan_socket.IsValid()) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "In WifiLanConnectImpl(), failed to connect to service "
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceInfoName()
|
||||
<< wifi_lan_service.GetServiceInfo().GetServiceName()
|
||||
<< " for endpoint(id=" << endpoint->endpoint_id << ").";
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.status = {Status::kWifiLanError},
|
||||
|
||||
@@ -70,9 +70,9 @@ ByteArray WifiLanBwuHandler::InitializeUpgradedMediumForEndpoint(
|
||||
// cache service ID to revert
|
||||
active_service_ids_.emplace(upgrade_service_id);
|
||||
|
||||
auto service_address = wifi_lan_medium_.GetServiceAddress(upgrade_service_id);
|
||||
auto ip_address = service_address.first;
|
||||
auto port = service_address.second;
|
||||
auto credential = wifi_lan_medium_.GetCredentials(upgrade_service_id);
|
||||
auto ip_address = credential.first;
|
||||
auto port = credential.second;
|
||||
if (ip_address.empty()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "WifiLanBwuHandler couldn't initiate the wifi_lan upgrade for "
|
||||
|
||||
@@ -77,7 +77,7 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
|
||||
}
|
||||
}
|
||||
|
||||
auto service_info_name = nsd_service_info.GetServiceInfoName();
|
||||
auto service_info_name = nsd_service_info.GetServiceName();
|
||||
ByteArray service_info_bytes = Base64Utils::Decode(service_info_name);
|
||||
if (service_info_bytes.Empty()) {
|
||||
NEARBY_LOG(
|
||||
@@ -193,7 +193,7 @@ WifiLanServiceInfo::operator NsdServiceInfo() const {
|
||||
}
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(
|
||||
nsd_service_info.SetServiceName(
|
||||
Base64Utils::Encode(ByteArray{std::move(out)}));
|
||||
nsd_service_info.SetTxtRecord(std::string(kKeyEndpointInfo),
|
||||
Base64Utils::Encode(endpoint_info_));
|
||||
|
||||
@@ -146,7 +146,7 @@ TEST(WifiLanServiceInfoTest, ConstructionFailsWithShortServiceNameLength) {
|
||||
ByteArray wifi_lan_service_info_bytes{wifi_lan_service_info_name};
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(
|
||||
nsd_service_info.SetServiceName(
|
||||
Base64Utils::Encode(wifi_lan_service_info_bytes));
|
||||
|
||||
WifiLanServiceInfo wifi_lan_service_info{nsd_service_info};
|
||||
|
||||
@@ -120,7 +120,11 @@ class WifiLanMedium {
|
||||
virtual WifiLanService* GetRemoteService(const std::string& ip_address,
|
||||
int port) = 0;
|
||||
|
||||
virtual std::pair<std::string, int> GetServiceAddress(
|
||||
// Gets ip address + port for remote services on the network to identify and
|
||||
// connect to this service.
|
||||
//
|
||||
// Credential is for the currently-hosted Wifi ServerSocket (if any).
|
||||
virtual std::pair<std::string, int> GetCredentials(
|
||||
const std::string& service_id) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -291,27 +291,27 @@ void MediumEnvironment::RegisterBluetoothMedium(
|
||||
void MediumEnvironment::UpdateBluetoothMedium(
|
||||
api::BluetoothClassicMedium& medium, BluetoothDiscoveryCallback callback) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium,
|
||||
callback = std::move(callback)]() {
|
||||
auto item = bluetooth_mediums_.find(&medium);
|
||||
if (item == bluetooth_mediums_.end()) return;
|
||||
auto& context = item->second;
|
||||
context.callback = std::move(callback);
|
||||
auto* owned_adapter = context.adapter;
|
||||
NEARBY_LOGS(INFO) << "Updated: this=" << this << "; medium=" << &medium
|
||||
<< "; adapter=" << owned_adapter
|
||||
<< "; name=" << owned_adapter->GetName()
|
||||
<< "; enabled=" << owned_adapter->IsEnabled()
|
||||
<< "; mode=" << int32_t(owned_adapter->GetScanMode());
|
||||
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(),
|
||||
adapter->IsEnabled());
|
||||
}
|
||||
});
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, callback = std::move(callback)]() {
|
||||
auto item = bluetooth_mediums_.find(&medium);
|
||||
if (item == bluetooth_mediums_.end()) return;
|
||||
auto& context = item->second;
|
||||
context.callback = std::move(callback);
|
||||
auto* owned_adapter = context.adapter;
|
||||
NEARBY_LOGS(INFO) << "Updated: this=" << this << "; medium=" << &medium
|
||||
<< "; adapter=" << owned_adapter
|
||||
<< "; name=" << owned_adapter->GetName()
|
||||
<< "; enabled=" << owned_adapter->IsEnabled()
|
||||
<< "; mode=" << int32_t(owned_adapter->GetScanMode());
|
||||
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(),
|
||||
adapter->IsEnabled());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UnregisterBluetoothMedium(
|
||||
@@ -369,35 +369,37 @@ void MediumEnvironment::UpdateBleMediumForScanning(
|
||||
const std::string& fast_advertisement_service_uuid,
|
||||
BleDiscoveredPeripheralCallback callback, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_id,
|
||||
fast_advertisement_service_uuid,
|
||||
callback = std::move(callback), enabled]() {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateBleMediumFoScanning failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.discovery_callback = std::move(callback);
|
||||
NEARBY_LOGS(INFO) << "Update Ble medium for scanning: this=" << this
|
||||
<< "; medium=" << &medium << "; service_id=" << service_id
|
||||
<< "; fast_advertisement_service_uuid="
|
||||
<< fast_advertisement_service_uuid
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : ble_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) {
|
||||
OnBlePeripheralStateChanged(context, *(info.ble_peripheral), service_id,
|
||||
info.fast_advertisement, enabled);
|
||||
}
|
||||
}
|
||||
});
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, service_id, fast_advertisement_service_uuid,
|
||||
callback = std::move(callback), enabled]() {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateBleMediumFoScanning failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.discovery_callback = std::move(callback);
|
||||
NEARBY_LOGS(INFO) << "Update Ble medium for scanning: this=" << this
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_id=" << service_id
|
||||
<< "; fast_advertisement_service_uuid="
|
||||
<< fast_advertisement_service_uuid
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : ble_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) {
|
||||
OnBlePeripheralStateChanged(context, *(info.ble_peripheral),
|
||||
service_id, info.fast_advertisement,
|
||||
enabled);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateBleMediumForAcceptedConnection(
|
||||
@@ -433,17 +435,18 @@ void MediumEnvironment::CallBleAcceptedConnectionCallback(
|
||||
api::BleMedium& medium, api::BleSocket& socket,
|
||||
const std::string& service_id) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &socket, service_id]() {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Call AcceptedConnectionCallback failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& info = item->second;
|
||||
info.accepted_connection_callback.accepted_cb(socket, service_id);
|
||||
});
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, &socket, service_id]() {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Call AcceptedConnectionCallback failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& info = item->second;
|
||||
info.accepted_connection_callback.accepted_cb(socket, service_id);
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::RegisterWebRtcSignalingMessenger(
|
||||
@@ -536,113 +539,116 @@ 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().GetServiceInfoName()
|
||||
<< "; 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);
|
||||
}
|
||||
});
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
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) {
|
||||
@@ -658,40 +664,43 @@ 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);
|
||||
}
|
||||
});
|
||||
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;
|
||||
auto addr = remote_wifi_lan_service->GetServiceInfo().GetServiceAddress();
|
||||
if (addr.first == ip_address && addr.second == port) {
|
||||
remote_wifi_lan_service = wifi_lan_service;
|
||||
break;
|
||||
}
|
||||
}
|
||||
latch.CountDown();
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ namespace nearby {
|
||||
// https://developer.android.com/reference/android/net/nsd/NsdServiceInfo.html.
|
||||
class NsdServiceInfo {
|
||||
public:
|
||||
static constexpr int kTypeFromServiceIdHashLength = 6;
|
||||
static constexpr absl::string_view kNsdTypeFormat{"_%s._tcp."};
|
||||
|
||||
NsdServiceInfo() = default;
|
||||
NsdServiceInfo(const NsdServiceInfo&) = default;
|
||||
NsdServiceInfo& operator=(const NsdServiceInfo&) = default;
|
||||
@@ -32,12 +35,12 @@ class NsdServiceInfo {
|
||||
NsdServiceInfo& operator=(NsdServiceInfo&&) = default;
|
||||
~NsdServiceInfo() = default;
|
||||
|
||||
// Returns the packed string of |WifiLanServiceInfo|.
|
||||
std::string GetServiceInfoName() const { return service_info_name_; }
|
||||
// Gets the service name.
|
||||
std::string GetServiceName() const { return service_name_; }
|
||||
|
||||
// Sets the packed string of |WifiLanServiceInfo|.
|
||||
void SetServiceInfoName(std::string service_info_name) {
|
||||
service_info_name_ = std::move(service_info_name);
|
||||
// Sets the service name.
|
||||
void SetServiceName(std::string service_name) {
|
||||
service_name_ = std::move(service_name);
|
||||
}
|
||||
|
||||
// Gets the TXTRecord value of the specified TXTRecord key assigned.
|
||||
@@ -54,25 +57,45 @@ class NsdServiceInfo {
|
||||
txt_records_.emplace(txt_record_key, txt_record_value);
|
||||
}
|
||||
|
||||
// Returns the advertising device's <IP address, port> as a pair.
|
||||
// IP address is in byte sequence, in network order.
|
||||
std::pair<std::string, int> GetServiceAddress() const {
|
||||
return std::make_pair(ip_address_, port_);
|
||||
// Gets all TXTRecord.
|
||||
absl::flat_hash_map<std::string, std::string> GetTxtRecords() const {
|
||||
return txt_records_;
|
||||
}
|
||||
|
||||
// Sets the ip address and port of the local device.
|
||||
void SetServiceAddress(const std::string& ip_address, int port) {
|
||||
ip_address_ = ip_address;
|
||||
port_ = port;
|
||||
// Sets all TXTRecord.
|
||||
void SetTxtRecords(
|
||||
absl::flat_hash_map<std::string, std::string>& txt_records) {
|
||||
txt_records_ = txt_records;
|
||||
}
|
||||
|
||||
bool IsValid() const { return !service_info_name_.empty(); }
|
||||
// Gets IP Address, which is in byte sequence, in network order.
|
||||
std::string GetIPAddress() const { return ip_address_; }
|
||||
|
||||
// Sets IP Address.
|
||||
void SetIPAddress(const std::string& ip_address) { ip_address_ = ip_address; }
|
||||
|
||||
// Gets the port number
|
||||
int GetPort() const { return port_; }
|
||||
|
||||
// Sets the port number.
|
||||
void SetPort(int port) { port_ = port; }
|
||||
|
||||
// Gets the service type.
|
||||
std::string GetServiceType() const { return service_type_; }
|
||||
|
||||
// Sets the service type.
|
||||
void SetServiceType(const std::string& service_type) {
|
||||
service_type_ = service_type;
|
||||
}
|
||||
|
||||
bool IsValid() const { return !service_name_.empty(); }
|
||||
|
||||
private:
|
||||
std::string service_info_name_;
|
||||
std::string service_name_;
|
||||
absl::flat_hash_map<std::string, std::string> txt_records_;
|
||||
std::string ip_address_;
|
||||
int port_;
|
||||
std::string service_type_;
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
@@ -203,7 +204,7 @@ bool WifiLanMedium::StartAdvertising(const std::string& service_id,
|
||||
"G3 WifiLan StartAdvertising: service_id=%s, nsd_service_info=%p, "
|
||||
"service_info_name=%s",
|
||||
service_id.c_str(), &nsd_service_info,
|
||||
nsd_service_info.GetServiceInfoName().c_str());
|
||||
nsd_service_info.GetServiceName().c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
NsdServiceInfo local_nsd_service_info{nsd_service_info};
|
||||
SetWifiLanService(nsd_service_info);
|
||||
@@ -320,13 +321,12 @@ bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) {
|
||||
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().GetServiceInfoName().c_str(),
|
||||
service_id.c_str());
|
||||
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();
|
||||
@@ -334,13 +334,12 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
|
||||
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().GetServiceInfoName().c_str(),
|
||||
service_id.c_str());
|
||||
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());
|
||||
// Then, find our server socket context in this medium.
|
||||
{
|
||||
absl::MutexLock medium_lock(&remote_medium->mutex_);
|
||||
@@ -389,22 +388,22 @@ api::WifiLanService* WifiLanMedium::GetRemoteService(
|
||||
return env.GetWifiLanService(ip_address, port);
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetServiceAddress(
|
||||
std::pair<std::string, int> WifiLanMedium::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan GetServiceAddress: service_id="
|
||||
<< service_id;
|
||||
return wifi_lan_service_.GetServiceInfo().GetServiceAddress();
|
||||
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 service_address = GetFakeServiceAddress();
|
||||
local_nsd_service_info.SetServiceAddress(service_address.first,
|
||||
service_address.second);
|
||||
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::GetFakeServiceAddress() const {
|
||||
std::pair<std::string, int> WifiLanMedium::GetFakeCredentials() const {
|
||||
std::string ip_address;
|
||||
ip_address.resize(4);
|
||||
uint32_t raw_ip_addr = Prng().NextUint32();
|
||||
|
||||
@@ -210,7 +210,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
api::WifiLanService* GetRemoteService(const std::string& ip_address,
|
||||
int port) override;
|
||||
|
||||
std::pair<std::string, int> GetServiceAddress(
|
||||
std::pair<std::string, int> GetCredentials(
|
||||
const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
@@ -231,7 +231,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
};
|
||||
|
||||
void SetWifiLanService(const NsdServiceInfo& nsd_service_info);
|
||||
std::pair<std::string, int> GetFakeServiceAddress() const;
|
||||
std::pair<std::string, int> GetFakeCredentials() const;
|
||||
|
||||
absl::Mutex mutex_;
|
||||
WifiLanService wifi_lan_service_;
|
||||
|
||||
@@ -220,7 +220,7 @@ class WifiLanNsd {
|
||||
|
||||
// A pair of IP Address and Port. A remote device can use this information
|
||||
// to connect to us. This is non-null while IsAccepting is true.
|
||||
std::pair<std::string, int> GetServiceAddress();
|
||||
std::pair<std::string, int> GetCredentials();
|
||||
|
||||
// DnsServiceDeRegister is a async process, after operation finish, callback
|
||||
// will call this method to notify the waiting method StopAdvertising to
|
||||
@@ -263,9 +263,9 @@ class WifiLanNsd {
|
||||
// Private methods
|
||||
//
|
||||
|
||||
// Generates prefered listening port. If cannot bind to this port,
|
||||
// Generates preferred listening port. If cannot bind to this port,
|
||||
// NSD will assign a random port for the service.
|
||||
// TODO: Windows firewall may break the solution, need to futher solution to
|
||||
// TODO: Windows firewall may break the solution, need to further solution to
|
||||
// resolve the potential issue
|
||||
uint16 GenerateSocketPort(const std::string& service_id);
|
||||
|
||||
@@ -384,7 +384,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
int port) override;
|
||||
|
||||
// returns advertising service address
|
||||
std::pair<std::string, int> GetServiceAddress(
|
||||
std::pair<std::string, int> GetCredentials(
|
||||
const std::string& service_id) override;
|
||||
|
||||
// for internal to clean closed connection.
|
||||
|
||||
@@ -160,14 +160,15 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
|
||||
api::WifiLanService& wifi_lan_service, const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
try {
|
||||
auto address = wifi_lan_service.GetServiceInfo().GetServiceAddress();
|
||||
if (address.first.empty() || address.second == 0) {
|
||||
std::string ip_address = wifi_lan_service.GetServiceInfo().GetIPAddress();
|
||||
int port = wifi_lan_service.GetServiceInfo().GetPort();
|
||||
if (ip_address.empty() || port == 0) {
|
||||
NEARBY_LOGS(ERROR) << "no valid service address and port to connect.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HostName host_name{string_to_wstring(address.first)};
|
||||
winrt::hstring service_name{winrt::to_hstring(address.second)};
|
||||
HostName host_name{string_to_wstring(ip_address)};
|
||||
winrt::hstring service_name{winrt::to_hstring(port)};
|
||||
|
||||
StreamSocket socket{};
|
||||
|
||||
@@ -232,7 +233,7 @@ api::WifiLanService* WifiLanMedium::GetRemoteService(
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetServiceAddress(
|
||||
std::pair<std::string, int> WifiLanMedium::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
try {
|
||||
WifiLanNsd* nsd = GetNsd(service_id, true);
|
||||
@@ -242,7 +243,7 @@ std::pair<std::string, int> WifiLanMedium::GetServiceAddress(
|
||||
return std::pair<std::string, int>{"", 0};
|
||||
}
|
||||
|
||||
return nsd->GetServiceAddress();
|
||||
return nsd->GetCredentials();
|
||||
} catch (...) {
|
||||
NEARBY_LOGS(ERROR) << "failed to get service address due to "
|
||||
<< GetErrorMessage(std::current_exception());
|
||||
|
||||
@@ -43,7 +43,7 @@ bool WifiLanNsd::StartAcceptingConnections(
|
||||
|
||||
if (ip_addresses_.empty()) {
|
||||
NEARBY_LOGS(WARNING) << "failed to start accepting connection without IP "
|
||||
"addreses configured on computer.";
|
||||
"addresses configured on computer.";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ bool WifiLanNsd::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (nsd_service_info.GetServiceInfoName().empty()) {
|
||||
if (nsd_service_info.GetServiceName().empty()) {
|
||||
NEARBY_LOGS(ERROR) << "cannot start advertising without service name.";
|
||||
return false;
|
||||
}
|
||||
@@ -131,13 +131,14 @@ bool WifiLanNsd::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
|
||||
NsdServiceInfo new_nsd_servcie_info = nsd_service_info;
|
||||
// TODO: need feature enhancement to support multiple network interfaces
|
||||
new_nsd_servcie_info.SetServiceAddress(ip_addresses_[0], port);
|
||||
new_nsd_servcie_info.SetIPAddress(ip_addresses_[0]);
|
||||
new_nsd_servcie_info.SetPort(port);
|
||||
wifi_lan_service_ = WifiLanService(new_nsd_servcie_info);
|
||||
wifi_lan_service_.SetMedium(medium_);
|
||||
|
||||
std::string instance_name = absl::StrFormat(
|
||||
MDNS_INSTANCE_NAME_FORMAT.data(),
|
||||
wifi_lan_service_.GetServiceInfo().GetServiceInfoName(), service_type_);
|
||||
wifi_lan_service_.GetServiceInfo().GetServiceName(), service_type_);
|
||||
|
||||
NEARBY_LOGS(INFO) << "mDNS instance name is " << instance_name;
|
||||
|
||||
@@ -212,8 +213,8 @@ bool WifiLanNsd::StopAdvertising() {
|
||||
// Init DNS service instance
|
||||
std::string instance_name = absl::StrFormat(
|
||||
MDNS_INSTANCE_NAME_FORMAT.data(),
|
||||
wifi_lan_service_.GetServiceInfo().GetServiceInfoName(), service_type_);
|
||||
int port = wifi_lan_service_.GetServiceInfo().GetServiceAddress().second;
|
||||
wifi_lan_service_.GetServiceInfo().GetServiceName(), service_type_);
|
||||
int port = wifi_lan_service_.GetServiceInfo().GetPort();
|
||||
dns_service_instance_name_ =
|
||||
std::make_unique<std::wstring>(string_to_wstring(instance_name));
|
||||
|
||||
@@ -311,14 +312,15 @@ bool WifiLanNsd::StopDiscovery() {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanNsd::GetServiceAddress() {
|
||||
std::pair<std::string, int> WifiLanNsd::GetCredentials() {
|
||||
if (!IsAdvertising()) {
|
||||
// no advertising is running
|
||||
NEARBY_LOGS(WARNING) << "no advertising for service id " << service_id_;
|
||||
return std::pair<std::string, int>{"", 0};
|
||||
}
|
||||
|
||||
return wifi_lan_service_.GetServiceInfo().GetServiceAddress();
|
||||
return std::make_pair(wifi_lan_service_.GetServiceInfo().GetIPAddress(),
|
||||
wifi_lan_service_.GetServiceInfo().GetPort());
|
||||
}
|
||||
|
||||
std::vector<std::string> WifiLanNsd::GetIpAddresses() {
|
||||
@@ -361,7 +363,7 @@ NsdServiceInfo WifiLanNsd::GetNsdServiceInformation(
|
||||
<< "no service name information in device information.";
|
||||
return nsd_service_info;
|
||||
}
|
||||
nsd_service_info.SetServiceInfoName(
|
||||
nsd_service_info.SetServiceName(
|
||||
InspectableReader::ReadString(inspectable));
|
||||
|
||||
// IP Address information
|
||||
@@ -387,7 +389,8 @@ NsdServiceInfo WifiLanNsd::GetNsdServiceInformation(
|
||||
}
|
||||
|
||||
int port = InspectableReader::ReadUint16(inspectable);
|
||||
nsd_service_info.SetServiceAddress(ip_address, port);
|
||||
nsd_service_info.SetIPAddress(ip_address);
|
||||
nsd_service_info.SetPort(port);
|
||||
|
||||
// read text record
|
||||
inspectable = properties.TryLookup(L"System.Devices.Dnssd.TextAttributes");
|
||||
@@ -418,7 +421,7 @@ fire_and_forget WifiLanNsd::Watcher_DeviceAdded(DeviceWatcher sender,
|
||||
DeviceInformation deviceInfo) {
|
||||
NEARBY_LOGS(INFO) << "device added for service " << service_id_;
|
||||
|
||||
// need to read IP address and port informaiton from deviceInfo
|
||||
// need to read IP address and port information from deviceInfo
|
||||
NsdServiceInfo nsd_service_info =
|
||||
GetNsdServiceInformation(deviceInfo.Properties());
|
||||
|
||||
@@ -447,7 +450,7 @@ fire_and_forget WifiLanNsd::Watcher_DeviceUpdated(
|
||||
fire_and_forget WifiLanNsd::Watcher_DeviceRemoved(
|
||||
DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
|
||||
NEARBY_LOGS(INFO) << "device removed for service " << service_id_;
|
||||
// need to read IP address and port informaiton from deviceInfo
|
||||
// need to read IP address and port information from deviceInfo
|
||||
NsdServiceInfo nsd_service_info =
|
||||
GetNsdServiceInformation(deviceInfoUpdate.Properties());
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_id,
|
||||
"service_info_name=%s",
|
||||
&context.wifi_lan_service, &wifi_lan_service,
|
||||
wifi_lan_service.GetServiceInfo()
|
||||
.GetServiceInfoName()
|
||||
.GetServiceName()
|
||||
.c_str());
|
||||
return;
|
||||
} else {
|
||||
@@ -63,7 +63,7 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_id,
|
||||
"Discovering wifi_lan_service=%p, service_info_name=%s",
|
||||
&wifi_lan_service,
|
||||
wifi_lan_service.GetServiceInfo()
|
||||
.GetServiceInfoName()
|
||||
.GetServiceName()
|
||||
.c_str());
|
||||
}
|
||||
discovered_service_callback_.service_discovered_cb(
|
||||
@@ -143,7 +143,7 @@ WifiLanSocket WifiLanMedium::Connect(WifiLanService& wifi_lan_service,
|
||||
INFO,
|
||||
"WifiLanMedium::Connect: service=%p [impl=%p, service_info_name=%s]",
|
||||
&wifi_lan_service, &wifi_lan_service.GetImpl(),
|
||||
wifi_lan_service.GetServiceInfo().GetServiceInfoName().c_str());
|
||||
wifi_lan_service.GetServiceInfo().GetServiceName().c_str());
|
||||
return WifiLanSocket(impl_->Connect(wifi_lan_service.GetImpl(), service_id,
|
||||
cancellation_flag));
|
||||
}
|
||||
@@ -153,9 +153,9 @@ WifiLanService WifiLanMedium::GetRemoteService(const std::string& ip_address,
|
||||
return WifiLanService(impl_->GetRemoteService(ip_address, port));
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetServiceAddress(
|
||||
std::pair<std::string, int> WifiLanMedium::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
return impl_->GetServiceAddress(service_id);
|
||||
return impl_->GetCredentials(service_id);
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -161,7 +161,7 @@ class WifiLanMedium final {
|
||||
|
||||
WifiLanService GetRemoteService(const std::string& ip_address, int port);
|
||||
|
||||
std::pair<std::string, int> GetServiceAddress(const std::string& service_id);
|
||||
std::pair<std::string, int> GetCredentials(const std::string& service_id);
|
||||
|
||||
private:
|
||||
Mutex mutex_;
|
||||
|
||||
@@ -73,17 +73,16 @@ TEST_P(WifiLanMediumTest, CanStartAcceptingConnectionsAndConnect) {
|
||||
.service_discovered_cb =
|
||||
[&found_latch, &discovered_service](
|
||||
WifiLanService& service, const std::string& service_id) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Service discovered: %s, %p",
|
||||
service.GetServiceInfo().GetServiceInfoName().c_str(),
|
||||
&service);
|
||||
NEARBY_LOG(INFO, "Service discovered: %s, %p",
|
||||
service.GetServiceInfo().GetServiceName().c_str(),
|
||||
&service);
|
||||
discovered_service = &service;
|
||||
found_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
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);
|
||||
@@ -135,17 +134,16 @@ TEST_P(WifiLanMediumTest, CanCancelConnect) {
|
||||
.service_discovered_cb =
|
||||
[&found_latch, &discovered_service](
|
||||
WifiLanService& service, const std::string& service_id) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Service discovered: %s, %p",
|
||||
service.GetServiceInfo().GetServiceInfoName().c_str(),
|
||||
&service);
|
||||
NEARBY_LOG(INFO, "Service discovered: %s, %p",
|
||||
service.GetServiceInfo().GetServiceName().c_str(),
|
||||
&service);
|
||||
discovered_service = &service;
|
||||
found_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
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);
|
||||
@@ -212,7 +210,7 @@ TEST_F(WifiLanMediumTest, CanStartAdvertising) {
|
||||
CountDownLatch found_latch(1);
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
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);
|
||||
@@ -256,7 +254,7 @@ TEST_F(WifiLanMediumTest, CanStartDiscovery) {
|
||||
});
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
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));
|
||||
@@ -292,7 +290,7 @@ TEST_F(WifiLanMediumTest, CanStopDiscovery) {
|
||||
});
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceInfoName(service_info_name);
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user