Internal change

PiperOrigin-RevId: 377900845
This commit is contained in:
Edwin Wu
2021-06-07 06:31:26 -07:00
committed by Copybara-Service
parent 957ce1052b
commit af4701c237
5 changed files with 269 additions and 196 deletions
+122 -119
View File
@@ -47,8 +47,14 @@ const std::size_t kEndpointIdLength = 4u;
const std::size_t kMaxEndpointInfoLength = 131u;
} // namespace
ServiceControllerRouter::ServiceControllerRouter(
std::function<ServiceController*()> factory)
: service_controller_factory_(std::move(factory)) {
NEARBY_LOGS(INFO) << "ServiceControllerRouter going up.";
}
ServiceControllerRouter::~ServiceControllerRouter() {
NEARBY_LOG(INFO, "ServiceControllerRouter going down.");
NEARBY_LOGS(INFO) << "ServiceControllerRouter going down.";
if (service_controller_) {
service_controller_->Stop();
@@ -98,26 +104,26 @@ void ServiceControllerRouter::StartDiscovery(ClientProxy* client,
const ConnectionOptions& options,
const DiscoveryListener& listener,
const ResultCallback& callback) {
RouteToServiceController("scr-start-discovery", [this, client,
service_id =
std::string(service_id),
options, listener,
callback]() {
Status status = AcquireServiceControllerForClient(client, options.strategy);
if (!status.Ok()) {
callback.result_cb(status);
return;
}
RouteToServiceController(
"scr-start-discovery",
[this, client, service_id = std::string(service_id), options, listener,
callback]() {
Status status =
AcquireServiceControllerForClient(client, options.strategy);
if (!status.Ok()) {
callback.result_cb(status);
return;
}
if (client->IsDiscovering()) {
callback.result_cb({Status::kAlreadyDiscovering});
return;
}
if (client->IsDiscovering()) {
callback.result_cb({Status::kAlreadyDiscovering});
return;
}
status = service_controller_->StartDiscovery(client, service_id, options,
listener);
callback.result_cb(status);
});
status = service_controller_->StartDiscovery(client, service_id,
options, listener);
callback.result_cb(status);
});
}
void ServiceControllerRouter::StopDiscovery(ClientProxy* client,
@@ -134,37 +140,38 @@ void ServiceControllerRouter::InjectEndpoint(
ClientProxy* client, absl::string_view service_id,
const OutOfBandConnectionMetadata& metadata,
const ResultCallback& callback) {
RouteToServiceController("scr-inject-endpoint", [this, client,
service_id =
std::string(service_id),
metadata, callback]() {
// 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});
return;
}
RouteToServiceController(
"scr-inject-endpoint",
[this, client, service_id = std::string(service_id), metadata,
callback]() {
// 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});
return;
}
if (metadata.endpoint_id.size() != kEndpointIdLength) {
callback.result_cb({Status::kError});
return;
}
if (metadata.endpoint_id.size() != kEndpointIdLength) {
callback.result_cb({Status::kError});
return;
}
if (metadata.endpoint_info.Empty() ||
metadata.endpoint_info.size() > kMaxEndpointInfoLength) {
callback.result_cb({Status::kError});
return;
}
if (metadata.endpoint_info.Empty() ||
metadata.endpoint_info.size() > kMaxEndpointInfoLength) {
callback.result_cb({Status::kError});
return;
}
if (!ClientHasAcquiredServiceController(client) ||
!client->IsDiscovering()) {
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
if (!ClientHasAcquiredServiceController(client) ||
!client->IsDiscovering()) {
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
service_controller_->InjectEndpoint(client, service_id, metadata);
callback.result_cb({Status::kSuccess});
});
service_controller_->InjectEndpoint(client, service_id, metadata);
callback.result_cb({Status::kSuccess});
});
}
void ServiceControllerRouter::RequestConnection(
@@ -203,32 +210,33 @@ void ServiceControllerRouter::AcceptConnection(ClientProxy* client,
absl::string_view endpoint_id,
const PayloadListener& listener,
const ResultCallback& callback) {
RouteToServiceController("scr-accept-connection", [this, client,
endpoint_id = std::string(
endpoint_id),
listener, callback]() {
if (!ClientHasAcquiredServiceController(client)) {
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
RouteToServiceController(
"scr-accept-connection",
[this, client, endpoint_id = std::string(endpoint_id), listener,
callback]() {
if (!ClientHasAcquiredServiceController(client)) {
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
if (client->IsConnectedToEndpoint(endpoint_id)) {
callback.result_cb({Status::kAlreadyConnectedToEndpoint});
return;
}
if (client->IsConnectedToEndpoint(endpoint_id)) {
callback.result_cb({Status::kAlreadyConnectedToEndpoint});
return;
}
if (client->HasLocalEndpointResponded(endpoint_id)) {
NEARBY_LOG(INFO,
"[ServiceControllerRouter:Accept]: Client has local "
"endpoint responded; id=%s",
endpoint_id.c_str());
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
if (client->HasLocalEndpointResponded(endpoint_id)) {
NEARBY_LOGS(WARNING)
<< "Client " << client->GetClientId()
<< " invoked acceptConnectionRequest() after having already "
"accepted/rejected the connection to endpoint(id="
<< endpoint_id << ")";
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
callback.result_cb(
service_controller_->AcceptConnection(client, endpoint_id, listener));
});
callback.result_cb(service_controller_->AcceptConnection(
client, endpoint_id, listener));
});
}
void ServiceControllerRouter::RejectConnection(ClientProxy* client,
@@ -250,10 +258,11 @@ void ServiceControllerRouter::RejectConnection(ClientProxy* client,
}
if (client->HasLocalEndpointResponded(endpoint_id)) {
NEARBY_LOG(INFO,
"[ServiceControllerRouter:Reject]: Client has local "
"endpoint responded; id=%s",
endpoint_id.c_str());
NEARBY_LOGS(WARNING)
<< "Client " << client->GetClientId()
<< " invoked rejectConnectionRequest() after having already "
"accepted/rejected the connection to endpoint(id="
<< endpoint_id << ")";
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
@@ -296,28 +305,27 @@ void ServiceControllerRouter::SendPayload(
const std::vector<std::string> endpoints =
std::vector<std::string>(endpoint_ids.begin(), endpoint_ids.end());
RouteToServiceController(
"scr-send-payload",
[this, client, shared_payload, endpoints, callback]() {
if (!ClientHasAcquiredServiceController(client)) {
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
RouteToServiceController("scr-send-payload", [this, client, shared_payload,
endpoints, callback]() {
if (!ClientHasAcquiredServiceController(client)) {
callback.result_cb({Status::kOutOfOrderApiCall});
return;
}
if (!ClientHasConnectionToAtLeastOneEndpoint(client, endpoints)) {
callback.result_cb({Status::kEndpointUnknown});
return;
}
if (!ClientHasConnectionToAtLeastOneEndpoint(client, endpoints)) {
callback.result_cb({Status::kEndpointUnknown});
return;
}
service_controller_->SendPayload(client, endpoints,
std::move(*shared_payload));
service_controller_->SendPayload(client, endpoints,
std::move(*shared_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.result_cb({Status::kSuccess});
});
}
void ServiceControllerRouter::CancelPayload(ClientProxy* client,
@@ -362,16 +370,16 @@ void ServiceControllerRouter::StopAllEndpoints(ClientProxy* client,
// without further posting it.
client->CancelAllEndpoints();
RouteToServiceController("scr-stop-all-endpoints", [this, client,
callback]() {
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
<< " has requested us to stop all endpoints. We will now "
"reset the client.";
if (ClientHasAcquiredServiceController(client)) {
DoneWithStrategySessionForClient(client);
}
callback.result_cb({Status::kSuccess});
});
RouteToServiceController(
"scr-stop-all-endpoints", [this, client, callback]() {
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
<< " has requested us to stop all endpoints. We will "
"now reset the client.";
if (ClientHasAcquiredServiceController(client)) {
DoneWithStrategySessionForClient(client);
}
callback.result_cb({Status::kSuccess});
});
}
void ServiceControllerRouter::ClientDisconnecting(
@@ -380,16 +388,15 @@ void ServiceControllerRouter::ClientDisconnecting(
// without further posting it.
client->CancelAllEndpoints();
RouteToServiceController("scr-client-disconnecting", [this, client,
callback]() {
if (ClientHasAcquiredServiceController(client)) {
DoneWithStrategySessionForClient(client);
NEARBY_LOG(INFO,
"[ServiceControllerRouter:Disconnect]: Client has completed "
"the client's connection");
}
callback.result_cb({Status::kSuccess});
});
RouteToServiceController(
"scr-client-disconnecting", [this, client, callback]() {
if (ClientHasAcquiredServiceController(client)) {
DoneWithStrategySessionForClient(client);
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
<< " has completed the client's connection.";
}
callback.result_cb({Status::kSuccess});
});
}
Status ServiceControllerRouter::AcquireServiceControllerForClient(
@@ -419,18 +426,14 @@ Status ServiceControllerRouter::AcquireServiceControllerForClient(
bool is_the_only_client_of_service_controller =
clients_.size() == 1 && ClientHasAcquiredServiceController(client);
if (!is_the_only_client_of_service_controller) {
NEARBY_LOG(INFO,
"[ServiceControllerRouter:AcquireServiceControllerForClient]: "
"Client has already active strategy.");
NEARBY_LOGS(INFO) << "Client has already active strategy.";
return {Status::kAlreadyHaveActiveStrategy};
}
// If the client still has connected endpoints, they must disconnect before
// they can switch.
if (!client->GetConnectedEndpoints().empty()) {
NEARBY_LOG(INFO,
"[ServiceControllerRouter:AcquireServiceControllerForClient]: "
"Client has connected endpoints.");
NEARBY_LOGS(INFO) << "Client has connected endpoints.";
return {Status::kOutOfOrderApiCall};
}
@@ -497,7 +500,7 @@ bool ServiceControllerRouter::ClientHasConnectionToAtLeastOneEndpoint(
Status ServiceControllerRouter::UpdateCurrentServiceControllerAndStrategy(
Strategy strategy) {
if (!strategy.IsValid()) {
NEARBY_LOG(INFO, "Strategy is not valid.");
NEARBY_LOGS(INFO) << "Strategy is not valid.";
return {Status::kError};
}