Replace std::function with AnyInvocable

PiperOrigin-RevId: 556090653
This commit is contained in:
Janusz Sobczak
2023-08-11 14:27:08 -07:00
committed by Copybara-Service
parent 85bf78ef70
commit 8ccb9fcfec
20 changed files with 134 additions and 116 deletions
+12 -2
View File
@@ -23,8 +23,9 @@
namespace nearby {
namespace connections {
BaseBwuHandler::BaseBwuHandler(BwuNotifications bwu_notifications)
: bwu_notifications_(std::move(bwu_notifications)) {}
BaseBwuHandler::BaseBwuHandler(
IncomingConnectionCallback incoming_connection_callback)
: incoming_connection_callback_(std::move(incoming_connection_callback)) {}
ByteArray BaseBwuHandler::InitializeUpgradedMediumForEndpoint(
ClientProxy* client, const std::string& service_id,
@@ -78,5 +79,14 @@ void BaseBwuHandler::RevertResponderState(const std::string& service_id) {
HandleRevertInitiatorStateForService(service_id);
}
void BaseBwuHandler::NotifyOnIncomingConnection(
ClientProxy* client, std::unique_ptr<IncomingSocketConnection> connection) {
if (!incoming_connection_callback_) {
NEARBY_LOGS(WARNING)
<< "Ignoring incoming connection, no callback registered";
return;
}
incoming_connection_callback_(client, std::move(connection));
}
} // namespace connections
} // namespace nearby
@@ -30,7 +30,8 @@ namespace connections {
// of the service IDs and endpoint IDs that initiated a bandwidth upgrade.
class BaseBwuHandler : public BwuHandler {
public:
explicit BaseBwuHandler(BwuNotifications bwu_notifications);
explicit BaseBwuHandler(
IncomingConnectionCallback incoming_connection_callback);
// BwuHandler implementation:
ByteArray InitializeUpgradedMediumForEndpoint(
@@ -55,9 +56,13 @@ class BaseBwuHandler : public BwuHandler {
virtual void HandleRevertInitiatorStateForService(
const std::string& upgrade_service_id) = 0;
BwuNotifications bwu_notifications_;
// Notifies the caller about incoming connection.
void NotifyOnIncomingConnection(
ClientProxy* client,
std::unique_ptr<IncomingSocketConnection> connection);
private:
IncomingConnectionCallback incoming_connection_callback_;
// Map from the (wrapped) service ID to endpoint IDs that are initiating a
// bandwidth upgrade. Not used for endpoints that respond to bandwidth upgrade
// requests from another device.
@@ -38,7 +38,7 @@ class BwuHandlerImpl : public BaseBwuHandler {
absl::optional<std::string> endpoint_id;
};
BwuHandlerImpl() : BaseBwuHandler(BwuNotifications{}) {}
BwuHandlerImpl() : BaseBwuHandler(nullptr) {}
const std::vector<InputData>& handle_initialize_calls() const {
return handle_initialize_calls_;
@@ -28,9 +28,10 @@
namespace nearby {
namespace connections {
BluetoothBwuHandler::BluetoothBwuHandler(Mediums& mediums,
BwuNotifications notifications)
: BaseBwuHandler(std::move(notifications)), mediums_(mediums) {}
BluetoothBwuHandler::BluetoothBwuHandler(
Mediums& mediums, IncomingConnectionCallback incoming_connection_callback)
: BaseBwuHandler(std::move(incoming_connection_callback)),
mediums_(mediums) {}
// Called by BWU target. Retrieves a new medium info from incoming message,
// and establishes connection over BT using this info.
@@ -153,7 +154,7 @@ void BluetoothBwuHandler::OnIncomingBluetoothConnection(
upgrade_service_id, socket),
.channel = std::move(channel),
}};
bwu_notifications_.incoming_connection_cb(client, std::move(connection));
NotifyOnIncomingConnection(client, std::move(connection));
}
} // namespace connections
@@ -31,8 +31,9 @@ namespace connections {
// per-Medium-specific operations needed to upgrade an EndpointChannel.
class BluetoothBwuHandler : public BaseBwuHandler {
public:
explicit BluetoothBwuHandler(Mediums& mediums,
BwuNotifications notifications);
explicit BluetoothBwuHandler(
Mediums& mediums,
IncomingConnectionCallback incoming_connection_callback);
private:
class BluetoothIncomingSocket : public IncomingSocket {
+4 -6
View File
@@ -18,6 +18,7 @@
#include <functional>
#include <string>
#include "absl/functional/any_invocable.h"
#include "connections/implementation/client_proxy.h"
#include "connections/implementation/endpoint_channel.h"
#include "connections/implementation/offline_frames.h"
@@ -47,12 +48,9 @@ class BwuHandler {
std::unique_ptr<IncomingSocket> socket;
std::unique_ptr<EndpointChannel> channel;
};
struct BwuNotifications {
std::function<void(ClientProxy* client,
std::unique_ptr<IncomingSocketConnection> connection)>
incoming_connection_cb;
};
using IncomingConnectionCallback = absl::AnyInvocable<void(
ClientProxy* client,
std::unique_ptr<IncomingSocketConnection> connection)>;
virtual ~BwuHandler() = default;
+21 -12
View File
@@ -103,31 +103,40 @@ BwuManager::~BwuManager() {
void BwuManager::InitBwuHandlers() {
// Register the supported concrete BwuMedium implementations.
BwuHandler::BwuNotifications notifications{
.incoming_connection_cb =
absl::bind_front(&BwuManager::OnIncomingConnection, this),
};
if (config_.allow_upgrade_to.wifi_hotspot) {
handlers_.emplace(
Medium::WIFI_HOTSPOT,
std::make_unique<WifiHotspotBwuHandler>(*mediums_, notifications));
std::make_unique<WifiHotspotBwuHandler>(
*mediums_,
absl::bind_front(&BwuManager::OnIncomingConnection, this)));
}
if (config_.allow_upgrade_to.wifi_direct) {
handlers_.emplace(
Medium::WIFI_DIRECT,
std::make_unique<WifiDirectBwuHandler>(*mediums_, notifications));
std::make_unique<WifiDirectBwuHandler>(
*mediums_,
absl::bind_front(&BwuManager::OnIncomingConnection, this)));
}
if (config_.allow_upgrade_to.wifi_lan) {
handlers_.emplace(Medium::WIFI_LAN, std::make_unique<WifiLanBwuHandler>(
*mediums_, notifications));
handlers_.emplace(
Medium::WIFI_LAN,
std::make_unique<WifiLanBwuHandler>(
*mediums_,
absl::bind_front(&BwuManager::OnIncomingConnection, this)));
}
if (config_.allow_upgrade_to.web_rtc) {
handlers_.emplace(Medium::WEB_RTC, std::make_unique<WebrtcBwuHandler>(
*mediums_, notifications));
handlers_.emplace(
Medium::WEB_RTC,
std::make_unique<WebrtcBwuHandler>(
*mediums_,
absl::bind_front(&BwuManager::OnIncomingConnection, this)));
}
if (config_.allow_upgrade_to.bluetooth) {
handlers_.emplace(Medium::BLUETOOTH, std::make_unique<BluetoothBwuHandler>(
*mediums_, notifications));
handlers_.emplace(
Medium::BLUETOOTH,
std::make_unique<BluetoothBwuHandler>(
*mediums_,
absl::bind_front(&BwuManager::OnIncomingConnection, this)));
}
}
@@ -47,7 +47,7 @@ class FakeBwuHandler : public BaseBwuHandler {
};
explicit FakeBwuHandler(Medium medium)
: BaseBwuHandler(BwuNotifications{}), medium_(medium) {}
: BaseBwuHandler(nullptr), medium_(medium) {}
~FakeBwuHandler() override = default;
const std::vector<InputData>& create_calls() const { return create_calls_; }
@@ -40,9 +40,10 @@ void WebrtcBwuHandler::WebrtcIncomingSocket::Close() { socket_.Close(); }
std::string WebrtcBwuHandler::WebrtcIncomingSocket::ToString() { return name_; }
WebrtcBwuHandler::WebrtcBwuHandler(Mediums& mediums,
BwuNotifications notifications)
: BaseBwuHandler(std::move(notifications)), mediums_(mediums) {}
WebrtcBwuHandler::WebrtcBwuHandler(
Mediums& mediums, IncomingConnectionCallback incoming_connection_callback)
: BaseBwuHandler(std::move(incoming_connection_callback)),
mediums_(mediums) {}
// Called by BWU target. Retrieves a new medium info from incoming message,
// and establishes connection over WebRTC using this info.
@@ -146,7 +147,7 @@ void WebrtcBwuHandler::OnIncomingWebrtcConnection(
new IncomingSocketConnection{std::move(webrtc_socket),
std::move(channel)});
bwu_notifications_.incoming_connection_cb(client, std::move(connection));
NotifyOnIncomingConnection(client, std::move(connection));
}
} // namespace connections
@@ -32,7 +32,9 @@ namespace connections {
// per-Medium-specific operations needed to upgrade an EndpointChannel.
class WebrtcBwuHandler : public BaseBwuHandler {
public:
explicit WebrtcBwuHandler(Mediums& mediums, BwuNotifications notifications);
explicit WebrtcBwuHandler(
Mediums& mediums,
IncomingConnectionCallback incoming_connection_callback);
private:
class WebrtcIncomingSocket : public BwuHandler::IncomingSocket {
@@ -37,9 +37,9 @@ void WebrtcBwuHandler::WebrtcIncomingSocket::Close() {}
std::string WebrtcBwuHandler::WebrtcIncomingSocket::ToString() { return ""; }
WebrtcBwuHandler::WebrtcBwuHandler(Mediums& mediums,
BwuNotifications notifications)
: BaseBwuHandler(std::move(notifications)),
WebrtcBwuHandler::WebrtcBwuHandler(
Mediums& mediums, IncomingConnectionCallback incoming_connection_callback)
: BaseBwuHandler(std::move(incoming_connection_callback)),
mediums_(mediums) {}
// Called by BWU target. Retrieves a new medium info from incoming message,
@@ -36,7 +36,9 @@ namespace connections {
// per-Medium-specific operations needed to upgrade an EndpointChannel.
class WebrtcBwuHandler : public BaseBwuHandler {
public:
explicit WebrtcBwuHandler(Mediums& mediums, BwuNotifications notifications);
explicit WebrtcBwuHandler(
Mediums& mediums,
IncomingConnectionCallback incoming_connection_callback);
private:
class WebrtcIncomingSocket : public BwuHandler::IncomingSocket {
@@ -28,9 +28,10 @@
namespace nearby {
namespace connections {
WifiDirectBwuHandler::WifiDirectBwuHandler(Mediums& mediums,
BwuNotifications notifications)
: BaseBwuHandler(std::move(notifications)), mediums_(mediums) {}
WifiDirectBwuHandler::WifiDirectBwuHandler(
Mediums& mediums, IncomingConnectionCallback incoming_connection_callback)
: BaseBwuHandler(std::move(incoming_connection_callback)),
mediums_(mediums) {}
ByteArray WifiDirectBwuHandler::HandleInitializeUpgradedMediumForEndpoint(
ClientProxy* client, const std::string& upgrade_service_id,
@@ -149,7 +150,7 @@ void WifiDirectBwuHandler::OnIncomingWifiDirectConnection(
upgrade_service_id, socket),
.channel = std::move(channel),
});
bwu_notifications_.incoming_connection_cb(client, std::move(connection));
NotifyOnIncomingConnection(client, std::move(connection));
}
} // namespace connections
@@ -29,14 +29,15 @@ namespace connections {
// per-Medium-specific operations needed to upgrade an EndpointChannel.
class WifiDirectBwuHandler : public BaseBwuHandler {
public:
explicit WifiDirectBwuHandler(Mediums& mediums,
BwuNotifications notifications);
explicit WifiDirectBwuHandler(
Mediums& mediums,
IncomingConnectionCallback incoming_connection_callback);
private:
class WifiDirectIncomingSocket : public BwuHandler::IncomingSocket {
public:
explicit WifiDirectIncomingSocket(const std::string& name,
WifiDirectSocket socket)
WifiDirectSocket socket)
: name_(name), socket_(socket) {}
std::string ToString() override { return name_; }
@@ -73,8 +74,8 @@ class WifiDirectBwuHandler : public BaseBwuHandler {
// Accept Connection Callback.
void OnIncomingWifiDirectConnection(ClientProxy* client,
const std::string& upgrade_service_id,
WifiDirectSocket socket);
const std::string& upgrade_service_id,
WifiDirectSocket socket);
Mediums& mediums_;
Wifi& wifi_medium_ = mediums_.GetWifi();
@@ -38,11 +38,10 @@ class WifiDirectTest : public testing::Test {
};
TEST_F(WifiDirectTest, CanCreateBwuHandler) {
BwuHandler::BwuNotifications notifications = {.incoming_connection_cb = {}};
ClientProxy client;
Mediums mediums;
auto handler = std::make_unique<WifiDirectBwuHandler>(mediums, notifications);
auto handler = std::make_unique<WifiDirectBwuHandler>(mediums, nullptr);
handler->InitializeUpgradedMediumForEndpoint(&client, /*service_id=*/"B",
/*endpoint_id=*/"2");
@@ -56,29 +55,23 @@ TEST_F(WifiDirectTest, WFDGOBWUInit_GCCreateEndpointChannel) {
CountDownLatch accept_latch(1);
CountDownLatch end_latch(1);
BwuHandler::BwuNotifications notifications_1{
.incoming_connection_cb =
[&accept_latch, &end_latch](
ClientProxy* client,
std::unique_ptr<BwuHandler::IncomingSocketConnection>
mutable_connection) {
NEARBY_LOGS(WARNING) << "Server socket connection accept call back";
std::shared_ptr<BwuHandler::IncomingSocketConnection> connection(
mutable_connection.release());
accept_latch.CountDown();
EXPECT_TRUE(end_latch.Await(kWaitDuration).result());
NEARBY_LOGS(WARNING) << "Test is done. Close the socket";
connection->channel->Close();
connection->socket->Close();
},
};
BwuHandler::BwuNotifications notifications_2 = {.incoming_connection_cb = {}};
ClientProxy wifi_direct_go, wifi_direct_gc;
Mediums mediums_1, mediums_2;
ExceptionOr<OfflineFrame> upgrade_frame;
auto handler_1 =
std::make_unique<WifiDirectBwuHandler>(mediums_1, notifications_1);
auto handler_1 = std::make_unique<WifiDirectBwuHandler>(
mediums_1, [&](ClientProxy* client,
std::unique_ptr<BwuHandler::IncomingSocketConnection>
mutable_connection) {
NEARBY_LOGS(WARNING) << "Server socket connection accept call back";
std::shared_ptr<BwuHandler::IncomingSocketConnection> connection(
mutable_connection.release());
accept_latch.CountDown();
EXPECT_TRUE(end_latch.Await(kWaitDuration).result());
NEARBY_LOGS(WARNING) << "Test is done. Close the socket";
connection->channel->Close();
connection->socket->Close();
});
SingleThreadExecutor server_executor;
server_executor.Execute(
@@ -98,10 +91,9 @@ TEST_F(WifiDirectTest, WFDGOBWUInit_GCCreateEndpointChannel) {
EXPECT_TRUE(start_latch.Await(kWaitDuration).result());
EXPECT_FALSE(mediums_2.GetWifiDirect().IsConnectedToGO());
std::unique_ptr<BwuHandler> handler_2 =
std::make_unique<WifiDirectBwuHandler>(mediums_2, notifications_2);
std::make_unique<WifiDirectBwuHandler>(mediums_2, nullptr);
client_executor.Execute([&handler_2, &wifi_direct_gc, &upgrade_frame,
&accept_latch, &end_latch, &mediums_2]() {
client_executor.Execute([&]() {
auto bwu_frame =
upgrade_frame.result().v1().bandwidth_upgrade_negotiation();
@@ -29,9 +29,10 @@
namespace nearby {
namespace connections {
WifiHotspotBwuHandler::WifiHotspotBwuHandler(Mediums& mediums,
BwuNotifications notifications)
: BaseBwuHandler(std::move(notifications)), mediums_(mediums) {}
WifiHotspotBwuHandler::WifiHotspotBwuHandler(
Mediums& mediums, IncomingConnectionCallback incoming_connection_callback)
: BaseBwuHandler(std::move(incoming_connection_callback)),
mediums_(mediums) {}
// Called by BWU initiator. Set up WifiHotspot upgraded medium for this
// endpoint, and returns a upgrade path info (SSID, Password, Gateway used as
@@ -157,7 +158,7 @@ void WifiHotspotBwuHandler::OnIncomingWifiHotspotConnection(
upgrade_service_id, socket),
.channel = std::move(channel),
});
bwu_notifications_.incoming_connection_cb(client, std::move(connection));
NotifyOnIncomingConnection(client, std::move(connection));
}
} // namespace connections
@@ -29,14 +29,15 @@ namespace connections {
// per-Medium-specific operations needed to upgrade an EndpointChannel.
class WifiHotspotBwuHandler : public BaseBwuHandler {
public:
explicit WifiHotspotBwuHandler(Mediums& mediums,
BwuNotifications notifications);
explicit WifiHotspotBwuHandler(
Mediums& mediums,
IncomingConnectionCallback incoming_connection_callback);
private:
class WifiHotspotIncomingSocket : public BwuHandler::IncomingSocket {
public:
explicit WifiHotspotIncomingSocket(const std::string& name,
WifiHotspotSocket socket)
WifiHotspotSocket socket)
: name_(name), socket_(socket) {}
std::string ToString() override { return name_; }
@@ -64,8 +65,8 @@ class WifiHotspotBwuHandler : public BaseBwuHandler {
const std::string& upgrade_service_id) final;
void OnIncomingWifiHotspotConnection(ClientProxy* client,
const std::string& upgrade_service_id,
WifiHotspotSocket socket);
const std::string& upgrade_service_id,
WifiHotspotSocket socket);
Mediums& mediums_;
WifiHotspot& wifi_hotspot_medium_{mediums_.GetWifiHotspot()};
+20 -30
View File
@@ -37,12 +37,10 @@ class WifiHotspotTest : public testing::Test {
};
TEST_F(WifiHotspotTest, CanCreateBwuHandler) {
BwuHandler::BwuNotifications notifications{.incoming_connection_cb = {}};
ClientProxy client;
Mediums mediums;
auto handler =
std::make_unique<WifiHotspotBwuHandler>(mediums, notifications);
auto handler = std::make_unique<WifiHotspotBwuHandler>(mediums, nullptr);
handler->InitializeUpgradedMediumForEndpoint(&client, /*service_id=*/"B",
/*endpoint_id=*/"2");
@@ -56,48 +54,40 @@ TEST_F(WifiHotspotTest, SoftAPBWUInit_STACreateEndpointChannel) {
CountDownLatch accept_latch(1);
CountDownLatch end_latch(1);
BwuHandler::BwuNotifications notifications_1{
.incoming_connection_cb =
[&accept_latch, &end_latch](
ClientProxy* client,
std::unique_ptr<BwuHandler::IncomingSocketConnection>
mutable_connection) {
NEARBY_LOGS(WARNING) << "Server socket connection accept call back";
accept_latch.CountDown();
EXPECT_TRUE(end_latch.Await(kWaitDuration).result());
},
};
BwuHandler::BwuNotifications notifications_2{.incoming_connection_cb = {}};
ClientProxy client_1, client_2;
Mediums mediums_1, mediums_2;
ExceptionOr<OfflineFrame> upgrade_frame;
auto handler_1 =
std::make_unique<WifiHotspotBwuHandler>(mediums_1, notifications_1);
auto handler_1 = std::make_unique<WifiHotspotBwuHandler>(
mediums_1, [&](ClientProxy* client,
std::unique_ptr<BwuHandler::IncomingSocketConnection>
mutable_connection) {
NEARBY_LOGS(WARNING) << "Server socket connection accept call back";
accept_latch.CountDown();
EXPECT_TRUE(end_latch.Await(kWaitDuration).result());
});
// client_1 works as Hotspot SoftAP
SingleThreadExecutor server_executor;
server_executor.Execute(
[&handler_1, &client_1, &upgrade_frame, &start_latch]() {
ByteArray upgrade_path_available_frame =
handler_1->InitializeUpgradedMediumForEndpoint(&client_1,
/*service_id=*/"A",
/*endpoint_id=*/"1");
EXPECT_FALSE(upgrade_path_available_frame.Empty());
server_executor.Execute([&]() {
ByteArray upgrade_path_available_frame =
handler_1->InitializeUpgradedMediumForEndpoint(&client_1,
/*service_id=*/"A",
/*endpoint_id=*/"1");
EXPECT_FALSE(upgrade_path_available_frame.Empty());
upgrade_frame = parser::FromBytes(upgrade_path_available_frame);
start_latch.CountDown();
});
upgrade_frame = parser::FromBytes(upgrade_path_available_frame);
start_latch.CountDown();
});
// client_2 works as Hotspot STA which will connect to client_1
SingleThreadExecutor client_executor;
// Wait till client_1 started as hotspot and then connect to it
EXPECT_TRUE(start_latch.Await(kWaitDuration).result());
std::unique_ptr<BwuHandler> handler_2 =
std::make_unique<WifiHotspotBwuHandler>(mediums_2, notifications_2);
std::make_unique<WifiHotspotBwuHandler>(mediums_2, nullptr);
client_executor.Execute([&handler_2, &client_2, &upgrade_frame, &accept_latch,
&end_latch, &mediums_2]() {
client_executor.Execute([&]() {
auto bwu_frame =
upgrade_frame.result().v1().bandwidth_upgrade_negotiation();
@@ -28,9 +28,10 @@
namespace nearby {
namespace connections {
WifiLanBwuHandler::WifiLanBwuHandler(Mediums& mediums,
BwuNotifications notifications)
: BaseBwuHandler(std::move(notifications)), mediums_(mediums) {}
WifiLanBwuHandler::WifiLanBwuHandler(
Mediums& mediums, IncomingConnectionCallback incoming_connection_callback)
: BaseBwuHandler(std::move(incoming_connection_callback)),
mediums_(mediums) {}
// Called by BWU target. Retrieves a new medium info from incoming message,
// and establishes connection over WifiLan using this info.
@@ -153,7 +154,7 @@ void WifiLanBwuHandler::OnIncomingWifiLanConnection(
socket),
.channel = std::move(channel),
});
bwu_notifications_.incoming_connection_cb(client, std::move(connection));
NotifyOnIncomingConnection(client, std::move(connection));
}
} // namespace connections
@@ -29,7 +29,9 @@ namespace connections {
// per-Medium-specific operations needed to upgrade an EndpointChannel.
class WifiLanBwuHandler : public BaseBwuHandler {
public:
explicit WifiLanBwuHandler(Mediums& mediums, BwuNotifications notifications);
explicit WifiLanBwuHandler(
Mediums& mediums,
IncomingConnectionCallback incoming_connection_callback);
private:
class WifiLanIncomingSocket : public BwuHandler::IncomingSocket {