Move hotspot credential construction into each platform.

PiperOrigin-RevId: 826272274
This commit is contained in:
Francis Tsui
2025-10-30 18:58:19 -07:00
committed by Copybara-Service
parent 03474168b5
commit ca1ab209b7
8 changed files with 66 additions and 56 deletions
@@ -110,6 +110,13 @@ Exception WifiHotspotServerSocket::DoClose() {
return {Exception::kSuccess};
}
void WifiHotspotServerSocket::PopulateHotspotCredentials(
HotspotCredentials& hotspot_credentials) {
absl::MutexLock lock(mutex_);
hotspot_credentials.SetGateway(ip_address_);
hotspot_credentials.SetPort(port_);
}
// Code for WifiHotspotMedium
WifiHotspotMedium::WifiHotspotMedium() {
auto& env = MediumEnvironment::Instance();
@@ -263,9 +270,10 @@ WifiHotspotMedium::ListenForService(int port) {
dot_decimal_ip.pop_back();
server_socket->SetIPAddress(dot_decimal_ip);
server_socket->SetPort(port == 0 ? env.GetFakePort() : port);
std::string socket_name = WifiHotspotServerSocket::GetName(
server_socket->GetIPAddress(), server_socket->GetPort());
int port_to_use = port == 0 ? env.GetFakePort() : port;
server_socket->SetPort(port_to_use);
std::string socket_name =
WifiHotspotServerSocket::GetName(dot_decimal_ip, port_to_use);
server_socket->SetCloseNotifier([this, socket_name]() {
absl::MutexLock lock(mutex_);
server_sockets_.erase(socket_name);
@@ -20,13 +20,13 @@
#include <string>
#include <utility>
#include "absl/base/thread_annotations.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/g3/multi_thread_executor.h"
#include "internal/platform/implementation/g3/socket_base.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
#include "internal/platform/wifi_credential.h"
namespace nearby {
namespace g3 {
@@ -65,11 +65,6 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
static std::string GetName(absl::string_view ip_address, int port);
std::string GetIPAddress() const override ABSL_LOCKS_EXCLUDED(mutex_) {
absl::MutexLock lock(&mutex_);
return ip_address_;
}
void SetIPAddress(const std::string& ip_address) ABSL_LOCKS_EXCLUDED(mutex_) {
absl::MutexLock lock(&mutex_);
ip_address_ = ip_address;
@@ -113,6 +108,9 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
// Calls close_notifier if it was previously set, and marks socket as closed.
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
void PopulateHotspotCredentials(HotspotCredentials& hotspot_credentials)
override ABSL_LOCKS_EXCLUDED(mutex_);
private:
// Retrieves IP addresses from local machine
std::vector<std::string> GetIpAddresses() const;