Skip to content

Commit 5536ef8

Browse files
authored
refer to grpc::Channel by forward decl (#342)
1 parent 8f977f9 commit 5536ef8

File tree

6 files changed

+51
-29
lines changed

6 files changed

+51
-29
lines changed

src/viam/sdk/common/grpc_client_fwd.hpp.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace grpc {
1111

12+
class Channel;
13+
1214
class ClientContext;
1315

1416
template <typename>
@@ -19,6 +21,8 @@ class ClientReaderInterface;
1921
namespace viam {
2022
namespace sdk {
2123

24+
using GrpcChannel = ::grpc::Channel;
25+
2226
using GrpcClientContext = ::grpc::ClientContext;
2327

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

3539
namespace grpc_impl {
3640

41+
class Channel;
42+
3743
class ClientContext;
3844

3945
template <typename>
@@ -44,6 +50,8 @@ class ClientReaderInterface;
4450
namespace viam {
4551
namespace sdk {
4652

53+
using GrpcChannel = ::grpc_impl::Channel;
54+
4755
using GrpcClientContext = ::grpc_impl::ClientContext;
4856

4957
template <typename T>

src/viam/sdk/module/module.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <viam/sdk/common/grpc_client_fwd.hpp>
34
#include <viam/sdk/module/handler_map.hpp>
45
#include <viam/sdk/resource/resource.hpp>
56
#include <viam/sdk/resource/resource_manager.hpp>
@@ -18,14 +19,14 @@ class Module {
1819
bool ready() const;
1920
const HandlerMap_& handles() const;
2021
HandlerMap_& mutable_handles();
21-
const std::shared_ptr<grpc::Channel>& channel() const;
22+
const std::shared_ptr<GrpcChannel>& channel() const;
2223

2324
private:
2425
std::string name_;
2526
std::string addr_;
2627
bool ready_;
2728
HandlerMap_ handles_;
28-
std::shared_ptr<grpc::Channel> channel_;
29+
std::shared_ptr<GrpcChannel> channel_;
2930
};
3031

3132
} // namespace sdk

src/viam/sdk/registry/registry.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,32 @@
5555
namespace viam {
5656
namespace sdk {
5757

58+
ResourceServerRegistration::ResourceServerRegistration(
59+
const google::protobuf::ServiceDescriptor* service_descriptor)
60+
: service_descriptor_(service_descriptor) {}
61+
5862
ResourceServerRegistration::~ResourceServerRegistration() = default;
5963
ResourceClientRegistration::~ResourceClientRegistration() = default;
6064

65+
ModelRegistration::ModelRegistration(
66+
API api,
67+
Model model,
68+
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor)
69+
: construct_resource(std::move(constructor)),
70+
validate(default_validator),
71+
model_(std::move(model)),
72+
api_(std::move(api)) {}
73+
74+
ModelRegistration::ModelRegistration(
75+
API api,
76+
Model model,
77+
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor,
78+
std::function<std::vector<std::string>(ResourceConfig)> validator)
79+
: construct_resource(std::move(constructor)),
80+
validate(std::move(validator)),
81+
model_(std::move(model)),
82+
api_(std::move(api)) {}
83+
6184
const API& ModelRegistration::api() const {
6285
return api_;
6386
};

src/viam/sdk/registry/registry.hpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
#include <google/protobuf/descriptor.h>
99
#include <google/protobuf/message.h>
10-
#include <grpcpp/channel.h>
1110
#include <grpcpp/impl/service_type.h>
1211
#include <grpcpp/server.h>
1312

13+
#include <viam/sdk/common/grpc_client_fwd.hpp>
1414
#include <viam/sdk/config/resource.hpp>
1515
#include <viam/sdk/resource/resource.hpp>
1616
#include <viam/sdk/resource/resource_api.hpp>
@@ -24,6 +24,8 @@ namespace sdk {
2424
// TODO(RSDK-6617): one class per header
2525
class ResourceServerRegistration {
2626
public:
27+
ResourceServerRegistration(const google::protobuf::ServiceDescriptor* service_descriptor);
28+
2729
virtual ~ResourceServerRegistration();
2830

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

39-
ResourceServerRegistration(const google::protobuf::ServiceDescriptor* service_descriptor)
40-
: service_descriptor_(service_descriptor){};
41-
4241
private:
4342
const google::protobuf::ServiceDescriptor* service_descriptor_;
4443
};
@@ -47,16 +46,16 @@ class ResourceServerRegistration {
4746
/// @brief Defines registered `Resource` client creation functionality.
4847
class ResourceClientRegistration {
4948
public:
49+
ResourceClientRegistration() = default;
50+
5051
virtual ~ResourceClientRegistration();
5152

5253
/// @brief Create gRPC client to a resource.
5354
/// @param name The name of the resource.
5455
/// @param channel A channel connected to the client.
5556
/// @return A `shared_ptr` to the resource client.
5657
virtual std::shared_ptr<Resource> create_rpc_client(
57-
std::string name, std::shared_ptr<grpc::Channel> channel) const = 0;
58-
59-
ResourceClientRegistration() = default;
58+
std::string name, std::shared_ptr<GrpcChannel> channel) const = 0;
6059
};
6160

6261
// TODO(RSDK-6616): instead of std::functions, consider making these functions
@@ -68,21 +67,13 @@ class ModelRegistration {
6867
ModelRegistration(
6968
API api,
7069
Model model,
71-
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor)
72-
: construct_resource(std::move(constructor)),
73-
validate(default_validator),
74-
model_(std::move(model)),
75-
api_(std::move(api)){};
70+
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor);
7671

7772
ModelRegistration(
7873
API api,
7974
Model model,
8075
std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor,
81-
std::function<std::vector<std::string>(ResourceConfig)> validator)
82-
: construct_resource(std::move(constructor)),
83-
validate(std::move(validator)),
84-
model_(std::move(model)),
85-
api_(std::move(api)){};
76+
std::function<std::vector<std::string>(ResourceConfig)> validator);
8677

8778
const API& api() const;
8879
const Model& model() const;
@@ -134,7 +125,7 @@ class Registry {
134125
using ResourceClientRegistration::ResourceClientRegistration;
135126

136127
std::shared_ptr<Resource> create_rpc_client(
137-
std::string name, std::shared_ptr<grpc::Channel> chan) const override {
128+
std::string name, std::shared_ptr<GrpcChannel> chan) const override {
138129
return std::make_shared<ResourceClientT>(std::move(name), std::move(chan));
139130
}
140131
};

src/viam/sdk/robot/client.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#include <string>
77
#include <thread>
88

9-
#include <grpcpp/channel.h>
10-
9+
#include <viam/sdk/common/grpc_client_fwd.hpp>
1110
#include <viam/sdk/common/pose.hpp>
1211
#include <viam/sdk/common/utils.hpp>
1312
#include <viam/sdk/common/world_state.hpp>
@@ -20,8 +19,6 @@
2019
namespace viam {
2120
namespace sdk {
2221

23-
using grpc::Channel;
24-
2522
/// @defgroup Robot Classes related to a Robot representation.
2623

2724
/// @class RobotClient client.hpp "robot/client.hpp"
@@ -152,7 +149,7 @@ class RobotClient {
152149
std::vector<std::shared_ptr<std::thread>> threads_;
153150
std::atomic<bool> should_refresh_;
154151
unsigned int refresh_interval_;
155-
std::shared_ptr<Channel> channel_;
152+
std::shared_ptr<GrpcChannel> channel_;
156153
std::shared_ptr<ViamChannel> viam_channel_;
157154
bool should_close_channel_;
158155
struct impl;

src/viam/sdk/rpc/dial.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#pragma once
22

3+
#include <chrono>
34
#include <memory>
45
#include <string>
56

67
#include <boost/optional.hpp>
7-
#include <grpcpp/channel.h>
8+
9+
#include <viam/sdk/common/grpc_client_fwd.hpp>
810

911
namespace viam {
1012
namespace sdk {
@@ -13,14 +15,14 @@ class DialOptions;
1315
class ViamChannel {
1416
public:
1517
void close();
16-
ViamChannel(std::shared_ptr<grpc::Channel> channel, const char* path, void* runtime);
18+
ViamChannel(std::shared_ptr<GrpcChannel> channel, const char* path, void* runtime);
1719
static std::shared_ptr<ViamChannel> dial(const char* uri,
1820
const boost::optional<DialOptions>& options);
1921

20-
const std::shared_ptr<grpc::Channel>& channel() const;
22+
const std::shared_ptr<GrpcChannel>& channel() const;
2123

2224
private:
23-
std::shared_ptr<grpc::Channel> channel_;
25+
std::shared_ptr<GrpcChannel> channel_;
2426
const char* path_;
2527
bool closed_;
2628
void* rust_runtime_;

0 commit comments

Comments
 (0)