WifiLan::Change GetUpgradeAddressCandidates to use ServiceAddress.

PiperOrigin-RevId: 842257461
This commit is contained in:
Francis Tsui
2025-12-09 08:48:31 -08:00
committed by Copybara-Service
parent 8e597c9df0
commit 4652aef48c
22 changed files with 121 additions and 82 deletions
@@ -36,6 +36,7 @@
#include "internal/platform/nsd_service_info.h"
#include "internal/platform/socket.h"
#include "internal/platform/types.h"
#include "internal/platform/wifi_credential.h"
#include "internal/platform/wifi_lan.h"
namespace nearby {
@@ -607,15 +608,14 @@ ExceptionOr<WifiLanSocket> WifiLan::CreateOutgoingMultiplexSocketLocked(
return ExceptionOr<WifiLanSocket>(Exception::kFailed);
}
std::pair<std::vector<std::string>, int> WifiLan::GetUpgradeAddressCandidates(
std::vector<ServiceAddress> WifiLan::GetUpgradeAddressCandidates(
const std::string& service_id) {
MutexLock lock(&mutex_);
const auto& it = server_sockets_.find(service_id);
if (it == server_sockets_.end()) {
return std::pair<std::vector<std::string>, int>();
return {};
}
return {medium_.GetUpgradeAddressCandidates(it->second),
it->second.GetPort()};
return medium_.GetUpgradeAddressCandidates(it->second);
}
std::string WifiLan::GenerateServiceType(const std::string& service_id) {
@@ -33,6 +33,7 @@
#include "internal/platform/multi_thread_executor.h"
#include "internal/platform/mutex.h"
#include "internal/platform/nsd_service_info.h"
#include "internal/platform/wifi_credential.h"
#include "internal/platform/wifi_lan.h"
namespace nearby {
@@ -116,7 +117,7 @@ class WifiLan {
// The candidates list is ordered to have IPv6 addresses first, then IPv4.
// Both IPv4 and IPv6 adddresses are represented as network order byte
// sequence.
std::pair<std::vector<std::string>, int> GetUpgradeAddressCandidates(
std::vector<ServiceAddress> GetUpgradeAddressCandidates(
const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
private:
@@ -16,6 +16,7 @@
#include <string>
#include <utility>
#include <vector>
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
@@ -31,6 +32,7 @@
#include "internal/platform/medium_environment.h"
#include "internal/platform/nsd_service_info.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/platform/wifi_credential.h"
#include "internal/platform/wifi_lan.h"
namespace nearby {
@@ -73,15 +75,15 @@ TEST_P(WifiLanTest, AdvertiseSameServiceNameReusesPort) {
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceName(std::string(kServiceInfoName));
wifi_lan_server.StartAdvertising(service_id, nsd_service_info, {});
auto [addresses, port] =
std::vector<ServiceAddress> addresses =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
wifi_lan_server.StopAdvertising(service_id);
wifi_lan_server.StopAcceptingConnections(service_id);
wifi_lan_server.StartAdvertising(service_id, nsd_service_info, {});
auto [addresses2, port2] =
std::vector<ServiceAddress> addresses2 =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
EXPECT_EQ(port, port2);
EXPECT_EQ(addresses.back().port, addresses2.back().port);
env_.Stop();
}
@@ -97,16 +99,16 @@ TEST_P(WifiLanTest, AdvertiseDifferentServiceNameUsesDifferentPort) {
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceName(std::string(kServiceInfoName));
wifi_lan_server.StartAdvertising(service_id, nsd_service_info, {});
auto [addresses, port] =
std::vector<ServiceAddress> addresses =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
wifi_lan_server.StopAdvertising(service_id);
wifi_lan_server.StopAcceptingConnections(service_id);
nsd_service_info.SetServiceName("ServiceInfoName2");
wifi_lan_server.StartAdvertising(service_id, nsd_service_info, {});
auto [addresses2, port2] =
std::vector<ServiceAddress> addresses2 =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
EXPECT_NE(port, port2);
EXPECT_NE(addresses.back().port, addresses2.back().port);
env_.Stop();
}
@@ -317,15 +319,16 @@ TEST_P(WifiLanTest, CanConnectWithIpAddressAndPort) {
accept_latch.CountDown();
}));
auto server_candidates =
std::vector<ServiceAddress> server_candidates =
wifi_lan_server.GetUpgradeAddressCandidates(service_id);
ASSERT_FALSE(server_candidates.first.empty());
ASSERT_NE(server_candidates.second, 0);
ASSERT_FALSE(server_candidates.empty());
ASSERT_NE(server_candidates.back().port, 0);
CancellationFlag flag;
ErrorOr<WifiLanSocket> socket_for_client_result =
wifi_lan_client.Connect(service_id, server_candidates.first.front(),
server_candidates.second, &flag);
std::string ip_address{server_candidates.front().address.begin(),
server_candidates.front().address.end()};
ErrorOr<WifiLanSocket> socket_for_client_result = wifi_lan_client.Connect(
service_id, ip_address, server_candidates.front().port, &flag);
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));