Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef FIREBOLT_RIALTO_IPC_IPC_CLIENT_CONTROLLER_IMPL_H_
21 : #define FIREBOLT_RIALTO_IPC_IPC_CLIENT_CONTROLLER_IMPL_H_
22 :
23 : #include <google/protobuf/service.h>
24 :
25 : #include <cstdarg>
26 : #include <mutex>
27 : #include <string>
28 :
29 : namespace firebolt::rialto::ipc
30 : {
31 : class ClientControllerImpl final : public google::protobuf::RpcController
32 : {
33 : public:
34 15 : ClientControllerImpl() = default;
35 15 : ~ClientControllerImpl() final = default;
36 :
37 : /**
38 : * Client-side methods
39 : * Calls can only be made from the client side. Calling these apis from server side
40 : * is undefined and could cause crashes.
41 : */
42 :
43 : /**
44 : * @brief Reset the RpcController.
45 : *
46 : * So that the RpcController can be reused for a new call, sets to the inital state.
47 : */
48 : void Reset() override;
49 :
50 : /**
51 : * @brief Checks id the previosu call has failed.
52 : *
53 : * The reason for the failure depends on the implementaion of the RPC. Failed should only
54 : * be called after after the call has finished. If this call returns true, the response
55 : * structure returned from the failed call is undefined.
56 : *
57 : * @retval true if previous call failed.
58 : */
59 : bool Failed() const override;
60 :
61 : /**
62 : * @brief Returns a description of the error if the previous call has failed.
63 : *
64 : * @retval error string.
65 : */
66 : std::string ErrorText() const override;
67 :
68 : /**
69 : * @brief Starts the cancellation of a RPC call
70 : *
71 : * RPC may cancel the call immediatly, wait and cancel or not cancel at all. If a call is cancelled
72 : * failure will be set for the client and "done" will still be called.
73 : */
74 : void StartCancel() override;
75 :
76 : /**
77 : * Server-side methods
78 : * Calls can only be made from the server side. Calling these apis from client side
79 : * is undefined and could cause crashes.
80 : */
81 :
82 : /**
83 : * @brief Causes failure to be returned to the client.
84 : *
85 : * Failed() shall return true. The failure reason can be fetched from the ErrorText().
86 : * To return machine-readable info on failure, use the method response structure rather than
87 : * SetFailed().
88 : *
89 : * @param[in] reason : The reason for the falure.
90 : */
91 0 : void SetFailed(const std::string &reason) override { (void)reason; }
92 :
93 : /**
94 : * @brief Check if the client had cancelled the RPC.
95 : *
96 : * If true, indicates that the client canceled the RPC, so the server may
97 : * as well give up on replying to it. The server should still call the
98 : * final "done" callback.
99 : *
100 : * @retval true of cancelled, false otherwise.
101 : */
102 0 : bool IsCanceled() const override { return false; }
103 :
104 : /**
105 : * @brief Request a notification when RPC is cancelled.
106 : *
107 : * The callback will always be called exactly once. If the RPC completes without
108 : * being canceled, the callback will be called after completion. If the RPC
109 : * has already been canceled when NotifyOnCancel() is called, the callback
110 : * will be called immediately.
111 : *
112 : * @param[in] callback : Callback method on cancelled.
113 : */
114 0 : void NotifyOnCancel(google::protobuf::Closure *callback) override { (void)callback; }
115 :
116 : private:
117 : friend class ChannelImpl;
118 :
119 : void setMethodCallFailed(std::string reason);
120 : void setMethodCallFailed(const char *format, va_list ap) __attribute__((format(printf, 2, 0)));
121 :
122 : private:
123 : mutable std::mutex m_lock;
124 : bool m_failed = false;
125 : std::string m_reason;
126 : };
127 :
128 : } // namespace firebolt::rialto::ipc
129 :
130 : #endif // FIREBOLT_RIALTO_IPC_IPC_CLIENT_CONTROLLER_IMPL_H_
|