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"
35 namespace security_containers {
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
68 * @return is the communication thread running
73 * Stops all working threads
78 * Set the callback called for each new connection to a peer
80 * @param newPeerCallback the callback
82 void setNewPeerCallback(const PeerCallback& newPeerCallback);
85 * Set the callback called when connection to a peer is lost
87 * @param removedPeerCallback the callback
89 void setRemovedPeerCallback(const PeerCallback& removedPeerCallback);
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.
96 * @param methodID API dependent id of the method
97 * @param methodCallback method handling implementation
99 template<typename SentDataType, typename ReceivedDataType>
100 void addMethodHandler(const MethodID methodID,
101 const typename MethodHandler<SentDataType, ReceivedDataType>::type& method);
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.
108 * @param methodID API dependent id of the method
109 * @param handler handling implementation
110 * @tparam ReceivedDataType data type to serialize
112 template<typename ReceivedDataType>
113 void addSignalHandler(const MethodID methodID,
114 const typename SignalHandler<ReceivedDataType>::type& handler);
117 * Removes the callback
119 * @param methodID API dependent id of the method
121 void removeMethod(const MethodID methodID);
124 * Synchronous method call.
126 * @param methodID API dependent id of the method
127 * @param data data to send
128 * @return result data
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);
137 * Asynchronous method call. The return callback will be called on
138 * return data arrival. It will be run in the PROCESSOR thread.
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
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);
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
156 * @param methodID API dependent id of the method
157 * @param data data to sent
158 * @tparam SentDataType data type to send
160 template<typename SentDataType>
161 void signal(const MethodID methodID,
162 const std::shared_ptr<SentDataType>& data);
164 typedef std::lock_guard<std::mutex> Lock;
165 Processor mProcessor;
170 template<typename SentDataType, typename ReceivedDataType>
171 void Service::addMethodHandler(const MethodID methodID,
172 const typename MethodHandler<SentDataType, ReceivedDataType>::type& method)
174 LOGD("Adding method with id " << methodID);
175 mProcessor.addMethodHandler<SentDataType, ReceivedDataType>(methodID, method);
176 LOGD("Added method with id " << methodID);
179 template<typename ReceivedDataType>
180 void Service::addSignalHandler(const MethodID methodID,
181 const typename SignalHandler<ReceivedDataType>::type& handler)
183 LOGD("Adding signal with id " << methodID);
184 mProcessor.addSignalHandler<ReceivedDataType>(methodID, handler);
185 LOGD("Added signal with id " << methodID);
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)
194 LOGD("Sync calling method: " << methodID << " for user: " << peerFD);
195 return mProcessor.callSync<SentDataType, ReceivedDataType>(methodID, peerFD, data, timeoutMS);
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)
204 LOGD("Async calling method: " << methodID << " for user: " << peerFD);
205 mProcessor.callAsync<SentDataType,
206 ReceivedDataType>(methodID,
210 LOGD("Async called method: " << methodID << "for user: " << peerFD);
213 template<typename SentDataType>
214 void Service::signal(const MethodID methodID,
215 const std::shared_ptr<SentDataType>& data)
217 LOGD("Signaling: " << methodID);
218 mProcessor.signal<SentDataType>(methodID, data);
219 LOGD("Signaled: " << methodID);
223 } // namespace security_containers
225 #endif // COMMON_IPC_SERVICE_HPP