Skip to content

Commit

Permalink
refer to grpc::Channel by forward decl (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
lia-viam authored Dec 23, 2024
1 parent 8f977f9 commit 5536ef8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
8 changes: 8 additions & 0 deletions src/viam/sdk/common/grpc_client_fwd.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace grpc {

class Channel;

class ClientContext;

template <typename>
Expand All @@ -19,6 +21,8 @@ class ClientReaderInterface;
namespace viam {
namespace sdk {

using GrpcChannel = ::grpc::Channel;

using GrpcClientContext = ::grpc::ClientContext;

template <typename T>
Expand All @@ -34,6 +38,8 @@ using GrpcClientReaderInterface = ::grpc::ClientReaderInterface<T>;

namespace grpc_impl {

class Channel;

class ClientContext;

template <typename>
Expand All @@ -44,6 +50,8 @@ class ClientReaderInterface;
namespace viam {
namespace sdk {

using GrpcChannel = ::grpc_impl::Channel;

using GrpcClientContext = ::grpc_impl::ClientContext;

template <typename T>
Expand Down
5 changes: 3 additions & 2 deletions src/viam/sdk/module/module.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <viam/sdk/common/grpc_client_fwd.hpp>
#include <viam/sdk/module/handler_map.hpp>
#include <viam/sdk/resource/resource.hpp>
#include <viam/sdk/resource/resource_manager.hpp>
Expand All @@ -18,14 +19,14 @@ class Module {
bool ready() const;
const HandlerMap_& handles() const;
HandlerMap_& mutable_handles();
const std::shared_ptr<grpc::Channel>& channel() const;
const std::shared_ptr<GrpcChannel>& channel() const;

private:
std::string name_;
std::string addr_;
bool ready_;
HandlerMap_ handles_;
std::shared_ptr<grpc::Channel> channel_;
std::shared_ptr<GrpcChannel> channel_;
};

} // namespace sdk
Expand Down
23 changes: 23 additions & 0 deletions src/viam/sdk/registry/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,32 @@
namespace viam {
namespace sdk {

ResourceServerRegistration::ResourceServerRegistration(
const google::protobuf::ServiceDescriptor* service_descriptor)
: service_descriptor_(service_descriptor) {}

ResourceServerRegistration::~ResourceServerRegistration() = default;
ResourceClientRegistration::~ResourceClientRegistration() = default;

ModelRegistration::ModelRegistration(
API api,
Model model,
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor)
: construct_resource(std::move(constructor)),
validate(default_validator),
model_(std::move(model)),
api_(std::move(api)) {}

ModelRegistration::ModelRegistration(
API api,
Model model,
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor,
std::function<std::vector<std::string>(ResourceConfig)> validator)
: construct_resource(std::move(constructor)),
validate(std::move(validator)),
model_(std::move(model)),
api_(std::move(api)) {}

const API& ModelRegistration::api() const {
return api_;
};
Expand Down
27 changes: 9 additions & 18 deletions src/viam/sdk/registry/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
#include <grpcpp/channel.h>
#include <grpcpp/impl/service_type.h>
#include <grpcpp/server.h>

#include <viam/sdk/common/grpc_client_fwd.hpp>
#include <viam/sdk/config/resource.hpp>
#include <viam/sdk/resource/resource.hpp>
#include <viam/sdk/resource/resource_api.hpp>
Expand All @@ -24,6 +24,8 @@ namespace sdk {
// TODO(RSDK-6617): one class per header
class ResourceServerRegistration {
public:
ResourceServerRegistration(const google::protobuf::ServiceDescriptor* service_descriptor);

virtual ~ResourceServerRegistration();

/// @brief Create a resource's gRPC server.
Expand All @@ -36,9 +38,6 @@ class ResourceServerRegistration {
/// @brief Returns a reference to the `ResourceServerRegistration`'s service descriptor.
const google::protobuf::ServiceDescriptor* service_descriptor() const;

ResourceServerRegistration(const google::protobuf::ServiceDescriptor* service_descriptor)
: service_descriptor_(service_descriptor){};

private:
const google::protobuf::ServiceDescriptor* service_descriptor_;
};
Expand All @@ -47,16 +46,16 @@ class ResourceServerRegistration {
/// @brief Defines registered `Resource` client creation functionality.
class ResourceClientRegistration {
public:
ResourceClientRegistration() = default;

virtual ~ResourceClientRegistration();

/// @brief Create gRPC client to a resource.
/// @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<Resource> create_rpc_client(
std::string name, std::shared_ptr<grpc::Channel> channel) const = 0;

ResourceClientRegistration() = default;
std::string name, std::shared_ptr<GrpcChannel> channel) const = 0;
};

// TODO(RSDK-6616): instead of std::functions, consider making these functions
Expand All @@ -68,21 +67,13 @@ class ModelRegistration {
ModelRegistration(
API api,
Model model,
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor)
: construct_resource(std::move(constructor)),
validate(default_validator),
model_(std::move(model)),
api_(std::move(api)){};
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor);

ModelRegistration(
API api,
Model model,
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor,
std::function<std::vector<std::string>(ResourceConfig)> validator)
: construct_resource(std::move(constructor)),
validate(std::move(validator)),
model_(std::move(model)),
api_(std::move(api)){};
std::function<std::vector<std::string>(ResourceConfig)> validator);

const API& api() const;
const Model& model() const;
Expand Down Expand Up @@ -134,7 +125,7 @@ class Registry {
using ResourceClientRegistration::ResourceClientRegistration;

std::shared_ptr<Resource> create_rpc_client(
std::string name, std::shared_ptr<grpc::Channel> chan) const override {
std::string name, std::shared_ptr<GrpcChannel> chan) const override {
return std::make_shared<ResourceClientT>(std::move(name), std::move(chan));
}
};
Expand Down
7 changes: 2 additions & 5 deletions src/viam/sdk/robot/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#include <string>
#include <thread>

#include <grpcpp/channel.h>

#include <viam/sdk/common/grpc_client_fwd.hpp>
#include <viam/sdk/common/pose.hpp>
#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/common/world_state.hpp>
Expand All @@ -20,8 +19,6 @@
namespace viam {
namespace sdk {

using grpc::Channel;

/// @defgroup Robot Classes related to a Robot representation.

/// @class RobotClient client.hpp "robot/client.hpp"
Expand Down Expand Up @@ -152,7 +149,7 @@ class RobotClient {
std::vector<std::shared_ptr<std::thread>> threads_;
std::atomic<bool> should_refresh_;
unsigned int refresh_interval_;
std::shared_ptr<Channel> channel_;
std::shared_ptr<GrpcChannel> channel_;
std::shared_ptr<ViamChannel> viam_channel_;
bool should_close_channel_;
struct impl;
Expand Down
10 changes: 6 additions & 4 deletions src/viam/sdk/rpc/dial.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include <chrono>
#include <memory>
#include <string>

#include <boost/optional.hpp>
#include <grpcpp/channel.h>

#include <viam/sdk/common/grpc_client_fwd.hpp>

namespace viam {
namespace sdk {
Expand All @@ -13,14 +15,14 @@ class DialOptions;
class ViamChannel {
public:
void close();
ViamChannel(std::shared_ptr<grpc::Channel> channel, const char* path, void* runtime);
ViamChannel(std::shared_ptr<GrpcChannel> channel, const char* path, void* runtime);
static std::shared_ptr<ViamChannel> dial(const char* uri,
const boost::optional<DialOptions>& options);

const std::shared_ptr<grpc::Channel>& channel() const;
const std::shared_ptr<GrpcChannel>& channel() const;

private:
std::shared_ptr<grpc::Channel> channel_;
std::shared_ptr<GrpcChannel> channel_;
const char* path_;
bool closed_;
void* rust_runtime_;
Expand Down

0 comments on commit 5536ef8

Please sign in to comment.