From a56574f90b17b48981442423f434f50449f2bff4 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Fri, 11 Apr 2025 09:46:18 -0400 Subject: [PATCH 01/37] make dial_options a struct --- src/viam/examples/camera/example_camera.cpp | 4 +- src/viam/examples/dial/example_dial.cpp | 6 +- .../dial_api_key/example_dial_api_key.cpp | 4 +- .../example_audio_classification_client.cpp | 4 +- src/viam/examples/modules/complex/client.cpp | 2 +- src/viam/examples/modules/simple/client.cpp | 2 +- src/viam/examples/motor/example_motor.cpp | 4 +- src/viam/sdk/rpc/dial.cpp | 85 +++---------------- src/viam/sdk/rpc/dial.hpp | 38 ++------- 9 files changed, 33 insertions(+), 116 deletions(-) diff --git a/src/viam/examples/camera/example_camera.cpp b/src/viam/examples/camera/example_camera.cpp index 570ae0d59..b4b68988c 100644 --- a/src/viam/examples/camera/example_camera.cpp +++ b/src/viam/examples/camera/example_camera.cpp @@ -31,8 +31,8 @@ int main() { // dial_options.credentials = credentials; // This is for an example. Care should be taken before exercising this option in production. - dial_options.set_allow_insecure_downgrade( - (credentials.type().empty() && credentials.payload().empty())); + dial_options.allow_insecure_downgrade = + (credentials.type().empty() && credentials.payload().empty()); // Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial // options diff --git a/src/viam/examples/dial/example_dial.cpp b/src/viam/examples/dial/example_dial.cpp index 5289e3aab..1241088a8 100644 --- a/src/viam/examples/dial/example_dial.cpp +++ b/src/viam/examples/dial/example_dial.cpp @@ -24,11 +24,9 @@ int main() { DialOptions dial_options; std::string type = ""; std::string payload = ""; - Credentials credentials(type, payload); - dial_options.set_credentials(credentials); - boost::optional opts(dial_options); + dial_options.credentials = Credentials(type, payload); std::string address(uri); - Options options(1, opts); + Options options(1, dial_options); // connect to robot, ensure we can refresh it std::shared_ptr robot = RobotClient::at_address(address, options); diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index bc04b50c4..206c9bf9b 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -36,9 +36,9 @@ int main(int argc, char* argv[]) { boost::optional opts; if (vm.count("entity") && vm.count("api-key")) { DialOptions dial_options; - dial_options.set_entity(vm["entity"].as()); + dial_options.auth_entity = vm["entity"].as(); Credentials credentials("api-key", vm["api-key"].as()); - dial_options.set_credentials(credentials); + dial_options.credentials = credentials; opts = dial_options; } Options options(1, opts); diff --git a/src/viam/examples/mlmodel/example_audio_classification_client.cpp b/src/viam/examples/mlmodel/example_audio_classification_client.cpp index 6be570449..a7a021456 100644 --- a/src/viam/examples/mlmodel/example_audio_classification_client.cpp +++ b/src/viam/examples/mlmodel/example_audio_classification_client.cpp @@ -236,8 +236,8 @@ int main(int argc, char* argv[]) try { // secret. Please see other examples for more details on // connecting to robots with the C++ SDK. viam::sdk::DialOptions dial_options; - dial_options.set_entity(opt_api_key_id.get()); - dial_options.set_credentials(viam::sdk::Credentials("api-key", opt_api_key.get())); + dial_options.auth_entity = opt_api_key_id.get(); + dial_options.credentials = viam::sdk::Credentials("api-key", opt_api_key.get()); auto robot = vsdk::RobotClient::at_address(opt_robot_host.get(), {0, {std::move(dial_options)}}); diff --git a/src/viam/examples/modules/complex/client.cpp b/src/viam/examples/modules/complex/client.cpp index f579ec35e..e55af745d 100644 --- a/src/viam/examples/modules/complex/client.cpp +++ b/src/viam/examples/modules/complex/client.cpp @@ -27,7 +27,7 @@ int main() { const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely DialOptions dial_options; - dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely + dial_options.allow_insecure_downgrade = true; // set to false if connecting securely // Uncomment and fill out your credentials details if connecting securely // std::string type = ""; diff --git a/src/viam/examples/modules/simple/client.cpp b/src/viam/examples/modules/simple/client.cpp index f08410336..c0eebb436 100644 --- a/src/viam/examples/modules/simple/client.cpp +++ b/src/viam/examples/modules/simple/client.cpp @@ -17,7 +17,7 @@ int main() { const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely DialOptions dial_options; - dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely + dial_options.allow_insecure_downgrade = true; // set to false if connecting securely // Uncomment and fill out your credentials details if connecting securely // std::string type = ""; diff --git a/src/viam/examples/motor/example_motor.cpp b/src/viam/examples/motor/example_motor.cpp index 7456c60fa..f0603285b 100644 --- a/src/viam/examples/motor/example_motor.cpp +++ b/src/viam/examples/motor/example_motor.cpp @@ -44,8 +44,8 @@ int main() { // dial_options.credentials = credentials; // This is for an example. Care should be taken before exercising this option in production. - dial_options.set_allow_insecure_downgrade( - (credentials.type().empty() && credentials.payload().empty())); + dial_options.allow_insecure_downgrade = + (credentials.type().empty() && credentials.payload().empty()); // Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial // options diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 35a990363..b6e77c10c 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -42,83 +42,22 @@ const std::string& Credentials::payload() const { ViamChannel::ViamChannel(std::shared_ptr channel, const char* path, void* runtime) : channel_(std::move(channel)), path_(path), closed_(false), rust_runtime_(runtime) {} -DialOptions::DialOptions() = default; - -DialOptions& DialOptions::set_credentials(boost::optional creds) { - credentials_ = std::move(creds); - - return *this; -} - -DialOptions& DialOptions::set_entity(boost::optional entity) { - auth_entity_ = std::move(entity); - - return *this; -} - -DialOptions& DialOptions::set_initial_connection_attempts(int attempts) { - initial_connection_attempts_ = attempts; - - return *this; -} - -DialOptions& DialOptions::set_timeout(std::chrono::duration timeout) { - timeout_ = timeout; - - return *this; -} - -DialOptions& DialOptions::set_initial_connection_attempt_timeout( - std::chrono::duration timeout) { - initial_connection_attempt_timeout_ = timeout; - - return *this; -} - -const boost::optional& DialOptions::entity() const { - return auth_entity_; -} - -const boost::optional& DialOptions::credentials() const { - return credentials_; -} - -int DialOptions::initial_connection_attempts() const { - return initial_connection_attempts_; -} - -const std::chrono::duration& DialOptions::timeout() const { - return timeout_; -} - -std::chrono::duration DialOptions::initial_connection_attempt_timeout() const { - return initial_connection_attempt_timeout_; -} - -DialOptions& DialOptions::set_allow_insecure_downgrade(bool allow) { - allow_insecure_downgrade_ = allow; - - return *this; -} - -bool DialOptions::allows_insecure_downgrade() const { - return allow_insecure_downgrade_; -} +// DialOptions::DialOptions() = default; std::shared_ptr ViamChannel::dial_initial( const char* uri, const boost::optional& options) { DialOptions opts = options.get_value_or(DialOptions()); - auto timeout = opts.timeout(); - auto attempts_remaining = opts.initial_connection_attempts(); + auto timeout = opts.timeout; + auto attempts_remaining = opts.initial_connection_attempts; if (attempts_remaining == 0) { attempts_remaining = -1; } - opts.set_timeout(opts.initial_connection_attempt_timeout()); + opts.timeout = opts.initial_connection_attempt_timeout; while (attempts_remaining != 0) { try { auto connection = dial(uri, opts); - opts.set_timeout(timeout); + opts.timeout = timeout; return connection; } catch (const std::exception& e) { attempts_remaining -= 1; @@ -136,20 +75,20 @@ std::shared_ptr ViamChannel::dial(const char* uri, const boost::optional& options) { void* ptr = init_rust_runtime(); const DialOptions opts = options.get_value_or(DialOptions()); - const std::chrono::duration float_timeout = opts.timeout(); + const std::chrono::duration float_timeout = opts.timeout; const char* type = nullptr; const char* entity = nullptr; const char* payload = nullptr; - if (opts.credentials()) { - type = opts.credentials()->type().c_str(); - payload = opts.credentials()->payload().c_str(); + if (opts.credentials) { + type = opts.credentials->type().c_str(); + payload = opts.credentials->payload().c_str(); } - if (opts.entity()) { - entity = opts.entity()->c_str(); + if (opts.auth_entity) { + entity = opts.auth_entity->c_str(); } char* socket_path = ::dial( - uri, entity, type, payload, opts.allows_insecure_downgrade(), float_timeout.count(), ptr); + uri, entity, type, payload, opts.allow_insecure_downgrade, float_timeout.count(), ptr); if (socket_path == NULL) { free_rust_runtime(ptr); throw Exception(ErrorCondition::k_connection, "Unable to establish connecting path"); diff --git a/src/viam/sdk/rpc/dial.hpp b/src/viam/sdk/rpc/dial.hpp index 64cf4e5df..5a0a262e9 100644 --- a/src/viam/sdk/rpc/dial.hpp +++ b/src/viam/sdk/rpc/dial.hpp @@ -11,7 +11,8 @@ namespace viam { namespace sdk { -class DialOptions; +struct DialOptions; + class ViamChannel { public: void close(); @@ -58,49 +59,28 @@ class Credentials { std::string payload_; }; -class DialOptions { - public: - DialOptions(); - - const boost::optional& credentials() const; - const boost::optional& entity() const; - bool allows_insecure_downgrade() const; - const std::chrono::duration& timeout() const; - int initial_connection_attempts() const; - std::chrono::duration initial_connection_attempt_timeout() const; - - DialOptions& set_entity(boost::optional entity); - DialOptions& set_credentials(boost::optional creds); - DialOptions& set_allow_insecure_downgrade(bool allow); - DialOptions& set_timeout(std::chrono::duration timeout); - DialOptions& set_initial_connection_attempts(int attempts); - DialOptions& set_initial_connection_attempt_timeout(std::chrono::duration timeout); - - private: - // TODO (RSDK-917): We currently don't provide a flag for disabling webRTC, instead relying on a - // `local` uri. We should update dial logic to consider such a flag. - +struct DialOptions { /// @brief the URL to authenticate against. - boost::optional auth_entity_; + boost::optional auth_entity; /// @brief Credentials for connecting to the robot. - boost::optional credentials_; + boost::optional credentials; /// @brief Allows the RPC connection to be downgraded to an insecure connection if detected. /// This is only used when credentials are not present. - bool allow_insecure_downgrade_ = false; + bool allow_insecure_downgrade = false; /// @brief Duration before the dial connection times out /// Set to 20sec to match _defaultOfferDeadline in goutils/rpc/wrtc_call_queue.go - std::chrono::duration timeout_{20}; + std::chrono::duration timeout{20}; /// @brief Number of attempts to make when initially connecting to a robot /// If set to 0 or a negative integer, will attempt to reconnect forever. - int initial_connection_attempts_ = 3; + int initial_connection_attempts = 3; /// @brief Timeout of connection attempts when initially dialing a robot /// Defaults to 20sec to match the default timeout duration - std::chrono::duration initial_connection_attempt_timeout_{20}; + std::chrono::duration initial_connection_attempt_timeout{20}; }; class Options { From d86eac77e768fe08f512f97349746ef39344808e Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 15 Apr 2025 14:37:58 -0400 Subject: [PATCH 02/37] wip: direct dial --- src/viam/api/CMakeLists.txt | 8 ++++++ src/viam/sdk/rpc/dial.cpp | 54 +++++++++++++++++++++++++++++++++---- src/viam/sdk/rpc/dial.hpp | 13 ++++++++- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/viam/api/CMakeLists.txt b/src/viam/api/CMakeLists.txt index 72f03567b..1d8a93d25 100644 --- a/src/viam/api/CMakeLists.txt +++ b/src/viam/api/CMakeLists.txt @@ -228,6 +228,10 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS) ${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.cc ${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.h ${PROTO_GEN_DIR}/component/switch/v1/switch.pb.cc + ${PROTO_GEN_DIR}/proto/rpc/v1/auth.pb.h + ${PROTO_GEN_DIR}/proto/rpc/v1/auth.pb.cc + ${PROTO_GEN_DIR}/proto/rpc/v1/auth.grpc.pb.h + ${PROTO_GEN_DIR}/proto/rpc/v1/auth.grpc.pb.cc ${PROTO_GEN_DIR}/component/switch/v1/switch.pb.h ${PROTO_GEN_DIR}/google/api/annotations.pb.cc ${PROTO_GEN_DIR}/google/api/annotations.pb.h @@ -361,6 +365,8 @@ target_sources(viamapi ${PROTO_GEN_DIR}/component/servo/v1/servo.pb.cc ${PROTO_GEN_DIR}/component/switch/v1/switch.grpc.pb.cc ${PROTO_GEN_DIR}/component/switch/v1/switch.pb.cc + ${PROTO_GEN_DIR}/proto/rpc/v1/auth.grpc.pb.cc + ${PROTO_GEN_DIR}/proto/rpc/v1/auth.pb.cc ${PROTO_GEN_DIR}/google/api/annotations.pb.cc ${PROTO_GEN_DIR}/google/api/http.pb.cc ${PROTO_GEN_DIR}/google/api/httpbody.pb.cc @@ -425,6 +431,8 @@ target_sources(viamapi ${PROTO_GEN_DIR}/../../viam/api/component/servo/v1/servo.pb.h ${PROTO_GEN_DIR}/../../viam/api/component/switch/v1/switch.grpc.pb.h ${PROTO_GEN_DIR}/../../viam/api/component/switch/v1/switch.pb.h + ${PROTO_GEN_DIR}/../../viam/api/proto/rpc/v1/auth.grpc.pb.h + ${PROTO_GEN_DIR}/../../viam/api/proto/rpc/v1/auth.pb.h ${PROTO_GEN_DIR}/../../viam/api/google/api/annotations.pb.h ${PROTO_GEN_DIR}/../../viam/api/google/api/http.pb.h ${PROTO_GEN_DIR}/../../viam/api/google/api/httpbody.pb.h diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index b6e77c10c..f1ac0b65e 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -6,11 +6,16 @@ #include #include #include +#include #include +#include #include +#include +#include #include #include +#include #include #include #include @@ -42,7 +47,9 @@ const std::string& Credentials::payload() const { ViamChannel::ViamChannel(std::shared_ptr channel, const char* path, void* runtime) : channel_(std::move(channel)), path_(path), closed_(false), rust_runtime_(runtime) {} -// DialOptions::DialOptions() = default; +ViamChannel::~ViamChannel() { + close(); +} std::shared_ptr ViamChannel::dial_initial( const char* uri, const boost::optional& options) { @@ -73,9 +80,14 @@ std::shared_ptr ViamChannel::dial_initial( std::shared_ptr ViamChannel::dial(const char* uri, const boost::optional& options) { - void* ptr = init_rust_runtime(); const DialOptions opts = options.get_value_or(DialOptions()); + + if (opts.disable_webrtc) { + return dial_direct(uri, opts); + } + const std::chrono::duration float_timeout = opts.timeout; + void* ptr = init_rust_runtime(); const char* type = nullptr; const char* entity = nullptr; const char* payload = nullptr; @@ -84,11 +96,14 @@ std::shared_ptr ViamChannel::dial(const char* uri, type = opts.credentials->type().c_str(); payload = opts.credentials->payload().c_str(); } + if (opts.auth_entity) { entity = opts.auth_entity->c_str(); } + char* socket_path = ::dial( uri, entity, type, payload, opts.allow_insecure_downgrade, float_timeout.count(), ptr); + if (socket_path == NULL) { free_rust_runtime(ptr); throw Exception(ErrorCondition::k_connection, "Unable to establish connecting path"); @@ -96,12 +111,41 @@ std::shared_ptr ViamChannel::dial(const char* uri, std::string address("unix://"); address += socket_path; + const std::shared_ptr channel = impl::create_viam_channel(address, grpc::InsecureChannelCredentials()); - const std::unique_ptr st = - viam::robot::v1::RobotService::NewStub(channel); + return std::make_shared(channel, socket_path, ptr); -}; +} + +std::shared_ptr ViamChannel::dial_direct(const char* uri, const DialOptions& options) { + std::cerr << "\n\tCreating grpc channel " << uri << "\n"; + auto grpc_channel = impl::create_viam_channel(uri, grpc::InsecureChannelCredentials()); + std::cerr << "\n\ttest\n"; + + using namespace proto::rpc::v1; + + auto auth_stub = AuthService::NewStub(grpc_channel); + ClientContext ctx; + AuthenticateRequest req; + + *req.mutable_entity() = options.auth_entity.get(); + *req.mutable_credentials()->mutable_payload() = options.credentials->payload(); + *req.mutable_credentials()->mutable_type() = options.credentials->type(); + + std::cerr << req.DebugString() << "\n"; + + AuthenticateResponse resp; + + auto status = auth_stub->Authenticate(ctx, req, &resp); + + std::cerr << "\n\t status: " << status.error_message() << " code: " << status.error_code() + << "\n\t access token: " << resp.access_token() << "\n\n"; + + return std::make_shared(grpc_channel, // + nullptr, + nullptr); +} unsigned int Options::refresh_interval() const { return refresh_interval_; diff --git a/src/viam/sdk/rpc/dial.hpp b/src/viam/sdk/rpc/dial.hpp index 5a0a262e9..2a9b90dab 100644 --- a/src/viam/sdk/rpc/dial.hpp +++ b/src/viam/sdk/rpc/dial.hpp @@ -15,9 +15,12 @@ struct DialOptions; class ViamChannel { public: - void close(); + // TODO: we don't want to pass rust args for direct dialing + // null rust args === direct dial channel ViamChannel(std::shared_ptr channel, const char* path, void* runtime); + ~ViamChannel(); + /// @brief Connects to a robot at the given URI address, using the provided dial options (or /// default options is none are provided). Ignores initial connection options specifying /// how many times to attempt to connect and with what timeout. @@ -39,7 +42,11 @@ class ViamChannel { const std::shared_ptr& channel() const; + void close(); + private: + static std::shared_ptr dial_direct(const char* uri, const DialOptions& opts); + std::shared_ptr channel_; const char* path_; bool closed_; @@ -70,6 +77,10 @@ struct DialOptions { /// This is only used when credentials are not present. bool allow_insecure_downgrade = false; + /// @brief Bypass WebRTC and connect directly to the robot. + /// This dials directly through grpc bypassing rust utils. + bool disable_webrtc = false; + /// @brief Duration before the dial connection times out /// Set to 20sec to match _defaultOfferDeadline in goutils/rpc/wrtc_call_queue.go std::chrono::duration timeout{20}; From 4d5f8aa596dd66334324148c4205000884cd1cdd Mon Sep 17 00:00:00 2001 From: Ethan Rodkin Date: Wed, 16 Apr 2025 14:14:04 -0400 Subject: [PATCH 03/37] get auth token successfully --- .../dial_api_key/example_dial_api_key.cpp | 3 ++ .../examples/modules/complex/proto/buf.lock | 4 +- src/viam/sdk/common/client_helper.cpp | 5 +++ src/viam/sdk/common/client_helper.hpp | 2 + src/viam/sdk/registry/registry.cpp | 1 + src/viam/sdk/rpc/dial.cpp | 43 ++++++++++++++++--- .../sdk/rpc/private/viam_grpc_channel.cpp | 32 +++++++++++++- .../sdk/rpc/private/viam_grpc_channel.hpp | 5 +++ src/viam/sdk/rpc/server.cpp | 2 + 9 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index 206c9bf9b..ff2a731d9 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -34,6 +34,8 @@ int main(int argc, char* argv[]) { return 0; } boost::optional opts; + DialOptions dopts; + opts = dopts; if (vm.count("entity") && vm.count("api-key")) { DialOptions dial_options; dial_options.auth_entity = vm["entity"].as(); @@ -41,6 +43,7 @@ int main(int argc, char* argv[]) { dial_options.credentials = credentials; opts = dial_options; } + opts->disable_webrtc = true; Options options(1, opts); // connect to robot, ensure we can refresh it diff --git a/src/viam/examples/modules/complex/proto/buf.lock b/src/viam/examples/modules/complex/proto/buf.lock index 73ebfcf9b..2ca276594 100644 --- a/src/viam/examples/modules/complex/proto/buf.lock +++ b/src/viam/examples/modules/complex/proto/buf.lock @@ -4,5 +4,5 @@ deps: - remote: buf.build owner: googleapis repository: googleapis - commit: 751cbe31638d43a9bfb6162cd2352e67 - digest: shake256:87f55470d9d124e2d1dedfe0231221f4ed7efbc55bc5268917c678e2d9b9c41573a7f9a557f6d8539044524d9fc5ca8fbb7db05eb81379d168285d76b57eb8a4 + commit: 61b203b9a9164be9a834f58c37be6f62 + digest: shake256:e619113001d6e284ee8a92b1561e5d4ea89a47b28bf0410815cb2fa23914df8be9f1a6a98dcf069f5bc2d829a2cfb1ac614863be45cd4f8a5ad8606c5f200224 diff --git a/src/viam/sdk/common/client_helper.cpp b/src/viam/sdk/common/client_helper.cpp index b05bb37fe..58f66b335 100644 --- a/src/viam/sdk/common/client_helper.cpp +++ b/src/viam/sdk/common/client_helper.cpp @@ -25,10 +25,15 @@ bool isStatusCancelled(int status) noexcept { } } // namespace client_helper_details +std::string ClientContext::token; ClientContext::ClientContext() : wrapped_context_(std::make_unique()) { set_client_ctx_authority_(); add_viam_client_version_(); + if (!token.empty()) { + std::cout << "setting authorization to" << token << "\n"; + wrapped_context_->AddMetadata("authorization", "Bearer " + token); + } } ClientContext::~ClientContext() = default; diff --git a/src/viam/sdk/common/client_helper.hpp b/src/viam/sdk/common/client_helper.hpp index 2fb918c55..39f9dbd07 100644 --- a/src/viam/sdk/common/client_helper.hpp +++ b/src/viam/sdk/common/client_helper.hpp @@ -29,6 +29,8 @@ class ClientContext { void try_cancel(); + static std::string token; + operator GrpcClientContext*(); operator const GrpcClientContext*() const; diff --git a/src/viam/sdk/registry/registry.cpp b/src/viam/sdk/registry/registry.cpp index 90cfe9af2..3884d71ab 100644 --- a/src/viam/sdk/registry/registry.cpp +++ b/src/viam/sdk/registry/registry.cpp @@ -200,6 +200,7 @@ const google::protobuf::ServiceDescriptor* ResourceServerRegistration::service_d } void Registry::register_resources() { + std::cout << "doing a registry\n"; // Register all components register_resource(); register_resource(); diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index f1ac0b65e..d44d9830c 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -82,9 +83,12 @@ std::shared_ptr ViamChannel::dial(const char* uri, const boost::optional& options) { const DialOptions opts = options.get_value_or(DialOptions()); + std::cout << "checking if we're disabling webrtc\n"; if (opts.disable_webrtc) { + std::cout << "disabling webrtc\n"; return dial_direct(uri, opts); } + std::cout << "not disabling for some reason\n?"; const std::chrono::duration float_timeout = opts.timeout; void* ptr = init_rust_runtime(); @@ -118,30 +122,57 @@ std::shared_ptr ViamChannel::dial(const char* uri, return std::make_shared(channel, socket_path, ptr); } -std::shared_ptr ViamChannel::dial_direct(const char* uri, const DialOptions& options) { +std::shared_ptr ViamChannel::dial_direct(const char* uri, const DialOptions& opts) { std::cerr << "\n\tCreating grpc channel " << uri << "\n"; - auto grpc_channel = impl::create_viam_channel(uri, grpc::InsecureChannelCredentials()); + // auto grpc_channel = grpc::CreateChannel("app.viam.com:443", + // grpc::SslCredentialsOptions foo; + auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions()); + // auto grpc_channel = grpc::CreateChannel(uri, creds); + auto channel_for_auth = impl::create_viam_auth_channel(); + // auto grpc_channel = grpc::CreateChannel(uri, grpc::InsecureChannelCredentials()); + // grpc_connectivity_state prev = grpc_connectivity_state::GRPC_CHANNEL_READY; + // while (true) { + // grpc_connectivity_state foo = grpc_channel->GetState(true); + // if (foo != prev) { + // std::cout << "foo is " << foo << "\n"; + // prev = foo; + //} + // if (foo == grpc_connectivity_state::GRPC_CHANNEL_READY) { + // break; + //} + //}; + + // auto grpc_channel = impl::create_viam_channel(uri, creds); + // grpc::InsecureChannelCredentials()); std::cerr << "\n\ttest\n"; using namespace proto::rpc::v1; - auto auth_stub = AuthService::NewStub(grpc_channel); + // auto sopts = ::grpc::StubOptions(); + // std::cout << "suffix for stats is " << sopts.suffix_for_stats() << "\n"; + auto auth_stub = AuthService::NewStub(channel_for_auth); ClientContext ctx; AuthenticateRequest req; - *req.mutable_entity() = options.auth_entity.get(); - *req.mutable_credentials()->mutable_payload() = options.credentials->payload(); - *req.mutable_credentials()->mutable_type() = options.credentials->type(); + *req.mutable_entity() = opts.auth_entity.get(); + *req.mutable_credentials()->mutable_payload() = opts.credentials->payload(); + *req.mutable_credentials()->mutable_type() = opts.credentials->type(); std::cerr << req.DebugString() << "\n"; AuthenticateResponse resp; + std::cout << "authenticating now\n"; auto status = auth_stub->Authenticate(ctx, req, &resp); + std::cout << "authenticated\n"; std::cerr << "\n\t status: " << status.error_message() << " code: " << status.error_code() << "\n\t access token: " << resp.access_token() << "\n\n"; + ClientContext::token = resp.access_token(); + auto grpc_channel = + impl::create_viam_channel(uri, grpc::InsecureChannelCredentials(), resp.access_token()); + return std::make_shared(grpc_channel, // nullptr, nullptr); diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp index 909ec6a55..2dd26ec45 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp @@ -9,13 +9,43 @@ namespace viam { namespace sdk { namespace impl { +std::shared_ptr create_viam_auth_channel() { + grpc::experimental::TlsChannelCredentialsOptions opts; + opts.set_verify_server_certs(false); + opts.set_check_call_host(false); + auto tls_creds = grpc::experimental::TlsCredentials(opts); + std::cout << "setting ssl target name override\n"; + grpc::ChannelArguments args; + args.SetMaxSendMessageSize(kMaxMessageSize); + args.SetMaxReceiveMessageSize(kMaxMessageSize); + // args.SetSslTargetNameOverride("34.149.207.107"); + // args.SetSslTargetNameOverride("app.viam.com"); + + return grpc::CreateCustomChannel("app.viam.com", tls_creds, args); +} + std::shared_ptr create_viam_channel( const grpc::string& target, const std::shared_ptr& credentials) { + return create_viam_channel(target, credentials, ""); +} + +std::shared_ptr create_viam_channel( + const grpc::string& target, + const std::shared_ptr& credentials, + const std::string& token) { grpc::ChannelArguments args; args.SetMaxSendMessageSize(kMaxMessageSize); args.SetMaxReceiveMessageSize(kMaxMessageSize); - return grpc::CreateCustomChannel(target, credentials, args); + if (!token.empty()) { + args.SetString("authorization:", "Bearer " + token); + } + grpc::experimental::TlsChannelCredentialsOptions opts; + opts.set_verify_server_certs(false); + opts.set_check_call_host(false); + auto tls_creds = grpc::experimental::TlsCredentials(opts); + + return grpc::CreateCustomChannel(target, tls_creds, args); } } // namespace impl diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp index 1f6989317..dd45d06d2 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp @@ -13,6 +13,11 @@ namespace impl { /// size kMaxMessageSize. std::shared_ptr create_viam_channel( const grpc::string& target, const std::shared_ptr& credentials); +std::shared_ptr create_viam_channel( + const grpc::string& target, + const std::shared_ptr& credentials, + const std::string& token); +std::shared_ptr create_viam_auth_channel(); } // namespace impl } // namespace sdk diff --git a/src/viam/sdk/rpc/server.cpp b/src/viam/sdk/rpc/server.cpp index 9964abbac..19f66423a 100644 --- a/src/viam/sdk/rpc/server.cpp +++ b/src/viam/sdk/rpc/server.cpp @@ -16,6 +16,7 @@ namespace viam { namespace sdk { Server::Server() : builder_(std::make_unique()) { + std::cout << "making a server\n\n"; builder_->SetMaxReceiveMessageSize(kMaxMessageSize); builder_->SetMaxSendMessageSize(kMaxMessageSize); builder_->SetMaxMessageSize(kMaxMessageSize); @@ -39,6 +40,7 @@ std::shared_ptr Server::lookup_resource_server(const API& api) { } void Server::register_service(grpc::Service* service) { + std::cout << "registering a service\n"; if (!builder_) { throw Exception("Cannot register a new service after the server has started"); } From 6eb0fdd41f436fe7b6a4048c64dae17a93fa1fe2 Mon Sep 17 00:00:00 2001 From: Ethan Rodkin Date: Wed, 16 Apr 2025 15:41:31 -0400 Subject: [PATCH 04/37] make calls successfully --- .../examples/dial_api_key/example_dial_api_key.cpp | 5 +++++ src/viam/sdk/robot/client.cpp | 2 ++ src/viam/sdk/rpc/private/viam_grpc_channel.cpp | 10 ++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index ff2a731d9..a260de414 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -53,9 +53,14 @@ int main(int argc, char* argv[]) { // ensure we can query resources std::vector resource_names = robot->resource_names(); std::cout << "Resources" << std::endl; + if (resource_names.empty()) { + std::cout << "There are no resource names\n"; + } for (const Name& resource : resource_names) { std::cout << "\t" << resource << "\n"; } + std::cout << "About to return\n"; + return EXIT_SUCCESS; } diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index f8e63d221..595dbda50 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -160,6 +160,8 @@ void RobotClient::refresh() { viam::robot::v1::ResourceNamesResponse resp; ClientContext ctx; + std::cout << "getting resource names\n"; + // std::cout << ctx.token << std::endl; const grpc::Status response = impl_->stub_->ResourceNames(ctx, req, &resp); if (is_error_response(response)) { BOOST_LOG_TRIVIAL(error) << "Error getting resource names: " << response.error_message(); diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp index 2dd26ec45..af3cb0732 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp @@ -21,7 +21,8 @@ std::shared_ptr create_viam_auth_channel() { // args.SetSslTargetNameOverride("34.149.207.107"); // args.SetSslTargetNameOverride("app.viam.com"); - return grpc::CreateCustomChannel("app.viam.com", tls_creds, args); + return grpc::CreateCustomChannel( + "webrtc-test-main.jkek76kqnh.local.viam.cloud:8080", tls_creds, args); } std::shared_ptr create_viam_channel( @@ -38,12 +39,17 @@ std::shared_ptr create_viam_channel( args.SetMaxReceiveMessageSize(kMaxMessageSize); if (!token.empty()) { - args.SetString("authorization:", "Bearer " + token); + args.SetString("authorization", "Bearer " + token); } grpc::experimental::TlsChannelCredentialsOptions opts; + auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions()); opts.set_verify_server_certs(false); opts.set_check_call_host(false); + opts.set_min_tls_version(grpc_tls_version::TLS1_2); + // opts.watch_root_certs(); auto tls_creds = grpc::experimental::TlsCredentials(opts); + // args.SetSslTargetNameOverride("webrtc-test-main.jkek76kqnh.viam.cloud"); + // args.SetSslTargetNameOverride("app.viam.com"); return grpc::CreateCustomChannel(target, tls_creds, args); } From d247cc8996aee69ee3afdd6b81245104df0a645b Mon Sep 17 00:00:00 2001 From: Ethan Rodkin Date: Thu, 17 Apr 2025 10:27:05 -0400 Subject: [PATCH 05/37] we can still verify server certs --- src/viam/sdk/rpc/private/viam_grpc_channel.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp index af3cb0732..3e4bc8754 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp @@ -11,7 +11,7 @@ namespace impl { std::shared_ptr create_viam_auth_channel() { grpc::experimental::TlsChannelCredentialsOptions opts; - opts.set_verify_server_certs(false); + // opts.set_verify_server_certs(false); opts.set_check_call_host(false); auto tls_creds = grpc::experimental::TlsCredentials(opts); std::cout << "setting ssl target name override\n"; @@ -43,7 +43,7 @@ std::shared_ptr create_viam_channel( } grpc::experimental::TlsChannelCredentialsOptions opts; auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions()); - opts.set_verify_server_certs(false); + // opts.set_verify_server_certs(false); opts.set_check_call_host(false); opts.set_min_tls_version(grpc_tls_version::TLS1_2); // opts.watch_root_certs(); From 4b7e4c2e523c30f650655210b16348a1ed260796 Mon Sep 17 00:00:00 2001 From: Ethan Rodkin Date: Thu, 17 Apr 2025 11:38:31 -0400 Subject: [PATCH 06/37] cleanup --- .../dial_api_key/example_dial_api_key.cpp | 2 - src/viam/sdk/common/client_helper.cpp | 1 - src/viam/sdk/registry/registry.cpp | 1 - src/viam/sdk/robot/client.cpp | 2 - src/viam/sdk/rpc/dial.cpp | 42 +++---------------- .../sdk/rpc/private/viam_grpc_channel.cpp | 34 ++------------- .../sdk/rpc/private/viam_grpc_channel.hpp | 6 +-- 7 files changed, 9 insertions(+), 79 deletions(-) diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index a260de414..41cc79f4d 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -60,7 +60,5 @@ int main(int argc, char* argv[]) { std::cout << "\t" << resource << "\n"; } - std::cout << "About to return\n"; - return EXIT_SUCCESS; } diff --git a/src/viam/sdk/common/client_helper.cpp b/src/viam/sdk/common/client_helper.cpp index 58f66b335..8831f82b4 100644 --- a/src/viam/sdk/common/client_helper.cpp +++ b/src/viam/sdk/common/client_helper.cpp @@ -31,7 +31,6 @@ ClientContext::ClientContext() : wrapped_context_(std::make_uniqueAddMetadata("authorization", "Bearer " + token); } } diff --git a/src/viam/sdk/registry/registry.cpp b/src/viam/sdk/registry/registry.cpp index 3884d71ab..90cfe9af2 100644 --- a/src/viam/sdk/registry/registry.cpp +++ b/src/viam/sdk/registry/registry.cpp @@ -200,7 +200,6 @@ const google::protobuf::ServiceDescriptor* ResourceServerRegistration::service_d } void Registry::register_resources() { - std::cout << "doing a registry\n"; // Register all components register_resource(); register_resource(); diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 595dbda50..f8e63d221 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -160,8 +160,6 @@ void RobotClient::refresh() { viam::robot::v1::ResourceNamesResponse resp; ClientContext ctx; - std::cout << "getting resource names\n"; - // std::cout << ctx.token << std::endl; const grpc::Status response = impl_->stub_->ResourceNames(ctx, req, &resp); if (is_error_response(response)) { BOOST_LOG_TRIVIAL(error) << "Error getting resource names: " << response.error_message(); diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index d44d9830c..04aac6ac2 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -83,12 +83,9 @@ std::shared_ptr ViamChannel::dial(const char* uri, const boost::optional& options) { const DialOptions opts = options.get_value_or(DialOptions()); - std::cout << "checking if we're disabling webrtc\n"; if (opts.disable_webrtc) { - std::cout << "disabling webrtc\n"; return dial_direct(uri, opts); } - std::cout << "not disabling for some reason\n?"; const std::chrono::duration float_timeout = opts.timeout; void* ptr = init_rust_runtime(); @@ -123,33 +120,9 @@ std::shared_ptr ViamChannel::dial(const char* uri, } std::shared_ptr ViamChannel::dial_direct(const char* uri, const DialOptions& opts) { - std::cerr << "\n\tCreating grpc channel " << uri << "\n"; - // auto grpc_channel = grpc::CreateChannel("app.viam.com:443", - // grpc::SslCredentialsOptions foo; - auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions()); - // auto grpc_channel = grpc::CreateChannel(uri, creds); - auto channel_for_auth = impl::create_viam_auth_channel(); - // auto grpc_channel = grpc::CreateChannel(uri, grpc::InsecureChannelCredentials()); - // grpc_connectivity_state prev = grpc_connectivity_state::GRPC_CHANNEL_READY; - // while (true) { - // grpc_connectivity_state foo = grpc_channel->GetState(true); - // if (foo != prev) { - // std::cout << "foo is " << foo << "\n"; - // prev = foo; - //} - // if (foo == grpc_connectivity_state::GRPC_CHANNEL_READY) { - // break; - //} - //}; - - // auto grpc_channel = impl::create_viam_channel(uri, creds); - // grpc::InsecureChannelCredentials()); - std::cerr << "\n\ttest\n"; - + auto channel_for_auth = impl::create_viam_auth_channel(uri); using namespace proto::rpc::v1; - // auto sopts = ::grpc::StubOptions(); - // std::cout << "suffix for stats is " << sopts.suffix_for_stats() << "\n"; auto auth_stub = AuthService::NewStub(channel_for_auth); ClientContext ctx; AuthenticateRequest req; @@ -158,20 +131,15 @@ std::shared_ptr ViamChannel::dial_direct(const char* uri, const Dia *req.mutable_credentials()->mutable_payload() = opts.credentials->payload(); *req.mutable_credentials()->mutable_type() = opts.credentials->type(); - std::cerr << req.DebugString() << "\n"; - AuthenticateResponse resp; - std::cout << "authenticating now\n"; auto status = auth_stub->Authenticate(ctx, req, &resp); - std::cout << "authenticated\n"; - - std::cerr << "\n\t status: " << status.error_message() << " code: " << status.error_code() - << "\n\t access token: " << resp.access_token() << "\n\n"; ClientContext::token = resp.access_token(); - auto grpc_channel = - impl::create_viam_channel(uri, grpc::InsecureChannelCredentials(), resp.access_token()); + grpc::experimental::TlsChannelCredentialsOptions c_opts; + c_opts.set_check_call_host(false); + auto creds = grpc::experimental::TlsCredentials(c_opts); + auto grpc_channel = impl::create_viam_channel(uri, creds); return std::make_shared(grpc_channel, // nullptr, diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp index 3e4bc8754..6c7408099 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp @@ -9,49 +9,21 @@ namespace viam { namespace sdk { namespace impl { -std::shared_ptr create_viam_auth_channel() { +std::shared_ptr create_viam_auth_channel(const std::string& address) { grpc::experimental::TlsChannelCredentialsOptions opts; - // opts.set_verify_server_certs(false); opts.set_check_call_host(false); auto tls_creds = grpc::experimental::TlsCredentials(opts); - std::cout << "setting ssl target name override\n"; - grpc::ChannelArguments args; - args.SetMaxSendMessageSize(kMaxMessageSize); - args.SetMaxReceiveMessageSize(kMaxMessageSize); - // args.SetSslTargetNameOverride("34.149.207.107"); - // args.SetSslTargetNameOverride("app.viam.com"); - return grpc::CreateCustomChannel( - "webrtc-test-main.jkek76kqnh.local.viam.cloud:8080", tls_creds, args); + return grpc::CreateChannel(address, tls_creds); } std::shared_ptr create_viam_channel( const grpc::string& target, const std::shared_ptr& credentials) { - return create_viam_channel(target, credentials, ""); -} - -std::shared_ptr create_viam_channel( - const grpc::string& target, - const std::shared_ptr& credentials, - const std::string& token) { grpc::ChannelArguments args; args.SetMaxSendMessageSize(kMaxMessageSize); args.SetMaxReceiveMessageSize(kMaxMessageSize); - if (!token.empty()) { - args.SetString("authorization", "Bearer " + token); - } - grpc::experimental::TlsChannelCredentialsOptions opts; - auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions()); - // opts.set_verify_server_certs(false); - opts.set_check_call_host(false); - opts.set_min_tls_version(grpc_tls_version::TLS1_2); - // opts.watch_root_certs(); - auto tls_creds = grpc::experimental::TlsCredentials(opts); - // args.SetSslTargetNameOverride("webrtc-test-main.jkek76kqnh.viam.cloud"); - // args.SetSslTargetNameOverride("app.viam.com"); - - return grpc::CreateCustomChannel(target, tls_creds, args); + return grpc::CreateCustomChannel(target, credentials, args); } } // namespace impl diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp index dd45d06d2..bab110b15 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp @@ -13,11 +13,7 @@ namespace impl { /// size kMaxMessageSize. std::shared_ptr create_viam_channel( const grpc::string& target, const std::shared_ptr& credentials); -std::shared_ptr create_viam_channel( - const grpc::string& target, - const std::shared_ptr& credentials, - const std::string& token); -std::shared_ptr create_viam_auth_channel(); +std::shared_ptr create_viam_auth_channel(const std::string& address); } // namespace impl } // namespace sdk From c41325b1747937cd3860b53ab50dbc96e778de7b Mon Sep 17 00:00:00 2001 From: Ethan Rodkin Date: Thu, 17 Apr 2025 11:41:27 -0400 Subject: [PATCH 07/37] cleanup2 --- src/viam/examples/dial_api_key/example_dial_api_key.cpp | 3 --- src/viam/sdk/rpc/server.cpp | 2 -- 2 files changed, 5 deletions(-) diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index 41cc79f4d..ff2a731d9 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -53,9 +53,6 @@ int main(int argc, char* argv[]) { // ensure we can query resources std::vector resource_names = robot->resource_names(); std::cout << "Resources" << std::endl; - if (resource_names.empty()) { - std::cout << "There are no resource names\n"; - } for (const Name& resource : resource_names) { std::cout << "\t" << resource << "\n"; } diff --git a/src/viam/sdk/rpc/server.cpp b/src/viam/sdk/rpc/server.cpp index 19f66423a..9964abbac 100644 --- a/src/viam/sdk/rpc/server.cpp +++ b/src/viam/sdk/rpc/server.cpp @@ -16,7 +16,6 @@ namespace viam { namespace sdk { Server::Server() : builder_(std::make_unique()) { - std::cout << "making a server\n\n"; builder_->SetMaxReceiveMessageSize(kMaxMessageSize); builder_->SetMaxSendMessageSize(kMaxMessageSize); builder_->SetMaxMessageSize(kMaxMessageSize); @@ -40,7 +39,6 @@ std::shared_ptr Server::lookup_resource_server(const API& api) { } void Server::register_service(grpc::Service* service) { - std::cout << "registering a service\n"; if (!builder_) { throw Exception("Cannot register a new service after the server has started"); } From c549c07188396d2b9bf9aafeaaeffda88c8c13f6 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 22 Apr 2025 09:51:37 -0400 Subject: [PATCH 08/37] docs --- src/viam/sdk/rpc/private/viam_grpc_channel.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp index bab110b15..daad0d6c9 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp @@ -13,6 +13,9 @@ namespace impl { /// size kMaxMessageSize. std::shared_ptr create_viam_channel( const grpc::string& target, const std::shared_ptr& credentials); + +/// @brief Like grpc::CreateChannel, but for the express purpose of returning a channel for making +/// an AuthService request. std::shared_ptr create_viam_auth_channel(const std::string& address); } // namespace impl From 41e696025f96b1e4b9abd8596abf09e1594afa6c Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 24 Apr 2025 14:24:06 -0400 Subject: [PATCH 09/37] use instance for global token --- src/viam/sdk/common/client_helper.cpp | 5 ++++- src/viam/sdk/common/client_helper.hpp | 2 -- src/viam/sdk/common/instance.hpp | 2 ++ src/viam/sdk/common/private/instance.hpp | 5 +++++ src/viam/sdk/rpc/dial.cpp | 5 ++++- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/viam/sdk/common/client_helper.cpp b/src/viam/sdk/common/client_helper.cpp index e491dfae4..16b3d4ec0 100644 --- a/src/viam/sdk/common/client_helper.cpp +++ b/src/viam/sdk/common/client_helper.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -24,11 +25,13 @@ bool isStatusCancelled(int status) noexcept { } } // namespace client_helper_details -std::string ClientContext::token; ClientContext::ClientContext() : wrapped_context_(std::make_unique()) { set_client_ctx_authority_(); add_viam_client_version_(); + + const std::string& token = + Instance::current(Instance::Creation::open_existing).impl_->direct_dial_token; if (!token.empty()) { wrapped_context_->AddMetadata("authorization", "Bearer " + token); } diff --git a/src/viam/sdk/common/client_helper.hpp b/src/viam/sdk/common/client_helper.hpp index 39f9dbd07..2fb918c55 100644 --- a/src/viam/sdk/common/client_helper.hpp +++ b/src/viam/sdk/common/client_helper.hpp @@ -29,8 +29,6 @@ class ClientContext { void try_cancel(); - static std::string token; - operator GrpcClientContext*(); operator const GrpcClientContext*() const; diff --git a/src/viam/sdk/common/instance.hpp b/src/viam/sdk/common/instance.hpp index de10ae896..557301f46 100644 --- a/src/viam/sdk/common/instance.hpp +++ b/src/viam/sdk/common/instance.hpp @@ -29,6 +29,8 @@ class Instance { private: friend class Registry; friend class LogManager; + friend class ViamChannel; + friend class ClientContext; struct Impl; std::unique_ptr impl_; diff --git a/src/viam/sdk/common/private/instance.hpp b/src/viam/sdk/common/private/instance.hpp index cb09480d9..68ce1f37c 100644 --- a/src/viam/sdk/common/private/instance.hpp +++ b/src/viam/sdk/common/private/instance.hpp @@ -10,6 +10,11 @@ namespace sdk { struct Instance::Impl { Registry registry; LogManager log_mgr; + + // When dialing directly to gpc (no rust-utils/webrtc) there is global state in the form of a + // bearer token that needs to be added as metadata to every client call. This variable stores + // the auth token; see client_helper.cpp and dial.cpp for access. + std::string direct_dial_token; }; } // namespace sdk diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 41f0ed19c..9bff5fbb2 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -153,7 +154,9 @@ ViamChannel ViamChannel::dial_direct(const char* uri, const DialOptions& opts) { auto status = auth_stub->Authenticate(ctx, req, &resp); - ClientContext::token = resp.access_token(); + Instance::current(Instance::Creation::open_existing).impl_->direct_dial_token = + resp.access_token(); + grpc::experimental::TlsChannelCredentialsOptions c_opts; c_opts.set_check_call_host(false); auto creds = grpc::experimental::TlsCredentials(c_opts); From d0c3f21de31fd552ae4397de0944a412108c076c Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 24 Apr 2025 16:26:51 -0400 Subject: [PATCH 10/37] remove dtor logging --- src/viam/sdk/log/logging.hpp | 4 ++++ src/viam/sdk/module/service.cpp | 5 ++--- src/viam/sdk/robot/client.cpp | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/viam/sdk/log/logging.hpp b/src/viam/sdk/log/logging.hpp index aa86c40d2..6bb568b1e 100644 --- a/src/viam/sdk/log/logging.hpp +++ b/src/viam/sdk/log/logging.hpp @@ -150,6 +150,8 @@ BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_time, /// @ingroup Log /// /// Use this macro to generate log messages pertaining to the SDK at large. +/// @warning Using this macro outside of the lifetime of LogManager is UB and for this reason +/// logging in destructors should be done with caution or avoided entirely. #define VIAM_SDK_LOG(level) VIAM_SDK_LOG_IMPL(::viam::sdk::LogManager::get().global_logger(), level) /// @brief Log macro for resource-level logs. @@ -158,6 +160,8 @@ BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_time, /// This macro can only be called from the definition of a member function of a class inheriting /// @ref Resource. It will log messages to the log source of that specific resource, allowing /// resource-level log filtering. +/// @warning Using this macro outside of the lifetime of LogManager is UB and for this reason +/// logging in destructors should be done with caution or avoided entirely. #define VIAM_RESOURCE_LOG(level) VIAM_SDK_LOG_IMPL(this->logger_, level) } // namespace sdk diff --git a/src/viam/sdk/module/service.cpp b/src/viam/sdk/module/service.cpp index b270f629d..30aaeaef5 100644 --- a/src/viam/sdk/module/service.cpp +++ b/src/viam/sdk/module/service.cpp @@ -254,15 +254,14 @@ ModuleService::ModuleService(int argc, } ModuleService::~ModuleService() { - // TODO(RSDK-5509): Run registered cleanup functions here. - VIAM_SDK_LOG(info) << "Shutting down gracefully."; + std::cout << "Shutting down gracefully."; server_->shutdown(); if (parent_) { try { parent_->close(); } catch (const std::exception& exc) { - VIAM_SDK_LOG(error) << exc.what(); + std::cerr << exc.what(); } } } diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 6e172ac24..5ed2f42f0 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -120,9 +120,9 @@ RobotClient::~RobotClient() { try { this->close(); } catch (const std::exception& e) { - VIAM_SDK_LOG(error) << "Received err while closing RobotClient: " << e.what(); + std::cerr << "Received err while closing RobotClient: " << e.what(); } catch (...) { - VIAM_SDK_LOG(error) << "Received unknown err while closing RobotClient"; + std::cerr << "Received unknown err while closing RobotClient"; } } } From a53494ac007e30365a008c36e88662edefb9cfcb Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 24 Apr 2025 16:32:09 -0400 Subject: [PATCH 11/37] newline --- src/viam/sdk/module/service.cpp | 2 +- src/viam/sdk/robot/client.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/viam/sdk/module/service.cpp b/src/viam/sdk/module/service.cpp index 30aaeaef5..ae7c5707a 100644 --- a/src/viam/sdk/module/service.cpp +++ b/src/viam/sdk/module/service.cpp @@ -254,7 +254,7 @@ ModuleService::ModuleService(int argc, } ModuleService::~ModuleService() { - std::cout << "Shutting down gracefully."; + std::cout << "Shutting down gracefully.\n"; server_->shutdown(); if (parent_) { diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 5ed2f42f0..4d70ae4d1 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -120,9 +120,9 @@ RobotClient::~RobotClient() { try { this->close(); } catch (const std::exception& e) { - std::cerr << "Received err while closing RobotClient: " << e.what(); + std::cerr << "Received err while closing RobotClient: " << e.what() << "\n"; } catch (...) { - std::cerr << "Received unknown err while closing RobotClient"; + std::cerr << "Received unknown err while closing RobotClient\n"; } } } From bbc0adf2c7cfeda4dd4dffbe09a29f65c3d6278c Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 24 Apr 2025 16:56:08 -0400 Subject: [PATCH 12/37] join if joinable --- src/viam/sdk/robot/client.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 4d70ae4d1..57522ebbb 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -131,7 +131,9 @@ void RobotClient::close() { should_refresh_.store(false); for (auto& thread : threads_) { - thread.join(); + if (thread.joinable()) { + thread.join(); + } } threads_.clear(); From c3ff877447c1de1cdfa5c918ac1be641fa379d16 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 1 May 2025 15:36:07 -0400 Subject: [PATCH 13/37] spacing and variable simplification --- .../dial_api_key/example_dial_api_key.cpp | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index 95ceae37c..7ad68f655 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -27,31 +27,32 @@ int main(int argc, char* argv[]) { "uri", po::value(), "URI of robot")( "entity", po::value(), "api key ID")( "api-key", po::value(), "api key secret"); + po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); + if (vm.count("help")) { std::cout << desc << std::endl; return 0; } - boost::optional opts; - DialOptions dopts; - opts = dopts; + + DialOptions opts; + if (vm.count("entity") && vm.count("api-key")) { - DialOptions dial_options; - dial_options.auth_entity = vm["entity"].as(); + opts.auth_entity = vm["entity"].as(); Credentials credentials("api-key", vm["api-key"].as()); - dial_options.credentials = credentials; - opts = dial_options; + opts.credentials = credentials; } - opts->disable_webrtc = true; - Options options(1, opts); + + opts.disable_webrtc = true; // connect to robot, ensure we can refresh it std::shared_ptr robot = - RobotClient::at_address(vm["uri"].as(), options); + RobotClient::at_address(vm["uri"].as(), Options(1, opts)); // ensure we can query resources std::vector resource_names = robot->resource_names(); + VIAM_SDK_LOG(info) << "Resources:"; for (const Name& resource : resource_names) { VIAM_SDK_LOG(info) << resource; From 7d8175b58a857fb13e7a2f51d31b7e0eb5e4242f Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Fri, 2 May 2025 12:45:50 -0400 Subject: [PATCH 14/37] reset parent to prevent double close --- src/viam/sdk/module/service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viam/sdk/module/service.cpp b/src/viam/sdk/module/service.cpp index 9156b90e8..8921f9190 100644 --- a/src/viam/sdk/module/service.cpp +++ b/src/viam/sdk/module/service.cpp @@ -259,7 +259,7 @@ ModuleService::~ModuleService() { if (parent_) { try { - parent_->close(); + parent_.reset(); } catch (const std::exception& exc) { std::cerr << exc.what(); } From 26c2225ddd4bc51ce133f8f930d752c1336a8ca4 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Fri, 2 May 2025 13:36:14 -0400 Subject: [PATCH 15/37] do not disable webrtc by default --- src/viam/examples/dial_api_key/example_dial_api_key.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index 7ad68f655..d24903a16 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -44,8 +44,6 @@ int main(int argc, char* argv[]) { opts.credentials = credentials; } - opts.disable_webrtc = true; - // connect to robot, ensure we can refresh it std::shared_ptr robot = RobotClient::at_address(vm["uri"].as(), Options(1, opts)); From 0cd87238af5db89a43ae4bd42c92b589404eb249 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Fri, 2 May 2025 13:40:33 -0400 Subject: [PATCH 16/37] revert dtor logging --- src/viam/sdk/log/logging.hpp | 4 ---- src/viam/sdk/module/service.cpp | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/viam/sdk/log/logging.hpp b/src/viam/sdk/log/logging.hpp index 144e551f9..657088eb4 100644 --- a/src/viam/sdk/log/logging.hpp +++ b/src/viam/sdk/log/logging.hpp @@ -151,8 +151,6 @@ BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_time, /// @ingroup Log /// /// Use this macro to generate log messages pertaining to the SDK at large. -/// @warning Using this macro outside of the lifetime of LogManager is UB and for this reason -/// logging in destructors should be done with caution or avoided entirely. #define VIAM_SDK_LOG(level) VIAM_SDK_LOG_IMPL(::viam::sdk::LogManager::get().global_logger(), level) /// @brief Log macro for resource-level logs. @@ -161,8 +159,6 @@ BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_time, /// This macro can only be called from the definition of a member function of a class inheriting /// @ref Resource. It will log messages to the log source of that specific resource, allowing /// resource-level log filtering. -/// @warning Using this macro outside of the lifetime of LogManager is UB and for this reason -/// logging in destructors should be done with caution or avoided entirely. #define VIAM_RESOURCE_LOG(level) VIAM_SDK_LOG_IMPL(this->logger_, level) } // namespace sdk diff --git a/src/viam/sdk/module/service.cpp b/src/viam/sdk/module/service.cpp index 8921f9190..3e4c7a533 100644 --- a/src/viam/sdk/module/service.cpp +++ b/src/viam/sdk/module/service.cpp @@ -254,14 +254,14 @@ ModuleService::ModuleService(int argc, } ModuleService::~ModuleService() { - std::cout << "Shutting down gracefully.\n"; + VIAM_SDK_LOG(info) << "Shutting down gracefully.\n"; server_->shutdown(); if (parent_) { try { parent_.reset(); } catch (const std::exception& exc) { - std::cerr << exc.what(); + VIAM_SDK_LOG(error) << exc.what(); } } } From d3cdb58d3da2011c32d4a122d633ffcf344fba56 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Fri, 2 May 2025 14:15:09 -0400 Subject: [PATCH 17/37] unshare robot client --- src/viam/sdk/module/service.hpp | 2 +- src/viam/sdk/robot/client.cpp | 8 ++++---- src/viam/sdk/robot/client.hpp | 6 +++--- src/viam/sdk/tests/test_robot.cpp | 20 ++++++++++---------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/viam/sdk/module/service.hpp b/src/viam/sdk/module/service.hpp index 714d55051..03c35e6d3 100644 --- a/src/viam/sdk/module/service.hpp +++ b/src/viam/sdk/module/service.hpp @@ -68,7 +68,7 @@ class ModuleService { std::unique_ptr module_; - std::shared_ptr parent_; + std::unique_ptr parent_; std::string parent_addr_; std::unique_ptr server_; diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 740c50fff..d2e4243c2 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -270,9 +270,9 @@ void RobotClient::log(const std::string& name, } } -std::shared_ptr RobotClient::with_channel(ViamChannel channel, +std::unique_ptr RobotClient::with_channel(ViamChannel channel, const Options& options) { - auto robot = std::make_shared(std::move(channel)); + auto robot = std::make_unique(std::move(channel)); robot->refresh_interval_ = std::chrono::seconds{options.refresh_interval()}; robot->should_refresh_ = (robot->refresh_interval_ > std::chrono::seconds{0}); if (robot->should_refresh_) { @@ -283,7 +283,7 @@ std::shared_ptr RobotClient::with_channel(ViamChannel channel, return robot; }; -std::shared_ptr RobotClient::at_address(const std::string& address, +std::unique_ptr RobotClient::at_address(const std::string& address, const Options& options) { const char* uri = address.c_str(); auto robot = @@ -292,7 +292,7 @@ std::shared_ptr RobotClient::at_address(const std::string& address, return robot; }; -std::shared_ptr RobotClient::at_local_socket(const std::string& address, +std::unique_ptr RobotClient::at_local_socket(const std::string& address, const Options& options) { const std::string addr = "unix://" + address; auto robot = RobotClient::with_channel( diff --git a/src/viam/sdk/robot/client.hpp b/src/viam/sdk/robot/client.hpp index 3c19a9edc..00a1f7d01 100644 --- a/src/viam/sdk/robot/client.hpp +++ b/src/viam/sdk/robot/client.hpp @@ -91,7 +91,7 @@ class RobotClient { /// @brief Create a robot client connected to the robot at the provided address. /// @param address The address of the robot (IP address, URI, URL, etc.) /// @param options Options for connecting and refreshing. - static std::shared_ptr at_address(const std::string& address, + static std::unique_ptr at_address(const std::string& address, const Options& options); /// @brief Creates a robot client connected to the robot at the provided local socket. @@ -99,13 +99,13 @@ class RobotClient { /// @param options Options for connecting and refreshing. /// Creates a direct connection to the robot using the `unix://` scheme. /// Only useful for connecting to robots across Unix sockets. - static std::shared_ptr at_local_socket(const std::string& address, + static std::unique_ptr at_local_socket(const std::string& address, const Options& options); /// @brief Creates a robot client connected to the provided channel. /// @param channel The channel to connect with. /// @param options Options for connecting and refreshing. - static std::shared_ptr with_channel(ViamChannel channel, const Options& options); + static std::unique_ptr with_channel(ViamChannel channel, const Options& options); std::vector resource_names() const; diff --git a/src/viam/sdk/tests/test_robot.cpp b/src/viam/sdk/tests/test_robot.cpp index 18137d258..b0b735bbf 100644 --- a/src/viam/sdk/tests/test_robot.cpp +++ b/src/viam/sdk/tests/test_robot.cpp @@ -54,7 +54,7 @@ void robot_client_to_mocks_pipeline(F&& test_case) { // Run the passed-in test case on the created stack and give access to the // created RobotClient and MockRobotService. - std::forward(test_case)(client, service); + std::forward(test_case)(std::move(client), service); } BOOST_AUTO_TEST_CASE(test_registering_resources) { @@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(test_registering_resources) { BOOST_AUTO_TEST_CASE(test_resource_names) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { std::vector names = client->resource_names(); auto mocks = mock_resource_names_response(); BOOST_TEST(names == mocks, boost::test_tools::per_element()); @@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(test_resource_names) { // the proto and custom type versions. BOOST_AUTO_TEST_CASE(test_frame_system_config) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { auto configs = mock_config_response(); auto config1 = configs[0]; auto config2 = configs[1]; @@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE(test_frame_system_config) { BOOST_AUTO_TEST_CASE(test_get_frame_system_config) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { auto mock_fs_config = mock_config_response(); auto fs_config = client->get_frame_system_config(); @@ -150,7 +150,7 @@ BOOST_AUTO_TEST_CASE(test_get_frame_system_config) { // the proto and custom type versions. BOOST_AUTO_TEST_CASE(test_operation) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { auto ops = mock_operations_response(); auto op1 = ops[0]; auto op2 = ops[1]; @@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(test_operation) { BOOST_AUTO_TEST_CASE(test_get_operations) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { auto ops = client->get_operations(); auto mock_ops = mock_operations_response(); @@ -179,7 +179,7 @@ BOOST_AUTO_TEST_CASE(test_get_operations) { BOOST_AUTO_TEST_CASE(test_transform_pose) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { pose_in_frame pif; auto pose = client->transform_pose(pif, "", {}); auto mock_pose = mock_transform_response(); @@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE(test_transform_pose) { BOOST_AUTO_TEST_CASE(test_get_machine_status) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { auto status = client->get_machine_status(); BOOST_CHECK_EQUAL(status, RobotClient::status::k_running); @@ -199,7 +199,7 @@ BOOST_AUTO_TEST_CASE(test_get_machine_status) { BOOST_AUTO_TEST_CASE(test_stop_all) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { std::shared_ptr rb = service.resource_manager()->resource("mock_motor"); auto motor = std::dynamic_pointer_cast(rb); BOOST_CHECK(motor); @@ -215,7 +215,7 @@ BOOST_AUTO_TEST_CASE(test_stop_all) { BOOST_AUTO_TEST_CASE(test_get_resource) { robot_client_to_mocks_pipeline( - [](std::shared_ptr client, MockRobotService& service) -> void { + [](std::unique_ptr client, MockRobotService& service) -> void { auto mock_motor = client->resource_by_name("mock_motor"); BOOST_CHECK(mock_motor); From beab7d22f4f710a261363c2b64311aa1a73b4b6f Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 5 May 2025 11:02:18 -0400 Subject: [PATCH 18/37] include fixes and helper rename --- src/viam/sdk/robot/client.cpp | 2 +- src/viam/sdk/rpc/dial.cpp | 12 +++++++----- src/viam/sdk/rpc/private/viam_grpc_channel.cpp | 2 +- src/viam/sdk/rpc/private/viam_grpc_channel.hpp | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index d2e4243c2..f8c007778 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -296,7 +296,7 @@ std::unique_ptr RobotClient::at_local_socket(const std::string& add const Options& options) { const std::string addr = "unix://" + address; auto robot = RobotClient::with_channel( - ViamChannel(sdk::impl::create_viam_channel(addr, grpc::InsecureChannelCredentials())), + ViamChannel(sdk::impl::create_viam_grpc_channel(addr, grpc::InsecureChannelCredentials())), options); return robot; diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 9bff5fbb2..f44079f02 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -1,4 +1,3 @@ -#include #include #include @@ -8,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +16,7 @@ #include #include #include + #include #include #include @@ -133,9 +134,10 @@ ViamChannel ViamChannel::dial(const char* uri, const boost::optional& ViamChannel::channel() const { diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp index 6c7408099..18c85f36d 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp @@ -17,7 +17,7 @@ std::shared_ptr create_viam_auth_channel(const std::string& addre return grpc::CreateChannel(address, tls_creds); } -std::shared_ptr create_viam_channel( +std::shared_ptr create_viam_grpc_channel( const grpc::string& target, const std::shared_ptr& credentials) { grpc::ChannelArguments args; args.SetMaxSendMessageSize(kMaxMessageSize); diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp index daad0d6c9..30a50000e 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp @@ -11,7 +11,7 @@ namespace impl { /// @brief Like grpc::CreateChannel, but returns a channel suitable for transmitting messages of /// size kMaxMessageSize. -std::shared_ptr create_viam_channel( +std::shared_ptr create_viam_grpc_channel( const grpc::string& target, const std::shared_ptr& credentials); /// @brief Like grpc::CreateChannel, but for the express purpose of returning a channel for making From 89104c93a8eeea02dac9b071cc0b6e893b717910 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 5 May 2025 12:22:35 -0400 Subject: [PATCH 19/37] document direct dial uri --- src/viam/sdk/rpc/dial.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/viam/sdk/rpc/dial.hpp b/src/viam/sdk/rpc/dial.hpp index 8154ea557..c0bc31037 100644 --- a/src/viam/sdk/rpc/dial.hpp +++ b/src/viam/sdk/rpc/dial.hpp @@ -82,6 +82,8 @@ struct DialOptions { /// @brief Bypass WebRTC and connect directly to the robot. /// This dials directly through grpc bypassing rust utils. + /// @remark Direct dialing should generally be done with a machine URI of the form + /// ..local.viam.cloud:8080 bool disable_webrtc = false; /// @brief Duration before the dial connection times out From 8d68fd2f92f9e2543bdcf1e57c47686ed0059645 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 5 May 2025 12:30:40 -0400 Subject: [PATCH 20/37] factor auth request into helper function --- src/viam/sdk/rpc/dial.cpp | 12 +++++++++++- src/viam/sdk/rpc/dial.hpp | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index f44079f02..a6da81490 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -140,7 +140,7 @@ ViamChannel ViamChannel::dial(const char* uri, const boost::optionalAuthenticate(ctx, req, &resp); + if (!status.ok()) { + VIAM_SDK_LOG(warn) << "Direct dial authentication request failed: " + << status.error_message(); + throw GRPCException(&status); + } + Instance::current(Instance::Creation::open_existing).impl_->direct_dial_token = resp.access_token(); +} + +ViamChannel ViamChannel::dial_direct(const char* uri, const DialOptions& opts) { + ViamChannel::set_bearer_token(uri, opts); grpc::experimental::TlsChannelCredentialsOptions c_opts; c_opts.set_check_call_host(false); diff --git a/src/viam/sdk/rpc/dial.hpp b/src/viam/sdk/rpc/dial.hpp index c0bc31037..cb3c70905 100644 --- a/src/viam/sdk/rpc/dial.hpp +++ b/src/viam/sdk/rpc/dial.hpp @@ -49,6 +49,9 @@ class ViamChannel { private: struct impl; + // Sets the global bearer token used in client requests for direct dialing over gRPC. + static void set_bearer_token(const char* uri, const DialOptions& opts); + static ViamChannel dial_direct(const char* uri, const DialOptions& opts); std::shared_ptr channel_; From 29cfd3598cf7a2c1034a7748bafdf786a55c3cfa Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 5 May 2025 12:31:20 -0400 Subject: [PATCH 21/37] fix doxygen comment --- src/viam/sdk/rpc/dial.hpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/viam/sdk/rpc/dial.hpp b/src/viam/sdk/rpc/dial.hpp index cb3c70905..dfbceb573 100644 --- a/src/viam/sdk/rpc/dial.hpp +++ b/src/viam/sdk/rpc/dial.hpp @@ -33,11 +33,12 @@ class ViamChannel { /// @throws Exception if it is unable to establish a connection to the provided URI static ViamChannel dial(const char* uri, const boost::optional& options); - // @brief Dials to a robot at the given URI address, using the provided dial options (or default - // options is none are provided). Additionally specifies that this dial is an initial connection - // attempt and so uses the initial connection options. - /// In general, use of this method is discouraged. `RobotClient::at_address(...)` is the - /// preferred method to connect to a robot, and creates the channel itself. + /// @brief Dials to a robot at the given URI address, using the provided dial options (or + /// default options is none are provided). + /// Additionally specifies that this dial is an initial + /// connection attempt and so uses the initial connection options. In general, use of this + /// method is discouraged. `RobotClient::at_address(...)` is the preferred method to connect to + /// a robot, and creates the channel itself. /// @throws Exception if it is unable to establish a connection to the provided URI within /// the given number of initial connection attempts static ViamChannel dial_initial(const char* uri, const boost::optional& options); From fde961cbb5c85a7cb692644dee198251299b28cb Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 5 May 2025 12:36:17 -0400 Subject: [PATCH 22/37] ifdef namespace alias --- src/viam/sdk/rpc/dial.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index a6da81490..ab8274fd1 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -157,8 +157,8 @@ void ViamChannel::set_bearer_token(const char* uri, const DialOptions& opts) { auto status = auth_stub->Authenticate(ctx, req, &resp); if (!status.ok()) { - VIAM_SDK_LOG(warn) << "Direct dial authentication request failed: " - << status.error_message(); + VIAM_SDK_LOG(error) << "Direct dial authentication request failed: " + << status.error_message(); throw GRPCException(&status); } @@ -169,9 +169,16 @@ void ViamChannel::set_bearer_token(const char* uri, const DialOptions& opts) { ViamChannel ViamChannel::dial_direct(const char* uri, const DialOptions& opts) { ViamChannel::set_bearer_token(uri, opts); - grpc::experimental::TlsChannelCredentialsOptions c_opts; +#ifndef VIAMCPPSDK_GRPCXX_LEGACY_FWD + namespace grpc_experimental = grpc::experimental; + +#else + namespace grpc_experimental = grpc_impl::experimental; +#endif + + grpc_experimental::TlsChannelCredentialsOptions c_opts; c_opts.set_check_call_host(false); - auto creds = grpc::experimental::TlsCredentials(c_opts); + auto creds = grpc_experimental::TlsCredentials(c_opts); return ViamChannel(sdk::impl::create_viam_grpc_channel(uri, creds)); } From 44d8601da71e25790dcb0d334466f7bf38df342e Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 12 May 2025 15:59:50 -0400 Subject: [PATCH 23/37] use ifdef for direct dial --- CMakeLists.txt | 2 ++ src/viam/sdk/common/grpc_fwd.hpp.in | 10 ++++++ src/viam/sdk/rpc/dial.cpp | 33 +++++++++++-------- src/viam/sdk/rpc/dial.hpp | 3 -- .../sdk/rpc/private/viam_grpc_channel.cpp | 2 ++ .../sdk/rpc/private/viam_grpc_channel.hpp | 2 ++ 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 492c198ca..512c3e58e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -457,6 +457,8 @@ endif() if (VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.32.0) set(VIAMCPPSDK_GRPCXX_LEGACY_FWD 1) +elseif(VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.43.0) + set(VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL 1) endif() include(FetchContent) diff --git a/src/viam/sdk/common/grpc_fwd.hpp.in b/src/viam/sdk/common/grpc_fwd.hpp.in index 39bafd3ac..9a148e2ce 100644 --- a/src/viam/sdk/common/grpc_fwd.hpp.in +++ b/src/viam/sdk/common/grpc_fwd.hpp.in @@ -2,6 +2,16 @@ #cmakedefine VIAMCPPSDK_GRPCXX_LEGACY_FWD +// Preprocessor definition for direct dial support in grpc. +// For grpc >= 1.43.0 it is possible to dial directly over grpc. For older versions, +// `VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL` is defined. +// The default behavior is for the variable to be undefined, providing the behavior for recent +// versions. If you are experiencing compilation errors in an installed version of the SDK it may be +// due to a mismatch with the configured version of this header and the grpc version found by the +// compiler. You may wish to comment/uncomment the define above as needed, or add the definition +// with `-D` to the compiler. +#cmakedefine VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL + // Forward declaration file grpc client and server types. // This file provides includes for recent (>= 1.32.0) versions of grpc or older // versions, depending on if `VIAMCPPSDK_GRPCXX_LEGACY_FWD` is defined. diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index ab8274fd1..3e0c9564d 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -104,8 +105,15 @@ ViamChannel ViamChannel::dial_initial(const char* uri, ViamChannel ViamChannel::dial(const char* uri, const boost::optional& options) { const DialOptions opts = options.get_value_or(DialOptions()); + // If this flag is passed, try to dial directly through grpc if possible. + // If grpc is too old to do a direct dial, we fall back to the rust version below even if + // disable_webrtc was pased. This is consistent with a hoped-for future semantic where rust + // would get the disable_webrtc flag as well rather than deducing it from the URI format, which + // is what it currently does. if (opts.disable_webrtc) { +#ifndef VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL return dial_direct(uri, opts); +#endif } const std::chrono::duration float_timeout = opts.timeout; @@ -140,7 +148,11 @@ ViamChannel ViamChannel::dial(const char* uri, const boost::optionaldirect_dial_token = resp.access_token(); -} - -ViamChannel ViamChannel::dial_direct(const char* uri, const DialOptions& opts) { - ViamChannel::set_bearer_token(uri, opts); - -#ifndef VIAMCPPSDK_GRPCXX_LEGACY_FWD - namespace grpc_experimental = grpc::experimental; -#else - namespace grpc_experimental = grpc_impl::experimental; -#endif - - grpc_experimental::TlsChannelCredentialsOptions c_opts; + grpc::experimental::TlsChannelCredentialsOptions c_opts; c_opts.set_check_call_host(false); - auto creds = grpc_experimental::TlsCredentials(c_opts); + auto creds = grpc::experimental::TlsCredentials(c_opts); return ViamChannel(sdk::impl::create_viam_grpc_channel(uri, creds)); +#else + throw std::logic_error("Tried to call dial_direct on unsupported grpc version " + + grpc::Version()); +#endif } const std::shared_ptr& ViamChannel::channel() const { diff --git a/src/viam/sdk/rpc/dial.hpp b/src/viam/sdk/rpc/dial.hpp index dfbceb573..718a6dd0e 100644 --- a/src/viam/sdk/rpc/dial.hpp +++ b/src/viam/sdk/rpc/dial.hpp @@ -50,9 +50,6 @@ class ViamChannel { private: struct impl; - // Sets the global bearer token used in client requests for direct dialing over gRPC. - static void set_bearer_token(const char* uri, const DialOptions& opts); - static ViamChannel dial_direct(const char* uri, const DialOptions& opts); std::shared_ptr channel_; diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp index 18c85f36d..fe44fa491 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.cpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.cpp @@ -9,6 +9,7 @@ namespace viam { namespace sdk { namespace impl { +#ifndef VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL std::shared_ptr create_viam_auth_channel(const std::string& address) { grpc::experimental::TlsChannelCredentialsOptions opts; opts.set_check_call_host(false); @@ -16,6 +17,7 @@ std::shared_ptr create_viam_auth_channel(const std::string& addre return grpc::CreateChannel(address, tls_creds); } +#endif std::shared_ptr create_viam_grpc_channel( const grpc::string& target, const std::shared_ptr& credentials) { diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp index 30a50000e..8d74a9f2e 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp @@ -14,9 +14,11 @@ namespace impl { std::shared_ptr create_viam_grpc_channel( const grpc::string& target, const std::shared_ptr& credentials); +#ifndef VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL /// @brief Like grpc::CreateChannel, but for the express purpose of returning a channel for making /// an AuthService request. std::shared_ptr create_viam_auth_channel(const std::string& address); +#endif } // namespace impl } // namespace sdk From c7d8b4c4f4505a67be0874e81a2336a3f11d262e Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 12 May 2025 16:23:30 -0400 Subject: [PATCH 24/37] replace with custom unique_ptr deleters --- src/viam/sdk/rpc/dial.cpp | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 3e0c9564d..9393c9deb 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -28,30 +28,22 @@ namespace viam { namespace sdk { struct ViamChannel::impl { - impl(const char* path, void* runtime) : path(path), rust_runtime(runtime) {} - - impl(const impl&) = delete; - - impl(impl&& other) noexcept - : path(std::exchange(other.path, nullptr)), - rust_runtime(std::exchange(other.rust_runtime, nullptr)) {} - - impl& operator=(const impl&) = delete; - - impl& operator=(impl&& other) noexcept { - path = std::exchange(other.path, nullptr); - rust_runtime = std::exchange(other.rust_runtime, nullptr); + struct cstr_delete { + void operator()(const char* str) noexcept { + free_string(str); + } + }; - return *this; - } + struct rust_rt_delete { + void operator()(void* rt) noexcept { + free_rust_runtime(rt); + } + }; - ~impl() { - free_string(path); - free_rust_runtime(rust_runtime); - } + impl(const char* path, void* runtime) : path(path), rust_runtime(runtime) {} - const char* path; - void* rust_runtime; + std::unique_ptr path; + std::unique_ptr rust_runtime; }; ViamChannel::ViamChannel(std::shared_ptr channel, const char* path, void* runtime) From afa6c705926a3dd92229f958fe250924a82748ed Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Mon, 12 May 2025 16:53:51 -0400 Subject: [PATCH 25/37] fix if stmt --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 512c3e58e..ebf3a4ec7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -457,7 +457,9 @@ endif() if (VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.32.0) set(VIAMCPPSDK_GRPCXX_LEGACY_FWD 1) -elseif(VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.43.0) +endif() + +if(VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.43.0) set(VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL 1) endif() From 8ba5efac7d38d133bd04a6f91fae1e5706b26f69 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Tue, 13 May 2025 12:00:13 -0400 Subject: [PATCH 26/37] overhaul options and refresh interval --- src/viam/examples/camera/example_camera.cpp | 8 ++++---- src/viam/examples/dial/example_dial.cpp | 4 ++-- .../dial_api_key/example_dial_api_key.cpp | 2 +- .../example_audio_classification_client.cpp | 4 ++-- src/viam/examples/modules/complex/client.cpp | 10 ++++------ src/viam/examples/modules/simple/client.cpp | 11 +++++----- src/viam/examples/motor/example_motor.cpp | 8 ++++---- src/viam/sdk/module/service.cpp | 5 +++-- src/viam/sdk/robot/client.cpp | 13 ++++++------ src/viam/sdk/robot/client.hpp | 20 +++++++++++++------ src/viam/sdk/rpc/dial.cpp | 8 -------- src/viam/sdk/rpc/dial.hpp | 15 -------------- src/viam/sdk/tests/test_robot.cpp | 2 +- 13 files changed, 47 insertions(+), 63 deletions(-) diff --git a/src/viam/examples/camera/example_camera.cpp b/src/viam/examples/camera/example_camera.cpp index a3aa98b8f..03ddb2b59 100644 --- a/src/viam/examples/camera/example_camera.cpp +++ b/src/viam/examples/camera/example_camera.cpp @@ -31,11 +31,11 @@ int main() try { dial_options.allow_insecure_downgrade = (credentials.type().empty() && credentials.payload().empty()); - // Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial - // options - vs::Options options = vs::Options(1, dial_options); + // Pass the scheduled refresh interval of the robot (0 seconds = only on config update) and the + // dial options + std::shared_ptr robot = + vs::RobotClient::at_address(robot_address, std::chrono::seconds{1}, dial_options); - std::shared_ptr robot = vs::RobotClient::at_address(robot_address, options); VIAM_SDK_LOG(info) << "Successfully connected to the robot"; std::vector resource_names = robot->resource_names(); diff --git a/src/viam/examples/dial/example_dial.cpp b/src/viam/examples/dial/example_dial.cpp index b85d636e2..5c9d84fe7 100644 --- a/src/viam/examples/dial/example_dial.cpp +++ b/src/viam/examples/dial/example_dial.cpp @@ -27,10 +27,10 @@ int main() { std::string payload = ""; dial_options.credentials = Credentials(type, payload); std::string address(uri); - Options options(1, dial_options); // connect to robot, ensure we can refresh it - std::shared_ptr robot = RobotClient::at_address(address, options); + std::shared_ptr robot = + RobotClient::at_address(address, std::chrono::seconds{1}, dial_options); // ensure we can query resources std::vector resource_names = robot->resource_names(); diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index d24903a16..c4c795305 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -46,7 +46,7 @@ int main(int argc, char* argv[]) { // connect to robot, ensure we can refresh it std::shared_ptr robot = - RobotClient::at_address(vm["uri"].as(), Options(1, opts)); + RobotClient::at_address(vm["uri"].as(), std::chrono::seconds{1}, opts); // ensure we can query resources std::vector resource_names = robot->resource_names(); diff --git a/src/viam/examples/mlmodel/example_audio_classification_client.cpp b/src/viam/examples/mlmodel/example_audio_classification_client.cpp index dde1d8624..df6ab66e1 100644 --- a/src/viam/examples/mlmodel/example_audio_classification_client.cpp +++ b/src/viam/examples/mlmodel/example_audio_classification_client.cpp @@ -246,8 +246,8 @@ int main(int argc, char* argv[]) try { dial_options.auth_entity = opt_api_key_id.get(); dial_options.credentials = viam::sdk::Credentials("api-key", opt_api_key.get()); - auto robot = - vsdk::RobotClient::at_address(opt_robot_host.get(), {0, {std::move(dial_options)}}); + auto robot = vsdk::RobotClient::at_address( + opt_robot_host.get(), std::chrono::seconds{0}, dial_options); // Obtain a handle to the MLModelService module on the robot. Note that the string // `yamnet_classification_tflite` is arbitrary. It just matches what was used to name the diff --git a/src/viam/examples/modules/complex/client.cpp b/src/viam/examples/modules/complex/client.cpp index 4f215994c..b37bb563b 100644 --- a/src/viam/examples/modules/complex/client.cpp +++ b/src/viam/examples/modules/complex/client.cpp @@ -26,7 +26,7 @@ int main() { // any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed. Instance inst; - const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely + std::string address = "http://localhost:8080/"; // replace with your URI if connecting securely DialOptions dial_options; dial_options.allow_insecure_downgrade = true; // set to false if connecting securely @@ -36,17 +36,15 @@ int main() { // Credentials credentials(type, payload); // dial_options.set_credentials(credentials); - boost::optional opts(dial_options); - std::string address(uri); - Options options(1, opts); - // Register custom gizmo and summation clients so robot client can access resources // of that type from the server. Registry::get().register_resource_client(); Registry::get().register_resource_client(); // Connect to robot. - std::shared_ptr robot = RobotClient::at_address(address, options); + std::shared_ptr robot = + RobotClient::at_address(address, std::chrono::seconds{1}, dial_options); + // Print resources. VIAM_SDK_LOG(info) << "Resources:"; std::vector resource_names = robot->resource_names(); diff --git a/src/viam/examples/modules/simple/client.cpp b/src/viam/examples/modules/simple/client.cpp index ec01b396e..a2433307e 100644 --- a/src/viam/examples/modules/simple/client.cpp +++ b/src/viam/examples/modules/simple/client.cpp @@ -16,7 +16,9 @@ int main() { // any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed. Instance inst; - const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely + // replace with your URI if connecting securely + std::string address = "http://localhost:8080/"; + DialOptions dial_options; dial_options.allow_insecure_downgrade = true; // set to false if connecting securely @@ -26,11 +28,8 @@ int main() { // Credentials credentials(type, payload); // dial_options.set_credentials(credentials); - boost::optional opts(dial_options); - std::string address(uri); - Options options(1, opts); - - std::shared_ptr robot = RobotClient::at_address(address, options); + std::shared_ptr robot = + RobotClient::at_address(address, std::chrono::seconds{1}, dial_options); // Print resources VIAM_SDK_LOG(info) << "Resources"; diff --git a/src/viam/examples/motor/example_motor.cpp b/src/viam/examples/motor/example_motor.cpp index f6cc0e08e..9c9344a6f 100644 --- a/src/viam/examples/motor/example_motor.cpp +++ b/src/viam/examples/motor/example_motor.cpp @@ -44,11 +44,11 @@ int main() try { dial_options.allow_insecure_downgrade = (credentials.type().empty() && credentials.payload().empty()); - // Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial - // options - vs::Options options = vs::Options(1, dial_options); + // Pass the scheduled refresh interval of the robot (0 seconds = only on config update) and the + // dial options + std::shared_ptr robot = + vs::RobotClient::at_address(robot_address, std::chrono::seconds{1}, dial_options); - std::shared_ptr robot = vs::RobotClient::at_address(robot_address, options); VIAM_SDK_LOG(info) << "Successfully connected to the robot"; std::vector resource_names = robot->resource_names(); diff --git a/src/viam/sdk/module/service.cpp b/src/viam/sdk/module/service.cpp index 3e4c7a533..f51d26427 100644 --- a/src/viam/sdk/module/service.cpp +++ b/src/viam/sdk/module/service.cpp @@ -194,7 +194,8 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service { auto new_parent_addr = request->parent_address(); if (parent.parent_addr_ != new_parent_addr) { parent.parent_addr_ = std::move(new_parent_addr); - parent.parent_ = RobotClient::at_local_socket(parent.parent_addr_, {0, boost::none}); + parent.parent_ = + RobotClient::at_local_socket(parent.parent_addr_, std::chrono::seconds{0}); parent.parent_->connect_logging(); } response->set_ready(parent.module_->ready()); @@ -223,7 +224,7 @@ Dependencies ModuleService::get_dependencies_( std::shared_ptr ModuleService::get_parent_resource_(const Name& name) { if (!parent_) { // LS: I think maybe this is never hit - parent_ = RobotClient::at_local_socket(parent_addr_, {0, boost::none}); + parent_ = RobotClient::at_local_socket(parent_addr_, std::chrono::seconds{0}); parent_->connect_logging(); } diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index f8c007778..82959dceb 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -271,9 +271,9 @@ void RobotClient::log(const std::string& name, } std::unique_ptr RobotClient::with_channel(ViamChannel channel, - const Options& options) { + std::chrono::seconds refresh_interval) { auto robot = std::make_unique(std::move(channel)); - robot->refresh_interval_ = std::chrono::seconds{options.refresh_interval()}; + robot->refresh_interval_ = refresh_interval; robot->should_refresh_ = (robot->refresh_interval_ > std::chrono::seconds{0}); if (robot->should_refresh_) { robot->refresh_thread_ = std::thread{&RobotClient::refresh_every, robot.get()}; @@ -284,20 +284,21 @@ std::unique_ptr RobotClient::with_channel(ViamChannel channel, }; std::unique_ptr RobotClient::at_address(const std::string& address, - const Options& options) { + std::chrono::seconds refresh_interval, + const DialOptions& options) { const char* uri = address.c_str(); auto robot = - RobotClient::with_channel(ViamChannel::dial_initial(uri, options.dial_options()), options); + RobotClient::with_channel(ViamChannel::dial_initial(uri, options), refresh_interval); return robot; }; std::unique_ptr RobotClient::at_local_socket(const std::string& address, - const Options& options) { + std::chrono::seconds refresh_interval) { const std::string addr = "unix://" + address; auto robot = RobotClient::with_channel( ViamChannel(sdk::impl::create_viam_grpc_channel(addr, grpc::InsecureChannelCredentials())), - options); + refresh_interval); return robot; }; diff --git a/src/viam/sdk/robot/client.hpp b/src/viam/sdk/robot/client.hpp index 00a1f7d01..5df929a53 100644 --- a/src/viam/sdk/robot/client.hpp +++ b/src/viam/sdk/robot/client.hpp @@ -71,6 +71,11 @@ class RobotClient { friend bool operator==(const operation& lhs, const operation& rhs); }; + struct options { + std::chrono::seconds refresh_interval; + boost::optional dial_options; + }; + explicit RobotClient(ViamChannel channel); ~RobotClient(); @@ -90,22 +95,25 @@ class RobotClient { /// @brief Create a robot client connected to the robot at the provided address. /// @param address The address of the robot (IP address, URI, URL, etc.) - /// @param options Options for connecting and refreshing. + /// @param refresh_interval How often to call refresh. + /// @param options Options for dialing to the address. static std::unique_ptr at_address(const std::string& address, - const Options& options); + std::chrono::seconds refresh_interval, + const DialOptions& options); /// @brief Creates a robot client connected to the robot at the provided local socket. /// @param address The local socket of the robot (a .sock file, etc.). - /// @param options Options for connecting and refreshing. + /// @param refresh_interval How often to call refresh. /// Creates a direct connection to the robot using the `unix://` scheme. /// Only useful for connecting to robots across Unix sockets. static std::unique_ptr at_local_socket(const std::string& address, - const Options& options); + std::chrono::seconds refresh_interval); /// @brief Creates a robot client connected to the provided channel. /// @param channel The channel to connect with. - /// @param options Options for connecting and refreshing. - static std::unique_ptr with_channel(ViamChannel channel, const Options& options); + /// @param refresh_interval How often to call refresh. + static std::unique_ptr with_channel(ViamChannel channel, + std::chrono::seconds refresh_interval); std::vector resource_names() const; diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 9393c9deb..cbfd8999f 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -187,14 +187,6 @@ void ViamChannel::close() { pimpl_.reset(); } -unsigned int Options::refresh_interval() const { - return refresh_interval_; -} - -const boost::optional& Options::dial_options() const { - return dial_options_; -} - Credentials::Credentials(std::string payload) : type_("robot-location-secret"), payload_(std::move(payload)) {} diff --git a/src/viam/sdk/rpc/dial.hpp b/src/viam/sdk/rpc/dial.hpp index 718a6dd0e..03449f2c6 100644 --- a/src/viam/sdk/rpc/dial.hpp +++ b/src/viam/sdk/rpc/dial.hpp @@ -100,20 +100,5 @@ struct DialOptions { std::chrono::duration initial_connection_attempt_timeout{20}; }; -class Options { - public: - Options(unsigned int refresh_interval, boost::optional dial_options) - : refresh_interval_(std::move(refresh_interval)), dial_options_(std::move(dial_options)) {} - - unsigned int refresh_interval() const; - const boost::optional& dial_options() const; - - private: - /// @brief How often to refresh the status/parts of the robot, in seconds. If set to 0, the - /// robot will not automatically refresh. - unsigned int refresh_interval_; - boost::optional dial_options_; -}; - } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/tests/test_robot.cpp b/src/viam/sdk/tests/test_robot.cpp index b0b735bbf..0ad57f1c2 100644 --- a/src/viam/sdk/tests/test_robot.cpp +++ b/src/viam/sdk/tests/test_robot.cpp @@ -50,7 +50,7 @@ void robot_client_to_mocks_pipeline(F&& test_case) { // in-process gRPC channel. auto test_server = TestServer(server); auto grpc_channel = test_server.grpc_in_process_channel(); - auto client = RobotClient::with_channel(ViamChannel(grpc_channel), Options(0, boost::none)); + auto client = RobotClient::with_channel(ViamChannel(grpc_channel), {}); // Run the passed-in test case on the created stack and give access to the // created RobotClient and MockRobotService. From 95dc5f8bfc2f9bfd0eccbb2e87c9aaf1d8f8fdc2 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Wed, 14 May 2025 11:01:43 -0400 Subject: [PATCH 27/37] add include --- src/viam/sdk/rpc/private/viam_grpc_channel.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp index 8d74a9f2e..570030ee6 100644 --- a/src/viam/sdk/rpc/private/viam_grpc_channel.hpp +++ b/src/viam/sdk/rpc/private/viam_grpc_channel.hpp @@ -5,6 +5,8 @@ #include #include +#include + namespace viam { namespace sdk { namespace impl { From 4a82a6d86da777d1c2b9344663f33c048e3be9b2 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Wed, 14 May 2025 11:34:17 -0400 Subject: [PATCH 28/37] silence unused var warning on ifdef --- src/viam/sdk/rpc/dial.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index cbfd8999f..5d100609c 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -174,6 +174,8 @@ ViamChannel ViamChannel::dial_direct(const char* uri, const DialOptions& opts) { auto creds = grpc::experimental::TlsCredentials(c_opts); return ViamChannel(sdk::impl::create_viam_grpc_channel(uri, creds)); #else + (void)uri; + (void)opts; throw std::logic_error("Tried to call dial_direct on unsupported grpc version " + grpc::Version()); #endif From 8e99746d4ac50081a6f045f98410e76b11213590 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Wed, 14 May 2025 15:37:17 -0400 Subject: [PATCH 29/37] client context gets token from channel ctor --- src/viam/sdk/common/client_helper.cpp | 8 ++++---- src/viam/sdk/common/client_helper.hpp | 3 +++ src/viam/sdk/common/private/instance.hpp | 5 ----- src/viam/sdk/rpc/dial.cpp | 12 ++++++++---- src/viam/sdk/rpc/dial.hpp | 9 +++++++++ 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/viam/sdk/common/client_helper.cpp b/src/viam/sdk/common/client_helper.cpp index 692ece67d..2da472121 100644 --- a/src/viam/sdk/common/client_helper.cpp +++ b/src/viam/sdk/common/client_helper.cpp @@ -31,11 +31,11 @@ void set_name(...) {} // NOLINT(cert-dcl50-cpp) ClientContext::ClientContext() : wrapped_context_(std::make_unique()) { set_client_ctx_authority_(); add_viam_client_version_(); +} - const std::string& token = - Instance::current(Instance::Creation::open_existing).impl_->direct_dial_token; - if (!token.empty()) { - wrapped_context_->AddMetadata("authorization", "Bearer " + token); +ClientContext::ClientContext(const ViamChannel& channel) : ClientContext() { + if (channel.auth_token().has_value()) { + wrapped_context_->AddMetadata("authorization", "Bearer " + *channel.auth_token()); } } diff --git a/src/viam/sdk/common/client_helper.hpp b/src/viam/sdk/common/client_helper.hpp index d5e3a8240..6435d79c1 100644 --- a/src/viam/sdk/common/client_helper.hpp +++ b/src/viam/sdk/common/client_helper.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace viam { namespace sdk { @@ -38,6 +39,8 @@ void set_name(...); class ClientContext { public: ClientContext(); + ClientContext(const ViamChannel& channel); + ~ClientContext(); void try_cancel(); diff --git a/src/viam/sdk/common/private/instance.hpp b/src/viam/sdk/common/private/instance.hpp index 68ce1f37c..cb09480d9 100644 --- a/src/viam/sdk/common/private/instance.hpp +++ b/src/viam/sdk/common/private/instance.hpp @@ -10,11 +10,6 @@ namespace sdk { struct Instance::Impl { Registry registry; LogManager log_mgr; - - // When dialing directly to gpc (no rust-utils/webrtc) there is global state in the form of a - // bearer token that needs to be added as metadata to every client call. This variable stores - // the auth token; see client_helper.cpp and dial.cpp for access. - std::string direct_dial_token; }; } // namespace sdk diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 5d100609c..266c56ffb 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -166,13 +166,13 @@ ViamChannel ViamChannel::dial_direct(const char* uri, const DialOptions& opts) { throw GRPCException(&status); } - Instance::current(Instance::Creation::open_existing).impl_->direct_dial_token = - resp.access_token(); - grpc::experimental::TlsChannelCredentialsOptions c_opts; c_opts.set_check_call_host(false); auto creds = grpc::experimental::TlsCredentials(c_opts); - return ViamChannel(sdk::impl::create_viam_grpc_channel(uri, creds)); + auto result = ViamChannel(sdk::impl::create_viam_grpc_channel(uri, creds)); + result.auth_token_ = resp.access_token(); + + return result; #else (void)uri; (void)opts; @@ -185,6 +185,10 @@ const std::shared_ptr& ViamChannel::channel() const { return channel_; } +const boost::optional& ViamChannel::auth_token() const { + return auth_token_; +} + void ViamChannel::close() { pimpl_.reset(); } diff --git a/src/viam/sdk/rpc/dial.hpp b/src/viam/sdk/rpc/dial.hpp index 03449f2c6..3b16f00c7 100644 --- a/src/viam/sdk/rpc/dial.hpp +++ b/src/viam/sdk/rpc/dial.hpp @@ -45,6 +45,13 @@ class ViamChannel { const std::shared_ptr& channel() const; + /// @brief Returns the bearer token for connecting to the robot if one is needed; else returns + /// null. + /// @remark When dialing with disable_webrtc = true and grpc >= 1.43.0, a bearer token is needed + /// for all client requests. If you use ClientHelper for client requests this is handled + /// automatically, otherwise you will have to add this to the client context of a grpc call. + const boost::optional& auth_token() const; + void close(); private: @@ -54,6 +61,8 @@ class ViamChannel { std::shared_ptr channel_; + boost::optional auth_token_; + std::unique_ptr pimpl_; }; From a8b858de1d8e3b2e84b885a0b21d15e2f9ac4327 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 15 May 2025 15:41:56 -0400 Subject: [PATCH 30/37] sdk overhaul for channel/token --- src/viam/sdk/common/client_helper.hpp | 4 ++-- src/viam/sdk/components/private/arm_client.cpp | 8 +++++--- src/viam/sdk/components/private/arm_client.hpp | 11 +++++++---- src/viam/sdk/components/private/base_client.cpp | 8 +++++--- src/viam/sdk/components/private/base_client.hpp | 11 +++++++---- src/viam/sdk/components/private/board_client.cpp | 8 +++++--- src/viam/sdk/components/private/board_client.hpp | 11 +++++++---- src/viam/sdk/components/private/button_client.cpp | 8 +++++--- src/viam/sdk/components/private/button_client.hpp | 11 +++++++---- src/viam/sdk/components/private/camera_client.cpp | 8 +++++--- src/viam/sdk/components/private/camera_client.hpp | 13 ++++++++----- src/viam/sdk/components/private/encoder_client.cpp | 8 +++++--- src/viam/sdk/components/private/encoder_client.hpp | 11 +++++++---- src/viam/sdk/components/private/gantry_client.cpp | 8 +++++--- src/viam/sdk/components/private/gantry_client.hpp | 11 +++++++---- src/viam/sdk/components/private/generic_client.cpp | 9 +++++---- src/viam/sdk/components/private/generic_client.hpp | 13 ++++++++----- src/viam/sdk/components/private/gripper_client.cpp | 8 +++++--- src/viam/sdk/components/private/gripper_client.hpp | 11 +++++++---- src/viam/sdk/components/private/motor_client.cpp | 8 +++++--- src/viam/sdk/components/private/motor_client.hpp | 11 +++++++---- .../components/private/movement_sensor_client.cpp | 8 +++++--- .../components/private/movement_sensor_client.hpp | 11 +++++++---- .../sdk/components/private/pose_tracker_client.cpp | 8 +++++--- .../sdk/components/private/pose_tracker_client.hpp | 11 +++++++---- .../sdk/components/private/power_sensor_client.cpp | 8 +++++--- .../sdk/components/private/power_sensor_client.hpp | 11 +++++++---- src/viam/sdk/components/private/sensor_client.cpp | 8 +++++--- src/viam/sdk/components/private/sensor_client.hpp | 11 +++++++---- src/viam/sdk/components/private/servo_client.cpp | 8 +++++--- src/viam/sdk/components/private/servo_client.hpp | 11 +++++++---- src/viam/sdk/components/private/switch_client.cpp | 8 +++++--- src/viam/sdk/components/private/switch_client.hpp | 11 +++++++---- src/viam/sdk/registry/registry.hpp | 11 ++++++----- src/viam/sdk/robot/client.cpp | 13 ++++++++++--- src/viam/sdk/services/private/discovery_client.cpp | 8 +++++--- src/viam/sdk/services/private/discovery_client.hpp | 11 +++++++---- src/viam/sdk/services/private/generic_client.cpp | 8 +++++--- src/viam/sdk/services/private/generic_client.hpp | 14 +++++++++----- src/viam/sdk/services/private/mlmodel_client.cpp | 6 +++--- src/viam/sdk/services/private/mlmodel_client.hpp | 11 +++++++---- src/viam/sdk/services/private/motion_client.cpp | 7 ++++--- src/viam/sdk/services/private/motion_client.hpp | 12 ++++++++---- .../sdk/services/private/navigation_client.cpp | 7 ++++--- .../sdk/services/private/navigation_client.hpp | 11 +++++++---- 45 files changed, 271 insertions(+), 161 deletions(-) diff --git a/src/viam/sdk/common/client_helper.hpp b/src/viam/sdk/common/client_helper.hpp index 6435d79c1..828c18691 100644 --- a/src/viam/sdk/common/client_helper.hpp +++ b/src/viam/sdk/common/client_helper.hpp @@ -113,7 +113,7 @@ class ClientHelper { template auto invoke(ResponseHandlerCallable&& rhc, ErrorHandlerCallable&& ehc) { client_helper_details::set_name(&request_, client_); - ClientContext ctx; + ClientContext ctx(client_->channel()); if (debug_key_ != "") { ctx.set_debug_key(debug_key_); @@ -135,7 +135,7 @@ class ClientHelper { typename ErrorHandlerCallable = decltype(default_ehc_)> auto invoke_stream(ResponseHandlerCallable rhc, ErrorHandlerCallable&& ehc = default_ehc_) { *request_.mutable_name() = client_->name(); - ClientContext ctx; + ClientContext ctx(client_->channel()); auto reader = (stub_->*pfn_)(ctx, request_); diff --git a/src/viam/sdk/components/private/arm_client.cpp b/src/viam/sdk/components/private/arm_client.cpp index 619580ad4..0048dda79 100644 --- a/src/viam/sdk/components/private/arm_client.cpp +++ b/src/viam/sdk/components/private/arm_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -9,10 +11,10 @@ namespace viam { namespace sdk { namespace impl { -ArmClient::ArmClient(std::string name, std::shared_ptr channel) +ArmClient::ArmClient(std::string name, ViamChannel& channel) : Arm(std::move(name)), - stub_(viam::component::arm::v1::ArmService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::arm::v1::ArmService::NewStub(channel.channel())), + channel_(&channel) {} pose ArmClient::get_end_position(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetEndPosition) diff --git a/src/viam/sdk/components/private/arm_client.hpp b/src/viam/sdk/components/private/arm_client.hpp index 6608a1340..a599998e5 100644 --- a/src/viam/sdk/components/private/arm_client.hpp +++ b/src/viam/sdk/components/private/arm_client.hpp @@ -3,11 +3,10 @@ /// @brief Implements a gRPC client for the `Arm` component #pragma once -#include - #include #include +#include namespace viam { namespace sdk { @@ -19,7 +18,11 @@ namespace impl { class ArmClient : public Arm { public: using interface_type = Arm; - ArmClient(std::string name, std::shared_ptr channel); + ArmClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } pose get_end_position(const ProtoStruct& extra) override; void move_to_position(const pose& pose, const ProtoStruct& extra) override; @@ -49,7 +52,7 @@ class ArmClient : public Arm { private: using StubType = viam::component::arm::v1::ArmService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/base_client.cpp b/src/viam/sdk/components/private/base_client.cpp index 03be9d2cb..b383a761d 100644 --- a/src/viam/sdk/components/private/base_client.cpp +++ b/src/viam/sdk/components/private/base_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -22,10 +24,10 @@ namespace viam { namespace sdk { namespace impl { -BaseClient::BaseClient(std::string name, std::shared_ptr channel) +BaseClient::BaseClient(std::string name, ViamChannel& channel) : Base(std::move(name)), - stub_(viam::component::base::v1::BaseService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::base::v1::BaseService::NewStub(channel.channel())), + channel_(&channel) {} void BaseClient::move_straight(int64_t distance_mm, double mm_per_sec, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::MoveStraight) diff --git a/src/viam/sdk/components/private/base_client.hpp b/src/viam/sdk/components/private/base_client.hpp index 7547e98fb..81115eb95 100644 --- a/src/viam/sdk/components/private/base_client.hpp +++ b/src/viam/sdk/components/private/base_client.hpp @@ -3,8 +3,6 @@ /// @brief Implements a gRPC client for the `Base` component. #pragma once -#include - #include #include @@ -13,6 +11,7 @@ #include #include #include +#include namespace viam { namespace sdk { @@ -24,7 +23,11 @@ namespace impl { class BaseClient : public Base { public: using interface_type = Base; - BaseClient(std::string name, std::shared_ptr channel); + BaseClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } void move_straight(int64_t distance_mm, double mm_per_sec, const ProtoStruct& extra) override; void spin(double angle_deg, double degs_per_sec, const ProtoStruct& extra) override; void set_power(const Vector3& linear, @@ -59,7 +62,7 @@ class BaseClient : public Base { private: using StubType = viam::component::base::v1::BaseService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/board_client.cpp b/src/viam/sdk/components/private/board_client.cpp index 7bd7a6935..97fdf16e2 100644 --- a/src/viam/sdk/components/private/board_client.cpp +++ b/src/viam/sdk/components/private/board_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -49,10 +51,10 @@ viam::component::board::v1::PowerMode to_proto(Board::power_mode power_mode) { } } -BoardClient::BoardClient(std::string name, std::shared_ptr channel) +BoardClient::BoardClient(std::string name, ViamChannel& channel) : Board(std::move(name)), - stub_(viam::component::board::v1::BoardService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::board::v1::BoardService::NewStub(channel.channel())), + channel_(&channel) {} void BoardClient::set_gpio(const std::string& pin, bool high, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::SetGPIO) diff --git a/src/viam/sdk/components/private/board_client.hpp b/src/viam/sdk/components/private/board_client.hpp index c0c0a3161..76f8e79eb 100644 --- a/src/viam/sdk/components/private/board_client.hpp +++ b/src/viam/sdk/components/private/board_client.hpp @@ -3,14 +3,13 @@ /// @brief Implements a gRPC client for the `Board` component. #pragma once -#include - #include #include #include #include #include +#include namespace viam { namespace sdk { @@ -22,7 +21,11 @@ namespace impl { class BoardClient : public Board { public: using interface_type = Board; - BoardClient(std::string name, std::shared_ptr channel); + BoardClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } ProtoStruct do_command(const ProtoStruct& command) override; void set_gpio(const std::string& pin, bool high, const ProtoStruct& extra) override; bool get_gpio(const std::string& pin, const ProtoStruct& extra) override; @@ -73,7 +76,7 @@ class BoardClient : public Board { private: using StubType = viam::component::board::v1::BoardService::StubInterface; std::unique_ptr stub_; - const std::shared_ptr channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/button_client.cpp b/src/viam/sdk/components/private/button_client.cpp index 4a5042c39..bf5d7f761 100644 --- a/src/viam/sdk/components/private/button_client.cpp +++ b/src/viam/sdk/components/private/button_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -10,10 +12,10 @@ namespace viam { namespace sdk { namespace impl { -ButtonClient::ButtonClient(std::string name, std::shared_ptr channel) +ButtonClient::ButtonClient(std::string name, ViamChannel& channel) : Button(std::move(name)), - stub_(viam::component::button::v1::ButtonService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::button::v1::ButtonService::NewStub(channel.channel())), + channel_(&channel) {} void ButtonClient::push(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::Push).with(extra).invoke(); diff --git a/src/viam/sdk/components/private/button_client.hpp b/src/viam/sdk/components/private/button_client.hpp index e0c58f0ab..3d45efd35 100644 --- a/src/viam/sdk/components/private/button_client.hpp +++ b/src/viam/sdk/components/private/button_client.hpp @@ -5,11 +5,10 @@ #include -#include - #include #include +#include namespace viam { namespace sdk { @@ -21,7 +20,11 @@ namespace impl { class ButtonClient : public Button { public: using interface_type = Button; - ButtonClient(std::string name, std::shared_ptr channel); + ButtonClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } void push(const ProtoStruct& extra) override; ProtoStruct do_command(const ProtoStruct& command) override; @@ -31,7 +34,7 @@ class ButtonClient : public Button { private: using StubType = viam::component::button::v1::ButtonService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/camera_client.cpp b/src/viam/sdk/components/private/camera_client.cpp index ec09856fc..71a679202 100644 --- a/src/viam/sdk/components/private/camera_client.cpp +++ b/src/viam/sdk/components/private/camera_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -102,10 +104,10 @@ Camera::properties from_proto(const viam::component::camera::v1::GetPropertiesRe (proto.frame_rate())}; } -CameraClient::CameraClient(std::string name, std::shared_ptr channel) +CameraClient::CameraClient(std::string name, ViamChannel& channel) : Camera(std::move(name)), - stub_(viam::component::camera::v1::CameraService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::camera::v1::CameraService::NewStub(channel.channel())), + channel_(&channel) {} ProtoStruct CameraClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) diff --git a/src/viam/sdk/components/private/camera_client.hpp b/src/viam/sdk/components/private/camera_client.hpp index b61b593fb..f90b1b4e1 100644 --- a/src/viam/sdk/components/private/camera_client.hpp +++ b/src/viam/sdk/components/private/camera_client.hpp @@ -3,8 +3,6 @@ /// @brief Implements a gRPC client for the `Camera` component. #pragma once -#include - #include #include @@ -12,6 +10,7 @@ #include #include #include +#include namespace viam { namespace sdk { @@ -23,7 +22,11 @@ namespace impl { class CameraClient : public Camera { public: using interface_type = Camera; - CameraClient(std::string name, std::shared_ptr channel); + CameraClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } ProtoStruct do_command(const ProtoStruct& command) override; raw_image get_image(std::string mime_type, const ProtoStruct& extra) override; image_collection get_images() override; @@ -50,12 +53,12 @@ class CameraClient : public Camera { // avoid use of this constructor outside of tests. CameraClient(std::string name, std::unique_ptr stub) - : Camera(std::move(name)), stub_(std::move(stub)){}; + : Camera(std::move(name)), stub_(std::move(stub)) {}; private: using StubType = viam::component::camera::v1::CameraService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/encoder_client.cpp b/src/viam/sdk/components/private/encoder_client.cpp index 7ccbc3fa9..a9c805c9f 100644 --- a/src/viam/sdk/components/private/encoder_client.cpp +++ b/src/viam/sdk/components/private/encoder_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -45,10 +47,10 @@ Encoder::properties from_proto(const viam::component::encoder::v1::GetProperties return properties; } -EncoderClient::EncoderClient(std::string name, std::shared_ptr channel) +EncoderClient::EncoderClient(std::string name, ViamChannel& channel) : Encoder(std::move(name)), - stub_(viam::component::encoder::v1::EncoderService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::encoder::v1::EncoderService::NewStub(channel.channel())), + channel_(&channel) {} Encoder::position EncoderClient::get_position(const ProtoStruct& extra, position_type position_type) { diff --git a/src/viam/sdk/components/private/encoder_client.hpp b/src/viam/sdk/components/private/encoder_client.hpp index 73935401b..2e28dfd0c 100644 --- a/src/viam/sdk/components/private/encoder_client.hpp +++ b/src/viam/sdk/components/private/encoder_client.hpp @@ -3,12 +3,11 @@ /// @brief Implements a gRPC client for the `Encoder` component. #pragma once -#include - #include #include #include +#include namespace viam { namespace sdk { @@ -20,7 +19,11 @@ namespace impl { class EncoderClient : public Encoder { public: using interface_type = Encoder; - EncoderClient(std::string name, std::shared_ptr channel); + EncoderClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } position get_position(const ProtoStruct& extra, position_type position_type) override; void reset_position(const ProtoStruct& extra) override; properties get_properties(const ProtoStruct& extra) override; @@ -44,7 +47,7 @@ class EncoderClient : public Encoder { private: using StubType = viam::component::encoder::v1::EncoderService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/gantry_client.cpp b/src/viam/sdk/components/private/gantry_client.cpp index 7021dd5bc..e2d459fcc 100644 --- a/src/viam/sdk/components/private/gantry_client.cpp +++ b/src/viam/sdk/components/private/gantry_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -9,10 +11,10 @@ namespace viam { namespace sdk { namespace impl { -GantryClient::GantryClient(std::string name, std::shared_ptr channel) +GantryClient::GantryClient(std::string name, ViamChannel& channel) : Gantry(std::move(name)), - stub_(viam::component::gantry::v1::GantryService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::gantry::v1::GantryService::NewStub(channel.channel())), + channel_(&channel) {} std::vector GantryClient::get_position(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetPosition) diff --git a/src/viam/sdk/components/private/gantry_client.hpp b/src/viam/sdk/components/private/gantry_client.hpp index b499baef4..c1f536238 100644 --- a/src/viam/sdk/components/private/gantry_client.hpp +++ b/src/viam/sdk/components/private/gantry_client.hpp @@ -3,11 +3,10 @@ /// @brief Implements a gRPC client for the `Gantry` component #pragma once -#include - #include #include +#include namespace viam { namespace sdk { @@ -19,7 +18,11 @@ namespace impl { class GantryClient : public Gantry { public: using interface_type = Gantry; - GantryClient(std::string names, std::shared_ptr channel); + GantryClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } std::vector get_position(const ProtoStruct& extra) override; void move_to_position(const std::vector& coordinates, @@ -41,7 +44,7 @@ class GantryClient : public Gantry { private: using StubType = viam::component::gantry::v1::GantryService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/generic_client.cpp b/src/viam/sdk/components/private/generic_client.cpp index 2bb654539..27e9d4a12 100644 --- a/src/viam/sdk/components/private/generic_client.cpp +++ b/src/viam/sdk/components/private/generic_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -15,11 +17,10 @@ namespace viam { namespace sdk { namespace impl { -GenericComponentClient::GenericComponentClient(std::string name, - std::shared_ptr channel) +GenericComponentClient::GenericComponentClient(std::string name, ViamChannel& channel) : GenericComponent(std::move(name)), - stub_(viam::component::generic::v1::GenericService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::generic::v1::GenericService::NewStub(channel.channel())), + channel_(&channel) {} ProtoStruct GenericComponentClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) diff --git a/src/viam/sdk/components/private/generic_client.hpp b/src/viam/sdk/components/private/generic_client.hpp index 1e76be23e..ddfd4de27 100644 --- a/src/viam/sdk/components/private/generic_client.hpp +++ b/src/viam/sdk/components/private/generic_client.hpp @@ -3,12 +3,11 @@ /// @brief Implements a gRPC client for the `GenericComponent`. #pragma once -#include - #include #include #include +#include namespace viam { namespace sdk { @@ -20,7 +19,11 @@ namespace impl { class GenericComponentClient : public GenericComponent { public: using interface_type = GenericComponent; - GenericComponentClient(std::string name, std::shared_ptr channel); + GenericComponentClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } ProtoStruct do_command(const ProtoStruct& command) override; std::vector get_geometries(const ProtoStruct& extra) override; @@ -31,12 +34,12 @@ class GenericComponentClient : public GenericComponent { GenericComponentClient( std::string name, std::unique_ptr stub) - : GenericComponent(std::move(name)), stub_(std::move(stub)){}; + : GenericComponent(std::move(name)), stub_(std::move(stub)) {}; private: using StubType = viam::component::generic::v1::GenericService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/gripper_client.cpp b/src/viam/sdk/components/private/gripper_client.cpp index 439517bff..efa5c37a6 100644 --- a/src/viam/sdk/components/private/gripper_client.cpp +++ b/src/viam/sdk/components/private/gripper_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -11,10 +13,10 @@ namespace viam { namespace sdk { namespace impl { -GripperClient::GripperClient(std::string name, std::shared_ptr channel) +GripperClient::GripperClient(std::string name, ViamChannel& channel) : Gripper(std::move(name)), - stub_(viam::component::gripper::v1::GripperService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::gripper::v1::GripperService::NewStub(channel.channel())), + channel_(&channel) {} void GripperClient::open(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::Open).with(extra).invoke(); diff --git a/src/viam/sdk/components/private/gripper_client.hpp b/src/viam/sdk/components/private/gripper_client.hpp index 1d118eda9..2f29d39fe 100644 --- a/src/viam/sdk/components/private/gripper_client.hpp +++ b/src/viam/sdk/components/private/gripper_client.hpp @@ -5,11 +5,10 @@ #include -#include - #include #include +#include namespace viam { namespace sdk { @@ -21,7 +20,11 @@ namespace impl { class GripperClient : public Gripper { public: using interface_type = Gripper; - GripperClient(std::string names, std::shared_ptr channel); + GripperClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } void open(const ProtoStruct& extra) override; bool grab(const ProtoStruct& extra) override; @@ -38,7 +41,7 @@ class GripperClient : public Gripper { private: using StubType = viam::component::gripper::v1::GripperService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/motor_client.cpp b/src/viam/sdk/components/private/motor_client.cpp index e0f112b6a..dd4f09151 100644 --- a/src/viam/sdk/components/private/motor_client.cpp +++ b/src/viam/sdk/components/private/motor_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -37,10 +39,10 @@ Motor::properties from_proto(const viam::component::motor::v1::GetPropertiesResp return properties; } -MotorClient::MotorClient(std::string name, std::shared_ptr channel) +MotorClient::MotorClient(std::string name, ViamChannel& channel) : Motor(std::move(name)), - stub_(viam::component::motor::v1::MotorService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::motor::v1::MotorService::NewStub(channel.channel())), + channel_(&channel) {} void MotorClient::set_power(double power_pct, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::SetPower) diff --git a/src/viam/sdk/components/private/motor_client.hpp b/src/viam/sdk/components/private/motor_client.hpp index 223b77190..5bd1d8607 100644 --- a/src/viam/sdk/components/private/motor_client.hpp +++ b/src/viam/sdk/components/private/motor_client.hpp @@ -3,13 +3,12 @@ /// @brief Implements a gRPC client for the `Motor` component. #pragma once -#include - #include #include #include #include +#include namespace viam { namespace sdk { @@ -21,7 +20,11 @@ namespace impl { class MotorClient : public Motor { public: using interface_type = Motor; - MotorClient(std::string name, std::shared_ptr channel); + MotorClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } void set_power(double power_pct, const ProtoStruct& extra) override; void go_for(double rpm, double revolutions, const ProtoStruct& extra) override; void go_to(double rpm, double position_revolutions, const ProtoStruct& extra) override; @@ -58,7 +61,7 @@ class MotorClient : public Motor { private: using StubType = viam::component::motor::v1::MotorService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/movement_sensor_client.cpp b/src/viam/sdk/components/private/movement_sensor_client.cpp index ec9b19ee0..416958532 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.cpp +++ b/src/viam/sdk/components/private/movement_sensor_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -56,10 +58,10 @@ MovementSensor::properties from_proto( return properties; } -MovementSensorClient::MovementSensorClient(std::string name, std::shared_ptr channel) +MovementSensorClient::MovementSensorClient(std::string name, ViamChannel& channel) : MovementSensor(std::move(name)), - stub_(viam::component::movementsensor::v1::MovementSensorService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::movementsensor::v1::MovementSensorService::NewStub(channel.channel())), + channel_(&channel) {} using namespace viam::component::movementsensor::v1; diff --git a/src/viam/sdk/components/private/movement_sensor_client.hpp b/src/viam/sdk/components/private/movement_sensor_client.hpp index a64ae7c02..fd7643788 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.hpp +++ b/src/viam/sdk/components/private/movement_sensor_client.hpp @@ -3,8 +3,6 @@ /// @brief Implements a gRPC client for the `MovementSensor` component. #pragma once -#include - #include #include @@ -12,6 +10,7 @@ #include #include #include +#include namespace viam { namespace sdk { @@ -23,7 +22,11 @@ namespace impl { class MovementSensorClient : public MovementSensor { public: using interface_type = MovementSensor; - MovementSensorClient(std::string name, std::shared_ptr channel); + MovementSensorClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } Vector3 get_linear_velocity(const ProtoStruct& extra) override; Vector3 get_angular_velocity(const ProtoStruct& extra) override; compassheading get_compass_heading(const ProtoStruct& extra) override; @@ -48,7 +51,7 @@ class MovementSensorClient : public MovementSensor { private: using StubType = viam::component::movementsensor::v1::MovementSensorService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/pose_tracker_client.cpp b/src/viam/sdk/components/private/pose_tracker_client.cpp index 234244ef6..2c95f9a32 100644 --- a/src/viam/sdk/components/private/pose_tracker_client.cpp +++ b/src/viam/sdk/components/private/pose_tracker_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -10,10 +12,10 @@ namespace viam { namespace sdk { namespace impl { -PoseTrackerClient::PoseTrackerClient(std::string name, std::shared_ptr channel) +PoseTrackerClient::PoseTrackerClient(std::string name, ViamChannel& channel) : PoseTracker(std::move(name)), - stub_(viam::component::posetracker::v1::PoseTrackerService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::posetracker::v1::PoseTrackerService::NewStub(channel.channel())), + channel_(&channel) {} PoseTracker::pose_map PoseTrackerClient::get_poses(const std::vector& body_names, const ProtoStruct&) { diff --git a/src/viam/sdk/components/private/pose_tracker_client.hpp b/src/viam/sdk/components/private/pose_tracker_client.hpp index 5c947ca20..eef26cd9a 100644 --- a/src/viam/sdk/components/private/pose_tracker_client.hpp +++ b/src/viam/sdk/components/private/pose_tracker_client.hpp @@ -3,11 +3,10 @@ /// @brief Implements a gRPC client for the `PoseTracker` component #pragma once -#include - #include #include +#include namespace viam { namespace sdk { @@ -20,7 +19,11 @@ class PoseTrackerClient : public PoseTracker { public: using interface_type = PoseTracker; - PoseTrackerClient(std::string name, std::shared_ptr channel); + PoseTrackerClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } PoseTracker::pose_map get_poses(const std::vector& body_names, const ProtoStruct& extra) override; @@ -35,7 +38,7 @@ class PoseTrackerClient : public PoseTracker { private: using StubType = viam::component::posetracker::v1::PoseTrackerService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/power_sensor_client.cpp b/src/viam/sdk/components/private/power_sensor_client.cpp index 556dbf1a3..e2c5e03f8 100644 --- a/src/viam/sdk/components/private/power_sensor_client.cpp +++ b/src/viam/sdk/components/private/power_sensor_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -38,10 +40,10 @@ PowerSensor::current from_proto(const GetCurrentResponse& proto) { return c; } -PowerSensorClient::PowerSensorClient(std::string name, std::shared_ptr channel) +PowerSensorClient::PowerSensorClient(std::string name, ViamChannel& channel) : PowerSensor(std::move(name)), - stub_(PowerSensorService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(PowerSensorService::NewStub(channel.channel())), + channel_(&channel) {} PowerSensor::voltage PowerSensorClient::get_voltage(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetVoltage) diff --git a/src/viam/sdk/components/private/power_sensor_client.hpp b/src/viam/sdk/components/private/power_sensor_client.hpp index cf9927878..0c046491a 100644 --- a/src/viam/sdk/components/private/power_sensor_client.hpp +++ b/src/viam/sdk/components/private/power_sensor_client.hpp @@ -3,14 +3,13 @@ /// @brief Implements a gRPC client for the `PowerSensor` component. #pragma once -#include - #include #include #include #include #include +#include namespace viam { namespace sdk { @@ -22,7 +21,11 @@ namespace impl { class PowerSensorClient : public PowerSensor { public: using interface_type = PowerSensor; - PowerSensorClient(std::string name, std::shared_ptr channel); + PowerSensorClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } voltage get_voltage(const ProtoStruct& extra) override; current get_current(const ProtoStruct& extra) override; double get_power(const ProtoStruct& extra) override; @@ -37,7 +40,7 @@ class PowerSensorClient : public PowerSensor { private: using StubType = viam::component::powersensor::v1::PowerSensorService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/sensor_client.cpp b/src/viam/sdk/components/private/sensor_client.cpp index b1b7bce6c..44410682f 100644 --- a/src/viam/sdk/components/private/sensor_client.cpp +++ b/src/viam/sdk/components/private/sensor_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -16,10 +18,10 @@ namespace viam { namespace sdk { namespace impl { -SensorClient::SensorClient(std::string name, std::shared_ptr channel) +SensorClient::SensorClient(std::string name, ViamChannel& channel) : Sensor(std::move(name)), - stub_(viam::component::sensor::v1::SensorService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::sensor::v1::SensorService::NewStub(channel.channel())), + channel_(&channel) {} using namespace viam::common::v1; diff --git a/src/viam/sdk/components/private/sensor_client.hpp b/src/viam/sdk/components/private/sensor_client.hpp index 048b29bb3..e2a1ca08e 100644 --- a/src/viam/sdk/components/private/sensor_client.hpp +++ b/src/viam/sdk/components/private/sensor_client.hpp @@ -3,13 +3,12 @@ /// @brief Implements a gRPC client for the `Sensor` component. #pragma once -#include - #include #include #include #include +#include namespace viam { namespace sdk { @@ -21,7 +20,11 @@ namespace impl { class SensorClient : public Sensor { public: using interface_type = Sensor; - SensorClient(std::string name, std::shared_ptr channel); + SensorClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } ProtoStruct get_readings(const ProtoStruct& extra) override; ProtoStruct do_command(const ProtoStruct& command) override; std::vector get_geometries(const ProtoStruct& extra) override; @@ -32,7 +35,7 @@ class SensorClient : public Sensor { private: using StubType = viam::component::sensor::v1::SensorService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/servo_client.cpp b/src/viam/sdk/components/private/servo_client.cpp index 189e4d86a..687e87693 100644 --- a/src/viam/sdk/components/private/servo_client.cpp +++ b/src/viam/sdk/components/private/servo_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -25,10 +27,10 @@ Servo::position from_proto(const viam::component::servo::v1::GetPositionResponse return proto.position_deg(); } -ServoClient::ServoClient(std::string name, std::shared_ptr channel) +ServoClient::ServoClient(std::string name, ViamChannel& channel) : Servo(std::move(name)), - stub_(viam::component::servo::v1::ServoService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::servo::v1::ServoService::NewStub(channel.channel())), + channel_(&channel) {} void ServoClient::move(uint32_t angle_deg, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::Move) diff --git a/src/viam/sdk/components/private/servo_client.hpp b/src/viam/sdk/components/private/servo_client.hpp index e1a68dd7d..d373c513e 100644 --- a/src/viam/sdk/components/private/servo_client.hpp +++ b/src/viam/sdk/components/private/servo_client.hpp @@ -3,13 +3,12 @@ /// @brief Implements a gRPC client for the `Servo` component. #pragma once -#include - #include #include #include #include +#include namespace viam { namespace sdk { @@ -21,7 +20,11 @@ namespace impl { class ServoClient : public Servo { public: using interface_type = Servo; - ServoClient(std::string name, std::shared_ptr channel); + ServoClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } void move(uint32_t angle_deg, const ProtoStruct& extra) override; position get_position(const ProtoStruct& extra) override; void stop(const ProtoStruct& extra) override; @@ -36,7 +39,7 @@ class ServoClient : public Servo { using StubType = viam::component::servo::v1::ServoService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/switch_client.cpp b/src/viam/sdk/components/private/switch_client.cpp index 081f54277..6b13ab319 100644 --- a/src/viam/sdk/components/private/switch_client.cpp +++ b/src/viam/sdk/components/private/switch_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -9,10 +11,10 @@ namespace viam { namespace sdk { namespace impl { -SwitchClient::SwitchClient(std::string name, std::shared_ptr channel) +SwitchClient::SwitchClient(std::string name, ViamChannel& channel) : Switch(std::move(name)), - stub_(viam::component::switch_::v1::SwitchService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::component::switch_::v1::SwitchService::NewStub(channel.channel())), + channel_(&channel) {} void SwitchClient::set_position(uint32_t position, const ProtoStruct& extra) { make_client_helper(this, *stub_, &StubType::SetPosition) diff --git a/src/viam/sdk/components/private/switch_client.hpp b/src/viam/sdk/components/private/switch_client.hpp index ce93b1200..547520700 100644 --- a/src/viam/sdk/components/private/switch_client.hpp +++ b/src/viam/sdk/components/private/switch_client.hpp @@ -3,11 +3,10 @@ /// @brief Implements a gRPC client for the `Switch` component #pragma once -#include - #include #include +#include namespace viam { namespace sdk { @@ -19,7 +18,11 @@ namespace impl { class SwitchClient : public Switch { public: using interface_type = Switch; - SwitchClient(std::string name, std::shared_ptr channel); + SwitchClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } void set_position(uint32_t position, const ProtoStruct& extra) override; uint32_t get_position(const ProtoStruct& extra) override; @@ -35,7 +38,7 @@ class SwitchClient : public Switch { private: using StubType = viam::component::switch_::v1::SwitchService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/registry/registry.hpp b/src/viam/sdk/registry/registry.hpp index c1a5055a3..8e8f5e6da 100644 --- a/src/viam/sdk/registry/registry.hpp +++ b/src/viam/sdk/registry/registry.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace google { @@ -57,8 +58,8 @@ class ResourceClientRegistration { /// @param name The name of the resource. /// @param channel A channel connected to the client. /// @return A `shared_ptr` to the resource client. - virtual std::shared_ptr create_rpc_client( - std::string name, std::shared_ptr channel) const = 0; + virtual std::shared_ptr create_rpc_client(std::string name, + ViamChannel& channel) const = 0; }; // TODO(RSDK-6616): instead of std::functions, consider making these functions @@ -129,9 +130,9 @@ class Registry { public: using ResourceClientRegistration::ResourceClientRegistration; - std::shared_ptr create_rpc_client( - std::string name, std::shared_ptr chan) const override { - return std::make_shared(std::move(name), std::move(chan)); + std::shared_ptr create_rpc_client(std::string name, + ViamChannel& channel) const override { + return std::make_shared(std::move(name), channel); } }; diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 82959dceb..c16bda266 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -88,7 +88,8 @@ bool operator==(const RobotClient::operation& lhs, const RobotClient::operation& } struct RobotClient::impl { - impl(std::unique_ptr stub) : stub(std::move(stub)) {} + impl(std::unique_ptr stub, ViamChannel& channel) + : stub(std::move(stub)), channel_(&channel) {} ~impl() { if (log_sink) { @@ -106,7 +107,12 @@ struct RobotClient::impl { return make_client_helper(self.get(), *self->stub, m); } + const ViamChannel& channel() const { + return *channel_; + } + std::unique_ptr stub; + ViamChannel* channel_; // See doc comment for RobotClient::connect_logging. This pointer is non-null and installed as a // sink only for apps being run by viam-server as a module. @@ -201,7 +207,7 @@ void RobotClient::refresh() { if (rs) { try { const std::shared_ptr rpc_client = - rs->create_rpc_client(name.name(), viam_channel_.channel()); + rs->create_rpc_client(name.name(), viam_channel_); const Name name_({name.namespace_(), name.type(), name.subtype()}, "", name.name()); new_resources.emplace(name_, rpc_client); } catch (const std::exception& exc) { @@ -233,7 +239,8 @@ void RobotClient::refresh_every() { RobotClient::RobotClient(ViamChannel channel) : viam_channel_(std::move(channel)), - impl_(std::make_unique(RobotService::NewStub(viam_channel_.channel()))) {} + impl_(std::make_unique(RobotService::NewStub(viam_channel_.channel()), viam_channel_)) { +} std::vector RobotClient::resource_names() const { const std::lock_guard lock(lock_); diff --git a/src/viam/sdk/services/private/discovery_client.cpp b/src/viam/sdk/services/private/discovery_client.cpp index 398cfe609..e2d954c3a 100644 --- a/src/viam/sdk/services/private/discovery_client.cpp +++ b/src/viam/sdk/services/private/discovery_client.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -13,10 +15,10 @@ namespace viam { namespace sdk { namespace impl { -DiscoveryClient::DiscoveryClient(std::string name, std::shared_ptr channel) +DiscoveryClient::DiscoveryClient(std::string name, ViamChannel& channel) : Discovery(std::move(name)), - stub_(viam::service::discovery::v1::DiscoveryService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::service::discovery::v1::DiscoveryService::NewStub(channel.channel())), + channel_(&channel) {} std::vector DiscoveryClient::discover_resources(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::DiscoverResources) diff --git a/src/viam/sdk/services/private/discovery_client.hpp b/src/viam/sdk/services/private/discovery_client.hpp index 4b47cd4cc..2a2a3dc90 100644 --- a/src/viam/sdk/services/private/discovery_client.hpp +++ b/src/viam/sdk/services/private/discovery_client.hpp @@ -3,10 +3,9 @@ /// @brief Implements a gRPC client for the `Discovery` service #pragma once -#include - #include +#include #include namespace viam { @@ -19,7 +18,11 @@ namespace impl { class DiscoveryClient : public Discovery { public: using interface_type = Discovery; - DiscoveryClient(std::string name, std::shared_ptr channel); + DiscoveryClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } std::vector discover_resources(const ProtoStruct& extra) override; ProtoStruct do_command(const ProtoStruct& command) override; @@ -27,7 +30,7 @@ class DiscoveryClient : public Discovery { private: using StubType = viam::service::discovery::v1::DiscoveryService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/services/private/generic_client.cpp b/src/viam/sdk/services/private/generic_client.cpp index 50efa1a8b..fbbe5da08 100644 --- a/src/viam/sdk/services/private/generic_client.cpp +++ b/src/viam/sdk/services/private/generic_client.cpp @@ -2,6 +2,8 @@ #include +#include + #include #include @@ -15,10 +17,10 @@ namespace viam { namespace sdk { namespace impl { -GenericServiceClient::GenericServiceClient(std::string name, std::shared_ptr channel) +GenericServiceClient::GenericServiceClient(std::string name, ViamChannel& channel) : GenericService(std::move(name)), - stub_(viam::service::generic::v1::GenericService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(viam::service::generic::v1::GenericService::NewStub(channel.channel())), + channel_(&channel) {} ProtoStruct GenericServiceClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) diff --git a/src/viam/sdk/services/private/generic_client.hpp b/src/viam/sdk/services/private/generic_client.hpp index e5e72cd99..3e07a5019 100644 --- a/src/viam/sdk/services/private/generic_client.hpp +++ b/src/viam/sdk/services/private/generic_client.hpp @@ -3,11 +3,10 @@ /// @brief Implements a gRPC client for the `GenericService`. #pragma once -#include - #include #include +#include #include namespace viam { @@ -20,7 +19,12 @@ namespace impl { class GenericServiceClient : public GenericService { public: using interface_type = GenericService; - GenericServiceClient(std::string name, std::shared_ptr channel); + GenericServiceClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } + ProtoStruct do_command(const ProtoStruct& command) override; protected: @@ -30,12 +34,12 @@ class GenericServiceClient : public GenericService { GenericServiceClient( std::string name, std::unique_ptr stub) - : GenericService(std::move(name)), stub_(std::move(stub)){}; + : GenericService(std::move(name)), stub_(std::move(stub)) {}; private: using StubType = viam::service::generic::v1::GenericService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/services/private/mlmodel_client.cpp b/src/viam/sdk/services/private/mlmodel_client.cpp index be8cf40c1..318ff7f76 100644 --- a/src/viam/sdk/services/private/mlmodel_client.cpp +++ b/src/viam/sdk/services/private/mlmodel_client.cpp @@ -35,10 +35,10 @@ namespace viam { namespace sdk { namespace impl { -MLModelServiceClient::MLModelServiceClient(std::string name, std::shared_ptr channel) +MLModelServiceClient::MLModelServiceClient(std::string name, ViamChannel& channel) : MLModelService(std::move(name)), - channel_(std::move(channel)), - stub_(service_type::NewStub(channel_)) {} + stub_(service_type::NewStub(channel.channel())), + channel_(&channel) {} std::shared_ptr MLModelServiceClient::infer( const named_tensor_views& inputs, const ProtoStruct& extra) { diff --git a/src/viam/sdk/services/private/mlmodel_client.hpp b/src/viam/sdk/services/private/mlmodel_client.hpp index 4cab6aa5d..b3cb092bc 100644 --- a/src/viam/sdk/services/private/mlmodel_client.hpp +++ b/src/viam/sdk/services/private/mlmodel_client.hpp @@ -16,8 +16,7 @@ #include -#include - +#include #include namespace viam { @@ -34,7 +33,11 @@ class MLModelServiceClient : public MLModelService { using interface_type = MLModelService; using service_type = viam::service::mlmodel::v1::MLModelService; - MLModelServiceClient(std::string name, std::shared_ptr channel); + MLModelServiceClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } std::shared_ptr infer(const named_tensor_views& inputs, const ProtoStruct& extra) override; @@ -53,8 +56,8 @@ class MLModelServiceClient : public MLModelService { using MLModelService::metadata; private: - std::shared_ptr channel_; std::unique_ptr stub_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index eddcd959e..6ff2e352d 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -178,10 +179,10 @@ std::vector from_proto( return plans; } -MotionClient::MotionClient(std::string name, std::shared_ptr channel) +MotionClient::MotionClient(std::string name, ViamChannel& channel) : Motion(std::move(name)), - stub_(service::motion::v1::MotionService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(service::motion::v1::MotionService::NewStub(channel.channel())), + channel_(&channel) {} bool MotionClient::move(const pose_in_frame& destination, const Name& component_name, diff --git a/src/viam/sdk/services/private/motion_client.hpp b/src/viam/sdk/services/private/motion_client.hpp index 1fc57141c..62e797576 100644 --- a/src/viam/sdk/services/private/motion_client.hpp +++ b/src/viam/sdk/services/private/motion_client.hpp @@ -3,10 +3,9 @@ /// @brief Implements a gRPC client for the `Motion` service. #pragma once -#include - #include +#include #include namespace viam { @@ -19,7 +18,12 @@ namespace impl { class MotionClient : public Motion { public: using interface_type = Motion; - MotionClient(std::string name, std::shared_ptr channel); + MotionClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } + bool move(const pose_in_frame& destination, const Name& component_name, const std::shared_ptr& world_state, @@ -104,7 +108,7 @@ class MotionClient : public Motion { bool last_plan_only, const ProtoStruct& extra); std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/services/private/navigation_client.cpp b/src/viam/sdk/services/private/navigation_client.cpp index fdff15b9d..6234ac78d 100644 --- a/src/viam/sdk/services/private/navigation_client.cpp +++ b/src/viam/sdk/services/private/navigation_client.cpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -38,10 +39,10 @@ namespace impl { using namespace viam::service::navigation::v1; -NavigationClient::NavigationClient(std::string name, std::shared_ptr channel) +NavigationClient::NavigationClient(std::string name, ViamChannel& channel) : Navigation(std::move(name)), - stub_(service::navigation::v1::NavigationService::NewStub(channel)), - channel_(std::move(channel)) {} + stub_(service::navigation::v1::NavigationService::NewStub(channel.channel())), + channel_(&channel) {} Navigation::Mode NavigationClient::get_mode(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetMode) diff --git a/src/viam/sdk/services/private/navigation_client.hpp b/src/viam/sdk/services/private/navigation_client.hpp index 8f32fb38f..6b2467b31 100644 --- a/src/viam/sdk/services/private/navigation_client.hpp +++ b/src/viam/sdk/services/private/navigation_client.hpp @@ -3,10 +3,9 @@ /// @brief Implements a gRPC client for the `Navigation` service. #pragma once -#include - #include +#include #include namespace viam { @@ -19,7 +18,11 @@ namespace impl { class NavigationClient : public Navigation { public: using interface_type = Navigation; - NavigationClient(std::string name, std::shared_ptr channel); + NavigationClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } Mode get_mode(const ProtoStruct& extra) override; void set_mode(const Mode mode, const ProtoStruct& extra) override; @@ -35,7 +38,7 @@ class NavigationClient : public Navigation { private: using StubType = service::navigation::v1::NavigationService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; } // namespace impl From 2e2b2ecac3e2d0484a34977c4d4e3070df1a5f51 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 15 May 2025 15:46:37 -0400 Subject: [PATCH 31/37] test code update --- src/viam/sdk/tests/test_utils.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/viam/sdk/tests/test_utils.hpp b/src/viam/sdk/tests/test_utils.hpp index a06de3a30..9ba12976e 100644 --- a/src/viam/sdk/tests/test_utils.hpp +++ b/src/viam/sdk/tests/test_utils.hpp @@ -92,11 +92,11 @@ void client_to_mock_pipeline(std::shared_ptr mock, F&& test_case) { // Create a resource-specific client to the mock over an established // in-process gRPC channel. auto test_server = TestServer(server); - auto grpc_channel = test_server.grpc_in_process_channel(); + auto channel = sdk::ViamChannel(test_server.grpc_in_process_channel()); auto resource_client = sdk::Registry::get() .lookup_resource_client(API::get()) - ->create_rpc_client(mock->name(), std::move(grpc_channel)); + ->create_rpc_client(mock->name(), channel); // Run the passed-in test case on the created stack and give access to the // created resource-specific client. From 67aec53bd21284d03da9131ee5e7c7c3338fff11 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 15 May 2025 15:50:55 -0400 Subject: [PATCH 32/37] update complex module rpc clients --- src/viam/examples/modules/complex/gizmo/api.cpp | 8 ++++---- src/viam/examples/modules/complex/gizmo/api.hpp | 8 ++++++-- src/viam/examples/modules/complex/summation/api.cpp | 10 +++++----- src/viam/examples/modules/complex/summation/api.hpp | 9 +++++++-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/viam/examples/modules/complex/gizmo/api.cpp b/src/viam/examples/modules/complex/gizmo/api.cpp index 4059c9034..169e0ce95 100644 --- a/src/viam/examples/modules/complex/gizmo/api.cpp +++ b/src/viam/examples/modules/complex/gizmo/api.cpp @@ -24,12 +24,12 @@ API API::traits::api() { return {"viam", "component", "gizmo"}; } -Gizmo::Gizmo(std::string name) : Component(std::move(name)){}; +Gizmo::Gizmo(std::string name) : Component(std::move(name)) {}; /* Gizmo server methods */ GizmoServer::GizmoServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {}; grpc::Status GizmoServer::DoOne(grpc::ServerContext* context, const DoOneRequest* request, @@ -169,8 +169,8 @@ grpc::Status GizmoServer::DoTwo(::grpc::ServerContext* context, /* Gizmo client methods */ -GizmoClient::GizmoClient(std::string name, std::shared_ptr channel) - : Gizmo(std::move(name)), stub_(GizmoService::NewStub(channel)), channel_(std::move(channel)){}; +GizmoClient::GizmoClient(std::string name, ViamChannel& channel) + : Gizmo(std::move(name)), stub_(GizmoService::NewStub(channel.channel())), channel_(&channel) {} bool GizmoClient::do_one(std::string arg1) { return make_client_helper(this, *stub_, &StubType::DoOne) diff --git a/src/viam/examples/modules/complex/gizmo/api.hpp b/src/viam/examples/modules/complex/gizmo/api.hpp index fa5cc44ba..b7e89a4af 100644 --- a/src/viam/examples/modules/complex/gizmo/api.hpp +++ b/src/viam/examples/modules/complex/gizmo/api.hpp @@ -41,7 +41,11 @@ struct API::traits { class GizmoClient : public Gizmo { public: using interface_type = Gizmo; - GizmoClient(std::string name, std::shared_ptr channel); + GizmoClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } bool do_one(std::string arg1) override; bool do_one_client_stream(std::vector arg1) override; @@ -52,7 +56,7 @@ class GizmoClient : public Gizmo { private: using StubType = GizmoService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; // `GizmoServer` is the gRPC server implementation of a `Gizmo` component. diff --git a/src/viam/examples/modules/complex/summation/api.cpp b/src/viam/examples/modules/complex/summation/api.cpp index cab868ad8..1c2e64f2e 100644 --- a/src/viam/examples/modules/complex/summation/api.cpp +++ b/src/viam/examples/modules/complex/summation/api.cpp @@ -24,12 +24,12 @@ API API::traits::api() { return {"viam", "service", "summation"}; } -Summation::Summation(std::string name) : Service(std::move(name)){}; +Summation::Summation(std::string name) : Service(std::move(name)) {}; /* Summation server methods */ SummationServer::SummationServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {}; grpc::Status SummationServer::Sum(grpc::ServerContext* context, const SumRequest* request, @@ -55,10 +55,10 @@ grpc::Status SummationServer::Sum(grpc::ServerContext* context, /* Summation client methods */ -SummationClient::SummationClient(std::string name, std::shared_ptr channel) +SummationClient::SummationClient(std::string name, ViamChannel& channel) : Summation(std::move(name)), - stub_(SummationService::NewStub(channel)), - channel_(std::move(channel)){}; + stub_(SummationService::NewStub(channel.channel())), + channel_(&channel) {} double SummationClient::sum(std::vector numbers) { return make_client_helper(this, *stub_, &StubType::Sum) diff --git a/src/viam/examples/modules/complex/summation/api.hpp b/src/viam/examples/modules/complex/summation/api.hpp index 923b75a5a..67110fba5 100644 --- a/src/viam/examples/modules/complex/summation/api.hpp +++ b/src/viam/examples/modules/complex/summation/api.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "summation.grpc.pb.h" @@ -41,14 +42,18 @@ struct API::traits { class SummationClient : public Summation { public: using interface_type = Summation; - SummationClient(std::string name, std::shared_ptr channel); + SummationClient(std::string name, ViamChannel& channel); + + const ViamChannel& channel() const { + return *channel_; + } double sum(std::vector numbers) override; private: using StubType = SummationService::StubInterface; std::unique_ptr stub_; - std::shared_ptr channel_; + ViamChannel* channel_; }; // `SummationServer` is the gRPC server implementation of a `Summation` From 5a3d47c00ce75fccc267766dedf5b0dc726262b2 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 15 May 2025 15:55:24 -0400 Subject: [PATCH 33/37] const viam channel --- src/viam/examples/modules/complex/gizmo/api.cpp | 2 +- src/viam/examples/modules/complex/gizmo/api.hpp | 4 ++-- src/viam/examples/modules/complex/summation/api.cpp | 2 +- src/viam/examples/modules/complex/summation/api.hpp | 4 ++-- src/viam/sdk/components/private/arm_client.cpp | 2 +- src/viam/sdk/components/private/arm_client.hpp | 4 ++-- src/viam/sdk/components/private/base_client.cpp | 2 +- src/viam/sdk/components/private/base_client.hpp | 4 ++-- src/viam/sdk/components/private/board_client.cpp | 2 +- src/viam/sdk/components/private/board_client.hpp | 2 +- src/viam/sdk/components/private/button_client.cpp | 2 +- src/viam/sdk/components/private/button_client.hpp | 4 ++-- src/viam/sdk/components/private/camera_client.cpp | 2 +- src/viam/sdk/components/private/camera_client.hpp | 4 ++-- src/viam/sdk/components/private/encoder_client.cpp | 2 +- src/viam/sdk/components/private/encoder_client.hpp | 4 ++-- src/viam/sdk/components/private/gantry_client.cpp | 2 +- src/viam/sdk/components/private/gantry_client.hpp | 4 ++-- src/viam/sdk/components/private/generic_client.cpp | 2 +- src/viam/sdk/components/private/generic_client.hpp | 4 ++-- src/viam/sdk/components/private/gripper_client.cpp | 2 +- src/viam/sdk/components/private/gripper_client.hpp | 4 ++-- src/viam/sdk/components/private/motor_client.cpp | 2 +- src/viam/sdk/components/private/motor_client.hpp | 4 ++-- src/viam/sdk/components/private/movement_sensor_client.cpp | 2 +- src/viam/sdk/components/private/movement_sensor_client.hpp | 4 ++-- src/viam/sdk/components/private/pose_tracker_client.cpp | 2 +- src/viam/sdk/components/private/pose_tracker_client.hpp | 4 ++-- src/viam/sdk/components/private/power_sensor_client.cpp | 2 +- src/viam/sdk/components/private/power_sensor_client.hpp | 4 ++-- src/viam/sdk/components/private/sensor_client.cpp | 2 +- src/viam/sdk/components/private/sensor_client.hpp | 4 ++-- src/viam/sdk/components/private/servo_client.cpp | 2 +- src/viam/sdk/components/private/servo_client.hpp | 4 ++-- src/viam/sdk/components/private/switch_client.cpp | 2 +- src/viam/sdk/components/private/switch_client.hpp | 4 ++-- src/viam/sdk/robot/client.cpp | 2 +- src/viam/sdk/services/private/discovery_client.cpp | 2 +- src/viam/sdk/services/private/discovery_client.hpp | 4 ++-- src/viam/sdk/services/private/generic_client.cpp | 2 +- src/viam/sdk/services/private/generic_client.hpp | 4 ++-- src/viam/sdk/services/private/mlmodel_client.cpp | 2 +- src/viam/sdk/services/private/mlmodel_client.hpp | 4 ++-- src/viam/sdk/services/private/motion_client.cpp | 2 +- src/viam/sdk/services/private/motion_client.hpp | 4 ++-- src/viam/sdk/services/private/navigation_client.cpp | 2 +- src/viam/sdk/services/private/navigation_client.hpp | 4 ++-- 47 files changed, 69 insertions(+), 69 deletions(-) diff --git a/src/viam/examples/modules/complex/gizmo/api.cpp b/src/viam/examples/modules/complex/gizmo/api.cpp index 169e0ce95..d23340d28 100644 --- a/src/viam/examples/modules/complex/gizmo/api.cpp +++ b/src/viam/examples/modules/complex/gizmo/api.cpp @@ -169,7 +169,7 @@ grpc::Status GizmoServer::DoTwo(::grpc::ServerContext* context, /* Gizmo client methods */ -GizmoClient::GizmoClient(std::string name, ViamChannel& channel) +GizmoClient::GizmoClient(std::string name, const ViamChannel& channel) : Gizmo(std::move(name)), stub_(GizmoService::NewStub(channel.channel())), channel_(&channel) {} bool GizmoClient::do_one(std::string arg1) { diff --git a/src/viam/examples/modules/complex/gizmo/api.hpp b/src/viam/examples/modules/complex/gizmo/api.hpp index b7e89a4af..47aee558c 100644 --- a/src/viam/examples/modules/complex/gizmo/api.hpp +++ b/src/viam/examples/modules/complex/gizmo/api.hpp @@ -41,7 +41,7 @@ struct API::traits { class GizmoClient : public Gizmo { public: using interface_type = Gizmo; - GizmoClient(std::string name, ViamChannel& channel); + GizmoClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -56,7 +56,7 @@ class GizmoClient : public Gizmo { private: using StubType = GizmoService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; // `GizmoServer` is the gRPC server implementation of a `Gizmo` component. diff --git a/src/viam/examples/modules/complex/summation/api.cpp b/src/viam/examples/modules/complex/summation/api.cpp index 1c2e64f2e..f0367defe 100644 --- a/src/viam/examples/modules/complex/summation/api.cpp +++ b/src/viam/examples/modules/complex/summation/api.cpp @@ -55,7 +55,7 @@ grpc::Status SummationServer::Sum(grpc::ServerContext* context, /* Summation client methods */ -SummationClient::SummationClient(std::string name, ViamChannel& channel) +SummationClient::SummationClient(std::string name, const ViamChannel& channel) : Summation(std::move(name)), stub_(SummationService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/examples/modules/complex/summation/api.hpp b/src/viam/examples/modules/complex/summation/api.hpp index 67110fba5..1d3cfda9e 100644 --- a/src/viam/examples/modules/complex/summation/api.hpp +++ b/src/viam/examples/modules/complex/summation/api.hpp @@ -42,7 +42,7 @@ struct API::traits { class SummationClient : public Summation { public: using interface_type = Summation; - SummationClient(std::string name, ViamChannel& channel); + SummationClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -53,7 +53,7 @@ class SummationClient : public Summation { private: using StubType = SummationService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; // `SummationServer` is the gRPC server implementation of a `Summation` diff --git a/src/viam/sdk/components/private/arm_client.cpp b/src/viam/sdk/components/private/arm_client.cpp index 0048dda79..a1a5d21a5 100644 --- a/src/viam/sdk/components/private/arm_client.cpp +++ b/src/viam/sdk/components/private/arm_client.cpp @@ -11,7 +11,7 @@ namespace viam { namespace sdk { namespace impl { -ArmClient::ArmClient(std::string name, ViamChannel& channel) +ArmClient::ArmClient(std::string name, const ViamChannel& channel) : Arm(std::move(name)), stub_(viam::component::arm::v1::ArmService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/arm_client.hpp b/src/viam/sdk/components/private/arm_client.hpp index a599998e5..426902ba4 100644 --- a/src/viam/sdk/components/private/arm_client.hpp +++ b/src/viam/sdk/components/private/arm_client.hpp @@ -18,7 +18,7 @@ namespace impl { class ArmClient : public Arm { public: using interface_type = Arm; - ArmClient(std::string name, ViamChannel& channel); + ArmClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -52,7 +52,7 @@ class ArmClient : public Arm { private: using StubType = viam::component::arm::v1::ArmService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/base_client.cpp b/src/viam/sdk/components/private/base_client.cpp index b383a761d..231fe6feb 100644 --- a/src/viam/sdk/components/private/base_client.cpp +++ b/src/viam/sdk/components/private/base_client.cpp @@ -24,7 +24,7 @@ namespace viam { namespace sdk { namespace impl { -BaseClient::BaseClient(std::string name, ViamChannel& channel) +BaseClient::BaseClient(std::string name, const ViamChannel& channel) : Base(std::move(name)), stub_(viam::component::base::v1::BaseService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/base_client.hpp b/src/viam/sdk/components/private/base_client.hpp index 81115eb95..acf691239 100644 --- a/src/viam/sdk/components/private/base_client.hpp +++ b/src/viam/sdk/components/private/base_client.hpp @@ -23,7 +23,7 @@ namespace impl { class BaseClient : public Base { public: using interface_type = Base; - BaseClient(std::string name, ViamChannel& channel); + BaseClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -62,7 +62,7 @@ class BaseClient : public Base { private: using StubType = viam::component::base::v1::BaseService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/board_client.cpp b/src/viam/sdk/components/private/board_client.cpp index 97fdf16e2..486fd62b9 100644 --- a/src/viam/sdk/components/private/board_client.cpp +++ b/src/viam/sdk/components/private/board_client.cpp @@ -51,7 +51,7 @@ viam::component::board::v1::PowerMode to_proto(Board::power_mode power_mode) { } } -BoardClient::BoardClient(std::string name, ViamChannel& channel) +BoardClient::BoardClient(std::string name, const ViamChannel& channel) : Board(std::move(name)), stub_(viam::component::board::v1::BoardService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/board_client.hpp b/src/viam/sdk/components/private/board_client.hpp index 76f8e79eb..123bfd04c 100644 --- a/src/viam/sdk/components/private/board_client.hpp +++ b/src/viam/sdk/components/private/board_client.hpp @@ -21,7 +21,7 @@ namespace impl { class BoardClient : public Board { public: using interface_type = Board; - BoardClient(std::string name, ViamChannel& channel); + BoardClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; diff --git a/src/viam/sdk/components/private/button_client.cpp b/src/viam/sdk/components/private/button_client.cpp index bf5d7f761..8de978b41 100644 --- a/src/viam/sdk/components/private/button_client.cpp +++ b/src/viam/sdk/components/private/button_client.cpp @@ -12,7 +12,7 @@ namespace viam { namespace sdk { namespace impl { -ButtonClient::ButtonClient(std::string name, ViamChannel& channel) +ButtonClient::ButtonClient(std::string name, const ViamChannel& channel) : Button(std::move(name)), stub_(viam::component::button::v1::ButtonService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/button_client.hpp b/src/viam/sdk/components/private/button_client.hpp index 3d45efd35..dc6e7684d 100644 --- a/src/viam/sdk/components/private/button_client.hpp +++ b/src/viam/sdk/components/private/button_client.hpp @@ -20,7 +20,7 @@ namespace impl { class ButtonClient : public Button { public: using interface_type = Button; - ButtonClient(std::string name, ViamChannel& channel); + ButtonClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -34,7 +34,7 @@ class ButtonClient : public Button { private: using StubType = viam::component::button::v1::ButtonService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/camera_client.cpp b/src/viam/sdk/components/private/camera_client.cpp index 71a679202..4d03d772f 100644 --- a/src/viam/sdk/components/private/camera_client.cpp +++ b/src/viam/sdk/components/private/camera_client.cpp @@ -104,7 +104,7 @@ Camera::properties from_proto(const viam::component::camera::v1::GetPropertiesRe (proto.frame_rate())}; } -CameraClient::CameraClient(std::string name, ViamChannel& channel) +CameraClient::CameraClient(std::string name, const ViamChannel& channel) : Camera(std::move(name)), stub_(viam::component::camera::v1::CameraService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/camera_client.hpp b/src/viam/sdk/components/private/camera_client.hpp index f90b1b4e1..e73f62df4 100644 --- a/src/viam/sdk/components/private/camera_client.hpp +++ b/src/viam/sdk/components/private/camera_client.hpp @@ -22,7 +22,7 @@ namespace impl { class CameraClient : public Camera { public: using interface_type = Camera; - CameraClient(std::string name, ViamChannel& channel); + CameraClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -58,7 +58,7 @@ class CameraClient : public Camera { private: using StubType = viam::component::camera::v1::CameraService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/encoder_client.cpp b/src/viam/sdk/components/private/encoder_client.cpp index a9c805c9f..5ee1a477c 100644 --- a/src/viam/sdk/components/private/encoder_client.cpp +++ b/src/viam/sdk/components/private/encoder_client.cpp @@ -47,7 +47,7 @@ Encoder::properties from_proto(const viam::component::encoder::v1::GetProperties return properties; } -EncoderClient::EncoderClient(std::string name, ViamChannel& channel) +EncoderClient::EncoderClient(std::string name, const ViamChannel& channel) : Encoder(std::move(name)), stub_(viam::component::encoder::v1::EncoderService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/encoder_client.hpp b/src/viam/sdk/components/private/encoder_client.hpp index 2e28dfd0c..67868175e 100644 --- a/src/viam/sdk/components/private/encoder_client.hpp +++ b/src/viam/sdk/components/private/encoder_client.hpp @@ -19,7 +19,7 @@ namespace impl { class EncoderClient : public Encoder { public: using interface_type = Encoder; - EncoderClient(std::string name, ViamChannel& channel); + EncoderClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -47,7 +47,7 @@ class EncoderClient : public Encoder { private: using StubType = viam::component::encoder::v1::EncoderService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/gantry_client.cpp b/src/viam/sdk/components/private/gantry_client.cpp index e2d459fcc..4d86227db 100644 --- a/src/viam/sdk/components/private/gantry_client.cpp +++ b/src/viam/sdk/components/private/gantry_client.cpp @@ -11,7 +11,7 @@ namespace viam { namespace sdk { namespace impl { -GantryClient::GantryClient(std::string name, ViamChannel& channel) +GantryClient::GantryClient(std::string name, const ViamChannel& channel) : Gantry(std::move(name)), stub_(viam::component::gantry::v1::GantryService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/gantry_client.hpp b/src/viam/sdk/components/private/gantry_client.hpp index c1f536238..75b73d21c 100644 --- a/src/viam/sdk/components/private/gantry_client.hpp +++ b/src/viam/sdk/components/private/gantry_client.hpp @@ -18,7 +18,7 @@ namespace impl { class GantryClient : public Gantry { public: using interface_type = Gantry; - GantryClient(std::string name, ViamChannel& channel); + GantryClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -44,7 +44,7 @@ class GantryClient : public Gantry { private: using StubType = viam::component::gantry::v1::GantryService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/generic_client.cpp b/src/viam/sdk/components/private/generic_client.cpp index 27e9d4a12..42917e228 100644 --- a/src/viam/sdk/components/private/generic_client.cpp +++ b/src/viam/sdk/components/private/generic_client.cpp @@ -17,7 +17,7 @@ namespace viam { namespace sdk { namespace impl { -GenericComponentClient::GenericComponentClient(std::string name, ViamChannel& channel) +GenericComponentClient::GenericComponentClient(std::string name, const ViamChannel& channel) : GenericComponent(std::move(name)), stub_(viam::component::generic::v1::GenericService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/generic_client.hpp b/src/viam/sdk/components/private/generic_client.hpp index ddfd4de27..f862a865f 100644 --- a/src/viam/sdk/components/private/generic_client.hpp +++ b/src/viam/sdk/components/private/generic_client.hpp @@ -19,7 +19,7 @@ namespace impl { class GenericComponentClient : public GenericComponent { public: using interface_type = GenericComponent; - GenericComponentClient(std::string name, ViamChannel& channel); + GenericComponentClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -39,7 +39,7 @@ class GenericComponentClient : public GenericComponent { private: using StubType = viam::component::generic::v1::GenericService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/gripper_client.cpp b/src/viam/sdk/components/private/gripper_client.cpp index efa5c37a6..bf325402e 100644 --- a/src/viam/sdk/components/private/gripper_client.cpp +++ b/src/viam/sdk/components/private/gripper_client.cpp @@ -13,7 +13,7 @@ namespace viam { namespace sdk { namespace impl { -GripperClient::GripperClient(std::string name, ViamChannel& channel) +GripperClient::GripperClient(std::string name, const ViamChannel& channel) : Gripper(std::move(name)), stub_(viam::component::gripper::v1::GripperService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/gripper_client.hpp b/src/viam/sdk/components/private/gripper_client.hpp index 2f29d39fe..10527414f 100644 --- a/src/viam/sdk/components/private/gripper_client.hpp +++ b/src/viam/sdk/components/private/gripper_client.hpp @@ -20,7 +20,7 @@ namespace impl { class GripperClient : public Gripper { public: using interface_type = Gripper; - GripperClient(std::string name, ViamChannel& channel); + GripperClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -41,7 +41,7 @@ class GripperClient : public Gripper { private: using StubType = viam::component::gripper::v1::GripperService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/motor_client.cpp b/src/viam/sdk/components/private/motor_client.cpp index dd4f09151..466e0dada 100644 --- a/src/viam/sdk/components/private/motor_client.cpp +++ b/src/viam/sdk/components/private/motor_client.cpp @@ -39,7 +39,7 @@ Motor::properties from_proto(const viam::component::motor::v1::GetPropertiesResp return properties; } -MotorClient::MotorClient(std::string name, ViamChannel& channel) +MotorClient::MotorClient(std::string name, const ViamChannel& channel) : Motor(std::move(name)), stub_(viam::component::motor::v1::MotorService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/motor_client.hpp b/src/viam/sdk/components/private/motor_client.hpp index 5bd1d8607..4cff0088c 100644 --- a/src/viam/sdk/components/private/motor_client.hpp +++ b/src/viam/sdk/components/private/motor_client.hpp @@ -20,7 +20,7 @@ namespace impl { class MotorClient : public Motor { public: using interface_type = Motor; - MotorClient(std::string name, ViamChannel& channel); + MotorClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -61,7 +61,7 @@ class MotorClient : public Motor { private: using StubType = viam::component::motor::v1::MotorService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/movement_sensor_client.cpp b/src/viam/sdk/components/private/movement_sensor_client.cpp index 416958532..39aad6c18 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.cpp +++ b/src/viam/sdk/components/private/movement_sensor_client.cpp @@ -58,7 +58,7 @@ MovementSensor::properties from_proto( return properties; } -MovementSensorClient::MovementSensorClient(std::string name, ViamChannel& channel) +MovementSensorClient::MovementSensorClient(std::string name, const ViamChannel& channel) : MovementSensor(std::move(name)), stub_(viam::component::movementsensor::v1::MovementSensorService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/movement_sensor_client.hpp b/src/viam/sdk/components/private/movement_sensor_client.hpp index fd7643788..f5d62fc50 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.hpp +++ b/src/viam/sdk/components/private/movement_sensor_client.hpp @@ -22,7 +22,7 @@ namespace impl { class MovementSensorClient : public MovementSensor { public: using interface_type = MovementSensor; - MovementSensorClient(std::string name, ViamChannel& channel); + MovementSensorClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -51,7 +51,7 @@ class MovementSensorClient : public MovementSensor { private: using StubType = viam::component::movementsensor::v1::MovementSensorService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/pose_tracker_client.cpp b/src/viam/sdk/components/private/pose_tracker_client.cpp index 2c95f9a32..b183032e1 100644 --- a/src/viam/sdk/components/private/pose_tracker_client.cpp +++ b/src/viam/sdk/components/private/pose_tracker_client.cpp @@ -12,7 +12,7 @@ namespace viam { namespace sdk { namespace impl { -PoseTrackerClient::PoseTrackerClient(std::string name, ViamChannel& channel) +PoseTrackerClient::PoseTrackerClient(std::string name, const ViamChannel& channel) : PoseTracker(std::move(name)), stub_(viam::component::posetracker::v1::PoseTrackerService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/pose_tracker_client.hpp b/src/viam/sdk/components/private/pose_tracker_client.hpp index eef26cd9a..0cba469b6 100644 --- a/src/viam/sdk/components/private/pose_tracker_client.hpp +++ b/src/viam/sdk/components/private/pose_tracker_client.hpp @@ -19,7 +19,7 @@ class PoseTrackerClient : public PoseTracker { public: using interface_type = PoseTracker; - PoseTrackerClient(std::string name, ViamChannel& channel); + PoseTrackerClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -38,7 +38,7 @@ class PoseTrackerClient : public PoseTracker { private: using StubType = viam::component::posetracker::v1::PoseTrackerService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/power_sensor_client.cpp b/src/viam/sdk/components/private/power_sensor_client.cpp index e2c5e03f8..d2fa23839 100644 --- a/src/viam/sdk/components/private/power_sensor_client.cpp +++ b/src/viam/sdk/components/private/power_sensor_client.cpp @@ -40,7 +40,7 @@ PowerSensor::current from_proto(const GetCurrentResponse& proto) { return c; } -PowerSensorClient::PowerSensorClient(std::string name, ViamChannel& channel) +PowerSensorClient::PowerSensorClient(std::string name, const ViamChannel& channel) : PowerSensor(std::move(name)), stub_(PowerSensorService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/power_sensor_client.hpp b/src/viam/sdk/components/private/power_sensor_client.hpp index 0c046491a..189e65828 100644 --- a/src/viam/sdk/components/private/power_sensor_client.hpp +++ b/src/viam/sdk/components/private/power_sensor_client.hpp @@ -21,7 +21,7 @@ namespace impl { class PowerSensorClient : public PowerSensor { public: using interface_type = PowerSensor; - PowerSensorClient(std::string name, ViamChannel& channel); + PowerSensorClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -40,7 +40,7 @@ class PowerSensorClient : public PowerSensor { private: using StubType = viam::component::powersensor::v1::PowerSensorService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/sensor_client.cpp b/src/viam/sdk/components/private/sensor_client.cpp index 44410682f..5ea1cdea5 100644 --- a/src/viam/sdk/components/private/sensor_client.cpp +++ b/src/viam/sdk/components/private/sensor_client.cpp @@ -18,7 +18,7 @@ namespace viam { namespace sdk { namespace impl { -SensorClient::SensorClient(std::string name, ViamChannel& channel) +SensorClient::SensorClient(std::string name, const ViamChannel& channel) : Sensor(std::move(name)), stub_(viam::component::sensor::v1::SensorService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/sensor_client.hpp b/src/viam/sdk/components/private/sensor_client.hpp index e2a1ca08e..30460048f 100644 --- a/src/viam/sdk/components/private/sensor_client.hpp +++ b/src/viam/sdk/components/private/sensor_client.hpp @@ -20,7 +20,7 @@ namespace impl { class SensorClient : public Sensor { public: using interface_type = Sensor; - SensorClient(std::string name, ViamChannel& channel); + SensorClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -35,7 +35,7 @@ class SensorClient : public Sensor { private: using StubType = viam::component::sensor::v1::SensorService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/servo_client.cpp b/src/viam/sdk/components/private/servo_client.cpp index 687e87693..fabb9945f 100644 --- a/src/viam/sdk/components/private/servo_client.cpp +++ b/src/viam/sdk/components/private/servo_client.cpp @@ -27,7 +27,7 @@ Servo::position from_proto(const viam::component::servo::v1::GetPositionResponse return proto.position_deg(); } -ServoClient::ServoClient(std::string name, ViamChannel& channel) +ServoClient::ServoClient(std::string name, const ViamChannel& channel) : Servo(std::move(name)), stub_(viam::component::servo::v1::ServoService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/servo_client.hpp b/src/viam/sdk/components/private/servo_client.hpp index d373c513e..e478b9a4c 100644 --- a/src/viam/sdk/components/private/servo_client.hpp +++ b/src/viam/sdk/components/private/servo_client.hpp @@ -20,7 +20,7 @@ namespace impl { class ServoClient : public Servo { public: using interface_type = Servo; - ServoClient(std::string name, ViamChannel& channel); + ServoClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -39,7 +39,7 @@ class ServoClient : public Servo { using StubType = viam::component::servo::v1::ServoService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/components/private/switch_client.cpp b/src/viam/sdk/components/private/switch_client.cpp index 6b13ab319..f4617fe18 100644 --- a/src/viam/sdk/components/private/switch_client.cpp +++ b/src/viam/sdk/components/private/switch_client.cpp @@ -11,7 +11,7 @@ namespace viam { namespace sdk { namespace impl { -SwitchClient::SwitchClient(std::string name, ViamChannel& channel) +SwitchClient::SwitchClient(std::string name, const ViamChannel& channel) : Switch(std::move(name)), stub_(viam::component::switch_::v1::SwitchService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/components/private/switch_client.hpp b/src/viam/sdk/components/private/switch_client.hpp index 547520700..2177ff874 100644 --- a/src/viam/sdk/components/private/switch_client.hpp +++ b/src/viam/sdk/components/private/switch_client.hpp @@ -18,7 +18,7 @@ namespace impl { class SwitchClient : public Switch { public: using interface_type = Switch; - SwitchClient(std::string name, ViamChannel& channel); + SwitchClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -38,7 +38,7 @@ class SwitchClient : public Switch { private: using StubType = viam::component::switch_::v1::SwitchService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index c16bda266..ebbdf0626 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -112,7 +112,7 @@ struct RobotClient::impl { } std::unique_ptr stub; - ViamChannel* channel_; + const ViamChannel* channel_; // See doc comment for RobotClient::connect_logging. This pointer is non-null and installed as a // sink only for apps being run by viam-server as a module. diff --git a/src/viam/sdk/services/private/discovery_client.cpp b/src/viam/sdk/services/private/discovery_client.cpp index e2d954c3a..4dfc9362f 100644 --- a/src/viam/sdk/services/private/discovery_client.cpp +++ b/src/viam/sdk/services/private/discovery_client.cpp @@ -15,7 +15,7 @@ namespace viam { namespace sdk { namespace impl { -DiscoveryClient::DiscoveryClient(std::string name, ViamChannel& channel) +DiscoveryClient::DiscoveryClient(std::string name, const ViamChannel& channel) : Discovery(std::move(name)), stub_(viam::service::discovery::v1::DiscoveryService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/services/private/discovery_client.hpp b/src/viam/sdk/services/private/discovery_client.hpp index 2a2a3dc90..60d797ab4 100644 --- a/src/viam/sdk/services/private/discovery_client.hpp +++ b/src/viam/sdk/services/private/discovery_client.hpp @@ -18,7 +18,7 @@ namespace impl { class DiscoveryClient : public Discovery { public: using interface_type = Discovery; - DiscoveryClient(std::string name, ViamChannel& channel); + DiscoveryClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -30,7 +30,7 @@ class DiscoveryClient : public Discovery { private: using StubType = viam::service::discovery::v1::DiscoveryService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/services/private/generic_client.cpp b/src/viam/sdk/services/private/generic_client.cpp index fbbe5da08..5c6c97a22 100644 --- a/src/viam/sdk/services/private/generic_client.cpp +++ b/src/viam/sdk/services/private/generic_client.cpp @@ -17,7 +17,7 @@ namespace viam { namespace sdk { namespace impl { -GenericServiceClient::GenericServiceClient(std::string name, ViamChannel& channel) +GenericServiceClient::GenericServiceClient(std::string name, const ViamChannel& channel) : GenericService(std::move(name)), stub_(viam::service::generic::v1::GenericService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/services/private/generic_client.hpp b/src/viam/sdk/services/private/generic_client.hpp index 3e07a5019..91e84327e 100644 --- a/src/viam/sdk/services/private/generic_client.hpp +++ b/src/viam/sdk/services/private/generic_client.hpp @@ -19,7 +19,7 @@ namespace impl { class GenericServiceClient : public GenericService { public: using interface_type = GenericService; - GenericServiceClient(std::string name, ViamChannel& channel); + GenericServiceClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -39,7 +39,7 @@ class GenericServiceClient : public GenericService { private: using StubType = viam::service::generic::v1::GenericService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/services/private/mlmodel_client.cpp b/src/viam/sdk/services/private/mlmodel_client.cpp index 318ff7f76..4383010be 100644 --- a/src/viam/sdk/services/private/mlmodel_client.cpp +++ b/src/viam/sdk/services/private/mlmodel_client.cpp @@ -35,7 +35,7 @@ namespace viam { namespace sdk { namespace impl { -MLModelServiceClient::MLModelServiceClient(std::string name, ViamChannel& channel) +MLModelServiceClient::MLModelServiceClient(std::string name, const ViamChannel& channel) : MLModelService(std::move(name)), stub_(service_type::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/services/private/mlmodel_client.hpp b/src/viam/sdk/services/private/mlmodel_client.hpp index b3cb092bc..93355c9f3 100644 --- a/src/viam/sdk/services/private/mlmodel_client.hpp +++ b/src/viam/sdk/services/private/mlmodel_client.hpp @@ -33,7 +33,7 @@ class MLModelServiceClient : public MLModelService { using interface_type = MLModelService; using service_type = viam::service::mlmodel::v1::MLModelService; - MLModelServiceClient(std::string name, ViamChannel& channel); + MLModelServiceClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -57,7 +57,7 @@ class MLModelServiceClient : public MLModelService { private: std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index 6ff2e352d..e62763cdc 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -179,7 +179,7 @@ std::vector from_proto( return plans; } -MotionClient::MotionClient(std::string name, ViamChannel& channel) +MotionClient::MotionClient(std::string name, const ViamChannel& channel) : Motion(std::move(name)), stub_(service::motion::v1::MotionService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/services/private/motion_client.hpp b/src/viam/sdk/services/private/motion_client.hpp index 62e797576..efb7f1995 100644 --- a/src/viam/sdk/services/private/motion_client.hpp +++ b/src/viam/sdk/services/private/motion_client.hpp @@ -18,7 +18,7 @@ namespace impl { class MotionClient : public Motion { public: using interface_type = Motion; - MotionClient(std::string name, ViamChannel& channel); + MotionClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -108,7 +108,7 @@ class MotionClient : public Motion { bool last_plan_only, const ProtoStruct& extra); std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl diff --git a/src/viam/sdk/services/private/navigation_client.cpp b/src/viam/sdk/services/private/navigation_client.cpp index 6234ac78d..27914f1be 100644 --- a/src/viam/sdk/services/private/navigation_client.cpp +++ b/src/viam/sdk/services/private/navigation_client.cpp @@ -39,7 +39,7 @@ namespace impl { using namespace viam::service::navigation::v1; -NavigationClient::NavigationClient(std::string name, ViamChannel& channel) +NavigationClient::NavigationClient(std::string name, const ViamChannel& channel) : Navigation(std::move(name)), stub_(service::navigation::v1::NavigationService::NewStub(channel.channel())), channel_(&channel) {} diff --git a/src/viam/sdk/services/private/navigation_client.hpp b/src/viam/sdk/services/private/navigation_client.hpp index 6b2467b31..106ddf16b 100644 --- a/src/viam/sdk/services/private/navigation_client.hpp +++ b/src/viam/sdk/services/private/navigation_client.hpp @@ -18,7 +18,7 @@ namespace impl { class NavigationClient : public Navigation { public: using interface_type = Navigation; - NavigationClient(std::string name, ViamChannel& channel); + NavigationClient(std::string name, const ViamChannel& channel); const ViamChannel& channel() const { return *channel_; @@ -38,7 +38,7 @@ class NavigationClient : public Navigation { private: using StubType = service::navigation::v1::NavigationService::StubInterface; std::unique_ptr stub_; - ViamChannel* channel_; + const ViamChannel* channel_; }; } // namespace impl From 350f0ff99e189cf473c59b47e1dc36b958228c1f Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 15 May 2025 16:07:47 -0400 Subject: [PATCH 34/37] linter fixes --- src/viam/examples/modules/complex/gizmo/api.cpp | 2 +- src/viam/examples/modules/complex/summation/api.cpp | 4 ++-- src/viam/sdk/components/private/camera_client.hpp | 2 +- src/viam/sdk/components/private/generic_client.hpp | 2 +- src/viam/sdk/services/private/generic_client.hpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/viam/examples/modules/complex/gizmo/api.cpp b/src/viam/examples/modules/complex/gizmo/api.cpp index d23340d28..479512e07 100644 --- a/src/viam/examples/modules/complex/gizmo/api.cpp +++ b/src/viam/examples/modules/complex/gizmo/api.cpp @@ -29,7 +29,7 @@ Gizmo::Gizmo(std::string name) : Component(std::move(name)) {}; /* Gizmo server methods */ GizmoServer::GizmoServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)) {}; + : ResourceServer(std::move(manager)) {} grpc::Status GizmoServer::DoOne(grpc::ServerContext* context, const DoOneRequest* request, diff --git a/src/viam/examples/modules/complex/summation/api.cpp b/src/viam/examples/modules/complex/summation/api.cpp index f0367defe..ee125f3cc 100644 --- a/src/viam/examples/modules/complex/summation/api.cpp +++ b/src/viam/examples/modules/complex/summation/api.cpp @@ -24,12 +24,12 @@ API API::traits::api() { return {"viam", "service", "summation"}; } -Summation::Summation(std::string name) : Service(std::move(name)) {}; +Summation::Summation(std::string name) : Service(std::move(name)) {} /* Summation server methods */ SummationServer::SummationServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)) {}; + : ResourceServer(std::move(manager)) {} grpc::Status SummationServer::Sum(grpc::ServerContext* context, const SumRequest* request, diff --git a/src/viam/sdk/components/private/camera_client.hpp b/src/viam/sdk/components/private/camera_client.hpp index e73f62df4..ec30cd39f 100644 --- a/src/viam/sdk/components/private/camera_client.hpp +++ b/src/viam/sdk/components/private/camera_client.hpp @@ -53,7 +53,7 @@ class CameraClient : public Camera { // avoid use of this constructor outside of tests. CameraClient(std::string name, std::unique_ptr stub) - : Camera(std::move(name)), stub_(std::move(stub)) {}; + : Camera(std::move(name)), stub_(std::move(stub)) {} private: using StubType = viam::component::camera::v1::CameraService::StubInterface; diff --git a/src/viam/sdk/components/private/generic_client.hpp b/src/viam/sdk/components/private/generic_client.hpp index f862a865f..4a40fa3d9 100644 --- a/src/viam/sdk/components/private/generic_client.hpp +++ b/src/viam/sdk/components/private/generic_client.hpp @@ -34,7 +34,7 @@ class GenericComponentClient : public GenericComponent { GenericComponentClient( std::string name, std::unique_ptr stub) - : GenericComponent(std::move(name)), stub_(std::move(stub)) {}; + : GenericComponent(std::move(name)), stub_(std::move(stub)) {} private: using StubType = viam::component::generic::v1::GenericService::StubInterface; diff --git a/src/viam/sdk/services/private/generic_client.hpp b/src/viam/sdk/services/private/generic_client.hpp index 91e84327e..e849da70a 100644 --- a/src/viam/sdk/services/private/generic_client.hpp +++ b/src/viam/sdk/services/private/generic_client.hpp @@ -34,7 +34,7 @@ class GenericServiceClient : public GenericService { GenericServiceClient( std::string name, std::unique_ptr stub) - : GenericService(std::move(name)), stub_(std::move(stub)) {}; + : GenericService(std::move(name)), stub_(std::move(stub)) {} private: using StubType = viam::service::generic::v1::GenericService::StubInterface; From cb7ec67dd6352219778e1688a569e246a1f5d13f Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 15 May 2025 16:11:47 -0400 Subject: [PATCH 35/37] typo fix --- src/viam/sdk/common/grpc_fwd.hpp.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viam/sdk/common/grpc_fwd.hpp.in b/src/viam/sdk/common/grpc_fwd.hpp.in index 9a148e2ce..970a9db7f 100644 --- a/src/viam/sdk/common/grpc_fwd.hpp.in +++ b/src/viam/sdk/common/grpc_fwd.hpp.in @@ -8,7 +8,7 @@ // The default behavior is for the variable to be undefined, providing the behavior for recent // versions. If you are experiencing compilation errors in an installed version of the SDK it may be // due to a mismatch with the configured version of this header and the grpc version found by the -// compiler. You may wish to comment/uncomment the define above as needed, or add the definition +// compiler. You may wish to comment/uncomment the define below as needed, or add the definition // with `-D` to the compiler. #cmakedefine VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL From ed6bfed6f27651d60db27fd2ef1863c2786734b7 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 15 May 2025 16:12:26 -0400 Subject: [PATCH 36/37] remove no longer needed friend decl --- src/viam/sdk/common/instance.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/viam/sdk/common/instance.hpp b/src/viam/sdk/common/instance.hpp index 557301f46..de10ae896 100644 --- a/src/viam/sdk/common/instance.hpp +++ b/src/viam/sdk/common/instance.hpp @@ -29,8 +29,6 @@ class Instance { private: friend class Registry; friend class LogManager; - friend class ViamChannel; - friend class ClientContext; struct Impl; std::unique_ptr impl_; From 2c7a9427e2a6e8608be592e7138fbfe5ff124594 Mon Sep 17 00:00:00 2001 From: Lia Stratopoulos <167905060+lia-viam@users.noreply.github.com> Date: Thu, 15 May 2025 16:19:29 -0400 Subject: [PATCH 37/37] linter --- src/viam/examples/modules/complex/gizmo/api.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viam/examples/modules/complex/gizmo/api.cpp b/src/viam/examples/modules/complex/gizmo/api.cpp index 479512e07..dbcd6bc1d 100644 --- a/src/viam/examples/modules/complex/gizmo/api.cpp +++ b/src/viam/examples/modules/complex/gizmo/api.cpp @@ -24,7 +24,7 @@ API API::traits::api() { return {"viam", "component", "gizmo"}; } -Gizmo::Gizmo(std::string name) : Component(std::move(name)) {}; +Gizmo::Gizmo(std::string name) : Component(std::move(name)) {} /* Gizmo server methods */