|
| 1 | +// |
| 2 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// |
| 5 | + |
| 6 | +#ifndef OCVSMD_SDK_NODE_RPC_CLIENT_HPP_INCLUDED |
| 7 | +#define OCVSMD_SDK_NODE_RPC_CLIENT_HPP_INCLUDED |
| 8 | + |
| 9 | +#include "defines.hpp" |
| 10 | +#include "execution.hpp" |
| 11 | + |
| 12 | +#include <cetl/pf17/cetlpf.hpp> |
| 13 | + |
| 14 | +#include <chrono> |
| 15 | +#include <memory> |
| 16 | + |
| 17 | +namespace ocvsmd |
| 18 | +{ |
| 19 | +namespace sdk |
| 20 | +{ |
| 21 | + |
| 22 | +/// Defines the interface of RPC Client to a Cyphal network node. |
| 23 | +/// |
| 24 | +class NodeRpcClient |
| 25 | +{ |
| 26 | +public: |
| 27 | + /// Defines a smart pointer type for the interface. |
| 28 | + /// |
| 29 | + /// It's made "shared" b/c execution sender (see `request` method) implicitly |
| 30 | + /// holds reference to its client. |
| 31 | + /// |
| 32 | + using Ptr = std::shared_ptr<NodeRpcClient>; |
| 33 | + |
| 34 | + virtual ~NodeRpcClient() = default; |
| 35 | + |
| 36 | + // No copy/move semantics. |
| 37 | + NodeRpcClient(NodeRpcClient&&) = delete; |
| 38 | + NodeRpcClient(const NodeRpcClient&) = delete; |
| 39 | + NodeRpcClient& operator=(NodeRpcClient&&) = delete; |
| 40 | + NodeRpcClient& operator=(const NodeRpcClient&) = delete; |
| 41 | + |
| 42 | + /// Defines the result type of the RPC client raw response. |
| 43 | + /// |
| 44 | + /// On success, the result is a raw data buffer, its size, and extra metadata. |
| 45 | + /// On failure, the result is an SDK error. |
| 46 | + /// |
| 47 | + struct RawResponse final |
| 48 | + { |
| 49 | + struct Success |
| 50 | + { |
| 51 | + OwnedMutablePayload payload; |
| 52 | + CyphalPriority priority; |
| 53 | + CyphalNodeId server_node_id; |
| 54 | + }; |
| 55 | + using Failure = Error; |
| 56 | + using Result = cetl::variant<Success, Failure>; |
| 57 | + }; |
| 58 | + /// Sends raw RPC request. |
| 59 | + /// |
| 60 | + /// The client-side (the SDK) will forward the raw data to the corresponding Cyphal network service client |
| 61 | + /// on the server-side (the daemon). The raw data is forwarded as is, without any interpretation or validation. |
| 62 | + /// |
| 63 | + /// Note, only one request can be active at a time (per rpc client). |
| 64 | + /// In the case of multiple "concurrent" operations, only the last one will report the response result. |
| 65 | + /// Any previous still existing operations will be "stalled" and never complete. |
| 66 | + /// |
| 67 | + /// @param raw_payload The raw request data to be sent. |
| 68 | + /// @param request_timeout The maximum time to keep the raw request as valid in the Cyphal network. |
| 69 | + /// @param response_timeout The maximum time to wait for reply from the Cyphal node. |
| 70 | + /// @return An execution sender which emits the async result of the operation. |
| 71 | + /// |
| 72 | + virtual SenderOf<RawResponse::Result>::Ptr rawRequest(OwnedMutablePayload&& raw_payload, |
| 73 | + const std::chrono::microseconds request_timeout, |
| 74 | + const std::chrono::microseconds response_timeout) = 0; |
| 75 | + |
| 76 | + /// Sets priority for request to be issued by this client. |
| 77 | + /// |
| 78 | + /// The next and following `request` operations will use this priority. |
| 79 | + /// |
| 80 | + virtual OptError setPriority(const CyphalPriority priority) = 0; |
| 81 | + |
| 82 | +protected: |
| 83 | + NodeRpcClient() = default; |
| 84 | + |
| 85 | +}; // NodeRpcClient |
| 86 | + |
| 87 | +} // namespace sdk |
| 88 | +} // namespace ocvsmd |
| 89 | + |
| 90 | +#endif // OCVSMD_SDK_NODE_RPC_CLIENT_HPP_INCLUDED |
0 commit comments