Skip to content

Commit 27c0697

Browse files
authored
RSDK-9449 Miscellaneous further proto cleanups (#339)
1 parent f845acf commit 27c0697

21 files changed

+370
-217
lines changed

src/viam/sdk/common/client_helper.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@
88
#include <viam/sdk/common/proto_value.hpp>
99
#include <viam/sdk/common/utils.hpp>
1010

11+
namespace grpc {
12+
13+
class Status;
14+
15+
} // namespace grpc
16+
1117
namespace viam {
1218
namespace sdk {
1319

1420
namespace client_helper_details {
21+
1522
[[noreturn]] void errorHandlerReturnedUnexpectedly(const ::grpc::Status&) noexcept;
23+
1624
} // namespace client_helper_details
1725

1826
// Method type for a gRPC call that returns a response message type.
@@ -59,7 +67,8 @@ class ClientHelper {
5967
ProtoValue value = key->second;
6068
debug_key_ = *value.get<std::string>();
6169
}
62-
*request_.mutable_extra() = v2::to_proto(extra);
70+
71+
proto_convert_details::to_proto<ProtoStruct>{}(extra, request_.mutable_extra());
6372
return with(std::forward<RequestSetupCallable>(rsc));
6473
}
6574

@@ -102,7 +111,7 @@ class ClientHelper {
102111
while (reader->Read(&response_)) {
103112
if (!rhc(response_)) {
104113
cancelled_by_handler = true;
105-
static_cast<::grpc::ClientContext*>(ctx)->TryCancel();
114+
ctx.try_cancel();
106115
break;
107116
}
108117
}

src/viam/sdk/common/service_helper.hpp

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

33
#include <type_traits>
44

5+
#include <grpcpp/support/status.h>
6+
57
#include <viam/sdk/resource/resource_server_base.hpp>
68

79
namespace viam {

src/viam/sdk/common/utils.cpp

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
#include <unordered_map>
55
#include <vector>
66

7+
#include <google/protobuf/duration.pb.h>
8+
#include <google/protobuf/timestamp.pb.h>
9+
#include <grpcpp/client_context.h>
10+
711
#include <boost/algorithm/string.hpp>
812
#include <boost/blank.hpp>
913
#include <boost/log/core.hpp>
1014
#include <boost/log/expressions.hpp>
1115
#include <boost/log/trivial.hpp>
1216
#include <boost/optional/optional.hpp>
13-
#include <grpcpp/client_context.h>
1417

1518
#include <viam/api/common/v1/common.pb.h>
1619

@@ -22,52 +25,44 @@
2225
namespace viam {
2326
namespace sdk {
2427

25-
std::vector<unsigned char> string_to_bytes(const std::string& s) {
26-
std::vector<unsigned char> bytes(s.begin(), s.end());
27-
return bytes;
28-
};
29-
30-
std::string bytes_to_string(const std::vector<unsigned char>& b) {
31-
std::string img_string(b.begin(), b.end());
32-
return img_string;
33-
};
34-
35-
time_pt timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp) {
36-
return time_pt{std::chrono::seconds{timestamp.seconds()} +
37-
std::chrono::nanoseconds{timestamp.nanos()}};
28+
bool operator==(const response_metadata& lhs, const response_metadata& rhs) {
29+
return lhs.captured_at == rhs.captured_at;
3830
}
3931

40-
google::protobuf::Timestamp time_pt_to_timestamp(time_pt tp) {
32+
namespace proto_convert_details {
33+
34+
void to_proto<time_pt>::operator()(time_pt tp, google::protobuf::Timestamp* result) const {
4135
const std::chrono::nanoseconds since_epoch = tp.time_since_epoch();
4236

4337
const auto sec_floor = std::chrono::duration_cast<std::chrono::seconds>(since_epoch);
4438
const std::chrono::nanoseconds nano_part = since_epoch - sec_floor;
4539

46-
google::protobuf::Timestamp result;
47-
48-
result.set_seconds(sec_floor.count());
49-
result.set_nanos(static_cast<int32_t>(nano_part.count()));
50-
51-
return result;
40+
result->set_seconds(sec_floor.count());
41+
result->set_nanos(static_cast<int32_t>(nano_part.count()));
5242
}
5343

54-
response_metadata response_metadata::from_proto(const viam::common::v1::ResponseMetadata& proto) {
55-
response_metadata metadata;
56-
metadata.captured_at = timestamp_to_time_pt(proto.captured_at());
57-
return metadata;
44+
time_pt from_proto<google::protobuf::Timestamp>::operator()(
45+
const google::protobuf::Timestamp* timestamp) const {
46+
return time_pt{std::chrono::seconds{timestamp->seconds()} +
47+
std::chrono::nanoseconds{timestamp->nanos()}};
5848
}
5949

60-
viam::common::v1::ResponseMetadata response_metadata::to_proto(const response_metadata& metadata) {
61-
viam::common::v1::ResponseMetadata proto;
62-
google::protobuf::Timestamp ts = time_pt_to_timestamp(metadata.captured_at);
63-
*proto.mutable_captured_at() = std::move(ts);
64-
return proto;
50+
void to_proto<std::chrono::microseconds>::operator()(std::chrono::microseconds duration,
51+
google::protobuf::Duration* proto) const {
52+
namespace sc = std::chrono;
53+
54+
const sc::seconds seconds = sc::duration_cast<sc::seconds>(duration);
55+
const sc::nanoseconds nanos = duration - seconds;
56+
57+
proto->set_nanos(static_cast<int32_t>(nanos.count()));
58+
proto->set_seconds(seconds.count());
6559
}
6660

67-
std::chrono::microseconds from_proto(const google::protobuf::Duration& proto) {
61+
std::chrono::microseconds from_proto<google::protobuf::Duration>::operator()(
62+
const google::protobuf::Duration* proto) const {
6863
namespace sc = std::chrono;
69-
const sc::seconds seconds_part{proto.seconds()};
70-
const sc::nanoseconds nanos_part{proto.nanos()};
64+
const sc::seconds seconds_part{proto->seconds()};
65+
const sc::nanoseconds nanos_part{proto->nanos()};
7166

7267
const sc::microseconds from_seconds = sc::duration_cast<sc::microseconds>(seconds_part);
7368
sc::microseconds from_nanos = sc::duration_cast<sc::microseconds>(nanos_part);
@@ -80,16 +75,26 @@ std::chrono::microseconds from_proto(const google::protobuf::Duration& proto) {
8075
return from_seconds + from_nanos;
8176
}
8277

83-
google::protobuf::Duration to_proto(std::chrono::microseconds duration) {
84-
namespace sc = std::chrono;
78+
void to_proto<response_metadata>::operator()(const response_metadata& self,
79+
common::v1::ResponseMetadata* proto) const {
80+
*(proto->mutable_captured_at()) = v2::to_proto(self.captured_at);
81+
}
8582

86-
const sc::seconds seconds = sc::duration_cast<sc::seconds>(duration);
87-
const sc::nanoseconds nanos = duration - seconds;
83+
response_metadata from_proto<common::v1::ResponseMetadata>::operator()(
84+
const common::v1::ResponseMetadata* proto) const {
85+
return {v2::from_proto(proto->captured_at())};
86+
}
87+
88+
} // namespace proto_convert_details
89+
90+
std::vector<unsigned char> string_to_bytes(const std::string& s) {
91+
std::vector<unsigned char> bytes(s.begin(), s.end());
92+
return bytes;
93+
}
8894

89-
google::protobuf::Duration proto;
90-
proto.set_nanos(static_cast<int32_t>(nanos.count()));
91-
proto.set_seconds(seconds.count());
92-
return proto;
95+
std::string bytes_to_string(const std::vector<unsigned char>& b) {
96+
std::string img_string(b.begin(), b.end());
97+
return img_string;
9398
}
9499

95100
void set_logger_severity_from_args(int argc, char** argv) {
@@ -101,14 +106,6 @@ void set_logger_severity_from_args(int argc, char** argv) {
101106
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info);
102107
}
103108

104-
bool operator==(const response_metadata& lhs, const response_metadata& rhs) {
105-
return lhs.captured_at == rhs.captured_at;
106-
}
107-
108-
void ClientContext::set_client_ctx_authority_() {
109-
wrapped_context_.set_authority("viam-placeholder");
110-
}
111-
112109
std::string random_debug_key() {
113110
static const char alphanum[] = "abcdefghijklmnopqrstuvwxyz";
114111
static std::default_random_engine generator(
@@ -154,25 +151,35 @@ ProtoStruct with_debug_entry(ProtoStruct&& map) {
154151
return map;
155152
}
156153

157-
void ClientContext::set_debug_key(const std::string& debug_key) {
158-
wrapped_context_.AddMetadata("dtname", debug_key);
159-
}
160-
161-
void ClientContext::add_viam_client_version_() {
162-
wrapped_context_.AddMetadata("viam_client", impl::k_version);
163-
}
164-
165-
ClientContext::ClientContext() {
154+
ClientContext::ClientContext() : wrapped_context_(std::make_unique<grpc::ClientContext>()) {
166155
set_client_ctx_authority_();
167156
add_viam_client_version_();
168157
}
169158

159+
ClientContext::~ClientContext() = default;
160+
170161
ClientContext::operator const grpc::ClientContext*() const {
171-
return &wrapped_context_;
162+
return wrapped_context_.get();
172163
}
173164

174165
ClientContext::operator grpc::ClientContext*() {
175-
return &wrapped_context_;
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);
176183
}
177184

178185
bool from_dm_from_extra(const ProtoStruct& extra) {

src/viam/sdk/common/utils.hpp

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
#pragma once
22

3+
#include <memory>
4+
35
#include <boost/optional/optional.hpp>
46
#include <grpcpp/client_context.h>
57

6-
#include <viam/api/common/v1/common.pb.h>
7-
88
#include <viam/sdk/common/proto_value.hpp>
99
#include <viam/sdk/components/component.hpp>
1010
#include <viam/sdk/resource/resource_api.hpp>
1111

12+
namespace google {
13+
namespace protobuf {
14+
15+
class Duration;
16+
class Timestamp;
17+
18+
} // namespace protobuf
19+
} // namespace google
20+
21+
namespace viam {
22+
namespace common {
23+
namespace v1 {
24+
25+
class ResponseMetadata;
26+
27+
}
28+
} // namespace common
29+
} // namespace viam
30+
1231
namespace viam {
1332
namespace sdk {
1433

@@ -22,25 +41,47 @@ using time_pt = std::chrono::time_point<std::chrono::system_clock, std::chrono::
2241

2342
struct response_metadata {
2443
time_pt captured_at;
25-
26-
static response_metadata from_proto(const viam::common::v1::ResponseMetadata& proto);
27-
static viam::common::v1::ResponseMetadata to_proto(const response_metadata& metadata);
2844
};
2945

3046
bool operator==(const response_metadata& lhs, const response_metadata& rhs);
3147

32-
/// @brief convert a google::protobuf::Timestamp to time_pt
33-
time_pt timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp);
48+
namespace proto_convert_details {
3449

35-
/// @brief convert a time_pt to a google::protobuf::Timestamp.
36-
google::protobuf::Timestamp time_pt_to_timestamp(time_pt);
50+
template <>
51+
struct to_proto<time_pt> {
52+
void operator()(time_pt, google::protobuf::Timestamp*) const;
53+
};
54+
55+
template <>
56+
struct from_proto<google::protobuf::Timestamp> {
57+
time_pt operator()(const google::protobuf::Timestamp*) const;
58+
};
59+
60+
template <>
61+
struct to_proto<std::chrono::microseconds> {
62+
void operator()(std::chrono::microseconds, google::protobuf::Duration*) const;
63+
};
64+
65+
template <>
66+
struct from_proto<google::protobuf::Duration> {
67+
std::chrono::microseconds operator()(const google::protobuf::Duration*) const;
68+
};
69+
70+
template <>
71+
struct to_proto<response_metadata> {
72+
void operator()(const response_metadata&, common::v1::ResponseMetadata*) const;
73+
};
74+
75+
template <>
76+
struct from_proto<common::v1::ResponseMetadata> {
77+
response_metadata operator()(const common::v1::ResponseMetadata*) const;
78+
};
79+
80+
} // namespace proto_convert_details
3781

3882
std::vector<unsigned char> string_to_bytes(std::string const& s);
3983
std::string bytes_to_string(std::vector<unsigned char> const& b);
4084

41-
std::chrono::microseconds from_proto(const google::protobuf::Duration& proto);
42-
google::protobuf::Duration to_proto(std::chrono::microseconds duration);
43-
4485
// the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing
4586
// `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a
4687
// grpc ClientContext that allows us to make any necessary modifications to authority or else where
@@ -49,14 +90,19 @@ google::protobuf::Duration to_proto(std::chrono::microseconds duration);
4990
class ClientContext {
5091
public:
5192
ClientContext();
93+
~ClientContext();
94+
95+
void try_cancel();
96+
5297
operator grpc::ClientContext*();
5398
operator const grpc::ClientContext*() const;
99+
54100
void set_debug_key(const std::string& debug_key);
55101

56102
private:
57103
void set_client_ctx_authority_();
58104
void add_viam_client_version_();
59-
grpc::ClientContext wrapped_context_;
105+
std::unique_ptr<grpc::ClientContext> wrapped_context_;
60106
};
61107

62108
/// @brief Given a fully qualified resource name, returns remote name (or "" if no remote name

src/viam/sdk/components/private/board_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void BoardClient::set_power_mode(power_mode power_mode,
182182
[&](auto& request) {
183183
request.set_power_mode(to_proto(power_mode));
184184
if (duration.has_value()) {
185-
*request.mutable_duration() = ::viam::sdk::to_proto(duration.get());
185+
*request.mutable_duration() = v2::to_proto(duration.get());
186186
}
187187
})
188188
.invoke();

src/viam/sdk/components/private/board_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ ::grpc::Status BoardServer::SetPowerMode(
226226
return make_service_helper<Board>(
227227
"BoardServer::SetPowerMode", this, request)([&](auto& helper, auto& board) {
228228
if (request->has_duration()) {
229-
auto duration = ::viam::sdk::from_proto(request->duration());
229+
auto duration = v2::from_proto(request->duration());
230230
board->set_power_mode(from_proto(request->power_mode()), helper.getExtra(), duration);
231231
} else {
232232
board->set_power_mode(from_proto(request->power_mode()), helper.getExtra());

src/viam/sdk/components/private/camera_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Camera::image_collection from_proto(const viam::component::camera::v1::GetImages
5656
images.push_back(raw_image);
5757
}
5858
image_collection.images = std::move(images);
59-
image_collection.metadata = response_metadata::from_proto(proto.response_metadata());
59+
image_collection.metadata = v2::from_proto(proto.response_metadata());
6060
return image_collection;
6161
}
6262

src/viam/sdk/components/private/camera_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ ::grpc::Status CameraServer::GetImages(
9494
proto_image.set_image(img_string);
9595
*response->mutable_images()->Add() = std::move(proto_image);
9696
}
97-
*response->mutable_response_metadata() = response_metadata::to_proto(image_coll.metadata);
97+
*response->mutable_response_metadata() = v2::to_proto(image_coll.metadata);
9898
});
9999
}
100100

0 commit comments

Comments
 (0)