mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
make ResultCallback a bare function type and convert to AnyInvocable
PiperOrigin-RevId: 551081080
This commit is contained in:
committed by
Copybara-Service
parent
de3f638129
commit
4958a32c35
@@ -32,8 +32,7 @@ void CloseCore(Core *pCore) {
|
||||
if (pCore == nullptr) {
|
||||
return;
|
||||
}
|
||||
pCore->StopAllEndpoints(
|
||||
{.result_cb = std::function<void(Status)>{[](Status) {}}});
|
||||
pCore->StopAllEndpoints([](Status) {});
|
||||
delete pCore;
|
||||
}
|
||||
|
||||
@@ -80,14 +79,14 @@ void StartAdvertising(Core *pCore, const char *service_id,
|
||||
advertising_options.strategy = connections::Strategy::kP2pStar;
|
||||
|
||||
pCore->StartAdvertising(service_id, advertising_options, crInfo,
|
||||
*callback.GetImpl());
|
||||
std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void StopAdvertising(connections::Core *pCore, ResultCallbackW callback) {
|
||||
if (pCore == nullptr) {
|
||||
return;
|
||||
}
|
||||
pCore->StopAdvertising(*callback.GetImpl());
|
||||
pCore->StopAdvertising(std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void StartDiscovery(connections::Core *pCore, const char *service_id,
|
||||
@@ -134,7 +133,7 @@ void StopDiscovery(connections::Core *pCore, ResultCallbackW callback) {
|
||||
if (pCore == nullptr) {
|
||||
return;
|
||||
}
|
||||
pCore->StopDiscovery(*callback.GetImpl());
|
||||
pCore->StopDiscovery(std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void InjectEndpoint(connections::Core *pCore, char *service_id,
|
||||
@@ -153,7 +152,7 @@ void InjectEndpoint(connections::Core *pCore, char *service_id,
|
||||
metadata.remote_bluetooth_mac_address_size};
|
||||
|
||||
pCore->InjectEndpoint(service_id, outOfBandConnectionMetadata,
|
||||
*callback.GetImpl());
|
||||
std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void RequestConnection(connections::Core *pCore, const char *endpoint_id,
|
||||
@@ -203,7 +202,7 @@ void RequestConnection(connections::Core *pCore, const char *endpoint_id,
|
||||
connection_options.strategy = connections::Strategy::kP2pStar;
|
||||
|
||||
pCore->RequestConnection(endpoint_id, connectionRequestInfo,
|
||||
connection_options, *callback.GetImpl());
|
||||
connection_options, std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void AcceptConnection(connections::Core *pCore, const char *endpoint_id,
|
||||
@@ -214,7 +213,7 @@ void AcceptConnection(connections::Core *pCore, const char *endpoint_id,
|
||||
connections::PayloadListener payload_listener =
|
||||
std::move(*listener.GetImpl());
|
||||
pCore->AcceptConnection(endpoint_id, std::move(payload_listener),
|
||||
*callback.GetImpl());
|
||||
std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void RejectConnection(connections::Core *pCore, const char *endpoint_id,
|
||||
@@ -222,7 +221,7 @@ void RejectConnection(connections::Core *pCore, const char *endpoint_id,
|
||||
if (pCore == nullptr) {
|
||||
return;
|
||||
}
|
||||
pCore->RejectConnection(endpoint_id, *callback.GetImpl());
|
||||
pCore->RejectConnection(endpoint_id, std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void SendPayload(connections::Core *pCore,
|
||||
@@ -235,7 +234,8 @@ void SendPayload(connections::Core *pCore,
|
||||
}
|
||||
std::string payloadData = std::string(*endpoint_ids);
|
||||
absl::Span<const std::string> span{&payloadData, 1};
|
||||
pCore->SendPayload(span, std::move(*payloadw.GetImpl()), *callback.GetImpl());
|
||||
pCore->SendPayload(span, std::move(*payloadw.GetImpl()),
|
||||
std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void CancelPayload(connections::Core *pCore, std::int64_t payload_id,
|
||||
@@ -243,7 +243,7 @@ void CancelPayload(connections::Core *pCore, std::int64_t payload_id,
|
||||
if (pCore == nullptr) {
|
||||
return;
|
||||
}
|
||||
pCore->CancelPayload(payload_id, *callback.GetImpl());
|
||||
pCore->CancelPayload(payload_id, std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void DisconnectFromEndpoint(connections::Core *pCore, const char *endpoint_id,
|
||||
@@ -251,14 +251,14 @@ void DisconnectFromEndpoint(connections::Core *pCore, const char *endpoint_id,
|
||||
if (pCore == nullptr) {
|
||||
return;
|
||||
}
|
||||
pCore->DisconnectFromEndpoint(endpoint_id, *callback.GetImpl());
|
||||
pCore->DisconnectFromEndpoint(endpoint_id, std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void StopAllEndpoints(connections::Core *pCore, ResultCallbackW callback) {
|
||||
if (pCore == nullptr) {
|
||||
return;
|
||||
}
|
||||
pCore->StopAllEndpoints(*callback.GetImpl());
|
||||
pCore->StopAllEndpoints(std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
void InitiateBandwidthUpgrade(connections::Core *pCore, char *endpoint_id,
|
||||
@@ -266,7 +266,7 @@ void InitiateBandwidthUpgrade(connections::Core *pCore, char *endpoint_id,
|
||||
if (pCore == nullptr) {
|
||||
return;
|
||||
}
|
||||
pCore->InitiateBandwidthUpgrade(endpoint_id, *callback.GetImpl());
|
||||
pCore->InitiateBandwidthUpgrade(endpoint_id, std::move(*callback.GetImpl()));
|
||||
}
|
||||
|
||||
const char *GetLocalEndpointId(connections::Core *pCore) {
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "connections/c/listeners_w.h"
|
||||
|
||||
#include "connections/listeners.h"
|
||||
@@ -20,9 +23,6 @@ namespace nearby {
|
||||
// Must implement Deleters, since the connections classes weren't
|
||||
// fully defined in the header
|
||||
namespace connections {
|
||||
void ResultCallbackDeleter::operator()(connections::ResultCallback *p) {
|
||||
delete p;
|
||||
}
|
||||
void ConnectionListenerDeleter::operator()(connections::ConnectionListener *p) {
|
||||
delete p;
|
||||
}
|
||||
@@ -41,21 +41,18 @@ static ResultCallbackW *ResultCallbackImpl;
|
||||
void ResultCB(Status status) { ResultCallbackImpl->result_cb(status); }
|
||||
|
||||
ResultCallbackW::ResultCallbackW()
|
||||
: impl_(std::unique_ptr<connections::ResultCallback,
|
||||
connections::ResultCallbackDeleter>(
|
||||
new connections::ResultCallback())) {
|
||||
: impl(std::make_unique<connections::ResultCallback>(ResultCB)) {
|
||||
ResultCallbackImpl = this;
|
||||
impl_->result_cb = ResultCB;
|
||||
}
|
||||
|
||||
ResultCallbackW::~ResultCallbackW() {}
|
||||
|
||||
ResultCallbackW::ResultCallbackW(ResultCallbackW &other) {
|
||||
impl_ = std::move(other.impl_);
|
||||
impl = std::move(other.impl);
|
||||
}
|
||||
|
||||
ResultCallbackW::ResultCallbackW(ResultCallbackW &&other) noexcept {
|
||||
impl_ = std::move(other.impl_);
|
||||
impl = std::move(other.impl);
|
||||
}
|
||||
|
||||
ConnectionListenerW::ConnectionListenerW(InitiatedCB initiatedCB,
|
||||
|
||||
@@ -15,10 +15,8 @@
|
||||
#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_C_LISTENERS_W_H_
|
||||
#define THIRD_PARTY_NEARBY_CONNECTIONS_C_LISTENERS_W_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
// This file defines all the protocol listeners and their parameter structures.
|
||||
// Listeners are defined as collections of std::function<T> instances, which is
|
||||
@@ -50,10 +48,7 @@ struct DLL_API PayloadListenerDeleter {
|
||||
void operator()(connections::PayloadListener* p);
|
||||
};
|
||||
|
||||
struct ResultCallback;
|
||||
struct ResultCallbackDeleter {
|
||||
void operator()(connections::ResultCallback* p);
|
||||
};
|
||||
using ResultCallback = absl::AnyInvocable<void(Status)>;
|
||||
|
||||
struct ConnectionResponseInfo;
|
||||
struct PayloadProgressInfo;
|
||||
@@ -92,16 +87,12 @@ struct DLL_API ResultCallbackW {
|
||||
|
||||
void (*result_cb)(Status status) = DefaultConstructor;
|
||||
|
||||
std::unique_ptr<connections::ResultCallback,
|
||||
connections::ResultCallbackDeleter>
|
||||
GetImpl() {
|
||||
return std::move(impl_);
|
||||
std::unique_ptr<connections::ResultCallback> GetImpl() {
|
||||
return std::move(impl);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<connections::ResultCallback,
|
||||
connections::ResultCallbackDeleter>
|
||||
impl_;
|
||||
std::unique_ptr<connections::ResultCallback> impl;
|
||||
};
|
||||
|
||||
struct DLL_API ConnectionResponseInfoW {
|
||||
|
||||
+39
-35
@@ -54,10 +54,7 @@ Core::Core(ServiceControllerRouter* router) : router_(router) {}
|
||||
|
||||
Core::~Core() {
|
||||
CountDownLatch latch(1);
|
||||
router_->StopAllEndpoints(
|
||||
&client_, {
|
||||
.result_cb = [&latch](Status) { latch.CountDown(); },
|
||||
});
|
||||
router_->StopAllEndpoints(&client_, [&latch](Status) { latch.CountDown(); });
|
||||
if (!latch.Await(kWaitForDisconnect).result()) {
|
||||
NEARBY_LOG(FATAL, "Unable to shutdown");
|
||||
}
|
||||
@@ -75,11 +72,11 @@ void Core::StartAdvertising(absl::string_view service_id,
|
||||
CHECK(advertising_options.strategy.IsValid());
|
||||
|
||||
router_->StartAdvertising(&client_, service_id, advertising_options, info,
|
||||
callback);
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void Core::StopAdvertising(const ResultCallback callback) {
|
||||
router_->StopAdvertising(&client_, callback);
|
||||
void Core::StopAdvertising(ResultCallback callback) {
|
||||
router_->StopAdvertising(&client_, std::move(callback));
|
||||
}
|
||||
|
||||
void Core::StartDiscovery(absl::string_view service_id,
|
||||
@@ -89,18 +86,18 @@ void Core::StartDiscovery(absl::string_view service_id,
|
||||
CHECK(discovery_options.strategy.IsValid());
|
||||
|
||||
router_->StartDiscovery(&client_, service_id, discovery_options, listener,
|
||||
callback);
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void Core::InjectEndpoint(absl::string_view service_id,
|
||||
OutOfBandConnectionMetadata metadata,
|
||||
ResultCallback callback) {
|
||||
CheckServiceId(service_id);
|
||||
router_->InjectEndpoint(&client_, service_id, metadata, callback);
|
||||
router_->InjectEndpoint(&client_, service_id, metadata, std::move(callback));
|
||||
}
|
||||
|
||||
void Core::StopDiscovery(ResultCallback callback) {
|
||||
router_->StopDiscovery(&client_, callback);
|
||||
router_->StopDiscovery(&client_, std::move(callback));
|
||||
}
|
||||
|
||||
void Core::RequestConnection(absl::string_view endpoint_id,
|
||||
@@ -128,7 +125,7 @@ void Core::RequestConnection(absl::string_view endpoint_id,
|
||||
}
|
||||
|
||||
router_->RequestConnection(&client_, endpoint_id, info, connection_options,
|
||||
callback);
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void Core::AcceptConnection(absl::string_view endpoint_id,
|
||||
@@ -136,19 +133,19 @@ void Core::AcceptConnection(absl::string_view endpoint_id,
|
||||
CHECK(!endpoint_id.empty());
|
||||
|
||||
router_->AcceptConnection(&client_, endpoint_id, std::move(listener),
|
||||
callback);
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void Core::RejectConnection(absl::string_view endpoint_id,
|
||||
ResultCallback callback) {
|
||||
CHECK(!endpoint_id.empty());
|
||||
|
||||
router_->RejectConnection(&client_, endpoint_id, callback);
|
||||
router_->RejectConnection(&client_, endpoint_id, std::move(callback));
|
||||
}
|
||||
|
||||
void Core::InitiateBandwidthUpgrade(absl::string_view endpoint_id,
|
||||
ResultCallback callback) {
|
||||
router_->InitiateBandwidthUpgrade(&client_, endpoint_id, callback);
|
||||
router_->InitiateBandwidthUpgrade(&client_, endpoint_id, std::move(callback));
|
||||
}
|
||||
|
||||
void Core::SendPayload(absl::Span<const std::string> endpoint_ids,
|
||||
@@ -156,28 +153,29 @@ void Core::SendPayload(absl::Span<const std::string> endpoint_ids,
|
||||
CHECK(payload.GetType() != PayloadType::kUnknown);
|
||||
CHECK(!endpoint_ids.empty());
|
||||
|
||||
router_->SendPayload(&client_, endpoint_ids, std::move(payload), callback);
|
||||
router_->SendPayload(&client_, endpoint_ids, std::move(payload),
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void Core::CancelPayload(std::int64_t payload_id, ResultCallback callback) {
|
||||
CHECK_NE(payload_id, 0);
|
||||
|
||||
router_->CancelPayload(&client_, payload_id, callback);
|
||||
router_->CancelPayload(&client_, payload_id, std::move(callback));
|
||||
}
|
||||
|
||||
void Core::DisconnectFromEndpoint(absl::string_view endpoint_id,
|
||||
ResultCallback callback) {
|
||||
CHECK(!endpoint_id.empty());
|
||||
|
||||
router_->DisconnectFromEndpoint(&client_, endpoint_id, callback);
|
||||
router_->DisconnectFromEndpoint(&client_, endpoint_id, std::move(callback));
|
||||
}
|
||||
|
||||
void Core::StopAllEndpoints(ResultCallback callback) {
|
||||
router_->StopAllEndpoints(&client_, callback);
|
||||
router_->StopAllEndpoints(&client_, std::move(callback));
|
||||
}
|
||||
|
||||
void Core::SetCustomSavePath(absl::string_view path, ResultCallback callback) {
|
||||
router_->SetCustomSavePath(&client_, path, callback);
|
||||
router_->SetCustomSavePath(&client_, path, std::move(callback));
|
||||
}
|
||||
|
||||
std::string Core::Dump() { return client_.Dump(); }
|
||||
@@ -240,7 +238,8 @@ void Core::StartAdvertisingV3(absl::string_view service_id,
|
||||
.endpoint_info = local_endpoint_info,
|
||||
.listener = old_listener,
|
||||
};
|
||||
StartAdvertising(service_id, advertising_options, old_info, callback);
|
||||
StartAdvertising(service_id, advertising_options, old_info,
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void Core::StartAdvertisingV3(absl::string_view service_id,
|
||||
@@ -300,11 +299,12 @@ void Core::StartAdvertisingV3(absl::string_view service_id,
|
||||
.endpoint_info = local_endpoint_info,
|
||||
.listener = old_listener,
|
||||
};
|
||||
StartAdvertising(service_id, advertising_options, old_info, callback);
|
||||
StartAdvertising(service_id, advertising_options, old_info,
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void Core::StopAdvertisingV3(ResultCallback result_cb) {
|
||||
StopAdvertising(result_cb);
|
||||
StopAdvertising(std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::StartDiscoveryV3(absl::string_view service_id,
|
||||
@@ -332,11 +332,12 @@ void Core::StartDiscoveryV3(absl::string_view service_id,
|
||||
listener.endpoint_distance_changed_cb(remote, distance_info);
|
||||
},
|
||||
};
|
||||
StartDiscovery(service_id, discovery_options, old_listener, callback);
|
||||
StartDiscovery(service_id, discovery_options, old_listener,
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void Core::StopDiscoveryV3(ResultCallback result_cb) {
|
||||
router_->StopDiscovery(&client_, result_cb);
|
||||
router_->StopDiscovery(&client_, std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::StartListeningForIncomingConnectionsV3(
|
||||
@@ -383,7 +384,7 @@ void Core::RequestConnectionV3(const NearbyDevice& local_device,
|
||||
FeatureFlags::GetInstance().GetFlags().keep_alive_timeout_millis;
|
||||
}
|
||||
router_->RequestConnectionV3(&client_, remote_device, std::move(info),
|
||||
connection_options, result_cb);
|
||||
connection_options, std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::RequestConnectionV3(const NearbyDevice& remote_device,
|
||||
@@ -414,7 +415,7 @@ void Core::RequestConnectionV3(const NearbyDevice& remote_device,
|
||||
FeatureFlags::GetInstance().GetFlags().keep_alive_timeout_millis;
|
||||
}
|
||||
router_->RequestConnectionV3(&client_, remote_device, std::move(info),
|
||||
connection_options, result_cb);
|
||||
connection_options, std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::AcceptConnectionV3(const NearbyDevice& remote_device,
|
||||
@@ -423,14 +424,14 @@ void Core::AcceptConnectionV3(const NearbyDevice& remote_device,
|
||||
CHECK(!remote_device.GetEndpointId().empty());
|
||||
|
||||
router_->AcceptConnectionV3(&client_, remote_device, std::move(listener_cb),
|
||||
result_cb);
|
||||
std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::RejectConnectionV3(const NearbyDevice& remote_device,
|
||||
ResultCallback result_cb) {
|
||||
CHECK(!remote_device.GetEndpointId().empty());
|
||||
|
||||
router_->RejectConnectionV3(&client_, remote_device, result_cb);
|
||||
router_->RejectConnectionV3(&client_, remote_device, std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::SendPayloadV3(const NearbyDevice& remote_device, Payload payload,
|
||||
@@ -439,44 +440,47 @@ void Core::SendPayloadV3(const NearbyDevice& remote_device, Payload payload,
|
||||
CHECK(!remote_device.GetEndpointId().empty());
|
||||
|
||||
router_->SendPayloadV3(&client_, remote_device, std::move(payload),
|
||||
result_cb);
|
||||
std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::CancelPayloadV3(const NearbyDevice& remote_device,
|
||||
int64_t payload_id, ResultCallback result_cb) {
|
||||
CHECK_NE(payload_id, 0);
|
||||
|
||||
router_->CancelPayloadV3(&client_, remote_device, payload_id, result_cb);
|
||||
router_->CancelPayloadV3(&client_, remote_device, payload_id,
|
||||
std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::DisconnectFromDeviceV3(const NearbyDevice& remote_device,
|
||||
ResultCallback result_cb) {
|
||||
CHECK(!remote_device.GetEndpointId().empty());
|
||||
|
||||
router_->DisconnectFromDeviceV3(&client_, remote_device, result_cb);
|
||||
router_->DisconnectFromDeviceV3(&client_, remote_device,
|
||||
std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::StopAllDevicesV3(ResultCallback result_cb) {
|
||||
router_->StopAllEndpoints(&client_, result_cb);
|
||||
router_->StopAllEndpoints(&client_, std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::InitiateBandwidthUpgradeV3(const NearbyDevice& remote_device,
|
||||
ResultCallback result_cb) {
|
||||
router_->InitiateBandwidthUpgradeV3(&client_, remote_device, result_cb);
|
||||
router_->InitiateBandwidthUpgradeV3(&client_, remote_device,
|
||||
std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::UpdateAdvertisingOptionsV3(absl::string_view service_id,
|
||||
AdvertisingOptions advertising_options,
|
||||
ResultCallback result_cb) {
|
||||
router_->UpdateAdvertisingOptionsV3(&client_, service_id, advertising_options,
|
||||
result_cb);
|
||||
std::move(result_cb));
|
||||
}
|
||||
|
||||
void Core::UpdateDiscoveryOptionsV3(absl::string_view service_id,
|
||||
DiscoveryOptions discovery_options,
|
||||
ResultCallback result_cb) {
|
||||
router_->UpdateDiscoveryOptionsV3(&client_, service_id, discovery_options,
|
||||
result_cb);
|
||||
std::move(result_cb));
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
|
||||
+49
-13
@@ -54,8 +54,8 @@ TEST(CoreTest, ConstructorDestructorWorks) {
|
||||
MockServiceControllerRouter mock;
|
||||
// Called when Core is destroyed.
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, const ResultCallback& callback) {
|
||||
callback.result_cb({Status::kSuccess});
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
Core core{&mock};
|
||||
}
|
||||
@@ -71,6 +71,42 @@ TEST(CoreTest, DestructorReportsFatalFailure) {
|
||||
"Unable to shutdown");
|
||||
}
|
||||
|
||||
TEST(CoreTest, RequestConnectionCallsScRouter) {
|
||||
MockServiceControllerRouter mock;
|
||||
// Called when Core is destroyed.
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
EXPECT_CALL(mock, RequestConnection);
|
||||
Core core{&mock};
|
||||
core.RequestConnection("TEST", {}, {}, {});
|
||||
}
|
||||
|
||||
TEST(CoreTest, AcceptConnectionCallsScRouter) {
|
||||
MockServiceControllerRouter mock;
|
||||
// Called when Core is destroyed.
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
EXPECT_CALL(mock, AcceptConnection);
|
||||
Core core{&mock};
|
||||
core.AcceptConnection("TEST", {}, {});
|
||||
}
|
||||
|
||||
TEST(CoreTest, SendPayloadCallsScRouter) {
|
||||
MockServiceControllerRouter mock;
|
||||
// Called when Core is destroyed.
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
EXPECT_CALL(mock, SendPayload);
|
||||
Core core{&mock};
|
||||
core.SendPayload({"TEST"}, Payload(ByteArray("Hello world")), {});
|
||||
}
|
||||
|
||||
TEST(CoreV3Test, TestCallbackWrapWorksStartAdvertisingV3FourArgs) {
|
||||
MockServiceControllerRouter mock;
|
||||
EXPECT_CALL(mock, StartAdvertising)
|
||||
@@ -86,9 +122,9 @@ TEST(CoreV3Test, TestCallbackWrapWorksStartAdvertisingV3FourArgs) {
|
||||
info.listener.disconnected_cb("FAKE");
|
||||
});
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, const ResultCallback& callback) {
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "StopAllEndpoints called";
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
Core core{&mock};
|
||||
CountDownLatch result_latch(2);
|
||||
@@ -132,7 +168,7 @@ TEST(CoreV3Test, TestStartAdvertisingV3NonConnectionsDeviceProvider) {
|
||||
MockServiceControllerRouter mock;
|
||||
EXPECT_CALL(mock, StartAdvertising)
|
||||
.WillOnce([&](ClientProxy*, absl::string_view, const AdvertisingOptions&,
|
||||
const ConnectionRequestInfo& info, const ResultCallback&) {
|
||||
const ConnectionRequestInfo& info, ResultCallback) {
|
||||
NEARBY_LOGS(INFO) << "StartAdvertising called";
|
||||
ASSERT_TRUE(info.endpoint_info.Empty());
|
||||
// call all callbacks to make sure it all gets called correctly.
|
||||
@@ -143,9 +179,9 @@ TEST(CoreV3Test, TestStartAdvertisingV3NonConnectionsDeviceProvider) {
|
||||
info.listener.disconnected_cb("FAKE");
|
||||
});
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, const ResultCallback& callback) {
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "StopAllEndpoints called";
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
Core core{&mock};
|
||||
CountDownLatch result_latch(2);
|
||||
@@ -202,9 +238,9 @@ TEST(CoreV3Test, TestStartAdvertisingV3NonConnectionsDevice) {
|
||||
info.listener.disconnected_cb("FAKE");
|
||||
});
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, const ResultCallback& callback) {
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "StopAllEndpoints called";
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
Core core{&mock};
|
||||
CountDownLatch result_latch(2);
|
||||
@@ -260,9 +296,9 @@ TEST(CoreV3Test, TestCallbackWrapWorksStartAdvertisingV3FiveArgs) {
|
||||
info.listener.disconnected_cb("FAKE");
|
||||
});
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, const ResultCallback& callback) {
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "StopAllEndpoints called";
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
Core core{&mock};
|
||||
CountDownLatch result_latch(2);
|
||||
@@ -315,9 +351,9 @@ TEST(CoreV3Test, TestCallbackWrapWorksStartDiscoveryV3) {
|
||||
info.endpoint_lost_cb("FAKE");
|
||||
});
|
||||
EXPECT_CALL(mock, StopAllEndpoints)
|
||||
.WillOnce([&](ClientProxy* client, const ResultCallback& callback) {
|
||||
.WillOnce([&](ClientProxy* client, ResultCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "StopAllEndpoints called";
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
DiscoveryOptions options;
|
||||
options.strategy = Strategy::kP2pCluster;
|
||||
|
||||
@@ -26,118 +26,113 @@ class MockServiceControllerRouter : public ServiceControllerRouter {
|
||||
MOCK_METHOD(void, StartAdvertising,
|
||||
(ClientProxy * client, absl::string_view service_id,
|
||||
const AdvertisingOptions& advertising_options,
|
||||
const ConnectionRequestInfo& info,
|
||||
const ResultCallback& callback),
|
||||
const ConnectionRequestInfo& info, ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, StopAdvertising,
|
||||
(ClientProxy * client, const ResultCallback& callback),
|
||||
(override));
|
||||
(ClientProxy * client, ResultCallback callback), (override));
|
||||
|
||||
MOCK_METHOD(void, StartDiscovery,
|
||||
(ClientProxy * client, absl::string_view service_id,
|
||||
const DiscoveryOptions& discovery_options,
|
||||
const DiscoveryListener& listener,
|
||||
const ResultCallback& callback),
|
||||
const DiscoveryListener& listener, ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, StopDiscovery,
|
||||
(ClientProxy * client, const ResultCallback& callback),
|
||||
(override));
|
||||
(ClientProxy * client, ResultCallback callback), (override));
|
||||
|
||||
MOCK_METHOD(void, InjectEndpoint,
|
||||
(ClientProxy * client, absl::string_view service_id,
|
||||
const OutOfBandConnectionMetadata& metadata,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, RequestConnection,
|
||||
(ClientProxy * client, absl::string_view endpoint_id,
|
||||
const ConnectionRequestInfo& info,
|
||||
const ConnectionOptions& connection_options,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, AcceptConnection,
|
||||
(ClientProxy * client, absl::string_view endpoint_id,
|
||||
PayloadListener listener, const ResultCallback& callback),
|
||||
PayloadListener listener, ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, RejectConnection,
|
||||
(ClientProxy * client, absl::string_view endpoint_id,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, InitiateBandwidthUpgrade,
|
||||
(ClientProxy * client, absl::string_view endpoint_id,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, SendPayload,
|
||||
(ClientProxy * client, absl::Span<const std::string> endpoint_ids,
|
||||
Payload payload, const ResultCallback& callback),
|
||||
Payload payload, ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, CancelPayload,
|
||||
(ClientProxy * client, std::uint64_t payload_id,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, DisconnectFromEndpoint,
|
||||
(ClientProxy * client, absl::string_view endpoint_id,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, StopAllEndpoints,
|
||||
(ClientProxy * client, const ResultCallback& callback),
|
||||
(override));
|
||||
(ClientProxy * client, ResultCallback callback), (override));
|
||||
|
||||
MOCK_METHOD(void, SetCustomSavePath,
|
||||
(ClientProxy * client, absl::string_view path,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, RequestConnectionV3,
|
||||
(ClientProxy * client, const NearbyDevice&,
|
||||
v3::ConnectionRequestInfo, const ConnectionOptions&,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, AcceptConnectionV3,
|
||||
(ClientProxy * client, const NearbyDevice&, v3::PayloadListener,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, RejectConnectionV3,
|
||||
(ClientProxy * client, const NearbyDevice&,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, InitiateBandwidthUpgradeV3,
|
||||
(ClientProxy * client, const NearbyDevice&,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, SendPayloadV3,
|
||||
(ClientProxy * client, const NearbyDevice&, Payload,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, DisconnectFromDeviceV3,
|
||||
(ClientProxy * client, const NearbyDevice&,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, UpdateAdvertisingOptionsV3,
|
||||
(ClientProxy * client, absl::string_view service_id,
|
||||
const AdvertisingOptions& advertising_options,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, UpdateDiscoveryOptionsV3,
|
||||
(ClientProxy * client, absl::string_view service_id,
|
||||
const DiscoveryOptions& discovery_options,
|
||||
const ResultCallback& callback),
|
||||
ResultCallback callback),
|
||||
(override));
|
||||
};
|
||||
|
||||
|
||||
@@ -98,101 +98,103 @@ ServiceControllerRouter::~ServiceControllerRouter() {
|
||||
void ServiceControllerRouter::StartAdvertising(
|
||||
ClientProxy* client, absl::string_view service_id,
|
||||
const AdvertisingOptions& advertising_options,
|
||||
const ConnectionRequestInfo& info, const ResultCallback& callback) {
|
||||
const ConnectionRequestInfo& info, ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-start-advertising",
|
||||
[this, client, service_id = std::string(service_id), advertising_options,
|
||||
info, callback]() {
|
||||
info, callback = std::move(callback)]() mutable {
|
||||
if (client->IsAdvertising()) {
|
||||
callback.result_cb({Status::kAlreadyAdvertising});
|
||||
callback({Status::kAlreadyAdvertising});
|
||||
return;
|
||||
}
|
||||
|
||||
callback.result_cb(GetServiceController()->StartAdvertising(
|
||||
callback(GetServiceController()->StartAdvertising(
|
||||
client, service_id, advertising_options, info));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::StopAdvertising(ClientProxy* client,
|
||||
const ResultCallback& callback) {
|
||||
RouteToServiceController("scr-stop-advertising", [this, client, callback]() {
|
||||
if (client->IsAdvertising()) {
|
||||
GetServiceController()->StopAdvertising(client);
|
||||
}
|
||||
callback.result_cb({Status::kSuccess});
|
||||
});
|
||||
ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-stop-advertising",
|
||||
[this, client, callback = std::move(callback)]() mutable {
|
||||
if (client->IsAdvertising()) {
|
||||
GetServiceController()->StopAdvertising(client);
|
||||
}
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::StartDiscovery(
|
||||
ClientProxy* client, absl::string_view service_id,
|
||||
const DiscoveryOptions& discovery_options,
|
||||
const DiscoveryListener& listener, const ResultCallback& callback) {
|
||||
const DiscoveryListener& listener, ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-start-discovery",
|
||||
[this, client, service_id = std::string(service_id), discovery_options,
|
||||
listener, callback]() {
|
||||
listener, callback = std::move(callback)]() mutable {
|
||||
if (client->IsDiscovering()) {
|
||||
callback.result_cb({Status::kAlreadyDiscovering});
|
||||
callback({Status::kAlreadyDiscovering});
|
||||
return;
|
||||
}
|
||||
|
||||
callback.result_cb(GetServiceController()->StartDiscovery(
|
||||
callback(GetServiceController()->StartDiscovery(
|
||||
client, service_id, discovery_options, listener));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::StopDiscovery(ClientProxy* client,
|
||||
const ResultCallback& callback) {
|
||||
RouteToServiceController("scr-stop-discovery", [this, client, callback]() {
|
||||
if (client->IsDiscovering()) {
|
||||
GetServiceController()->StopDiscovery(client);
|
||||
}
|
||||
callback.result_cb({Status::kSuccess});
|
||||
});
|
||||
ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-stop-discovery",
|
||||
[this, client, callback = std::move(callback)]() mutable {
|
||||
if (client->IsDiscovering()) {
|
||||
GetServiceController()->StopDiscovery(client);
|
||||
}
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::InjectEndpoint(
|
||||
ClientProxy* client, absl::string_view service_id,
|
||||
const OutOfBandConnectionMetadata& metadata,
|
||||
const ResultCallback& callback) {
|
||||
const OutOfBandConnectionMetadata& metadata, ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-inject-endpoint",
|
||||
[this, client, service_id = std::string(service_id), metadata,
|
||||
callback]() {
|
||||
callback = std::move(callback)]() mutable {
|
||||
// Currently, Bluetooth is the only supported medium for endpoint
|
||||
// injection.
|
||||
if (metadata.medium != Medium::BLUETOOTH ||
|
||||
metadata.remote_bluetooth_mac_address.size() != kMacAddressLength) {
|
||||
callback.result_cb({Status::kError});
|
||||
callback({Status::kError});
|
||||
return;
|
||||
}
|
||||
|
||||
if (metadata.endpoint_id.size() != kEndpointIdLength) {
|
||||
callback.result_cb({Status::kError});
|
||||
callback({Status::kError});
|
||||
return;
|
||||
}
|
||||
|
||||
if (metadata.endpoint_info.Empty() ||
|
||||
metadata.endpoint_info.size() > kMaxEndpointInfoLength) {
|
||||
callback.result_cb({Status::kError});
|
||||
callback({Status::kError});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!client->IsDiscovering()) {
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
GetServiceController()->InjectEndpoint(client, service_id, metadata);
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::RequestConnection(
|
||||
ClientProxy* client, absl::string_view endpoint_id,
|
||||
const ConnectionRequestInfo& info,
|
||||
const ConnectionOptions& connection_options,
|
||||
const ResultCallback& callback) {
|
||||
const ConnectionOptions& connection_options, ResultCallback callback) {
|
||||
// Cancellations can be fired from clients anytime, need to add the
|
||||
// CancellationListener as soon as possible.
|
||||
client->AddCancellationFlag(std::string(endpoint_id));
|
||||
@@ -200,10 +202,10 @@ void ServiceControllerRouter::RequestConnection(
|
||||
RouteToServiceController(
|
||||
"scr-request-connection",
|
||||
[this, client, endpoint_id = std::string(endpoint_id), info,
|
||||
connection_options, callback]() {
|
||||
connection_options, callback = std::move(callback)]() mutable {
|
||||
if (client->HasPendingConnectionToEndpoint(endpoint_id) ||
|
||||
client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kAlreadyConnectedToEndpoint});
|
||||
callback({Status::kAlreadyConnectedToEndpoint});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -212,20 +214,21 @@ void ServiceControllerRouter::RequestConnection(
|
||||
if (!status.Ok()) {
|
||||
client->CancelEndpoint(endpoint_id);
|
||||
}
|
||||
callback.result_cb(status);
|
||||
callback(status);
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::AcceptConnection(ClientProxy* client,
|
||||
absl::string_view endpoint_id,
|
||||
PayloadListener listener,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-accept-connection",
|
||||
[this, client, endpoint_id = std::string(endpoint_id),
|
||||
listener = std::move(listener), callback]() mutable {
|
||||
listener = std::move(listener),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kAlreadyConnectedToEndpoint});
|
||||
callback({Status::kAlreadyConnectedToEndpoint});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -235,25 +238,26 @@ void ServiceControllerRouter::AcceptConnection(ClientProxy* client,
|
||||
<< " invoked acceptConnectionRequest() after having already "
|
||||
"accepted/rejected the connection to endpoint(id="
|
||||
<< endpoint_id << ")";
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
callback.result_cb(GetServiceController()->AcceptConnection(
|
||||
client, endpoint_id, std::move(listener)));
|
||||
callback(GetServiceController()->AcceptConnection(client, endpoint_id,
|
||||
std::move(listener)));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::RejectConnection(ClientProxy* client,
|
||||
absl::string_view endpoint_id,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
client->CancelEndpoint(std::string(endpoint_id));
|
||||
|
||||
RouteToServiceController(
|
||||
"scr-reject-connection",
|
||||
[this, client, endpoint_id = std::string(endpoint_id), callback]() {
|
||||
[this, client, endpoint_id = std::string(endpoint_id),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kAlreadyConnectedToEndpoint});
|
||||
callback({Status::kAlreadyConnectedToEndpoint});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -263,23 +267,22 @@ void ServiceControllerRouter::RejectConnection(ClientProxy* client,
|
||||
<< " invoked rejectConnectionRequest() after having already "
|
||||
"accepted/rejected the connection to endpoint(id="
|
||||
<< endpoint_id << ")";
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
callback.result_cb(
|
||||
GetServiceController()->RejectConnection(client, endpoint_id));
|
||||
callback(GetServiceController()->RejectConnection(client, endpoint_id));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::InitiateBandwidthUpgrade(
|
||||
ClientProxy* client, absl::string_view endpoint_id,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-init-bwu",
|
||||
[this, client, endpoint_id = std::string(endpoint_id), callback]() {
|
||||
"scr-init-bwu", [this, client, endpoint_id = std::string(endpoint_id),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (!client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -287,62 +290,65 @@ void ServiceControllerRouter::InitiateBandwidthUpgrade(
|
||||
|
||||
// Operation is triggered; the caller can listen to
|
||||
// ConnectionListener::OnBandwidthChanged() to determine its success.
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::SendPayload(
|
||||
ClientProxy* client, absl::Span<const std::string> endpoint_ids,
|
||||
Payload payload, const ResultCallback& callback) {
|
||||
Payload payload, ResultCallback callback) {
|
||||
const std::vector<std::string> endpoints =
|
||||
std::vector<std::string>(endpoint_ids.begin(), endpoint_ids.end());
|
||||
|
||||
RouteToServiceController("scr-send-payload", [this, client,
|
||||
payload = std::move(payload),
|
||||
endpoints, callback]() mutable {
|
||||
if (!ClientHasConnectionToAtLeastOneEndpoint(client, endpoints)) {
|
||||
callback.result_cb({Status::kEndpointUnknown});
|
||||
return;
|
||||
}
|
||||
RouteToServiceController(
|
||||
"scr-send-payload",
|
||||
[this, client, payload = std::move(payload), endpoints,
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (!ClientHasConnectionToAtLeastOneEndpoint(client, endpoints)) {
|
||||
callback({Status::kEndpointUnknown});
|
||||
return;
|
||||
}
|
||||
|
||||
GetServiceController()->SendPayload(client, endpoints, std::move(payload));
|
||||
GetServiceController()->SendPayload(client, endpoints,
|
||||
std::move(payload));
|
||||
|
||||
// At this point, we've queued up the send Payload request with the
|
||||
// ServiceController; any further failures (e.g. one of the endpoints is
|
||||
// unknown, goes away, or otherwise fails) will be returned to the
|
||||
// client as a PayloadTransferUpdate.
|
||||
callback.result_cb({Status::kSuccess});
|
||||
});
|
||||
// At this point, we've queued up the send Payload request with the
|
||||
// ServiceController; any further failures (e.g. one of the endpoints is
|
||||
// unknown, goes away, or otherwise fails) will be returned to the
|
||||
// client as a PayloadTransferUpdate.
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::CancelPayload(ClientProxy* client,
|
||||
std::uint64_t payload_id,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-cancel-payload", [this, client, payload_id, callback]() {
|
||||
callback.result_cb(
|
||||
GetServiceController()->CancelPayload(client, payload_id));
|
||||
"scr-cancel-payload",
|
||||
[this, client, payload_id, callback = std::move(callback)]() mutable {
|
||||
callback(GetServiceController()->CancelPayload(client, payload_id));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::DisconnectFromEndpoint(
|
||||
ClientProxy* client, absl::string_view endpoint_id,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
// Client can emit the cancellation at anytime, we need to execute the request
|
||||
// without further posting it.
|
||||
client->CancelEndpoint(std::string(endpoint_id));
|
||||
|
||||
RouteToServiceController(
|
||||
"scr-disconnect-endpoint",
|
||||
[this, client, endpoint_id = std::string(endpoint_id), callback]() {
|
||||
[this, client, endpoint_id = std::string(endpoint_id),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (!client->IsConnectedToEndpoint(endpoint_id) &&
|
||||
!client->HasPendingConnectionToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
GetServiceController()->DisconnectFromEndpoint(client, endpoint_id);
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -388,7 +394,7 @@ void ServiceControllerRouter::StopListeningForIncomingConnectionsV3(
|
||||
void ServiceControllerRouter::RequestConnectionV3(
|
||||
ClientProxy* client, const NearbyDevice& remote_device,
|
||||
v3::ConnectionRequestInfo info, const ConnectionOptions& connection_options,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
// Cancellations can be fired from clients anytime, need to add the
|
||||
// CancellationListener as soon as possible.
|
||||
client->AddCancellationFlag(remote_device.GetEndpointId());
|
||||
@@ -396,10 +402,11 @@ void ServiceControllerRouter::RequestConnectionV3(
|
||||
RouteToServiceController(
|
||||
"scr-request-connection",
|
||||
[this, client, endpoint_id = remote_device.GetEndpointId(),
|
||||
v3_info = std::move(info), connection_options, callback]() mutable {
|
||||
v3_info = std::move(info), connection_options,
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (client->HasPendingConnectionToEndpoint(endpoint_id) ||
|
||||
client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kAlreadyConnectedToEndpoint});
|
||||
callback({Status::kAlreadyConnectedToEndpoint});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -473,19 +480,20 @@ void ServiceControllerRouter::RequestConnectionV3(
|
||||
<< endpoint_id << ": " << status.ToString();
|
||||
client->CancelEndpoint(endpoint_id);
|
||||
}
|
||||
callback.result_cb(status);
|
||||
callback(status);
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::AcceptConnectionV3(
|
||||
ClientProxy* client, const NearbyDevice& remote_device,
|
||||
v3::PayloadListener listener, const ResultCallback& callback) {
|
||||
v3::PayloadListener listener, ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-accept-connection",
|
||||
[this, client, endpoint_id = remote_device.GetEndpointId(),
|
||||
v3_listener = std::move(listener), callback]() mutable {
|
||||
v3_listener = std::move(listener),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kAlreadyConnectedToEndpoint});
|
||||
callback({Status::kAlreadyConnectedToEndpoint});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -495,7 +503,7 @@ void ServiceControllerRouter::AcceptConnectionV3(
|
||||
<< " invoked acceptConnectionRequest() after having already "
|
||||
"accepted/rejected the connection to endpoint(id="
|
||||
<< endpoint_id << ")";
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -513,21 +521,22 @@ void ServiceControllerRouter::AcceptConnectionV3(
|
||||
v3_cb(v3::ConnectionsDevice(endpoint_id, "", {}), info);
|
||||
}};
|
||||
|
||||
callback.result_cb(GetServiceController()->AcceptConnection(
|
||||
callback(GetServiceController()->AcceptConnection(
|
||||
client, endpoint_id, std::move(old_listener)));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::RejectConnectionV3(
|
||||
ClientProxy* client, const NearbyDevice& remote_device,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
client->CancelEndpoint(remote_device.GetEndpointId());
|
||||
|
||||
RouteToServiceController(
|
||||
"scr-reject-connection",
|
||||
[this, client, endpoint_id = remote_device.GetEndpointId(), callback]() {
|
||||
[this, client, endpoint_id = remote_device.GetEndpointId(),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kAlreadyConnectedToEndpoint});
|
||||
callback({Status::kAlreadyConnectedToEndpoint});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -537,23 +546,23 @@ void ServiceControllerRouter::RejectConnectionV3(
|
||||
<< " invoked rejectConnectionRequest() after having already "
|
||||
"accepted/rejected the connection to endpoint(id="
|
||||
<< endpoint_id << ")";
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
callback.result_cb(
|
||||
GetServiceController()->RejectConnection(client, endpoint_id));
|
||||
callback(GetServiceController()->RejectConnection(client, endpoint_id));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::InitiateBandwidthUpgradeV3(
|
||||
ClientProxy* client, const NearbyDevice& remote_device,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-init-bwu",
|
||||
[this, client, endpoint_id = remote_device.GetEndpointId(), callback]() {
|
||||
[this, client, endpoint_id = remote_device.GetEndpointId(),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (!client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -561,19 +570,19 @@ void ServiceControllerRouter::InitiateBandwidthUpgradeV3(
|
||||
|
||||
// Operation is triggered; the caller can listen to
|
||||
// ConnectionListener::OnBandwidthChanged() to determine its success.
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::SendPayloadV3(
|
||||
ClientProxy* client, const NearbyDevice& recipient_device, Payload payload,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-send-payload",
|
||||
[this, client, payload = std::move(payload),
|
||||
endpoint_id = recipient_device.GetEndpointId(), callback]() mutable {
|
||||
"scr-send-payload", [this, client, payload = std::move(payload),
|
||||
endpoint_id = recipient_device.GetEndpointId(),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (!client->IsConnectedToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kEndpointUnknown});
|
||||
callback({Status::kEndpointUnknown});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -584,90 +593,94 @@ void ServiceControllerRouter::SendPayloadV3(
|
||||
// ServiceController; any further failures (e.g. one of the endpoints is
|
||||
// unknown, goes away, or otherwise fails) will be returned to the
|
||||
// client as a PayloadTransferUpdate.
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::CancelPayloadV3(
|
||||
ClientProxy* client, const NearbyDevice& recipient_device,
|
||||
uint64_t payload_id, const ResultCallback& callback) {
|
||||
uint64_t payload_id, ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-cancel-payload", [this, client, payload_id, callback]() {
|
||||
callback.result_cb(
|
||||
GetServiceController()->CancelPayload(client, payload_id));
|
||||
"scr-cancel-payload",
|
||||
[this, client, payload_id, callback = std::move(callback)]() mutable {
|
||||
callback(GetServiceController()->CancelPayload(client, payload_id));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::DisconnectFromDeviceV3(
|
||||
ClientProxy* client, const NearbyDevice& remote_device,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
// Client can emit the cancellation at anytime, we need to execute the request
|
||||
// without further posting it.
|
||||
client->CancelEndpoint(remote_device.GetEndpointId());
|
||||
|
||||
RouteToServiceController(
|
||||
"scr-disconnect-endpoint",
|
||||
[this, client, endpoint_id = remote_device.GetEndpointId(), callback]() {
|
||||
[this, client, endpoint_id = remote_device.GetEndpointId(),
|
||||
callback = std::move(callback)]() mutable {
|
||||
if (!client->IsConnectedToEndpoint(endpoint_id) &&
|
||||
!client->HasPendingConnectionToEndpoint(endpoint_id)) {
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
callback({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
|
||||
GetServiceController()->DisconnectFromEndpoint(client, endpoint_id);
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::UpdateAdvertisingOptionsV3(
|
||||
ClientProxy* client, absl::string_view service_id,
|
||||
const AdvertisingOptions& options, const ResultCallback& callback) {
|
||||
const AdvertisingOptions& options, ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-update-advertising-options",
|
||||
[this, client, options, callback, service_id]() {
|
||||
callback.result_cb(GetServiceController()->UpdateAdvertisingOptions(
|
||||
[this, client, options, callback = std::move(callback),
|
||||
service_id]() mutable {
|
||||
callback(GetServiceController()->UpdateAdvertisingOptions(
|
||||
client, service_id, options));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::UpdateDiscoveryOptionsV3(
|
||||
ClientProxy* client, absl::string_view service_id,
|
||||
const DiscoveryOptions& options, const ResultCallback& callback) {
|
||||
const DiscoveryOptions& options, ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-update-discovery-options",
|
||||
[this, client, options, callback, service_id]() {
|
||||
callback.result_cb(GetServiceController()->UpdateDiscoveryOptions(
|
||||
[this, client, options, callback = std::move(callback),
|
||||
service_id]() mutable {
|
||||
callback(GetServiceController()->UpdateDiscoveryOptions(
|
||||
client, service_id, options));
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::StopAllEndpoints(ClientProxy* client,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
// Client can emit the cancellation at anytime, we need to execute the request
|
||||
// without further posting it.
|
||||
client->CancelAllEndpoints();
|
||||
|
||||
RouteToServiceController(
|
||||
"scr-stop-all-endpoints", [this, client, callback]() {
|
||||
"scr-stop-all-endpoints",
|
||||
[this, client, callback = std::move(callback)]() mutable {
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " has requested us to stop all endpoints. We will "
|
||||
"now reset the client.";
|
||||
FinishClientSession(client);
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
void ServiceControllerRouter::SetCustomSavePath(
|
||||
ClientProxy* client, absl::string_view path,
|
||||
const ResultCallback& callback) {
|
||||
void ServiceControllerRouter::SetCustomSavePath(ClientProxy* client,
|
||||
absl::string_view path,
|
||||
ResultCallback callback) {
|
||||
RouteToServiceController(
|
||||
"scr-set-custom-save-path",
|
||||
[this, client, path = std::string(path), callback]() {
|
||||
"scr-set-custom-save-path", [this, client, path = std::string(path),
|
||||
callback = std::move(callback)]() mutable {
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " has requested us to set custom save path to "
|
||||
<< path;
|
||||
GetServiceController()->SetCustomSavePath(client, path);
|
||||
callback.result_cb({Status::kSuccess});
|
||||
callback({Status::kSuccess});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -70,52 +70,50 @@ class ServiceControllerRouter {
|
||||
absl::string_view service_id,
|
||||
const AdvertisingOptions& advertising_options,
|
||||
const ConnectionRequestInfo& info,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void StopAdvertising(ClientProxy* client,
|
||||
const ResultCallback& callback);
|
||||
virtual void StopAdvertising(ClientProxy* client, ResultCallback callback);
|
||||
|
||||
virtual void StartDiscovery(ClientProxy* client, absl::string_view service_id,
|
||||
const DiscoveryOptions& discovery_options,
|
||||
const DiscoveryListener& listener,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void StopDiscovery(ClientProxy* client,
|
||||
const ResultCallback& callback);
|
||||
virtual void StopDiscovery(ClientProxy* client, ResultCallback callback);
|
||||
|
||||
virtual void InjectEndpoint(ClientProxy* client, absl::string_view service_id,
|
||||
const OutOfBandConnectionMetadata& metadata,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void RequestConnection(ClientProxy* client,
|
||||
absl::string_view endpoint_id,
|
||||
const ConnectionRequestInfo& info,
|
||||
const ConnectionOptions& connection_options,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void AcceptConnection(ClientProxy* client,
|
||||
absl::string_view endpoint_id,
|
||||
PayloadListener listener,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void RejectConnection(ClientProxy* client,
|
||||
absl::string_view endpoint_id,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void InitiateBandwidthUpgrade(ClientProxy* client,
|
||||
absl::string_view endpoint_id,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void SendPayload(ClientProxy* client,
|
||||
absl::Span<const std::string> endpoint_ids,
|
||||
Payload payload, const ResultCallback& callback);
|
||||
Payload payload, ResultCallback callback);
|
||||
|
||||
virtual void CancelPayload(ClientProxy* client, std::uint64_t payload_id,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void DisconnectFromEndpoint(ClientProxy* client,
|
||||
absl::string_view endpoint_id,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
////////////////////////////// V3 ////////////////////////////////////////////
|
||||
virtual void StartListeningForIncomingConnectionsV3(
|
||||
@@ -130,50 +128,47 @@ class ServiceControllerRouter {
|
||||
const NearbyDevice& remote_device,
|
||||
v3::ConnectionRequestInfo info,
|
||||
const ConnectionOptions& connection_options,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void AcceptConnectionV3(ClientProxy* client,
|
||||
const NearbyDevice& remote_device,
|
||||
v3::PayloadListener listener,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void RejectConnectionV3(ClientProxy* client,
|
||||
const NearbyDevice& remote_device,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void InitiateBandwidthUpgradeV3(ClientProxy* client,
|
||||
const NearbyDevice& remote_device,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void SendPayloadV3(ClientProxy* client,
|
||||
const NearbyDevice& recipient_device,
|
||||
Payload payload, const ResultCallback& callback);
|
||||
Payload payload, ResultCallback callback);
|
||||
|
||||
virtual void CancelPayloadV3(ClientProxy* client,
|
||||
const NearbyDevice& recipient_device,
|
||||
std::uint64_t payload_id,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void DisconnectFromDeviceV3(ClientProxy* client,
|
||||
const NearbyDevice& remote_device,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
virtual void UpdateAdvertisingOptionsV3(
|
||||
ClientProxy* client, absl::string_view service_id,
|
||||
const AdvertisingOptions& advertising_options,
|
||||
const ResultCallback& callback);
|
||||
const AdvertisingOptions& advertising_options, ResultCallback callback);
|
||||
|
||||
virtual void UpdateDiscoveryOptionsV3(
|
||||
ClientProxy* client, absl::string_view service_id,
|
||||
const DiscoveryOptions& discovery_options,
|
||||
const ResultCallback& callback);
|
||||
const DiscoveryOptions& discovery_options, ResultCallback callback);
|
||||
/////////////////////////////// END V3 ///////////////////////////////////////
|
||||
|
||||
virtual void StopAllEndpoints(ClientProxy* client,
|
||||
const ResultCallback& callback);
|
||||
virtual void StopAllEndpoints(ClientProxy* client, ResultCallback callback);
|
||||
|
||||
virtual void SetCustomSavePath(ClientProxy* client, absl::string_view path,
|
||||
const ResultCallback& callback);
|
||||
ResultCallback callback);
|
||||
|
||||
void SetServiceControllerForTesting(
|
||||
std::unique_ptr<ServiceController> service_controller);
|
||||
|
||||
@@ -81,7 +81,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.StartAdvertising(client, service_id, advertising_options, info,
|
||||
callback);
|
||||
std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -95,7 +95,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.StopAdvertising(client, callback);
|
||||
router_.StopAdvertising(client, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
}
|
||||
client->StoppedAdvertising();
|
||||
@@ -105,14 +105,14 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
void StartDiscovery(ClientProxy* client, std::string service_id,
|
||||
DiscoveryOptions discovery_options,
|
||||
const DiscoveryListener& listener,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
EXPECT_CALL(*mock_, StartDiscovery)
|
||||
.WillOnce(Return(Status{Status::kSuccess}));
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.StartDiscovery(client, kServiceId, discovery_options, listener,
|
||||
callback);
|
||||
std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -126,7 +126,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.StopDiscovery(client, callback);
|
||||
router_.StopDiscovery(client, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
}
|
||||
client->StoppedDiscovery();
|
||||
@@ -140,7 +140,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.InjectEndpoint(client, service_id, metadata, callback);
|
||||
router_.InjectEndpoint(client, service_id, metadata, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
}
|
||||
}
|
||||
@@ -155,7 +155,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.RequestConnection(client, endpoint_id, request_info,
|
||||
connection_options, callback);
|
||||
connection_options, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -173,7 +173,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
}
|
||||
|
||||
void AcceptConnection(ClientProxy* client, const std::string endpoint_id,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
EXPECT_CALL(*mock_, AcceptConnection)
|
||||
.WillOnce(Return(Status{Status::kSuccess}));
|
||||
// Pre-condition for successful Accept is: connection must exist.
|
||||
@@ -181,8 +181,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.AcceptConnection(client, endpoint_id, {},
|
||||
callback);
|
||||
router_.AcceptConnection(client, endpoint_id, {}, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -203,7 +202,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.RejectConnection(client, endpoint_id, callback);
|
||||
router_.RejectConnection(client, endpoint_id, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -219,7 +218,8 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.InitiateBandwidthUpgrade(client, endpoint_id, callback);
|
||||
router_.InitiateBandwidthUpgrade(client, endpoint_id,
|
||||
std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -239,7 +239,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.SendPayload(client, absl::MakeSpan(endpoint_ids),
|
||||
std::move(payload), callback);
|
||||
std::move(payload), std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -252,7 +252,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.CancelPayload(client, payload_id, callback);
|
||||
router_.CancelPayload(client, payload_id, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -266,7 +266,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.DisconnectFromEndpoint(client, endpoint_id, callback);
|
||||
router_.DisconnectFromEndpoint(client, endpoint_id, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
}
|
||||
client->OnDisconnected(endpoint_id, false);
|
||||
@@ -306,7 +306,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
complete_ = false;
|
||||
router_.RequestConnectionV3(client, kRemoteDevice,
|
||||
std::move(request_info), connection_options,
|
||||
callback);
|
||||
std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
if (check_result) {
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
@@ -330,7 +330,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
|
||||
void AcceptConnectionV3(ClientProxy* client,
|
||||
const NearbyDevice& kRemoteDevice,
|
||||
const ResultCallback& callback) {
|
||||
ResultCallback callback) {
|
||||
EXPECT_CALL(*mock_, AcceptConnection)
|
||||
.WillOnce(Return(Status{Status::kSuccess}));
|
||||
// Pre-condition for successful Accept is: connection must exist.
|
||||
@@ -339,7 +339,8 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.AcceptConnectionV3(client, kRemoteDevice, {}, callback);
|
||||
router_.AcceptConnectionV3(client, kRemoteDevice, {},
|
||||
std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -360,7 +361,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.RejectConnectionV3(client, device, callback);
|
||||
router_.RejectConnectionV3(client, device, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -376,7 +377,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.InitiateBandwidthUpgradeV3(client, device, callback);
|
||||
router_.InitiateBandwidthUpgradeV3(client, device, std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -393,7 +394,7 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.SendPayloadV3(client, recipient_device, std::move(payload),
|
||||
callback);
|
||||
std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
EXPECT_EQ(result_, Status{Status::kSuccess});
|
||||
}
|
||||
@@ -401,13 +402,14 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
|
||||
void CancelPayloadV3(ClientProxy* client,
|
||||
const NearbyDevice& recipient_device,
|
||||
uint64_t payload_id, const ResultCallback& callback) {
|
||||
uint64_t payload_id, ResultCallback callback) {
|
||||
EXPECT_CALL(*mock_, CancelPayload).Times(1);
|
||||
EXPECT_TRUE(
|
||||
client->IsConnectedToEndpoint(recipient_device.GetEndpointId()));
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
router_.CancelPayloadV3(client, recipient_device, payload_id, callback);
|
||||
router_.CancelPayloadV3(client, recipient_device, payload_id,
|
||||
std::move(callback));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,7 +421,8 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
complete_ = false;
|
||||
router_.DisconnectFromDeviceV3(client, kRemoteDevice, callback);
|
||||
router_.DisconnectFromDeviceV3(client, kRemoteDevice,
|
||||
std::move(callback));
|
||||
while (!complete_) cond_.Wait();
|
||||
}
|
||||
client->OnDisconnected(kRemoteDevice.GetEndpointId(), false);
|
||||
@@ -455,15 +458,6 @@ class ServiceControllerRouterTest : public testing::Test {
|
||||
}
|
||||
|
||||
protected:
|
||||
const ResultCallback kCallback{
|
||||
.result_cb =
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
};
|
||||
const std::string kServiceId = "service id";
|
||||
const std::string kRequestorName = "requestor name";
|
||||
const std::string kRemoteEndpointId = "remote endpoint id";
|
||||
@@ -542,122 +536,293 @@ TEST_F(ServiceControllerRouterTest, QualityConversionWorks) {
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, StartAdvertisingCalled) {
|
||||
StartAdvertising(&client_, kServiceId, kAdvertisingOptions,
|
||||
kConnectionRequestInfo, kCallback);
|
||||
kConnectionRequestInfo, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, StopAdvertisingCalled) {
|
||||
StartAdvertising(&client_, kServiceId, kAdvertisingOptions,
|
||||
kConnectionRequestInfo, kCallback);
|
||||
StopAdvertising(&client_, kCallback);
|
||||
kConnectionRequestInfo, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
StopAdvertising(&client_, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, StartDiscoveryCalled) {
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, StopDiscoveryCalled) {
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
StopDiscovery(&client_, kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
StopDiscovery(&client_, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, InjectEndpointCalled) {
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
InjectEndpoint(&client_, kServiceId, kOutOfBandConnectionMetadata, kCallback);
|
||||
StopDiscovery(&client_, kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
InjectEndpoint(&client_, kServiceId, kOutOfBandConnectionMetadata,
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
StopDiscovery(&client_, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, RequestConnectionCalled) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, AcceptConnectionCalled) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now, we can accept connection.
|
||||
AcceptConnection(&client_, kRemoteEndpointId, kCallback);
|
||||
AcceptConnection(&client_, kRemoteEndpointId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, RejectConnectionCalled) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now, we can reject connection.
|
||||
RejectConnection(&client_, kRemoteEndpointId, kCallback);
|
||||
RejectConnection(&client_, kRemoteEndpointId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, InitiateBandwidthUpgradeCalled) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now, we can accept connection.
|
||||
AcceptConnection(&client_, kRemoteEndpointId, kCallback);
|
||||
AcceptConnection(&client_, kRemoteEndpointId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now we can change connection bandwidth.
|
||||
InitiateBandwidthUpgrade(&client_, kRemoteEndpointId, kCallback);
|
||||
InitiateBandwidthUpgrade(&client_, kRemoteEndpointId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, SendPayloadCalled) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now, we can accept connection.
|
||||
AcceptConnection(&client_, kRemoteEndpointId, kCallback);
|
||||
AcceptConnection(&client_, kRemoteEndpointId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now we can send payload.
|
||||
SendPayload(&client_, std::vector<std::string>{kRemoteEndpointId},
|
||||
Payload{ByteArray("data")}, kCallback);
|
||||
Payload{ByteArray("data")}, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, CancelPayloadCalled) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now, we can accept connection.
|
||||
AcceptConnection(&client_, kRemoteEndpointId, kCallback);
|
||||
AcceptConnection(&client_, kRemoteEndpointId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// We have to know payload id, before we can cancel payload transfer.
|
||||
// It is either after a call to SendPayload, or after receiving
|
||||
// PayloadProgress callback. Let's assume we have it, and proceed.
|
||||
CancelPayload(&client_, kPayloadId, kCallback);
|
||||
CancelPayload(&client_, kPayloadId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, DisconnectFromEndpointCalled) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now, we can accept connection.
|
||||
AcceptConnection(&client_, kRemoteEndpointId, kCallback);
|
||||
AcceptConnection(&client_, kRemoteEndpointId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// We can disconnect at any time after RequestConnection.
|
||||
DisconnectFromEndpoint(&client_, kRemoteEndpointId, kCallback);
|
||||
DisconnectFromEndpoint(&client_, kRemoteEndpointId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, RequestConnectionCalledV3) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device =
|
||||
v3::ConnectionsDevice(client_.GetLocalEndpointId(), kRequestorName, {});
|
||||
@@ -690,7 +855,13 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionCalledV3) {
|
||||
bandwidth_changed_latch.CountDown();
|
||||
}},
|
||||
},
|
||||
kCallback, true);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
true);
|
||||
EXPECT_TRUE(initiated_latch.Await().Ok());
|
||||
EXPECT_TRUE(result_latch.Await().Ok());
|
||||
EXPECT_TRUE(disconnected_latch.Await().Ok());
|
||||
@@ -700,7 +871,12 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionCalledV3) {
|
||||
TEST_F(ServiceControllerRouterTest, RequestConnectionV3FakeDevice) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device = FakeNearbyDevice();
|
||||
// Testing callback wrapping as well.
|
||||
@@ -732,7 +908,13 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionV3FakeDevice) {
|
||||
bandwidth_changed_latch.CountDown();
|
||||
}},
|
||||
},
|
||||
kCallback, true, true, false);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
true, true, false);
|
||||
EXPECT_TRUE(initiated_latch.Await().Ok());
|
||||
EXPECT_TRUE(result_latch.Await().Ok());
|
||||
EXPECT_TRUE(disconnected_latch.Await().Ok());
|
||||
@@ -742,22 +924,41 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionV3FakeDevice) {
|
||||
TEST_F(ServiceControllerRouterTest, RequestConnectionV3TwiceFails) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device =
|
||||
v3::ConnectionsDevice(client_.GetLocalEndpointId(), kRequestorName, {});
|
||||
RequestConnectionV3(&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
kCallback, false);
|
||||
RequestConnectionV3(&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
kCallback, false, false);
|
||||
RequestConnectionV3(
|
||||
&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
false);
|
||||
RequestConnectionV3(
|
||||
&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
false, false);
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
EXPECT_EQ(result_.value, Status::kAlreadyConnectedToEndpoint);
|
||||
@@ -767,113 +968,236 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionV3TwiceFails) {
|
||||
TEST_F(ServiceControllerRouterTest, AcceptConnectionCalledV3) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device =
|
||||
v3::ConnectionsDevice(client_.GetLocalEndpointId(), kRequestorName, {});
|
||||
RequestConnectionV3(&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
kCallback, false);
|
||||
RequestConnectionV3(
|
||||
&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
false);
|
||||
// Now, we can accept connection.
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, kCallback);
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, RejectConnectionCalledV3) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device =
|
||||
v3::ConnectionsDevice(client_.GetLocalEndpointId(), kRequestorName, {});
|
||||
RequestConnectionV3(&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
kCallback, false);
|
||||
RequestConnectionV3(
|
||||
&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
false);
|
||||
// Now, we can reject connection.
|
||||
RejectConnectionV3(&client_, kRemoteDevice, kCallback);
|
||||
RejectConnectionV3(&client_, kRemoteDevice, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, InitiateBandwidthUpgradeCalledV3) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device =
|
||||
v3::ConnectionsDevice(client_.GetLocalEndpointId(), kRequestorName, {});
|
||||
RequestConnectionV3(&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
kCallback, false);
|
||||
RequestConnectionV3(
|
||||
&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
false);
|
||||
// Now, we can accept connection.
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, kCallback);
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now we can change connection bandwidth.
|
||||
InitiateBandwidthUpgradeV3(&client_, kRemoteDevice, kCallback);
|
||||
InitiateBandwidthUpgradeV3(&client_, kRemoteDevice, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, SendPayloadCalledV3) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device =
|
||||
v3::ConnectionsDevice(client_.GetLocalEndpointId(), kRequestorName, {});
|
||||
RequestConnectionV3(&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
kCallback, false);
|
||||
RequestConnectionV3(
|
||||
&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
false);
|
||||
// Now, we can accept connection.
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, kCallback);
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Now we can send payload.
|
||||
SendPayloadV3(&client_, kRemoteDevice, Payload{ByteArray("data")}, kCallback);
|
||||
SendPayloadV3(&client_, kRemoteDevice, Payload{ByteArray("data")},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, DisconnectFromDeviceCalledV3) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device =
|
||||
v3::ConnectionsDevice(client_.GetLocalEndpointId(), kRequestorName, {});
|
||||
RequestConnectionV3(&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
kCallback, false);
|
||||
RequestConnectionV3(
|
||||
&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
false);
|
||||
// Now, we can accept connection.
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, kCallback);
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// We can disconnect at any time after RequestConnection.
|
||||
DisconnectFromDeviceV3(&client_, kRemoteDevice, kCallback);
|
||||
DisconnectFromDeviceV3(&client_, kRemoteDevice, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest, CancelPayloadV3Called) {
|
||||
// Either Advertising, or Discovery should be ongoing.
|
||||
StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_,
|
||||
kCallback);
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// Establish connection.
|
||||
auto local_device =
|
||||
v3::ConnectionsDevice(client_.GetLocalEndpointId(), kRequestorName, {});
|
||||
RequestConnectionV3(&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
kCallback, false);
|
||||
RequestConnectionV3(
|
||||
&client_, kRemoteDevice,
|
||||
v3::ConnectionRequestInfo{
|
||||
.local_device = local_device,
|
||||
.listener = {},
|
||||
},
|
||||
[this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
},
|
||||
false);
|
||||
// Now, we can accept connection.
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, kCallback);
|
||||
AcceptConnectionV3(&client_, kRemoteDevice, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
// We have to know payload id, before we can cancel payload transfer.
|
||||
// It is either after a call to SendPayload, or after receiving
|
||||
// PayloadProgress callback. Let's assume we have it, and proceed.
|
||||
CancelPayloadV3(&client_, kRemoteDevice, kPayloadId, kCallback);
|
||||
CancelPayloadV3(&client_, kRemoteDevice, kPayloadId, [this](Status status) {
|
||||
MutexLock lock(&mutex_);
|
||||
result_ = status;
|
||||
complete_ = true;
|
||||
cond_.Notify();
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ServiceControllerRouterTest,
|
||||
|
||||
@@ -42,12 +42,9 @@ namespace connections {
|
||||
// This is not the same as completion of the associated process,
|
||||
// which may have many states, and multiple async jobs, and be still ongoing.
|
||||
// Progress on the overall process is reported by the associated listener.
|
||||
struct ResultCallback {
|
||||
// Callback to access the status of the operation when available.
|
||||
// status - result of job execution;
|
||||
// Status::kSuccess, if successful; anything else indicates failure.
|
||||
std::function<void(Status)> result_cb = [](Status) {};
|
||||
};
|
||||
// status - result of job execution;
|
||||
// Status::kSuccess, if successful; anything else indicates failure.
|
||||
using ResultCallback = absl::AnyInvocable<void(Status)>;
|
||||
|
||||
struct ConnectionResponseInfo {
|
||||
std::string GetAuthenticationDigits() {
|
||||
|
||||
@@ -162,27 +162,26 @@ GNCStatus GNCStatusFromCppStatus(Status status) {
|
||||
ByteArray((const char *)endpointInfo.bytes, endpointInfo.length);
|
||||
connection_request_info.listener = std::move(listener);
|
||||
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->StartAdvertising(service_id, advertising_options, connection_request_info, result);
|
||||
_core->StartAdvertising(service_id, advertising_options, connection_request_info,
|
||||
std::move(result));
|
||||
}
|
||||
|
||||
- (void)stopAdvertisingWithCompletionHandler:(void (^)(NSError *error))completionHandler {
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->StopAdvertising(result);
|
||||
_core->StopAdvertising(std::move(result));
|
||||
}
|
||||
|
||||
- (void)startDiscoveryAsService:(NSString *)serviceID
|
||||
@@ -205,27 +204,25 @@ GNCStatus GNCStatusFromCppStatus(Status status) {
|
||||
[delegate lostEndpoint:endpointID];
|
||||
};
|
||||
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->StartDiscovery(service_id, discovery_options, std::move(listener), result);
|
||||
_core->StartDiscovery(service_id, discovery_options, std::move(listener), std::move(result));
|
||||
}
|
||||
|
||||
- (void)stopDiscoveryWithCompletionHandler:(void (^)(NSError *error))completionHandler {
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->StopDiscovery(result);
|
||||
_core->StopDiscovery(std::move(result));
|
||||
}
|
||||
|
||||
- (void)requestConnectionToEndpoint:(NSString *)endpointID
|
||||
@@ -265,15 +262,15 @@ GNCStatus GNCStatusFromCppStatus(Status status) {
|
||||
|
||||
ConnectionOptions connection_options = [connectionOptions toCpp];
|
||||
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->RequestConnection(endpoint_id, connection_request_info, connection_options, result);
|
||||
_core->RequestConnection(endpoint_id, connection_request_info, connection_options,
|
||||
std::move(result));
|
||||
}
|
||||
|
||||
- (void)acceptConnectionRequestFromEndpoint:(NSString *)endpointID
|
||||
@@ -312,30 +309,28 @@ GNCStatus GNCStatusFromCppStatus(Status status) {
|
||||
totalBytes:info.total_bytes];
|
||||
};
|
||||
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->AcceptConnection(endpoint_id, std::move(listener), result);
|
||||
_core->AcceptConnection(endpoint_id, std::move(listener), std::move(result));
|
||||
}
|
||||
|
||||
- (void)rejectConnectionRequestFromEndpoint:(NSString *)endpointID
|
||||
withCompletionHandler:(void (^)(NSError *error))completionHandler {
|
||||
std::string endpoint_id = [endpointID cStringUsingEncoding:[NSString defaultCStringEncoding]];
|
||||
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->RejectConnection(endpoint_id, result);
|
||||
_core->RejectConnection(endpoint_id, std::move(result));
|
||||
}
|
||||
|
||||
- (void)sendPayload:(GNCPayload *)payload
|
||||
@@ -348,69 +343,64 @@ GNCStatus GNCStatusFromCppStatus(Status status) {
|
||||
endpoint_ids.push_back(endpoint_id);
|
||||
}
|
||||
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->SendPayload(endpoint_ids, [payload toCpp], result);
|
||||
_core->SendPayload(endpoint_ids, [payload toCpp], std::move(result));
|
||||
}
|
||||
|
||||
- (void)cancelPayload:(int64_t)payloadID
|
||||
withCompletionHandler:(void (^)(NSError *error))completionHandler {
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->CancelPayload(payloadID, result);
|
||||
_core->CancelPayload(payloadID, std::move(result));
|
||||
}
|
||||
|
||||
- (void)disconnectFromEndpoint:(NSString *)endpointID
|
||||
withCompletionHandler:(void (^)(NSError *error))completionHandler {
|
||||
std::string endpoint_id = [endpointID cStringUsingEncoding:[NSString defaultCStringEncoding]];
|
||||
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->DisconnectFromEndpoint(endpoint_id, result);
|
||||
_core->DisconnectFromEndpoint(endpoint_id, std::move(result));
|
||||
}
|
||||
|
||||
- (void)stopAllEndpointsWithCompletionHandler:(void (^)(NSError *error))completionHandler {
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
_core->StopAllEndpoints(result);
|
||||
_core->StopAllEndpoints(std::move(result));
|
||||
}
|
||||
|
||||
- (void)initiateBandwidthUpgrade:(NSString *)endpointID
|
||||
withCompletionHandler:(void (^)(NSError *error))completionHandler {
|
||||
std::string endpoint_id = [endpointID cStringUsingEncoding:[NSString defaultCStringEncoding]];
|
||||
|
||||
ResultListener result;
|
||||
result.result_cb = ^(Status status) {
|
||||
ResultListener result = [completionHandler](Status status) {
|
||||
NSError *err = NSErrorFromCppStatus(status);
|
||||
if (completionHandler) {
|
||||
completionHandler(err);
|
||||
}
|
||||
};
|
||||
|
||||
_core->InitiateBandwidthUpgrade(endpoint_id, result);
|
||||
_core->InitiateBandwidthUpgrade(endpoint_id, std::move(result));
|
||||
}
|
||||
|
||||
- (NSString *)localEndpointID {
|
||||
|
||||
Reference in New Issue
Block a user