Add dump function to nearby connections.

Sample output:
```
Time: 2022-06-23T17:24:14.3815934+00:00
Windows version: 10.0 build 19042
NearbyShare version: 1.2.3.4
Nearby Share Settings
  Device name: Deling's PC
  Visibility: 1
  Enabled: 1
  ShowNotification: 0
  DataUsage: 0
  Allowed contacts:
Nearby Sharing Service state
  IsScanning: 0
  IsConnecting: 0
  IsTransferring: 0
  IsSendingFile: 0
  IsReceivingFile: 0
Nearby Connections State
  Client ID: -5841239073452700193
  Local Endpoint ID: IWV0
  High Vis Mode: 0
  Is Advertising: 0
  Advertising Service ID:
  Is Discovering: 0
  Discovery Service ID:
  Connections:
  Discovered endpoint IDs:
```
PiperOrigin-RevId: 458066027
This commit is contained in:
hai007
2022-06-29 14:33:41 -07:00
committed by Copybara-Service
parent ff9a986595
commit 7e081c663a
6 changed files with 60 additions and 1 deletions
+4
View File
@@ -168,6 +168,10 @@ void Core::StopAllEndpoints(ResultCallback callback) {
router_->StopAllEndpoints(&client_, callback);
}
std::string Core::Dump() {
return client_.Dump();
}
} // namespace connections
} // namespace nearby
} // namespace location
+2
View File
@@ -238,6 +238,8 @@ class Core {
// Gets the local endpoint generated by Nearby Connections.
std::string GetLocalEndpointId() { return client_.GetLocalEndpointId(); }
std::string Dump();
private:
ClientProxy client_;
ServiceControllerRouter* router_ = nullptr;
+2 -1
View File
@@ -148,6 +148,7 @@ cc_library(
"@com_google_absl//absl/functional:bind_front",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
],
@@ -232,7 +233,6 @@ cc_test(
":ukey2",
"//connections:core_types",
"//connections/implementation/mediums",
"//connections/implementation/mediums:utils",
"//connections/implementation/proto:offline_wire_formats_cc_proto",
"//internal/platform:base",
"//internal/platform:comm",
@@ -244,6 +244,7 @@ cc_test(
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
@@ -17,6 +17,7 @@
#include <cstdlib>
#include <functional>
#include <limits>
#include <sstream>
#include <string>
#include <utility>
@@ -24,6 +25,7 @@
#include "absl/container/flat_hash_set.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "internal/platform/error_code_recorder.h"
#include "internal/platform/feature_flags.h"
#include "internal/platform/logging.h"
@@ -762,6 +764,35 @@ std::string ClientProxy::ToString(PayloadProgressInfo::Status status) const {
}
}
std::string ClientProxy::Dump() {
std::stringstream sstream;
sstream << "Nearby Connections State" << std::endl;
sstream << " Client ID: " << GetClientId() << std::endl;
sstream << " Local Endpoint ID: " << GetLocalEndpointId() << std::endl;
sstream << " High Visibility Mode: " << high_vis_mode_ << std::endl;
sstream << " Is Advertising: " << IsAdvertising() << std::endl;
sstream << " Advertising Service ID: " << GetAdvertisingServiceId()
<< std::endl;
// TODO(deling): AdvertisingOptions
sstream << " Is Discovering: " << IsDiscovering() << std::endl;
sstream << " Discovery Service ID: " << GetDiscoveryServiceId() << std::endl;
// TODO(deling): DiscoveryOptions
sstream << " Connections: " << std::endl;
for (auto it = connections_.begin(); it != connections_.end(); ++it) {
// TODO(deling): write Connection.ToString()
sstream << " " << it->first << " : " << it->second.connection_token
<< std::endl;
}
sstream << " Discovered endpoint IDs: " << std::endl;
for (auto it = discovered_endpoint_ids_.begin();
it != discovered_endpoint_ids_.end(); ++it) {
sstream << " " << *it << std::endl;
}
return sstream.str();
}
} // namespace connections
} // namespace nearby
} // namespace location
@@ -188,6 +188,8 @@ class ClientProxy final {
// rotates.
void ExitHighVisibilityMode();
std::string Dump();
private:
struct Connection {
// Status: may be either:
@@ -14,12 +14,14 @@
#include "connections/implementation/client_proxy.h"
#include <cstdio>
#include <string>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/str_format.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
@@ -362,6 +364,23 @@ TEST_F(ClientProxyTest, ClientIdIsUnique) {
EXPECT_NE(client1_.GetClientId(), client2_.GetClientId());
}
TEST_F(ClientProxyTest, DumpString) {
std::string expect = absl::StrFormat(
"Nearby Connections State\n"
" Client ID: %d\n"
" Local Endpoint ID: %s\n"
" High Visibility Mode: 0\n"
" Is Advertising: 0\n"
" Advertising Service ID: \n"
" Is Discovering: 0\n"
" Discovery Service ID: \n"
" Connections: \n"
" Discovered endpoint IDs: \n",
client1_.GetClientId(), client1_.GetLocalEndpointId());
std::string dump = client1_.Dump();
EXPECT_EQ(dump, expect);
}
TEST_F(ClientProxyTest, GeneratedEndpointIdIsUnique) {
EXPECT_NE(client1_.GetLocalEndpointId(), client2_.GetLocalEndpointId());
}