Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
service.hpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2015 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 
25 #ifndef CARGO_IPC_SERVICE_HPP
26 #define CARGO_IPC_SERVICE_HPP
27 
30 #include "cargo-ipc/types.hpp"
31 #include "cargo-ipc/result.hpp"
32 #include "epoll/event-poll.hpp"
33 #include "logger/logger.hpp"
34 
35 #include <string>
36 
37 namespace cargo {
38 namespace ipc {
39 
40 
78 class Service {
79 public:
90  Service(epoll::EventPoll& eventPoll,
91  const std::string& path,
92  const PeerCallback& addPeerCallback = nullptr,
93  const PeerCallback& removePeerCallback = nullptr);
94  virtual ~Service();
95 
99  Service(const Service&) = delete;
103  Service& operator=(const Service&) = delete;
104 
109  void start();
110 
114  bool isStarted();
115 
121  void stop(bool wait = true);
122 
129  void setNewPeerCallback(const PeerCallback& newPeerCallback);
130 
137  void setRemovedPeerCallback(const PeerCallback& removedPeerCallback);
138 
151  template<typename SentDataType, typename ReceivedDataType>
152  void setMethodHandler(const MethodID methodID,
154 
169  template<typename ReceivedDataType>
170  void setSignalHandler(const MethodID methodID,
171  const typename SignalHandler<ReceivedDataType>::type& handler);
172 
180  void removeMethod(const MethodID methodID);
181 
186  bool isHandled(const MethodID methodID);
187 
199  template<typename SentDataType, typename ReceivedDataType>
200  std::shared_ptr<ReceivedDataType> callSync(const MethodID methodID,
201  const PeerID& peerID,
202  const std::shared_ptr<SentDataType>& data,
203  unsigned int timeoutMS = 5000);
204 
216  template<typename SentDataType, typename ReceivedDataType>
217  void callAsync(const MethodID methodID,
218  const PeerID& peerID,
219  const std::shared_ptr<SentDataType>& data,
220  const typename ResultHandler<ReceivedDataType>::type& resultCallback = nullptr);
221 
222  template<typename SentDataType, typename ReceivedDataType>
223  void callAsyncFromCallback(const MethodID methodID,
224  const PeerID& peerID,
225  const std::shared_ptr<SentDataType>& data,
226  const typename ResultHandler<ReceivedDataType>::type& resultCallback = nullptr);
227 
237  template<typename SentDataType>
238  void signal(const MethodID methodID,
239  const std::shared_ptr<SentDataType>& data);
240 private:
244 
245  void handle(const FileDescriptor fd, const epoll::Events pollEvents);
246 };
247 
248 
249 template<typename SentDataType, typename ReceivedDataType>
252 {
253  LOGS("Service setMethodHandler, methodID " << methodID);
254  mProcessor.setMethodHandler<SentDataType, ReceivedDataType>(methodID, method);
255 }
256 
257 template<typename ReceivedDataType>
259  const typename SignalHandler<ReceivedDataType>::type& handler)
260 {
261  LOGS("Service setSignalHandler, methodID " << methodID);
262  mProcessor.setSignalHandler<ReceivedDataType>(methodID, handler);
263 }
264 
265 template<typename SentDataType, typename ReceivedDataType>
266 std::shared_ptr<ReceivedDataType> Service::callSync(const MethodID methodID,
267  const PeerID& peerID,
268  const std::shared_ptr<SentDataType>& data,
269  unsigned int timeoutMS)
270 {
271  LOGS("Service callSync, methodID: " << methodID
272  << ", peerID: " << peerID
273  << ", timeoutMS: " << timeoutMS);
274  return mProcessor.callSync<SentDataType, ReceivedDataType>(methodID, peerID, data, timeoutMS);
275 }
276 
277 template<typename SentDataType, typename ReceivedDataType>
278 void Service::callAsync(const MethodID methodID,
279  const PeerID& peerID,
280  const std::shared_ptr<SentDataType>& data,
281  const typename ResultHandler<ReceivedDataType>::type& resultCallback)
282 {
283  LOGS("Service callAsync, methodID: " << methodID << ", peerID: " << peerID);
284  mProcessor.callAsync<SentDataType,
285  ReceivedDataType>(methodID,
286  peerID,
287  data,
288  resultCallback);
289 }
290 
291 template<typename SentDataType, typename ReceivedDataType>
293  const PeerID& peerID,
294  const std::shared_ptr<SentDataType>& data,
295  const typename ResultHandler<ReceivedDataType>::type& resultCallback)
296 {
297  LOGS("Service callAsyncFromCallback, methodID: " << methodID << ", peerID: " << peerID);
298  mProcessor.callAsyncNonBlock<SentDataType,
299  ReceivedDataType>(methodID,
300  peerID,
301  data,
302  resultCallback);
303 }
304 
305 
306 template<typename SentDataType>
307 void Service::signal(const MethodID methodID,
308  const std::shared_ptr<SentDataType>& data)
309 {
310  LOGS("Service signal, methodID: " << methodID);
311  mProcessor.signal<SentDataType>(methodID, data);
312 }
313 
314 } // namespace ipc
315 } // namespace cargo
316 
317 #endif // CARGO_IPC_SERVICE_HPP
C++ epoll wrapper.
void start()
Starts processing.
Definition: service.cpp:60
std::function< void(Result< Data > &&) > type
Definition: result.hpp:73
#define LOGS(MSG)
Automatically create LoggerScope object which logs at the construction and destruction.
Definition: logger-scope.hpp:78
std::string PeerID
Definition: types.hpp:45
Service & operator=(const Service &)=delete
Copying Service class is prohibited.
void signal(const MethodID methodID, const std::shared_ptr< SentDataType > &data)
Send a signal to the peer.
Definition: service.hpp:307
Data and event processing thread.
void setSignalHandler(const MethodID methodID, const typename SignalHandler< ReceivedDataType >::type &process)
Saves the callbacks connected to the method id.
Definition: processor.hpp:602
void setNewPeerCallback(const PeerCallback &newPeerCallback)
Set the callback called for each new connection to a peer.
Definition: service.cpp:109
Types definitions.
Class for storing result of a method - data or exception.
void stop(bool wait=true)
Stops all working threads.
Definition: service.cpp:76
void setMethodHandler(const MethodID methodID, const typename MethodHandler< SentDataType, ReceivedDataType >::type &method)
Saves the callbacks connected to the method id.
Definition: service.hpp:250
std::function< void(const cargo::ipc::PeerID peerID, const cargo::ipc::FileDescriptor fd)> PeerCallback
Generic function type used as callback for peer events.
Definition: types.hpp:54
MessageID callAsync(const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &process)
Asynchronous method call.
Definition: processor.hpp:637
void callAsyncFromCallback(const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &resultCallback=nullptr)
Definition: service.hpp:292
virtual ~Service()
Definition: service.cpp:50
Acceptor mAcceptor
Definition: service.hpp:243
std::function< bool(PeerID peerID, std::shared_ptr< ReceivedDataType > &data)> type
Definition: types.hpp:99
std::shared_ptr< ReceivedDataType > callSync(const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, unsigned int timeoutMS=5000)
Synchronous method call.
Definition: service.hpp:266
char data[368]
Definition: initctl.cpp:41
This class wraps communication via UX sockets.
Definition: service.hpp:78
void setMethodHandler(const MethodID methodID, const typename MethodHandler< SentDataType, ReceivedDataType >::type &process)
Saves the callbacks connected to the method id.
Definition: processor.hpp:560
void removeMethod(const MethodID methodID)
Removes the callback associated with specific method id.
Definition: service.cpp:136
unsigned int MethodID
Definition: types.hpp:43
void callAsync(const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &resultCallback=nullptr)
Asynchronous method call.
Definition: service.hpp:278
unsigned int Events
bitmask of EPOLL* constants
Definition: events.hpp:39
void setSignalHandler(const MethodID methodID, const typename SignalHandler< ReceivedDataType >::type &handler)
Saves the callbacks connected to the method id.
Definition: service.hpp:258
Class for accepting new connections.
void handle(const FileDescriptor fd, const epoll::Events pollEvents)
Definition: service.cpp:85
This class waits on registered file descriptor for events.
Definition: event-poll.hpp:47
epoll::EventPoll & mEventPoll
Definition: service.hpp:241
Processor mProcessor
Definition: service.hpp:242
std::function< bool(PeerID peerID, std::shared_ptr< ReceivedDataType > &data, MethodResult::Pointer methodResult) > type
Definition: method-result.hpp:78
std::shared_ptr< ReceivedDataType > callSync(const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, unsigned int timeoutMS=5000)
Synchronous method call.
Definition: processor.hpp:659
Service(epoll::EventPoll &eventPoll, const std::string &path, const PeerCallback &addPeerCallback=nullptr, const PeerCallback &removePeerCallback=nullptr)
Constructs the Service, but doesn't start it.
Definition: service.cpp:36
void setRemovedPeerCallback(const PeerCallback &removedPeerCallback)
Set the callback called when connection to a peer is lost.
Definition: service.cpp:124
This class wraps communication via UX sockets.
Definition: processor.hpp:88
int FileDescriptor
Definition: types.hpp:42
Accepts new connections and passes the new socket to a callback.
Definition: acceptor.hpp:42
bool isHandled(const MethodID methodID)
Definition: service.cpp:142
bool isStarted()
Definition: service.cpp:71
MessageID callAsyncNonBlock(const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &process)
The same as callAsync, but not blocking on the state mutex.
Definition: processor.hpp:647
void signal(const MethodID methodID, const std::shared_ptr< SentDataType > &data)
Send a signal to the peer.
Definition: processor.hpp:722