9392a42896ff617d7343f8e2445a4c744a11d180
[platform/core/security/vasum.git] / common / ipc / service.hpp
1 /*
2 *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 *  Contact: Jan Olszak <j.olszak@samsung.com>
5 *
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
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
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
17 */
18
19 /**
20  * @file
21  * @author  Jan Olszak (j.olszak@samsung.com)
22  * @brief   Declaration of the IPC handling class
23  */
24
25 #ifndef COMMON_IPC_SERVICE_HPP
26 #define COMMON_IPC_SERVICE_HPP
27
28 #include "ipc/internals/processor.hpp"
29 #include "ipc/internals/acceptor.hpp"
30 #include "ipc/types.hpp"
31 #include "logger/logger.hpp"
32
33 #include <string>
34
35 namespace vasum {
36 namespace ipc {
37
38
39 /**
40  * This class wraps communication via UX sockets.
41  * It uses serialization mechanism from libConfig.
42  *
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
46  *
47  * For message format @see ipc::Processor
48  */
49 class Service {
50 public:
51     /**
52      * @param path path to the socket
53      */
54     Service(const std::string& path,
55             const PeerCallback& addPeerCallback = nullptr,
56             const PeerCallback& removePeerCallback = nullptr);
57     ~Service();
58
59     Service(const Service&) = delete;
60     Service& operator=(const Service&) = delete;
61
62     /**
63      * Starts the worker and acceptor threads
64      *
65      * @param usesExternalPolling internal or external polling is used
66      */
67     void start(const bool usesExternalPolling = false);
68
69     /**
70     * @return is the communication thread running
71     */
72     bool isStarted();
73
74     /**
75      * Stops all working threads
76      */
77     void stop();
78
79     /**
80      * Used with an external polling loop
81      *
82      * @return vector of internal file descriptors
83      */
84     std::vector<FileDescriptor> getFDs();
85
86     /**
87      * Used with an external polling loop.
88      * Handles one event from the file descriptor.
89      *
90      * @param fd file descriptor
91      * @param pollEvent event on the fd. Defined in poll.h
92      *
93      */
94     void handle(const FileDescriptor fd, const short pollEvent);
95
96     /**
97     * Set the callback called for each new connection to a peer
98     *
99     * @param newPeerCallback the callback
100     */
101     void setNewPeerCallback(const PeerCallback& newPeerCallback);
102
103     /**
104      * Set the callback called when connection to a peer is lost
105      *
106      * @param removedPeerCallback the callback
107      */
108     void setRemovedPeerCallback(const PeerCallback& removedPeerCallback);
109
110     /**
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.
114      *
115      * @param methodID API dependent id of the method
116      * @param method method handling implementation
117      */
118     template<typename SentDataType, typename ReceivedDataType>
119     void addMethodHandler(const MethodID methodID,
120                           const typename MethodHandler<SentDataType, ReceivedDataType>::type& method);
121
122     /**
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.
126      *
127      * @param methodID API dependent id of the method
128      * @param handler handling implementation
129      * @tparam ReceivedDataType data type to serialize
130      */
131     template<typename ReceivedDataType>
132     void addSignalHandler(const MethodID methodID,
133                           const typename SignalHandler<ReceivedDataType>::type& handler);
134
135     /**
136      * Removes the callback
137      *
138      * @param methodID API dependent id of the method
139      */
140     void removeMethod(const MethodID methodID);
141
142     /**
143      * Synchronous method call.
144      *
145      * @param methodID API dependent id of the method
146      * @param data data to send
147      * @return result data
148      */
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);
154
155     /**
156      * Asynchronous method call. The return callback will be called on
157      * return data arrival. It will be run in the PROCESSOR thread.
158      *
159      *
160      * @param methodID API dependent id of the method
161      * @param data data to send
162      * @param resultCallback callback for result serialization and handling
163      */
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);
169
170     /**
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
174     *
175     * @param methodID API dependent id of the method
176     * @param data data to sent
177     * @tparam SentDataType data type to send
178     */
179     template<typename SentDataType>
180     void signal(const MethodID methodID,
181                 const std::shared_ptr<SentDataType>& data);
182 private:
183     typedef std::lock_guard<std::mutex> Lock;
184     Processor mProcessor;
185     Acceptor mAcceptor;
186 };
187
188
189 template<typename SentDataType, typename ReceivedDataType>
190 void Service::addMethodHandler(const MethodID methodID,
191                                const typename MethodHandler<SentDataType, ReceivedDataType>::type& method)
192 {
193     LOGS("Service addMethodHandler, methodID " << methodID);
194     mProcessor.addMethodHandler<SentDataType, ReceivedDataType>(methodID, method);
195 }
196
197 template<typename ReceivedDataType>
198 void Service::addSignalHandler(const MethodID methodID,
199                                const typename SignalHandler<ReceivedDataType>::type& handler)
200 {
201     LOGS("Service addSignalHandler, methodID " << methodID);
202     mProcessor.addSignalHandler<ReceivedDataType>(methodID, handler);
203 }
204
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)
210 {
211     LOGS("Service callSync, methodID: " << methodID
212          << ", peerFD: " << peerFD
213          << ", timeoutMS: " << timeoutMS);
214     return mProcessor.callSync<SentDataType, ReceivedDataType>(methodID, peerFD, data, timeoutMS);
215 }
216
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)
222 {
223     LOGS("Service callAsync, methodID: " << methodID << ", peerFD: " << peerFD);
224     mProcessor.callAsync<SentDataType,
225                          ReceivedDataType>(methodID,
226                                            peerFD,
227                                            data,
228                                            resultCallback);
229 }
230
231 template<typename SentDataType>
232 void Service::signal(const MethodID methodID,
233                      const std::shared_ptr<SentDataType>& data)
234 {
235     LOGS("Service signal, methodID: " << methodID);
236     mProcessor.signal<SentDataType>(methodID, data);
237 }
238
239 } // namespace ipc
240 } // namespace vasum
241
242 #endif // COMMON_IPC_SERVICE_HPP