IPC: Replace PeerID witch peer's file descriptor
[platform/core/security/vasum.git] / common / ipc / client.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   Handling client connections
23  */
24
25 #ifndef COMMON_IPC_CLIENT_HPP
26 #define COMMON_IPC_CLIENT_HPP
27
28 #include "ipc/internals/processor.hpp"
29 #include "ipc/types.hpp"
30 #include "logger/logger.hpp"
31
32 #include <string>
33
34 namespace security_containers {
35 namespace ipc {
36
37 /**
38  * This class wraps communication via UX sockets for client applications.
39  * It uses serialization mechanism from libConfig.
40  *
41  * There is one additional thread:
42  * - PROCESSOR is responsible for the communication and calling the callbacks
43  *
44  * For message format @see ipc::Processor
45  */
46 class Client {
47 public:
48     /**
49      * @param serverPath path to the server's socket
50      */
51     Client(const std::string& serverPath);
52     ~Client();
53
54     Client(const Client&) = delete;
55     Client& operator=(const Client&) = delete;
56
57     /**
58      * Starts the worker thread
59      */
60     void start();
61
62     /**
63     * @return is the communication thread running
64     */
65     bool isStarted();
66
67     /**
68      * Stops all worker thread
69      */
70     void stop();
71
72     /**
73      * Set the callback called for each new connection to a peer
74      *
75      * @param newPeerCallback the callback
76      */
77     void setNewPeerCallback(const PeerCallback& newPeerCallback);
78
79     /**
80      * Set the callback called when connection to a peer is lost
81      *
82      * @param removedPeerCallback the callback
83      */
84     void setRemovedPeerCallback(const PeerCallback& removedPeerCallback);
85
86     /**
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.
90      *
91      * @param methodID API dependent id of the method
92      * @param methodCallback method handling implementation
93      */
94     template<typename SentDataType, typename ReceivedDataType>
95     void addMethodHandler(const MethodID methodID,
96                           const typename MethodHandler<SentDataType, ReceivedDataType>::type& method);
97
98     /**
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.
102      *
103      * @param methodID API dependent id of the method
104      * @param SignalHandler signal handling implementation
105      * @tparam ReceivedDataType data type to serialize
106      */
107     template<typename ReceivedDataType>
108     void addSignalHandler(const MethodID methodID,
109                           const typename SignalHandler<ReceivedDataType>::type& signal);
110
111     /**
112      * Removes the callback
113      *
114      * @param methodID API dependent id of the method
115      */
116     void removeMethod(const MethodID methodID);
117
118     /**
119      * Synchronous method call.
120      *
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
125      */
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);
130
131     /**
132      * Asynchronous method call. The return callback will be called on
133      * return data arrival. It will be run in the PROCESSOR thread.
134      *
135      *
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
139      */
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);
144
145     /**
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
149     *
150     * @param methodID API dependent id of the method
151     * @param data data to sent
152     * @tparam SentDataType data type to send
153     */
154     template<typename SentDataType>
155     void signal(const MethodID methodID,
156                 const std::shared_ptr<SentDataType>& data);
157
158 private:
159     FileDescriptor mServiceFD;
160     Processor mProcessor;
161     std::string mSocketPath;
162 };
163
164 template<typename SentDataType, typename ReceivedDataType>
165 void Client::addMethodHandler(const MethodID methodID,
166                               const typename MethodHandler<SentDataType, ReceivedDataType>::type& method)
167 {
168     LOGD("Adding method with id " << methodID);
169     mProcessor.addMethodHandler<SentDataType, ReceivedDataType>(methodID, method);
170     LOGD("Added method with id " << methodID);
171 }
172
173 template<typename ReceivedDataType>
174 void Client::addSignalHandler(const MethodID methodID,
175                               const typename SignalHandler<ReceivedDataType>::type& handler)
176 {
177     LOGD("Adding signal with id " << methodID);
178     mProcessor.addSignalHandler<ReceivedDataType>(methodID, handler);
179     LOGD("Added signal with id " << methodID);
180 }
181
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)
186 {
187     LOGD("Sync calling method: " << methodID);
188     return mProcessor.callSync<SentDataType, ReceivedDataType>(methodID, mServiceFD, data, timeoutMS);
189 }
190
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)
195 {
196     LOGD("Async calling method: " << methodID);
197     mProcessor.callAsync<SentDataType,
198                          ReceivedDataType>(methodID,
199                                            mServiceFD,
200                                            data,
201                                            resultCallback);
202     LOGD("Async called method: " << methodID);
203 }
204
205 template<typename SentDataType>
206 void Client::signal(const MethodID methodID,
207                     const std::shared_ptr<SentDataType>& data)
208 {
209     LOGD("Signaling: " << methodID);
210     mProcessor.signal<SentDataType>(methodID, data);
211     LOGD("Signaled: " << methodID);
212 }
213
214 } // namespace ipc
215 } // namespace security_containers
216
217 #endif // COMMON_IPC_CLIENT_HPP