Skip to content

Commit 518a661

Browse files
committed
changed RpcClient signature to use method UUri only when calling
invokeMethod
1 parent 94ec934 commit 518a661

7 files changed

Lines changed: 279 additions & 256 deletions

File tree

include/up-cpp/communication/RpcClient.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ struct RpcClient {
4848
/// For guidance on the permeission_level and token parameters, see:
4949
/// https://github.com/eclipse-uprotocol/up-spec/blob/main/basics/permissions.adoc
5050
explicit RpcClient(std::shared_ptr<transport::UTransport> transport,
51-
v1::UUri&& method, v1::UPriority priority,
52-
std::chrono::milliseconds ttl,
51+
v1::UPriority priority, std::chrono::milliseconds ttl,
5352
std::optional<v1::UPayloadFormat> payload_format = {},
5453
std::optional<uint32_t> permission_level = {},
5554
std::optional<std::string> token = {});
@@ -100,6 +99,7 @@ struct RpcClient {
10099

101100
/// @brief Invokes an RPC method by sending a request message.
102101
///
102+
/// @param The method that will be invoked
103103
/// @param A Payload builder containing the payload to be sent with the
104104
/// request.
105105
/// @param A callback that will be called with the result.
@@ -111,11 +111,13 @@ struct RpcClient {
111111
/// * A UStatus based on the commstatus received in the response
112112
/// message (if not OK).
113113
/// * A UMessage containing the response from the RPC target.
114-
[[nodiscard]] InvokeHandle invokeMethod(datamodel::builder::Payload&&,
114+
[[nodiscard]] InvokeHandle invokeMethod(const v1::UUri&,
115+
datamodel::builder::Payload&&,
115116
Callback&&);
116117

117118
/// @brief Invokes an RPC method by sending a request message.
118119
///
120+
/// @param The method that will be invoked
119121
/// @param A Payload builder containing the payload to be sent with the
120122
/// request.
121123
///
@@ -128,13 +130,15 @@ struct RpcClient {
128130
/// * A UStatus based on the commstatus received in the response
129131
/// message (if not OK).
130132
/// * A UMessage containing the response from the RPC target.
131-
[[nodiscard]] InvokeFuture invokeMethod(datamodel::builder::Payload&&);
133+
[[nodiscard]] InvokeFuture invokeMethod(const v1::UUri&,
134+
datamodel::builder::Payload&&);
132135

133136
/// @brief Invokes an RPC method by sending a request message.
134137
///
135138
/// Request is sent with an empty payload. Can only be called if no payload
136139
/// format was provided at construction time.
137140
///
141+
/// @param The method that will be invoked
138142
/// @param A callback that will be called with the result.
139143
///
140144
/// @post The provided callback will be called with one of:
@@ -144,13 +148,14 @@ struct RpcClient {
144148
/// * A UStatus based on the commstatus received in the response
145149
/// message (if not OK).
146150
/// * A UMessage containing the response from the RPC target.
147-
[[nodiscard]] InvokeHandle invokeMethod(Callback&&);
151+
[[nodiscard]] InvokeHandle invokeMethod(const v1::UUri&, Callback&&);
148152

149153
/// @brief Invokes an RPC method by sending a request message.
150154
///
151155
/// Request is sent with an empty payload. Can only be called if no payload
152156
/// format was provided at construction time.
153157
///
158+
/// @param The method that will be invoked
154159
/// @remarks This is a wrapper around the callback form of invokeMethod.
155160
///
156161
/// @returns A promised future that can resolve to one of:
@@ -160,7 +165,7 @@ struct RpcClient {
160165
/// * A UStatus based on the commstatus received in the response
161166
/// message (if not OK).
162167
/// * A UMessage containing the response from the RPC target.
163-
[[nodiscard]] InvokeFuture invokeMethod();
168+
[[nodiscard]] InvokeFuture invokeMethod(const v1::UUri&);
164169

165170
/// @brief Default move constructor (defined in RpcClient.cpp)
166171
RpcClient(RpcClient&&) noexcept;

include/up-cpp/datamodel/builder/UMessage.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
#include <string>
2424
#include <vector>
2525

26+
#include "up-cpp/communication/NotificationSink.h"
2627
#include "up-cpp/datamodel/builder/Uuid.h"
2728

29+
namespace uprotocol::communication {
30+
struct RpcClient;
31+
}
32+
2833
namespace uprotocol::datamodel::builder {
2934

3035
/// @brief Interface for composing UMessage objects
@@ -38,6 +43,7 @@ namespace uprotocol::datamodel::builder {
3843
/// instance can be held and reused by calling .build(payload)
3944
/// for each new set of message data.
4045
struct UMessageBuilder {
46+
friend struct communication::RpcClient;
4147
/// @brief Pre-populates a message builder with the attributes of a
4248
/// "publish" type message.
4349
///
@@ -103,6 +109,17 @@ struct UMessageBuilder {
103109
/// @returns UMessageBuilder configured to build a "response" message
104110
static UMessageBuilder response(const v1::UMessage& request);
105111

112+
/// @brief Set the method attribute for built messages.
113+
///
114+
/// @param The method to use when building messages.
115+
///
116+
/// @throws std::out_of_range when setting a priority lower than CS4
117+
/// for "request" or "response" messages.
118+
/// @throws std::out_of_range if the value is outside of the range of
119+
/// v1::UPriority
120+
/// @returns A reference to this UMessageBuilder
121+
UMessageBuilder& withMethod(const v1::UUri&);
122+
106123
/// @brief Set the message priority attribute for built messages.
107124
///
108125
/// If not called, the default value as specified in

src/client/usubscription/v3/Consumer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ v1::UStatus Consumer::subscribe(
9191
v1::UPriority priority, std::chrono::milliseconds subscription_request_ttl,
9292
ListenCallback&& callback) {
9393
rpc_client_ = std::make_unique<communication::RpcClient>(
94-
transport_, uSubscriptionUUriBuilder_.getServiceUriWithResourceId(1),
95-
priority, subscription_request_ttl);
94+
transport_, priority, subscription_request_ttl);
9695

9796
auto on_response = [this](const auto& maybe_response) {
9897
if (maybe_response.has_value() &&
@@ -110,8 +109,9 @@ v1::UStatus Consumer::subscribe(
110109
SubscriptionRequest const subscription_request = buildSubscriptionRequest();
111110
auto payload = datamodel::builder::Payload(subscription_request);
112111

113-
rpc_handle_ =
114-
rpc_client_->invokeMethod(std::move(payload), std::move(on_response));
112+
rpc_handle_ = rpc_client_->invokeMethod(
113+
uSubscriptionUUriBuilder_.getServiceUriWithResourceId(1),
114+
std::move(payload), std::move(on_response));
115115

116116
// Create a L2 subscription
117117
auto result = communication::Subscriber::subscribe(
@@ -135,8 +135,7 @@ UnsubscribeRequest Consumer::buildUnsubscriptionRequest() {
135135
void Consumer::unsubscribe(v1::UPriority priority,
136136
std::chrono::milliseconds request_ttl) {
137137
rpc_client_ = std::make_unique<communication::RpcClient>(
138-
transport_, uSubscriptionUUriBuilder_.getServiceUriWithResourceId(2),
139-
priority, request_ttl);
138+
transport_, priority, request_ttl);
140139

141140
auto on_response = [](const auto& maybe_response) {
142141
if (!maybe_response.has_value()) {
@@ -147,8 +146,9 @@ void Consumer::unsubscribe(v1::UPriority priority,
147146
UnsubscribeRequest const unsubscribe_request = buildUnsubscriptionRequest();
148147
auto payload = datamodel::builder::Payload(unsubscribe_request);
149148

150-
rpc_handle_ =
151-
rpc_client_->invokeMethod(std::move(payload), std::move(on_response));
149+
rpc_handle_ = rpc_client_->invokeMethod(
150+
uSubscriptionUUriBuilder_.getServiceUriWithResourceId(2),
151+
std::move(payload), std::move(on_response));
152152

153153
subscriber_.reset();
154154
}

src/communication/RpcClient.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,19 @@ struct RpcClient::ExpireService {
9999

100100
////////////////////////////////////////////////////////////////////////////////
101101
RpcClient::RpcClient(std::shared_ptr<transport::UTransport> transport,
102-
v1::UUri&& method, v1::UPriority priority,
103-
std::chrono::milliseconds ttl,
102+
v1::UPriority priority, std::chrono::milliseconds ttl,
104103
std::optional<v1::UPayloadFormat> payload_format,
105104
std::optional<uint32_t> permission_level,
106105
std::optional<std::string> token)
107106
: transport_(std::move(transport)),
108107
ttl_(ttl),
109-
builder_(datamodel::builder::UMessageBuilder::request(
110-
std::move(method), v1::UUri(transport_->getEntityUri()), priority,
111-
ttl_)),
108+
builder_(datamodel::builder::UMessageBuilder(
109+
v1::UMESSAGE_TYPE_REQUEST, v1::UUri(transport_->getEntityUri()),
110+
v1::UUri{})),
112111
expire_service_(std::make_unique<ExpireService>()) {
112+
builder_.withPriority(priority);
113+
builder_.withTtl(ttl);
114+
113115
if (payload_format) {
114116
builder_.withPayloadFormat(*payload_format);
115117
}
@@ -204,41 +206,44 @@ RpcClient::InvokeHandle RpcClient::invokeMethod(v1::UMessage&& request,
204206
}
205207

206208
RpcClient::InvokeHandle RpcClient::invokeMethod(
207-
datamodel::builder::Payload&& payload, Callback&& callback) {
208-
return invokeMethod(builder_.build(std::move(payload)),
209+
const v1::UUri& method, datamodel::builder::Payload&& payload,
210+
Callback&& callback) {
211+
return invokeMethod(builder_.withMethod(method).build(std::move(payload)),
209212
std::move(callback));
210213
}
211214

212-
RpcClient::InvokeHandle RpcClient::invokeMethod(Callback&& callback) {
213-
return invokeMethod(builder_.build(), std::move(callback));
215+
RpcClient::InvokeHandle RpcClient::invokeMethod(const v1::UUri& method,
216+
Callback&& callback) {
217+
return invokeMethod(builder_.withMethod(method).build(),
218+
std::move(callback));
214219
}
215220

216221
RpcClient::InvokeFuture RpcClient::invokeMethod(
217-
datamodel::builder::Payload&& payload) {
222+
const v1::UUri& method, datamodel::builder::Payload&& payload) {
218223
// Note: functors need to be copy constructable. We work around this by
219224
// wrapping the promise in a shared_ptr. Unique access to it will be
220225
// assured by the implementation at the core of invokeMethod - it only
221226
// allows exactly one call to the callback via std::call_once.
222227
auto promise = std::make_shared<std::promise<MessageOrStatus>>();
223228
auto future = promise->get_future();
224229
auto handle =
225-
invokeMethod(std::move(payload),
230+
invokeMethod(method, std::move(payload),
226231
[promise](const MessageOrStatus& maybe_message) mutable {
227232
promise->set_value(maybe_message);
228233
});
229234

230235
return {std::move(future), std::move(handle)};
231236
}
232237

233-
RpcClient::InvokeFuture RpcClient::invokeMethod() {
238+
RpcClient::InvokeFuture RpcClient::invokeMethod(const v1::UUri& method) {
234239
// Note: functors need to be copy constructable. We work around this by
235240
// wrapping the promise in a shared_ptr. Unique access to it will be
236241
// assured by the implementation at the core of invokeMethod - it only
237242
// allows exactly one call to the callback via std::call_once.
238243
auto promise = std::make_shared<std::promise<MessageOrStatus>>();
239244
auto future = promise->get_future();
240-
auto handle =
241-
invokeMethod([promise](const MessageOrStatus& maybe_message) mutable {
245+
auto handle = invokeMethod(
246+
method, [promise](const MessageOrStatus& maybe_message) mutable {
242247
promise->set_value(maybe_message);
243248
});
244249

src/datamodel/builder/UMessage.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ UMessageBuilder& UMessageBuilder::withPriority(v1::UPriority priority) {
137137
return *this;
138138
}
139139

140+
UMessageBuilder& UMessageBuilder::withMethod(const v1::UUri& method) {
141+
auto [isValid, reason] = validator::uri::isValidRpcMethod(method);
142+
if (!isValid) {
143+
throw std::invalid_argument(
144+
"The UUri provided is not a valid RpcMethod");
145+
}
146+
147+
*attributes_.mutable_sink() = method;
148+
149+
return *this;
150+
}
151+
140152
UMessageBuilder& UMessageBuilder::withTtl(std::chrono::milliseconds ttl) {
141153
if ((ttl.count() <= 0) ||
142154
(ttl.count() > std::numeric_limits<uint32_t>::max())) {

0 commit comments

Comments
 (0)