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 Declaration of the IPC handling class
25 #ifndef COMMON_IPC_SERVICE_HPP
26 #define COMMON_IPC_SERVICE_HPP
28 #include "ipc/internals/processor.hpp"
29 #include "ipc/internals/acceptor.hpp"
30 #include "ipc/types.hpp"
31 #include "logger/logger.hpp"
40 * This class wraps communication via UX sockets.
41 * It uses serialization mechanism from libConfig.
43 * There are two working threads:
44 * - ACCEPTOR accepts incoming connections and passes them to PROCESSOR
45 * - PROCESSOR is responsible for the communication and calling the callbacks
47 * For message format @see ipc::Processor
52 * @param path path to the socket
54 Service(const std::string& path,
55 const PeerCallback& addPeerCallback = nullptr,
56 const PeerCallback& removePeerCallback = nullptr);
59 Service(const Service&) = delete;
60 Service& operator=(const Service&) = delete;
63 * Starts the worker and acceptor threads
65 * @param usesExternalPolling internal or external polling is used
67 void start(const bool usesExternalPolling = false);
70 * @return is the communication thread running
75 * Stops all working threads
80 * Used with an external polling loop
82 * @return vector of internal file descriptors
84 std::vector<FileDescriptor> getFDs();
87 * Used with an external polling loop.
88 * Handles one event from the file descriptor.
90 * @param fd file descriptor
91 * @param pollEvent event on the fd. Defined in poll.h
94 void handle(const FileDescriptor fd, const short pollEvent);
97 * Set the callback called for each new connection to a peer
99 * @param newPeerCallback the callback
101 void setNewPeerCallback(const PeerCallback& newPeerCallback);
104 * Set the callback called when connection to a peer is lost
106 * @param removedPeerCallback the callback
108 void setRemovedPeerCallback(const PeerCallback& removedPeerCallback);
111 * Saves the callback connected to the method id.
112 * When a message with the given method id is received
113 * the data will be parsed and passed to this callback.
115 * @param methodID API dependent id of the method
116 * @param method method handling implementation
118 template<typename SentDataType, typename ReceivedDataType>
119 void addMethodHandler(const MethodID methodID,
120 const typename MethodHandler<SentDataType, ReceivedDataType>::type& method);
123 * Saves the callback connected to the method id.
124 * When a message with the given method id is received
125 * the data will be parsed and passed to this callback.
127 * @param methodID API dependent id of the method
128 * @param handler handling implementation
129 * @tparam ReceivedDataType data type to serialize
131 template<typename ReceivedDataType>
132 void addSignalHandler(const MethodID methodID,
133 const typename SignalHandler<ReceivedDataType>::type& handler);
136 * Removes the callback
138 * @param methodID API dependent id of the method
140 void removeMethod(const MethodID methodID);
143 * Synchronous method call.
145 * @param methodID API dependent id of the method
146 * @param data data to send
147 * @return result data
149 template<typename SentDataType, typename ReceivedDataType>
150 std::shared_ptr<ReceivedDataType> callSync(const MethodID methodID,
151 const FileDescriptor peerFD,
152 const std::shared_ptr<SentDataType>& data,
153 unsigned int timeoutMS = 500);
156 * Asynchronous method call. The return callback will be called on
157 * return data arrival. It will be run in the PROCESSOR thread.
160 * @param methodID API dependent id of the method
161 * @param data data to send
162 * @param resultCallback callback for result serialization and handling
164 template<typename SentDataType, typename ReceivedDataType>
165 void callAsync(const MethodID methodID,
166 const FileDescriptor peerFD,
167 const std::shared_ptr<SentDataType>& data,
168 const typename ResultHandler<ReceivedDataType>::type& resultCallback);
171 * Send a signal to the peer.
172 * There is no return value from the peer
173 * Sends any data only if a peer registered this a signal
175 * @param methodID API dependent id of the method
176 * @param data data to sent
177 * @tparam SentDataType data type to send
179 template<typename SentDataType>
180 void signal(const MethodID methodID,
181 const std::shared_ptr<SentDataType>& data);
183 typedef std::lock_guard<std::mutex> Lock;
184 Processor mProcessor;
189 template<typename SentDataType, typename ReceivedDataType>
190 void Service::addMethodHandler(const MethodID methodID,
191 const typename MethodHandler<SentDataType, ReceivedDataType>::type& method)
193 LOGS("Service addMethodHandler, methodID " << methodID);
194 mProcessor.addMethodHandler<SentDataType, ReceivedDataType>(methodID, method);
197 template<typename ReceivedDataType>
198 void Service::addSignalHandler(const MethodID methodID,
199 const typename SignalHandler<ReceivedDataType>::type& handler)
201 LOGS("Service addSignalHandler, methodID " << methodID);
202 mProcessor.addSignalHandler<ReceivedDataType>(methodID, handler);
205 template<typename SentDataType, typename ReceivedDataType>
206 std::shared_ptr<ReceivedDataType> Service::callSync(const MethodID methodID,
207 const FileDescriptor peerFD,
208 const std::shared_ptr<SentDataType>& data,
209 unsigned int timeoutMS)
211 LOGS("Service callSync, methodID: " << methodID
212 << ", peerFD: " << peerFD
213 << ", timeoutMS: " << timeoutMS);
214 return mProcessor.callSync<SentDataType, ReceivedDataType>(methodID, peerFD, data, timeoutMS);
217 template<typename SentDataType, typename ReceivedDataType>
218 void Service::callAsync(const MethodID methodID,
219 const FileDescriptor peerFD,
220 const std::shared_ptr<SentDataType>& data,
221 const typename ResultHandler<ReceivedDataType>::type& resultCallback)
223 LOGS("Service callAsync, methodID: " << methodID << ", peerFD: " << peerFD);
224 mProcessor.callAsync<SentDataType,
225 ReceivedDataType>(methodID,
231 template<typename SentDataType>
232 void Service::signal(const MethodID methodID,
233 const std::shared_ptr<SentDataType>& data)
235 LOGS("Service signal, methodID: " << methodID);
236 mProcessor.signal<SentDataType>(methodID, data);
242 #endif // COMMON_IPC_SERVICE_HPP