mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Internal change
PiperOrigin-RevId: 372030567
This commit is contained in:
@@ -14,7 +14,6 @@
|
||||
|
||||
#include "core/internal/client_proxy.h"
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
@@ -48,10 +47,6 @@ std::int64_t ClientProxy::GetClientId() const { return client_id_; }
|
||||
std::string ClientProxy::GetLocalEndpointId() {
|
||||
if (local_endpoint_id_.empty()) {
|
||||
local_endpoint_id_ = GenerateLocalEndpointId();
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [Local Endpoint Generated]: client=%x" PRIx64
|
||||
"; endpoint_id=%s",
|
||||
GetClientId(), local_endpoint_id_.c_str());
|
||||
}
|
||||
return local_endpoint_id_;
|
||||
}
|
||||
@@ -60,10 +55,10 @@ std::string ClientProxy::GenerateLocalEndpointId() {
|
||||
if (high_vis_mode_) {
|
||||
if (!local_high_vis_mode_cache_endpoint_id_.empty()) {
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [Local Endpoint Re-using cached endpoint id]: "
|
||||
"client=%x" PRIx64
|
||||
"; local_high_vis_mode_cache_endpoint_id_=%s",
|
||||
GetClientId(), local_high_vis_mode_cache_endpoint_id_.c_str());
|
||||
"ClientProxy [Local Endpoint not Generated but return Cache]: "
|
||||
"client=%p; "
|
||||
"local_high_vis_mode_cache_endpoint_id_=%s",
|
||||
this, local_high_vis_mode_cache_endpoint_id_.c_str());
|
||||
return local_high_vis_mode_cache_endpoint_id_;
|
||||
}
|
||||
}
|
||||
@@ -73,6 +68,9 @@ std::string ClientProxy::GenerateLocalEndpointId() {
|
||||
// 4) Use only the first kEndpointIdLength bytes to make ID.
|
||||
ByteArray id_hash = Crypto::Sha256(absl::StrCat("client", prng_.NextInt64()));
|
||||
std::string id = Base64Utils::Encode(id_hash).substr(0, kEndpointIdLength);
|
||||
NEARBY_LOG(
|
||||
INFO, "ClientProxy [Local Endpoint Generated]: client=%p; endpoint_id=%s",
|
||||
this, id.c_str());
|
||||
|
||||
return id;
|
||||
}
|
||||
@@ -92,15 +90,15 @@ void ClientProxy::StartedAdvertising(
|
||||
absl::Span<proto::connections::Medium> mediums,
|
||||
const ConnectionOptions& advertising_options) {
|
||||
MutexLock lock(&mutex_);
|
||||
NEARBY_LOG(INFO, "ClientProxy [StartedAdvertising]: client=%x" PRIx64,
|
||||
GetClientId());
|
||||
NEARBY_LOG(INFO, "ClientProxy [StartedAdvertising]: client=%p; ", this);
|
||||
|
||||
if (high_vis_mode_) {
|
||||
local_high_vis_mode_cache_endpoint_id_ = local_endpoint_id_;
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [High Visibility Mode Adv, Cache EndpointId]: "
|
||||
"client=%x" PRIx64 "; local_high_vis_mode_cache_endpoint_id_=%s",
|
||||
GetClientId(), local_high_vis_mode_cache_endpoint_id_.c_str());
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"ClientProxy [High Visibility Mode Adv, Cache EndpointId]: client=%p; "
|
||||
"local_high_vis_mode_cache_endpoint_id_=%s",
|
||||
this, local_high_vis_mode_cache_endpoint_id_.c_str());
|
||||
CancelClearLocalHighVisModeCacheEndpointIdAlarm();
|
||||
}
|
||||
|
||||
@@ -110,8 +108,7 @@ void ClientProxy::StartedAdvertising(
|
||||
|
||||
void ClientProxy::StoppedAdvertising() {
|
||||
MutexLock lock(&mutex_);
|
||||
NEARBY_LOG(INFO, "ClientProxy [StoppedAdvertising]: client=%x" PRIx64,
|
||||
GetClientId());
|
||||
NEARBY_LOG(INFO, "ClientProxy [StoppedAdvertising]: client=%p; ", this);
|
||||
|
||||
if (IsAdvertising()) {
|
||||
advertising_info_.Clear();
|
||||
@@ -256,11 +253,10 @@ void ClientProxy::OnConnectionInitiated(const std::string& endpoint_id,
|
||||
// (can not use c++17 features, until chromium does) we unpack manually.
|
||||
auto& pair_iter = result.first;
|
||||
bool inserted = result.second;
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"ClientProxy [Connection Initiated]: add Connection: client=%x" PRIx64
|
||||
"; endpoint_id=%s; inserted=%d",
|
||||
GetClientId(), endpoint_id.c_str(), inserted);
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [Connection Initiated]: add Connection: client=%p, "
|
||||
"id=%s; inserted=%d",
|
||||
this, endpoint_id.c_str(), inserted);
|
||||
DCHECK(inserted);
|
||||
const Connection& item = pair_iter->second;
|
||||
// Notify the client.
|
||||
@@ -319,10 +315,6 @@ void ClientProxy::OnBandwidthChanged(const std::string& endpoint_id,
|
||||
const Connection* item = LookupConnection(endpoint_id);
|
||||
if (item != nullptr) {
|
||||
item->connection_listener.bandwidth_changed_cb(endpoint_id, new_medium);
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [reporting onBandwidthChanged]: client=%x" PRIx64
|
||||
"; endpoint_id=%s",
|
||||
GetClientId(), endpoint_id.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -581,11 +573,6 @@ void ClientProxy::OnPayload(const std::string& endpoint_id, Payload payload) {
|
||||
if (IsConnectedToEndpoint(endpoint_id)) {
|
||||
const Connection* item = LookupConnection(endpoint_id);
|
||||
if (item != nullptr) {
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [reporting onPayloadReceived]: "
|
||||
"client=%x" PRIx64 "; endpoint_id=%s; payload_id=%x" PRIx64,
|
||||
GetClientId(), endpoint_id.c_str(),
|
||||
static_cast<std::int64_t>(payload.GetId()));
|
||||
item->payload_listener.payload_cb(endpoint_id, std::move(payload));
|
||||
}
|
||||
}
|
||||
@@ -611,22 +598,6 @@ void ClientProxy::OnPayloadProgress(const std::string& endpoint_id,
|
||||
Connection* item = LookupConnection(endpoint_id);
|
||||
if (item != nullptr) {
|
||||
item->payload_listener.payload_progress_cb(endpoint_id, info);
|
||||
|
||||
if (info.status == PayloadProgressInfo::Status::kInProgress) {
|
||||
NEARBY_LOG(VERBOSE,
|
||||
"ClientProxy [reporting onPayloadProgress]: "
|
||||
"client=%x" PRIx64 "; endpoint_id=%s; payload_id=%x" PRIx64
|
||||
", payload_status=%d",
|
||||
GetClientId(), endpoint_id.c_str(), info.payload_id,
|
||||
info.status);
|
||||
} else {
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [reporting onPayloadProgress]: "
|
||||
"client=%x" PRIx64 "; endpoint_id=%s; payload_id=%x" PRIx64
|
||||
", payload_status=%d",
|
||||
GetClientId(), endpoint_id.c_str(), info.payload_id,
|
||||
info.status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -685,16 +656,14 @@ ConnectionOptions ClientProxy::GetDiscoveryOptions() const {
|
||||
|
||||
void ClientProxy::EnterHighVisibilityMode() {
|
||||
MutexLock lock(&mutex_);
|
||||
NEARBY_LOG(INFO, "ClientProxy [EnterHighVisibilityMode]: client=%x" PRIx64,
|
||||
GetClientId());
|
||||
NEARBY_LOG(INFO, "ClientProxy [EnterHighVisibilityMode]: client=%p; ", this);
|
||||
|
||||
high_vis_mode_ = true;
|
||||
}
|
||||
|
||||
void ClientProxy::ExitHighVisibilityMode() {
|
||||
MutexLock lock(&mutex_);
|
||||
NEARBY_LOG(INFO, "ClientProxy [ExitHighVisibilityMode]: client=%x" PRIx64,
|
||||
GetClientId());
|
||||
NEARBY_LOG(INFO, "ClientProxy [ExitHighVisibilityMode]: client=%p; ", this);
|
||||
|
||||
high_vis_mode_ = false;
|
||||
ScheduleClearLocalHighVisModeCacheEndpointIdAlarm();
|
||||
@@ -703,31 +672,19 @@ void ClientProxy::ExitHighVisibilityMode() {
|
||||
void ClientProxy::ScheduleClearLocalHighVisModeCacheEndpointIdAlarm() {
|
||||
CancelClearLocalHighVisModeCacheEndpointIdAlarm();
|
||||
|
||||
if (local_high_vis_mode_cache_endpoint_id_.empty()) {
|
||||
NEARBY_LOG(VERBOSE,
|
||||
"ClientProxy [There is no cached local high power advertising "
|
||||
"endpoint Id.]: client=%x" PRIx64,
|
||||
GetClientId());
|
||||
return;
|
||||
}
|
||||
if (local_high_vis_mode_cache_endpoint_id_.empty()) return;
|
||||
|
||||
// Schedule to clear cache high visibility mode advertisement endpoint id in
|
||||
// 30s.
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [High Visibility Mode Adv, Schedule to Clear Cache "
|
||||
"EndpointId]: client=%x" PRIx64
|
||||
"; local_high_vis_mode_cache_endpoint_id_=%s",
|
||||
GetClientId(), local_high_vis_mode_cache_endpoint_id_.c_str());
|
||||
"EndpointId]: client=%p; "
|
||||
"local_high_vis_mode_cache_endpoint_id_=%s",
|
||||
this, local_high_vis_mode_cache_endpoint_id_.c_str());
|
||||
clear_local_high_vis_mode_cache_endpoint_id_alarm_ = CancelableAlarm(
|
||||
"clear_high_power_endpoint_id_cache",
|
||||
[this]() {
|
||||
MutexLock lock(&mutex_);
|
||||
NEARBY_LOG(INFO,
|
||||
"ClientProxy [Cleared cached local high power advertising "
|
||||
"endpoint Id.]: client=%x" PRIx64
|
||||
"; local_high_vis_mode_cache_endpoint_id_=%s",
|
||||
GetClientId(),
|
||||
local_high_vis_mode_cache_endpoint_id_.c_str());
|
||||
local_high_vis_mode_cache_endpoint_id_.clear();
|
||||
},
|
||||
kHighPowerAdvertisementEndpointIdCacheTimeout, &single_thread_executor_);
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/str_join.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
@@ -25,26 +23,20 @@ namespace connections {
|
||||
OfflineServiceController::~OfflineServiceController() { Stop(); }
|
||||
|
||||
void OfflineServiceController::Stop() {
|
||||
NEARBY_LOGS(INFO) << "Initiating shutdown of OfflineServiceController.";
|
||||
if (stop_.Set(true)) return;
|
||||
payload_manager_.DisconnectFromEndpointManager();
|
||||
pcp_manager_.DisconnectFromEndpointManager();
|
||||
NEARBY_LOGS(INFO) << "OfflineServiceController has shut down.";
|
||||
}
|
||||
|
||||
Status OfflineServiceController::StartAdvertising(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
const ConnectionOptions& options, const ConnectionRequestInfo& info) {
|
||||
if (stop_) return {Status::kOutOfOrderApiCall};
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " requested advertising to start.";
|
||||
return pcp_manager_.StartAdvertising(client, service_id, options, info);
|
||||
}
|
||||
|
||||
void OfflineServiceController::StopAdvertising(ClientProxy* client) {
|
||||
if (stop_) return;
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " requested advertising to stop.";
|
||||
pcp_manager_.StopAdvertising(client);
|
||||
}
|
||||
|
||||
@@ -52,15 +44,11 @@ Status OfflineServiceController::StartDiscovery(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
const ConnectionOptions& options, const DiscoveryListener& listener) {
|
||||
if (stop_) return {Status::kOutOfOrderApiCall};
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " requested discovery to start.";
|
||||
return pcp_manager_.StartDiscovery(client, service_id, options, listener);
|
||||
}
|
||||
|
||||
void OfflineServiceController::StopDiscovery(ClientProxy* client) {
|
||||
if (stop_) return;
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " requested discovery to stop.";
|
||||
pcp_manager_.StopDiscovery(client);
|
||||
}
|
||||
|
||||
@@ -75,8 +63,6 @@ Status OfflineServiceController::RequestConnection(
|
||||
ClientProxy* client, const std::string& endpoint_id,
|
||||
const ConnectionRequestInfo& info, const ConnectionOptions& options) {
|
||||
if (stop_) return {Status::kOutOfOrderApiCall};
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " requested a connection to endpoint id=" << endpoint_id;
|
||||
return pcp_manager_.RequestConnection(client, endpoint_id, info, options);
|
||||
}
|
||||
|
||||
@@ -84,18 +70,12 @@ Status OfflineServiceController::AcceptConnection(
|
||||
ClientProxy* client, const std::string& endpoint_id,
|
||||
const PayloadListener& listener) {
|
||||
if (stop_) return {Status::kOutOfOrderApiCall};
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " accepted the connection with endpoint id="
|
||||
<< endpoint_id;
|
||||
return pcp_manager_.AcceptConnection(client, endpoint_id, listener);
|
||||
}
|
||||
|
||||
Status OfflineServiceController::RejectConnection(
|
||||
ClientProxy* client, const std::string& endpoint_id) {
|
||||
if (stop_) return {Status::kOutOfOrderApiCall};
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " rejected the connection with endpoint id="
|
||||
<< endpoint_id;
|
||||
return pcp_manager_.RejectConnection(client, endpoint_id);
|
||||
}
|
||||
|
||||
@@ -112,27 +92,18 @@ void OfflineServiceController::SendPayload(
|
||||
ClientProxy* client, const std::vector<std::string>& endpoint_ids,
|
||||
Payload payload) {
|
||||
if (stop_) return;
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " is sending payload=" << payload.GetId()
|
||||
<< " to endpoint ids="
|
||||
<< "{" << absl::StrJoin(endpoint_ids, ",").c_str() << "}";
|
||||
payload_manager_.SendPayload(client, endpoint_ids, std::move(payload));
|
||||
}
|
||||
|
||||
Status OfflineServiceController::CancelPayload(ClientProxy* client,
|
||||
std::int64_t payload_id) {
|
||||
if (stop_) return {Status::kOutOfOrderApiCall};
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " cancelled payload=" << payload_id;
|
||||
return payload_manager_.CancelPayload(client, payload_id);
|
||||
}
|
||||
|
||||
void OfflineServiceController::DisconnectFromEndpoint(
|
||||
ClientProxy* client, const std::string& endpoint_id) {
|
||||
if (stop_) return;
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " requested a disconnection from endpoint id="
|
||||
<< endpoint_id;
|
||||
endpoint_manager_.UnregisterEndpoint(client, endpoint_id);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,14 +47,8 @@ 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_LOGS(INFO) << "ServiceControllerRouter going down.";
|
||||
NEARBY_LOG(INFO, "ServiceControllerRouter going down.");
|
||||
|
||||
if (service_controller_) {
|
||||
service_controller_->Stop();
|
||||
@@ -224,11 +218,10 @@ void ServiceControllerRouter::AcceptConnection(ClientProxy* client,
|
||||
}
|
||||
|
||||
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 << ")";
|
||||
NEARBY_LOG(INFO,
|
||||
"[ServiceControllerRouter:Accept]: Client has local "
|
||||
"endpoint responded; id=%s",
|
||||
endpoint_id.c_str());
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
@@ -257,11 +250,10 @@ void ServiceControllerRouter::RejectConnection(ClientProxy* client,
|
||||
}
|
||||
|
||||
if (client->HasLocalEndpointResponded(endpoint_id)) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "Client " << client->GetClientId()
|
||||
<< " invoked rejectConnectionRequest() after having already "
|
||||
"accepted/rejected the connection to endpoint(id="
|
||||
<< endpoint_id << ")";
|
||||
NEARBY_LOG(INFO,
|
||||
"[ServiceControllerRouter:Reject]: Client has local "
|
||||
"endpoint responded; id=%s",
|
||||
endpoint_id.c_str());
|
||||
callback.result_cb({Status::kOutOfOrderApiCall});
|
||||
return;
|
||||
}
|
||||
@@ -392,8 +384,9 @@ void ServiceControllerRouter::ClientDisconnecting(
|
||||
callback]() {
|
||||
if (ClientHasAcquiredServiceController(client)) {
|
||||
DoneWithStrategySessionForClient(client);
|
||||
NEARBY_LOGS(INFO) << "Client " << client->GetClientId()
|
||||
<< " has completed the client's connection.";
|
||||
NEARBY_LOG(INFO,
|
||||
"[ServiceControllerRouter:Disconnect]: Client has completed "
|
||||
"the client's connection");
|
||||
}
|
||||
callback.result_cb({Status::kSuccess});
|
||||
});
|
||||
@@ -426,14 +419,18 @@ 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_LOGS(INFO) << "Client has already active strategy.";
|
||||
NEARBY_LOG(INFO,
|
||||
"[ServiceControllerRouter:AcquireServiceControllerForClient]: "
|
||||
"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_LOGS(INFO) << "Client has connected endpoints.";
|
||||
NEARBY_LOG(INFO,
|
||||
"[ServiceControllerRouter:AcquireServiceControllerForClient]: "
|
||||
"Client has connected endpoints.");
|
||||
return {Status::kOutOfOrderApiCall};
|
||||
}
|
||||
|
||||
@@ -500,7 +497,7 @@ bool ServiceControllerRouter::ClientHasConnectionToAtLeastOneEndpoint(
|
||||
Status ServiceControllerRouter::UpdateCurrentServiceControllerAndStrategy(
|
||||
Strategy strategy) {
|
||||
if (!strategy.IsValid()) {
|
||||
NEARBY_LOGS(INFO) << "Strategy is not valid.";
|
||||
NEARBY_LOG(INFO, "Strategy is not valid.");
|
||||
return {Status::kError};
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,8 @@ namespace connections {
|
||||
// of a ServiceController interface, which does the actual job.
|
||||
class ServiceControllerRouter {
|
||||
public:
|
||||
explicit ServiceControllerRouter(std::function<ServiceController*()> factory);
|
||||
explicit ServiceControllerRouter(std::function<ServiceController*()> factory)
|
||||
: service_controller_factory_(std::move(factory)) {}
|
||||
~ServiceControllerRouter();
|
||||
ServiceControllerRouter(ServiceControllerRouter&&) = default;
|
||||
ServiceControllerRouter& operator=(ServiceControllerRouter&&) = default;
|
||||
|
||||
Reference in New Issue
Block a user