Skip to content

RSDK-10435: Dial direct #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
a56574f
make dial_options a struct
lia-viam Apr 11, 2025
d86eac7
wip: direct dial
lia-viam Apr 15, 2025
4d5f8aa
get auth token successfully
stuqdog Apr 16, 2025
6eb0fdd
make calls successfully
stuqdog Apr 16, 2025
d247cc8
we can still verify server certs
stuqdog Apr 17, 2025
4b7e4c2
cleanup
stuqdog Apr 17, 2025
c41325b
cleanup2
stuqdog Apr 17, 2025
c549c07
docs
lia-viam Apr 22, 2025
a21c424
Merge branch 'main' into dial-direct
lia-viam Apr 24, 2025
41e6960
use instance for global token
lia-viam Apr 24, 2025
d0c3f21
remove dtor logging
lia-viam Apr 24, 2025
1f41cad
Merge branch 'fix/dtor-log' into dial-direct
lia-viam Apr 24, 2025
a53494a
newline
lia-viam Apr 24, 2025
bbc0adf
join if joinable
lia-viam Apr 24, 2025
a3add02
Merge branch 'fix/dtor-log' into dial-direct
lia-viam Apr 24, 2025
1ae3732
Merge branch 'main' into dial-direct
lia-viam May 1, 2025
c3ff877
spacing and variable simplification
lia-viam May 1, 2025
7d8175b
reset parent to prevent double close
lia-viam May 2, 2025
26c2225
do not disable webrtc by default
lia-viam May 2, 2025
0cd8723
revert dtor logging
lia-viam May 2, 2025
d3cdb58
unshare robot client
lia-viam May 2, 2025
c0e2197
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into dial…
lia-viam May 5, 2025
beab7d2
include fixes and helper rename
lia-viam May 5, 2025
89104c9
document direct dial uri
lia-viam May 5, 2025
8d68fd2
factor auth request into helper function
lia-viam May 5, 2025
29cfd35
fix doxygen comment
lia-viam May 5, 2025
fde961c
ifdef namespace alias
lia-viam May 5, 2025
44d8601
use ifdef for direct dial
lia-viam May 12, 2025
c7d8b4c
replace with custom unique_ptr deleters
lia-viam May 12, 2025
afa6c70
fix if stmt
lia-viam May 12, 2025
8ba5efa
overhaul options and refresh interval
lia-viam May 13, 2025
95dc5f8
add include
lia-viam May 14, 2025
f86011f
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into dial…
lia-viam May 14, 2025
4a82a6d
silence unused var warning on ifdef
lia-viam May 14, 2025
8e99746
client context gets token from channel ctor
lia-viam May 14, 2025
a8b858d
sdk overhaul for channel/token
lia-viam May 15, 2025
2e2b2ec
test code update
lia-viam May 15, 2025
67aec53
update complex module rpc clients
lia-viam May 15, 2025
5a3d47c
const viam channel
lia-viam May 15, 2025
350f0ff
linter fixes
lia-viam May 15, 2025
fa1f598
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into dial…
lia-viam May 15, 2025
cb7ec67
typo fix
lia-viam May 15, 2025
ed6bfed
remove no longer needed friend decl
lia-viam May 15, 2025
2c7a942
linter
lia-viam May 15, 2025
5f9d3b1
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into dial…
lia-viam May 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ if (VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.32.0)
set(VIAMCPPSDK_GRPCXX_LEGACY_FWD 1)
endif()

if(VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.43.0)
set(VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we haven't already, I think it is time to file a ticket to introduce substituted config headers, along with prelude/postlude headers to ensure macro hygiene.

endif()

include(FetchContent)

FetchContent_Declare(
Expand Down
8 changes: 8 additions & 0 deletions src/viam/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/viam/examples/camera/example_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ int main() try {
// 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
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<vs::RobotClient> robot =
vs::RobotClient::at_address(robot_address, std::chrono::seconds{1}, dial_options);

std::shared_ptr<vs::RobotClient> robot = vs::RobotClient::at_address(robot_address, options);
VIAM_SDK_LOG(info) << "Successfully connected to the robot";

std::vector<vs::Name> resource_names = robot->resource_names();
Expand Down
8 changes: 3 additions & 5 deletions src/viam/examples/dial/example_dial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ int main() {
DialOptions dial_options;
std::string type = "<your authentication type>";
std::string payload = "<your authentication payload>";
Credentials credentials(type, payload);
dial_options.set_credentials(credentials);
boost::optional<DialOptions> opts(dial_options);
dial_options.credentials = Credentials(type, payload);
std::string address(uri);
Options options(1, opts);

// connect to robot, ensure we can refresh it
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
std::shared_ptr<RobotClient> robot =
RobotClient::at_address(address, std::chrono::seconds{1}, dial_options);

// ensure we can query resources
std::vector<Name> resource_names = robot->resource_names();
Expand Down
16 changes: 9 additions & 7 deletions src/viam/examples/dial_api_key/example_dial_api_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,30 @@ int main(int argc, char* argv[]) {
"uri", po::value<std::string>(), "URI of robot")(
"entity", po::value<std::string>(), "api key ID")(
"api-key", po::value<std::string>(), "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<DialOptions> opts;

DialOptions opts;

if (vm.count("entity") && vm.count("api-key")) {
DialOptions dial_options;
dial_options.set_entity(vm["entity"].as<std::string>());
opts.auth_entity = vm["entity"].as<std::string>();
Credentials credentials("api-key", vm["api-key"].as<std::string>());
dial_options.set_credentials(credentials);
opts = dial_options;
opts.credentials = credentials;
}
Options options(1, opts);

// connect to robot, ensure we can refresh it
std::shared_ptr<RobotClient> robot =
RobotClient::at_address(vm["uri"].as<std::string>(), options);
RobotClient::at_address(vm["uri"].as<std::string>(), std::chrono::seconds{1}, opts);

// ensure we can query resources
std::vector<Name> resource_names = robot->resource_names();

VIAM_SDK_LOG(info) << "Resources:";
for (const Name& resource : resource_names) {
VIAM_SDK_LOG(info) << resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ 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)}});
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
Expand Down
12 changes: 5 additions & 7 deletions src/viam/examples/modules/complex/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,25 @@ 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.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 = "<your authentication type>";
// std::string payload = "<your authentication payload>";
// Credentials credentials(type, payload);
// dial_options.set_credentials(credentials);

boost::optional<DialOptions> 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<GizmoClient>();
Registry::get().register_resource_client<SummationClient>();

// Connect to robot.
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
std::shared_ptr<RobotClient> robot =
RobotClient::at_address(address, std::chrono::seconds{1}, dial_options);

// Print resources.
VIAM_SDK_LOG(info) << "Resources:";
std::vector<Name> resource_names = robot->resource_names();
Expand Down
8 changes: 4 additions & 4 deletions src/viam/examples/modules/complex/gizmo/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ API API::traits<Gizmo>::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<ResourceManager> manager)
: ResourceServer(std::move(manager)){};
: ResourceServer(std::move(manager)) {}

grpc::Status GizmoServer::DoOne(grpc::ServerContext* context,
const DoOneRequest* request,
Expand Down Expand Up @@ -169,8 +169,8 @@ grpc::Status GizmoServer::DoTwo(::grpc::ServerContext* context,

/* Gizmo client methods */

GizmoClient::GizmoClient(std::string name, std::shared_ptr<grpc::Channel> channel)
: Gizmo(std::move(name)), stub_(GizmoService::NewStub(channel)), channel_(std::move(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) {
return make_client_helper(this, *stub_, &StubType::DoOne)
Expand Down
8 changes: 6 additions & 2 deletions src/viam/examples/modules/complex/gizmo/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ struct API::traits<Gizmo> {
class GizmoClient : public Gizmo {
public:
using interface_type = Gizmo;
GizmoClient(std::string name, std::shared_ptr<grpc::Channel> channel);
GizmoClient(std::string name, const ViamChannel& channel);

const ViamChannel& channel() const {
return *channel_;
}

bool do_one(std::string arg1) override;
bool do_one_client_stream(std::vector<std::string> arg1) override;
Expand All @@ -54,7 +58,7 @@ class GizmoClient : public Gizmo {
private:
using StubType = GizmoService::StubInterface;
std::unique_ptr<StubType> stub_;
std::shared_ptr<grpc::Channel> channel_;
const ViamChannel* channel_;
};

// `GizmoServer` is the gRPC server implementation of a `Gizmo` component.
Expand Down
10 changes: 5 additions & 5 deletions src/viam/examples/modules/complex/summation/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ API API::traits<Summation>::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<ResourceManager> manager)
: ResourceServer(std::move(manager)){};
: ResourceServer(std::move(manager)) {}

grpc::Status SummationServer::Sum(grpc::ServerContext* context,
const SumRequest* request,
Expand All @@ -55,10 +55,10 @@ grpc::Status SummationServer::Sum(grpc::ServerContext* context,

/* Summation client methods */

SummationClient::SummationClient(std::string name, std::shared_ptr<grpc::Channel> channel)
SummationClient::SummationClient(std::string name, const 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<double> numbers) {
return make_client_helper(this, *stub_, &StubType::Sum)
Expand Down
9 changes: 7 additions & 2 deletions src/viam/examples/modules/complex/summation/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <viam/sdk/config/resource.hpp>
#include <viam/sdk/registry/registry.hpp>
#include <viam/sdk/resource/resource_manager.hpp>
#include <viam/sdk/rpc/dial.hpp>
#include <viam/sdk/services/service.hpp>

#include "summation.grpc.pb.h"
Expand Down Expand Up @@ -43,14 +44,18 @@ struct API::traits<Summation> {
class SummationClient : public Summation {
public:
using interface_type = Summation;
SummationClient(std::string name, std::shared_ptr<grpc::Channel> channel);
SummationClient(std::string name, const ViamChannel& channel);

const ViamChannel& channel() const {
return *channel_;
}

double sum(std::vector<double> numbers) override;

private:
using StubType = SummationService::StubInterface;
std::unique_ptr<StubType> stub_;
std::shared_ptr<grpc::Channel> channel_;
const ViamChannel* channel_;
};

// `SummationServer` is the gRPC server implementation of a `Summation`
Expand Down
13 changes: 6 additions & 7 deletions src/viam/examples/modules/simple/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ 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.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 = "<your authentication type>";
// std::string payload = "<your authentication payload>";
// Credentials credentials(type, payload);
// dial_options.set_credentials(credentials);

boost::optional<DialOptions> opts(dial_options);
std::string address(uri);
Options options(1, opts);

std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
std::shared_ptr<RobotClient> robot =
RobotClient::at_address(address, std::chrono::seconds{1}, dial_options);

// Print resources
VIAM_SDK_LOG(info) << "Resources";
Expand Down
12 changes: 6 additions & 6 deletions src/viam/examples/motor/example_motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ int main() try {
// 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
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<vs::RobotClient> robot =
vs::RobotClient::at_address(robot_address, std::chrono::seconds{1}, dial_options);

std::shared_ptr<vs::RobotClient> robot = vs::RobotClient::at_address(robot_address, options);
VIAM_SDK_LOG(info) << "Successfully connected to the robot";

std::vector<vs::Name> resource_names = robot->resource_names();
Expand Down
7 changes: 7 additions & 0 deletions src/viam/sdk/common/client_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <grpcpp/client_context.h>
#include <grpcpp/support/status.h>

#include <viam/sdk/common/private/instance.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed?

#include <viam/sdk/common/private/version_metadata.hpp>
#include <viam/sdk/log/logging.hpp>

Expand Down Expand Up @@ -32,6 +33,12 @@ ClientContext::ClientContext() : wrapped_context_(std::make_unique<GrpcClientCon
add_viam_client_version_();
}

ClientContext::ClientContext(const ViamChannel& channel) : ClientContext() {
if (channel.auth_token().has_value()) {
wrapped_context_->AddMetadata("authorization", "Bearer " + *channel.auth_token());
}
}

ClientContext::~ClientContext() = default;

ClientContext::operator const GrpcClientContext*() const {
Expand Down
7 changes: 5 additions & 2 deletions src/viam/sdk/common/client_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <viam/sdk/common/grpc_fwd.hpp>
#include <viam/sdk/common/private/utils.hpp>
#include <viam/sdk/common/proto_value.hpp>
#include <viam/sdk/rpc/dial.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the dial header actually required here?


namespace viam {
namespace sdk {
Expand Down Expand Up @@ -38,6 +39,8 @@ void set_name(...);
class ClientContext {
public:
ClientContext();
ClientContext(const ViamChannel& channel);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe explicit


~ClientContext();

void try_cancel();
Expand Down Expand Up @@ -110,7 +113,7 @@ class ClientHelper {
template <typename ResponseHandlerCallable, typename ErrorHandlerCallable>
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_);
Expand All @@ -132,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_);

Expand Down
10 changes: 10 additions & 0 deletions src/viam/sdk/common/grpc_fwd.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 below 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.
Expand Down
Loading