Skip to content

Commit d8cfe1c

Browse files
authored
Merge branch 'main' into module-service-insulate
2 parents 04bace5 + 5536ef8 commit d8cfe1c

File tree

16 files changed

+183
-97
lines changed

16 files changed

+183
-97
lines changed

.github/workflows/docs.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ on:
44
workflow_dispatch:
55
push:
66
paths:
7-
- 'src/**'
8-
- 'README.md'
7+
- "src/**"
8+
- "README.md"
99
branches: [main]
1010

1111
jobs:
@@ -30,7 +30,7 @@ jobs:
3030
run: doxygen Doxyfile
3131

3232
- name: Upload artifacts
33-
uses: actions/upload-artifact@v3
33+
uses: actions/upload-artifact@v4
3434
with:
3535
name: html-docs
3636
path: etc/docs/api/current

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ if ((VIAMCPPSDK_GRPCXX_VERSION VERSION_GREATER 1.51.1) AND (VIAMCPPSDK_PROTOC_VE
430430
set(VIAMCPPSDK_BUF_REMOTE_PLUGIN_SUPPORTED 1)
431431
endif()
432432

433+
if (VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.32.0)
434+
set(VIAMCPPSDK_GRPCXX_LEGACY_CLIENT_FWD 1)
435+
endif()
436+
433437
include(FetchContent)
434438

435439
FetchContent_Declare(

src/viam/sdk/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ endif()
2929
message(WARNING "api proto label is ${VIAMCPPSDK_API_PROTO_LABEL}")
3030
configure_file(common/private/version_metadata.hpp.in common/private/version_metadata.hpp @ONLY)
3131

32+
# Configure the grpc client forward declarations file
33+
configure_file(common/grpc_client_fwd.hpp.in common/grpc_client_fwd.hpp)
3234

3335
# Set compile and link options based on arguments
3436
if (VIAMCPPSDK_USE_WALL_WERROR)
@@ -137,6 +139,7 @@ target_sources(viamsdk
137139
PUBLIC FILE_SET viamsdk_public_includes TYPE HEADERS
138140
BASE_DIRS
139141
../..
142+
${CMAKE_CURRENT_BINARY_DIR}/../..
140143
FILES
141144
../../viam/sdk/common/client_helper.hpp
142145
../../viam/sdk/common/exception.hpp
@@ -189,6 +192,7 @@ target_sources(viamsdk
189192
../../viam/sdk/spatialmath/geometry.hpp
190193
../../viam/sdk/spatialmath/orientation.hpp
191194
../../viam/sdk/spatialmath/orientation_types.hpp
195+
${CMAKE_CURRENT_BINARY_DIR}/../../viam/sdk/common/grpc_client_fwd.hpp
192196
)
193197

194198
set_target_properties(
@@ -207,7 +211,6 @@ target_include_directories(viamsdk
207211
"$<INSTALL_INTERFACE:${VIAMCPPSDK_GRPC_INCLUDE_DIRS}>"
208212
"$<BUILD_INTERFACE:${VIAMCPPSDK_PROTOBUF_INCLUDE_DIRS}>"
209213
"$<INSTALL_INTERFACE:${VIAMCPPSDK_PROTOBUF_INCLUDE_DIRS}>"
210-
PRIVATE
211214
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../..>"
212215
)
213216

src/viam/sdk/common/client_helper.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
#include <cstdlib>
44

5+
#include <grpcpp/client_context.h>
6+
57
#include <boost/log/trivial.hpp>
68

9+
#include <viam/sdk/common/private/version_metadata.hpp>
10+
711
namespace viam {
812
namespace sdk {
913

@@ -16,5 +20,37 @@ namespace client_helper_details {
1620
}
1721

1822
} // namespace client_helper_details
23+
24+
ClientContext::ClientContext() : wrapped_context_(std::make_unique<GrpcClientContext>()) {
25+
set_client_ctx_authority_();
26+
add_viam_client_version_();
27+
}
28+
29+
ClientContext::~ClientContext() = default;
30+
31+
ClientContext::operator const GrpcClientContext*() const {
32+
return wrapped_context_.get();
33+
}
34+
35+
ClientContext::operator GrpcClientContext*() {
36+
return wrapped_context_.get();
37+
}
38+
39+
void ClientContext::try_cancel() {
40+
wrapped_context_->TryCancel();
41+
}
42+
43+
void ClientContext::set_debug_key(const std::string& debug_key) {
44+
wrapped_context_->AddMetadata("dtname", debug_key);
45+
}
46+
47+
void ClientContext::set_client_ctx_authority_() {
48+
wrapped_context_->set_authority("viam-placeholder");
49+
}
50+
51+
void ClientContext::add_viam_client_version_() {
52+
wrapped_context_->AddMetadata("viam_client", impl::k_version);
53+
}
54+
1955
} // namespace sdk
2056
} // namespace viam

src/viam/sdk/common/client_helper.hpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#pragma once
22

3-
#include <grpcpp/client_context.h>
4-
#include <grpcpp/support/sync_stream.h>
5-
63
#include <viam/sdk/common/exception.hpp>
4+
#include <viam/sdk/common/grpc_client_fwd.hpp>
75
#include <viam/sdk/common/private/utils.hpp>
86
#include <viam/sdk/common/proto_value.hpp>
9-
#include <viam/sdk/common/utils.hpp>
107

118
namespace grpc {
129

@@ -23,16 +20,39 @@ namespace client_helper_details {
2320

2421
} // namespace client_helper_details
2522

23+
// the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing
24+
// `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a
25+
// grpc ClientContext that allows us to make any necessary modifications to authority or else where
26+
// to avoid runtime issues.
27+
// For more details, see https://viam.atlassian.net/browse/RSDK-5194.
28+
class ClientContext {
29+
public:
30+
ClientContext();
31+
~ClientContext();
32+
33+
void try_cancel();
34+
35+
operator GrpcClientContext*();
36+
operator const GrpcClientContext*() const;
37+
38+
void set_debug_key(const std::string& debug_key);
39+
40+
private:
41+
void set_client_ctx_authority_();
42+
void add_viam_client_version_();
43+
std::unique_ptr<GrpcClientContext> wrapped_context_;
44+
};
45+
2646
// Method type for a gRPC call that returns a response message type.
2747
template <typename StubType, typename RequestType, typename ResponseType>
28-
using SyncMethodType = ::grpc::Status (StubType::*)(::grpc::ClientContext*,
48+
using SyncMethodType = ::grpc::Status (StubType::*)(GrpcClientContext*,
2949
const RequestType&,
3050
ResponseType*);
3151

3252
// Method type for a gRPC call that returns a stream of response message type.
3353
template <typename StubType, typename RequestType, typename ResponseType>
34-
using StreamingMethodType = std::unique_ptr<::grpc::ClientReaderInterface<ResponseType>> (
35-
StubType::*)(::grpc::ClientContext*, const RequestType&);
54+
using StreamingMethodType = std::unique_ptr<GrpcClientReaderInterface<ResponseType>> (StubType::*)(
55+
GrpcClientContext*, const RequestType&);
3656

3757
template <typename ClientType,
3858
typename StubType,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
3+
#cmakedefine VIAMCPPSDK_GRPCXX_LEGACY_CLIENT_FWD
4+
5+
#ifndef VIAMCPPSDK_GRPCXX_LEGACY_CLIENT_FWD
6+
7+
// Forward declaration file for grpc ClientContext and ClientReaderInterface<T>.
8+
// This file provides includes for recent (>= 1.32.0) versions of grpc.
9+
10+
namespace grpc {
11+
12+
class Channel;
13+
14+
class ClientContext;
15+
16+
template <typename>
17+
class ClientReaderInterface;
18+
19+
} // namespace grpc
20+
21+
namespace viam {
22+
namespace sdk {
23+
24+
using GrpcChannel = ::grpc::Channel;
25+
26+
using GrpcClientContext = ::grpc::ClientContext;
27+
28+
template <typename T>
29+
using GrpcClientReaderInterface = ::grpc::ClientReaderInterface<T>;
30+
31+
} // namespace sdk
32+
} // namespace viam
33+
34+
#else
35+
36+
// Forward declaration file for grpc ClientContext and ClientReaderInterface<T>.
37+
// This file provides includes for the oldest supported versions of grpc (< 1.32.0).
38+
39+
namespace grpc_impl {
40+
41+
class Channel;
42+
43+
class ClientContext;
44+
45+
template <typename>
46+
class ClientReaderInterface;
47+
48+
} // namespace grpc_impl
49+
50+
namespace viam {
51+
namespace sdk {
52+
53+
using GrpcChannel = ::grpc_impl::Channel;
54+
55+
using GrpcClientContext = ::grpc_impl::ClientContext;
56+
57+
template <typename T>
58+
using GrpcClientReaderInterface = ::grpc_impl::ClientReaderInterface<T>;
59+
60+
} // namespace sdk
61+
} // namespace viam
62+
63+
#endif

src/viam/sdk/common/utils.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <viam/api/common/v1/common.pb.h>
1919

2020
#include <viam/sdk/common/private/utils.hpp>
21-
#include <viam/sdk/common/private/version_metadata.hpp>
2221
#include <viam/sdk/components/component.hpp>
2322
#include <viam/sdk/registry/registry.hpp>
2423

@@ -151,37 +150,6 @@ ProtoStruct with_debug_entry(ProtoStruct&& map) {
151150
return map;
152151
}
153152

154-
ClientContext::ClientContext() : wrapped_context_(std::make_unique<grpc::ClientContext>()) {
155-
set_client_ctx_authority_();
156-
add_viam_client_version_();
157-
}
158-
159-
ClientContext::~ClientContext() = default;
160-
161-
ClientContext::operator const grpc::ClientContext*() const {
162-
return wrapped_context_.get();
163-
}
164-
165-
ClientContext::operator grpc::ClientContext*() {
166-
return wrapped_context_.get();
167-
}
168-
169-
void ClientContext::try_cancel() {
170-
wrapped_context_->TryCancel();
171-
}
172-
173-
void ClientContext::set_debug_key(const std::string& debug_key) {
174-
wrapped_context_->AddMetadata("dtname", debug_key);
175-
}
176-
177-
void ClientContext::set_client_ctx_authority_() {
178-
wrapped_context_->set_authority("viam-placeholder");
179-
}
180-
181-
void ClientContext::add_viam_client_version_() {
182-
wrapped_context_->AddMetadata("viam_client", impl::k_version);
183-
}
184-
185153
bool from_dm_from_extra(const ProtoStruct& extra) {
186154
auto pos = extra.find("fromDataManagement");
187155
if (pos != extra.end()) {

src/viam/sdk/common/utils.hpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <memory>
44

55
#include <boost/optional/optional.hpp>
6-
#include <grpcpp/client_context.h>
76

87
#include <viam/sdk/common/proto_value.hpp>
98
#include <viam/sdk/components/component.hpp>
@@ -82,29 +81,6 @@ struct from_proto<common::v1::ResponseMetadata> {
8281
std::vector<unsigned char> string_to_bytes(std::string const& s);
8382
std::string bytes_to_string(std::vector<unsigned char> const& b);
8483

85-
// the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing
86-
// `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a
87-
// grpc ClientContext that allows us to make any necessary modifications to authority or else where
88-
// to avoid runtime issues.
89-
// For more details, see https://viam.atlassian.net/browse/RSDK-5194.
90-
class ClientContext {
91-
public:
92-
ClientContext();
93-
~ClientContext();
94-
95-
void try_cancel();
96-
97-
operator grpc::ClientContext*();
98-
operator const grpc::ClientContext*() const;
99-
100-
void set_debug_key(const std::string& debug_key);
101-
102-
private:
103-
void set_client_ctx_authority_();
104-
void add_viam_client_version_();
105-
std::unique_ptr<grpc::ClientContext> wrapped_context_;
106-
};
107-
10884
/// @brief Given a fully qualified resource name, returns remote name (or "" if no remote name
10985
/// exists) and short name
11086
std::pair<std::string, std::string> long_name_to_remote_and_short(const std::string& long_name);

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
};

0 commit comments

Comments
 (0)