IPC: Replace PeerID witch peer's file descriptor
[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 security_containers {
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     void start();
66
67     /**
68     * @return is the communication thread running
69     */
70     bool isStarted();
71
72     /**
73      * Stops all working threads
74      */
75     void stop();
76
77     /**
78     * Set the callback called for each new connection to a peer
79     *
80     * @param newPeerCallback the callback
81     */
82     void setNewPeerCallback(const PeerCallback& newPeerCallback);
83
84     /**
85      * Set the callback called when connection to a peer is lost
86      *
87      * @param removedPeerCallback the callback
88      */
89     void setRemovedPeerCallback(const PeerCallback& removedPeerCallback);
90
91     /**
92      * Saves the callback connected to the method id.
93      * When a message with the given method id is received
94      * the data will be parsed and passed to this callback.
95      *
96      * @param methodID API dependent id of the method
97      * @param methodCallback method handling implementation
98      */
99     template<typename SentDataType, typename ReceivedDataType>
100     void addMethodHandler(const MethodID methodID,
101                           const typename MethodHandler<SentDataType, ReceivedDataType>::type& method);
102
103     /**
104      * Saves the callback connected to the method id.
105      * When a message with the given method id is received
106      * the data will be parsed and passed to this callback.
107      *
108      * @param methodID API dependent id of the method
109      * @param handler handling implementation
110      * @tparam ReceivedDataType data type to serialize
111      */
112     template<typename ReceivedDataType>
113     void addSignalHandler(const MethodID methodID,
114                           const typename SignalHandler<ReceivedDataType>::type& handler);
115
116     /**
117      * Removes the callback
118      *
119      * @param methodID API dependent id of the method
120      */
121     void removeMethod(const MethodID methodID);
122
123     /**
124      * Synchronous method call.
125      *
126      * @param methodID API dependent id of the method
127      * @param data data to send
128      * @return result data
129      */
130     template<typename SentDataType, typename ReceivedDataType>
131     std::shared_ptr<ReceivedDataType> callSync(const MethodID methodID,
132                                                const FileDescriptor peerFD,
133                                                const std::shared_ptr<SentDataType>& data,
134                                                unsigned int timeoutMS = 500);
135
136     /**
137      * Asynchronous method call. The return callback will be called on
138      * return data arrival. It will be run in the PROCESSOR thread.
139      *
140      *
141      * @param methodID API dependent id of the method
142      * @param sendCallback callback for data serialization
143      * @param resultCallback callback for result serialization and handling
144      */
145     template<typename SentDataType, typename ReceivedDataType>
146     void callAsync(const MethodID methodID,
147                    const FileDescriptor peerFD,
148                    const std::shared_ptr<SentDataType>& data,
149                    const typename ResultHandler<ReceivedDataType>::type& resultCallback);
150
151     /**
152     * Send a signal to the peer.
153     * There is no return value from the peer
154     * Sends any data only if a peer registered this a signal
155     *
156     * @param methodID API dependent id of the method
157     * @param data data to sent
158     * @tparam SentDataType data type to send
159     */
160     template<typename SentDataType>
161     void signal(const MethodID methodID,
162                 const std::shared_ptr<SentDataType>& data);
163 private:
164     typedef std::lock_guard<std::mutex> Lock;
165     Processor mProcessor;
166     Acceptor mAcceptor;
167 };
168
169
170 template<typename SentDataType, typename ReceivedDataType>
171 void Service::addMethodHandler(const MethodID methodID,
172                                const typename MethodHandler<SentDataType, ReceivedDataType>::type& method)
173 {
174     LOGD("Adding method with id " << methodID);
175     mProcessor.addMethodHandler<SentDataType, ReceivedDataType>(methodID, method);
176     LOGD("Added method with id " << methodID);
177 }
178
179 template<typename ReceivedDataType>
180 void Service::addSignalHandler(const MethodID methodID,
181                                const typename SignalHandler<ReceivedDataType>::type& handler)
182 {
183     LOGD("Adding signal with id " << methodID);
184     mProcessor.addSignalHandler<ReceivedDataType>(methodID, handler);
185     LOGD("Added signal with id " << methodID);
186 }
187
188 template<typename SentDataType, typename ReceivedDataType>
189 std::shared_ptr<ReceivedDataType> Service::callSync(const MethodID methodID,
190                                                     const FileDescriptor peerFD,
191                                                     const std::shared_ptr<SentDataType>& data,
192                                                     unsigned int timeoutMS)
193 {
194     LOGD("Sync calling method: " << methodID << " for user: " << peerFD);
195     return mProcessor.callSync<SentDataType, ReceivedDataType>(methodID, peerFD, data, timeoutMS);
196 }
197
198 template<typename SentDataType, typename ReceivedDataType>
199 void Service::callAsync(const MethodID methodID,
200                         const FileDescriptor peerFD,
201                         const std::shared_ptr<SentDataType>& data,
202                         const typename ResultHandler<ReceivedDataType>::type& resultCallback)
203 {
204     LOGD("Async calling method: " << methodID << " for user: " << peerFD);
205     mProcessor.callAsync<SentDataType,
206                          ReceivedDataType>(methodID,
207                                            peerFD,
208                                            data,
209                                            resultCallback);
210     LOGD("Async called method: " << methodID << "for user: " << peerFD);
211 }
212
213 template<typename SentDataType>
214 void Service::signal(const MethodID methodID,
215                      const std::shared_ptr<SentDataType>& data)
216 {
217     LOGD("Signaling: " << methodID);
218     mProcessor.signal<SentDataType>(methodID, data);
219     LOGD("Signaled: " << methodID);
220 }
221
222 } // namespace ipc
223 } // namespace security_containers
224
225 #endif // COMMON_IPC_SERVICE_HPP