diff --git a/connections/core.cc b/connections/core.cc index 284d5d27..ad20be9c 100644 --- a/connections/core.cc +++ b/connections/core.cc @@ -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 diff --git a/connections/core.h b/connections/core.h index 29cb8219..3e39fc17 100644 --- a/connections/core.h +++ b/connections/core.h @@ -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; diff --git a/connections/implementation/BUILD b/connections/implementation/BUILD index 08812729..3d9d421a 100644 --- a/connections/implementation/BUILD +++ b/connections/implementation/BUILD @@ -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", diff --git a/connections/implementation/client_proxy.cc b/connections/implementation/client_proxy.cc index bb52fac9..632dbb26 100644 --- a/connections/implementation/client_proxy.cc +++ b/connections/implementation/client_proxy.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -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 diff --git a/connections/implementation/client_proxy.h b/connections/implementation/client_proxy.h index 87af16b3..b27fdf51 100644 --- a/connections/implementation/client_proxy.h +++ b/connections/implementation/client_proxy.h @@ -188,6 +188,8 @@ class ClientProxy final { // rotates. void ExitHighVisibilityMode(); + std::string Dump(); + private: struct Connection { // Status: may be either: diff --git a/connections/implementation/client_proxy_test.cc b/connections/implementation/client_proxy_test.cc index 35112b7d..14cd04ba 100644 --- a/connections/implementation/client_proxy_test.cc +++ b/connections/implementation/client_proxy_test.cc @@ -14,12 +14,14 @@ #include "connections/implementation/client_proxy.h" +#include #include #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()); }