Roll forward to cl/328359974

Change-Id: If2b57ecc852aecf7dea454648f485fd7c08e72a9
This commit is contained in:
Alexey Polyudov
2020-08-25 11:27:27 -07:00
parent 6f9228fa6b
commit c673bf6ac0
110 changed files with 4758 additions and 1245 deletions
+41 -19
View File
@@ -12,6 +12,7 @@
#include "proto/connections_enums.pb.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
namespace location {
@@ -24,21 +25,22 @@ ClientProxy::~ClientProxy() { Reset(); }
std::int64_t ClientProxy::GetClientId() const { return client_id_; }
std::string ClientProxy::GenerateLocalEndpointId() {
// 1) Concatenate the Random 64-bit value with "client" string.
// 2) Compute a hash of that concatenation.
// 3) Base64-encode that hash, to make it human-readable.
// 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;
std::string ClientProxy::GetLocalEndpointId() {
if (local_endpoint_id_.empty()) {
// 1) Concatenate the Random 64-bit value with "client" string.
// 2) Compute a hash of that concatenation.
// 3) Base64-encode that hash, to make it human-readable.
// 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());
local_endpoint_id_ = id;
}
return local_endpoint_id_;
}
void ClientProxy::Reset() {
@@ -55,6 +57,7 @@ void ClientProxy::StartedAdvertising(
absl::Span<proto::connections::Medium> mediums) {
MutexLock lock(&mutex_);
if (connections_.empty()) local_endpoint_id_.clear();
advertising_info_ = {service_id, listener};
}
@@ -64,6 +67,7 @@ void ClientProxy::StoppedAdvertising() {
if (IsAdvertising()) {
advertising_info_.Clear();
}
if (connections_.empty()) local_endpoint_id_.clear();
}
bool ClientProxy::IsAdvertising() const {
@@ -83,6 +87,7 @@ void ClientProxy::StartedDiscovery(
absl::Span<proto::connections::Medium> mediums) {
MutexLock lock(&mutex_);
if (connections_.empty()) local_endpoint_id_.clear();
discovery_info_ = DiscoveryInfo{service_id, listener};
}
@@ -93,6 +98,7 @@ void ClientProxy::StoppedDiscovery() {
discovered_endpoint_ids_.clear();
discovery_info_.Clear();
}
if (connections_.empty()) local_endpoint_id_.clear();
}
bool ClientProxy::IsDiscoveringServiceId(const std::string& service_id) const {
@@ -115,13 +121,14 @@ std::string ClientProxy::GetDiscoveryServiceId() const {
void ClientProxy::OnEndpointFound(const std::string& service_id,
const std::string& endpoint_id,
const std::string& endpoint_name,
const ByteArray& endpoint_info,
proto::connections::Medium medium) {
MutexLock lock(&mutex_);
NEARBY_LOG(INFO,
"ClientProxy [Endpoint Found]: [enter] id=%s; service=%s; name=%s",
endpoint_id.c_str(), service_id.c_str(), endpoint_name.c_str());
"ClientProxy [Endpoint Found]: [enter] id=%s; service=%s; info=%s",
endpoint_id.c_str(), service_id.c_str(),
absl::BytesToHexString(endpoint_info.data()).c_str());
if (!IsDiscoveringServiceId(service_id)) {
NEARBY_LOG(INFO, "ClientProxy [Endpoint Found]: [no discovery] id=%s",
endpoint_id.c_str());
@@ -133,7 +140,7 @@ void ClientProxy::OnEndpointFound(const std::string& service_id,
return;
}
discovered_endpoint_ids_.insert(endpoint_id);
discovery_info_.listener.endpoint_found_cb(endpoint_id, endpoint_name,
discovery_info_.listener.endpoint_found_cb(endpoint_id, endpoint_info,
service_id);
}
@@ -150,6 +157,7 @@ void ClientProxy::OnEndpointLost(const std::string& service_id,
void ClientProxy::OnConnectionInitiated(const std::string& endpoint_id,
const ConnectionResponseInfo& info,
const ConnectionOptions& options,
const ConnectionListener& listener) {
MutexLock lock(&mutex_);
@@ -160,6 +168,7 @@ void ClientProxy::OnConnectionInitiated(const std::string& endpoint_id,
endpoint_id, Connection{
.is_incoming = info.is_incoming_connection,
.connection_listener = listener,
.connection_options = options,
});
// Instead of using structured binding which is nice, but banned
// (can not use c++17 features, until chromium does) we unpack manually.
@@ -234,6 +243,7 @@ void ClientProxy::OnDisconnected(const std::string& endpoint_id, bool notify) {
item->connection_listener.disconnected_cb({endpoint_id});
}
connections_.erase(endpoint_id);
if (connections_.empty()) local_endpoint_id_.clear();
}
}
@@ -248,6 +258,17 @@ bool ClientProxy::ConnectionStatusMatches(const std::string& endpoint_id,
return false;
}
BooleanMediumSelector ClientProxy::GetUpgradeMediums(
const std::string& endpoint_id) const {
MutexLock lock(&mutex_);
const Connection* item = LookupConnection(endpoint_id);
if (item != nullptr) {
return item->connection_options.allowed;
}
return {};
}
bool ClientProxy::IsConnectedToEndpoint(const std::string& endpoint_id) const {
return ConnectionStatusMatches(endpoint_id, Connection::kConnected);
}
@@ -469,6 +490,7 @@ void ClientProxy::RemoveAllEndpoints() {
// endpoint, in the case when this is called from stopAllEndpoints(). For now,
// just remove without notifying.
connections_.clear();
local_endpoint_id_.clear();
}
bool ClientProxy::ConnectionStatusesContains(