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 : #include "IpcClientControllerImpl.h"
21 : #include "IpcControllerFactory.h"
22 : #include "IpcLogging.h"
23 : #include <memory>
24 : #include <utility>
25 :
26 : namespace firebolt::rialto::ipc
27 : {
28 24 : std::shared_ptr<IControllerFactory> IControllerFactory::createFactory()
29 : {
30 24 : std::shared_ptr<IControllerFactory> factory;
31 :
32 : try
33 : {
34 24 : factory = std::make_shared<ControllerFactory>();
35 : }
36 0 : catch (const std::exception &e)
37 : {
38 0 : RIALTO_IPC_LOG_ERROR("Failed to create the controller factory, reason: %s", e.what());
39 : }
40 :
41 24 : return factory;
42 : }
43 :
44 15 : std::shared_ptr<google::protobuf::RpcController> ControllerFactory::create()
45 : {
46 15 : return std::make_shared<ClientControllerImpl>();
47 : }
48 :
49 : // -----------------------------------------------------------------------------
50 : /*!
51 : \overload
52 :
53 : \quote
54 : Resets the RpcController to its initial state so that it may be reused in
55 : a new call. Must not be called while an RPC is in progress.
56 :
57 :
58 : */
59 0 : void ClientControllerImpl::Reset() // NOLINT(build/function_format)
60 : {
61 0 : std::lock_guard<std::mutex> locker(m_lock);
62 0 : m_failed = false;
63 0 : m_reason.clear();
64 : }
65 :
66 : // -----------------------------------------------------------------------------
67 : /*!
68 : \overload
69 :
70 : \quote
71 : After a call has finished, returns true if the call failed. The possible
72 : reasons for failure depend on the RPC implementation. Failed() must not
73 : be called before a call has finished. If Failed() returns true, the
74 : contents of the response message are undefined.
75 :
76 : */
77 14 : bool ClientControllerImpl::Failed() const // NOLINT(build/function_format)
78 : {
79 14 : std::lock_guard<std::mutex> locker(m_lock);
80 14 : return m_failed;
81 : }
82 :
83 : // -----------------------------------------------------------------------------
84 : /*!
85 : \overload
86 :
87 : \quote
88 : If Failed() is true, returns a human-readable description of the error.
89 :
90 : */
91 4 : std::string ClientControllerImpl::ErrorText() const // NOLINT(build/function_format)
92 : {
93 4 : std::lock_guard<std::mutex> locker(m_lock);
94 8 : return m_reason;
95 4 : }
96 :
97 : // -----------------------------------------------------------------------------
98 : /*!
99 : \overload
100 : \warning Not implemented
101 :
102 : \quote
103 : Advises the RPC system that the caller desires that the RPC call be
104 : canceled. The RPC system may cancel it immediately, may wait awhile and
105 : then cancel it, or may not even cancel the call at all. If the call is
106 : canceled, the "done" callback will still be called and the RpcController
107 : will indicate that the call failed at that time.
108 :
109 : */
110 0 : void ClientControllerImpl::StartCancel() // NOLINT(build/function_format)
111 : {
112 0 : RIALTO_IPC_LOG_WARN("Cancel not implemented");
113 : }
114 :
115 : // -----------------------------------------------------------------------------
116 : /*!
117 : \internal
118 :
119 : Called from RialtoIpcChannelImpl when a method call failed for any reason.
120 :
121 : */
122 6 : void ClientControllerImpl::setMethodCallFailed(std::string reason)
123 : {
124 6 : std::lock_guard<std::mutex> locker(m_lock);
125 6 : m_failed = true;
126 6 : m_reason = std::move(reason);
127 : }
128 :
129 : // -----------------------------------------------------------------------------
130 : /*!
131 : \internal
132 :
133 : Called from RialtoIpcChannelImpl when a method call failed for any reason.
134 :
135 : */
136 0 : void ClientControllerImpl::setMethodCallFailed(const char *format, va_list ap)
137 : {
138 : char buf[512];
139 0 : vsnprintf(buf, sizeof(buf), format, ap);
140 :
141 0 : std::lock_guard<std::mutex> locker(m_lock);
142 0 : m_failed = true;
143 0 : m_reason.assign(buf);
144 : }
145 :
146 : } // namespace firebolt::rialto::ipc
|