Skip to content

Commit 772efcb

Browse files
committed
client context out of utils
1 parent 7d26e94 commit 772efcb

File tree

6 files changed

+61
-58
lines changed

6 files changed

+61
-58
lines changed

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<grpc::ClientContext>()) {
25+
set_client_ctx_authority_();
26+
add_viam_client_version_();
27+
}
28+
29+
ClientContext::~ClientContext() = default;
30+
31+
ClientContext::operator const grpc::ClientContext*() const {
32+
return wrapped_context_.get();
33+
}
34+
35+
ClientContext::operator grpc::ClientContext*() {
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: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <viam/sdk/common/grpc_client_fwd.hpp>
55
#include <viam/sdk/common/private/utils.hpp>
66
#include <viam/sdk/common/proto_value.hpp>
7-
#include <viam/sdk/common/utils.hpp>
87

98
namespace grpc {
109

@@ -21,6 +20,29 @@ namespace client_helper_details {
2120

2221
} // namespace client_helper_details
2322

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 grpc::ClientContext*();
36+
operator const grpc::ClientContext*() 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<grpc::ClientContext> wrapped_context_;
44+
};
45+
2446
// Method type for a gRPC call that returns a response message type.
2547
template <typename StubType, typename RequestType, typename ResponseType>
2648
using SyncMethodType = ::grpc::Status (StubType::*)(::grpc::ClientContext*,

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
@@ -4,7 +4,6 @@
44

55
#include <boost/optional/optional.hpp>
66

7-
#include <viam/sdk/common/grpc_client_fwd.hpp>
87
#include <viam/sdk/common/proto_value.hpp>
98
#include <viam/sdk/components/component.hpp>
109
#include <viam/sdk/resource/resource_api.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/robot/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <viam/api/robot/v1/robot.grpc.pb.h>
2020
#include <viam/api/robot/v1/robot.pb.h>
2121

22+
#include <viam/sdk/common/client_helper.hpp>
2223
#include <viam/sdk/common/private/repeated_ptr_convert.hpp>
2324
#include <viam/sdk/common/proto_value.hpp>
2425
#include <viam/sdk/common/utils.hpp>

src/viam/sdk/services/private/mlmodel_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include <grpcpp/channel.h>
1818

19-
#include <viam/sdk/common/utils.hpp>
19+
#include <viam/sdk/common/client_helper.hpp>
2020
#include <viam/sdk/services/private/mlmodel.hpp>
2121

2222
#include <viam/sdk/common/exception.hpp>

0 commit comments

Comments
 (0)