2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Handling client connections
25 #ifndef COMMON_IPC_CLIENT_HPP
26 #define COMMON_IPC_CLIENT_HPP
28 #include "ipc/internals/processor.hpp"
29 #include "ipc/types.hpp"
30 #include "logger/logger.hpp"
34 namespace security_containers {
38 * This class wraps communication via UX sockets for client applications.
39 * It uses serialization mechanism from libConfig.
41 * There is one additional thread:
42 * - PROCESSOR is responsible for the communication and calling the callbacks
44 * For message format @see ipc::Processor
49 * @param serverPath path to the server's socket
51 Client(const std::string& serverPath);
54 Client(const Client&) = delete;
55 Client& operator=(const Client&) = delete;
58 * Starts the worker thread
63 * @return is the communication thread running
68 * Stops all worker thread
73 * Set the callback called for each new connection to a peer
75 * @param newPeerCallback the callback
77 void setNewPeerCallback(const PeerCallback& newPeerCallback);
80 * Set the callback called when connection to a peer is lost
82 * @param removedPeerCallback the callback
84 void setRemovedPeerCallback(const PeerCallback& removedPeerCallback);
87 * Saves the callback connected to the method id.
88 * When a message with the given method id is received
89 * the data will be parsed and passed to this callback.
91 * @param methodID API dependent id of the method
92 * @param methodCallback method handling implementation
94 template<typename SentDataType, typename ReceivedDataType>
95 void addMethodHandler(const MethodID methodID,
96 const typename MethodHandler<SentDataType, ReceivedDataType>::type& method);
99 * Saves the callback connected to the method id.
100 * When a message with the given method id is received
101 * the data will be parsed and passed to this callback.
103 * @param methodID API dependent id of the method
104 * @param SignalHandler signal handling implementation
105 * @tparam ReceivedDataType data type to serialize
107 template<typename ReceivedDataType>
108 void addSignalHandler(const MethodID methodID,
109 const typename SignalHandler<ReceivedDataType>::type& signal);
112 * Removes the callback
114 * @param methodID API dependent id of the method
116 void removeMethod(const MethodID methodID);
119 * Synchronous method call.
121 * @param methodID API dependent id of the method
122 * @param data data to send
123 * @param timeoutMS how long to wait for the return value before throw
124 * @return result data
126 template<typename SentDataType, typename ReceivedDataType>
127 std::shared_ptr<ReceivedDataType> callSync(const MethodID methodID,
128 const std::shared_ptr<SentDataType>& data,
129 unsigned int timeoutMS = 500);
132 * Asynchronous method call. The return callback will be called on
133 * return data arrival. It will be run in the PROCESSOR thread.
136 * @param methodID API dependent id of the method
137 * @param sendCallback callback for data serialization
138 * @param resultCallback callback for result serialization and handling
140 template<typename SentDataType, typename ReceivedDataType>
141 void callAsync(const MethodID methodID,
142 const std::shared_ptr<SentDataType>& data,
143 const typename ResultHandler<ReceivedDataType>::type& resultCallback);
146 * Send a signal to the peer.
147 * There is no return value from the peer
148 * Sends any data only if a peer registered this a signal
150 * @param methodID API dependent id of the method
151 * @param data data to sent
152 * @tparam SentDataType data type to send
154 template<typename SentDataType>
155 void signal(const MethodID methodID,
156 const std::shared_ptr<SentDataType>& data);
159 FileDescriptor mServiceFD;
160 Processor mProcessor;
161 std::string mSocketPath;
164 template<typename SentDataType, typename ReceivedDataType>
165 void Client::addMethodHandler(const MethodID methodID,
166 const typename MethodHandler<SentDataType, ReceivedDataType>::type& method)
168 LOGD("Adding method with id " << methodID);
169 mProcessor.addMethodHandler<SentDataType, ReceivedDataType>(methodID, method);
170 LOGD("Added method with id " << methodID);
173 template<typename ReceivedDataType>
174 void Client::addSignalHandler(const MethodID methodID,
175 const typename SignalHandler<ReceivedDataType>::type& handler)
177 LOGD("Adding signal with id " << methodID);
178 mProcessor.addSignalHandler<ReceivedDataType>(methodID, handler);
179 LOGD("Added signal with id " << methodID);
182 template<typename SentDataType, typename ReceivedDataType>
183 std::shared_ptr<ReceivedDataType> Client::callSync(const MethodID methodID,
184 const std::shared_ptr<SentDataType>& data,
185 unsigned int timeoutMS)
187 LOGD("Sync calling method: " << methodID);
188 return mProcessor.callSync<SentDataType, ReceivedDataType>(methodID, mServiceFD, data, timeoutMS);
191 template<typename SentDataType, typename ReceivedDataType>
192 void Client::callAsync(const MethodID methodID,
193 const std::shared_ptr<SentDataType>& data,
194 const typename ResultHandler<ReceivedDataType>::type& resultCallback)
196 LOGD("Async calling method: " << methodID);
197 mProcessor.callAsync<SentDataType,
198 ReceivedDataType>(methodID,
202 LOGD("Async called method: " << methodID);
205 template<typename SentDataType>
206 void Client::signal(const MethodID methodID,
207 const std::shared_ptr<SentDataType>& data)
209 LOGD("Signaling: " << methodID);
210 mProcessor.signal<SentDataType>(methodID, data);
211 LOGD("Signaled: " << methodID);
215 } // namespace security_containers
217 #endif // COMMON_IPC_CLIENT_HPP