Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
client.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 
26 #ifndef CARGO_IPC_CLIENT_HPP
27 #define CARGO_IPC_CLIENT_HPP
28 
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 
73 class Client {
74 public:
82  Client(epoll::EventPoll& eventPoll, const std::string& serverPath);
83  ~Client();
84 
88  Client(const Client&) = delete;
92  Client& operator=(const Client&) = delete;
93 
98  void start();
99 
105  bool isStarted();
106 
112  void stop(bool wait = true);
113 
120  void setNewPeerCallback(const PeerCallback& newPeerCallback);
121 
128  void setRemovedPeerCallback(const PeerCallback& removedPeerCallback);
129 
140  template<typename SentDataType, typename ReceivedDataType>
141  void setMethodHandler(const MethodID methodID,
143 
153  template<typename ReceivedDataType>
154  void setSignalHandler(const MethodID methodID,
156 
164  void removeMethod(const MethodID methodID);
165 
170  bool isHandled(const MethodID methodID);
171 
182  template<typename SentDataType, typename ReceivedDataType>
183  std::shared_ptr<ReceivedDataType> callSync(const MethodID methodID,
184  const std::shared_ptr<SentDataType>& data,
185  unsigned int timeoutMS = 5000);
186 
197  template<typename SentDataType, typename ReceivedDataType>
198  void callAsync(const MethodID methodID,
199  const std::shared_ptr<SentDataType>& data,
200  const typename ResultHandler<ReceivedDataType>::type& resultCallback = nullptr);
201 
202 
203  template<typename SentDataType, typename ReceivedDataType>
204  void callAsyncFromCallback(const MethodID methodID,
205  const std::shared_ptr<SentDataType>& data,
206  const typename ResultHandler<ReceivedDataType>::type& resultCallback = nullptr);
207 
217  template<typename SentDataType>
218  void signal(const MethodID methodID,
219  const std::shared_ptr<SentDataType>& data);
220 
221 private:
225  std::string mSocketPath;
226 
227  void handle(const FileDescriptor fd, const epoll::Events pollEvents);
228 
229 };
230 
231 template<typename SentDataType, typename ReceivedDataType>
232 void Client::setMethodHandler(const MethodID methodID,
234 {
235  LOGS("Client setMethodHandler, methodID: " << methodID);
236  mProcessor.setMethodHandler<SentDataType, ReceivedDataType>(methodID, method);
237 }
238 
239 template<typename ReceivedDataType>
240 void Client::setSignalHandler(const MethodID methodID,
241  const typename SignalHandler<ReceivedDataType>::type& handler)
242 {
243  LOGS("Client setSignalHandler, methodID: " << methodID);
244  mProcessor.setSignalHandler<ReceivedDataType>(methodID, handler);
245 }
246 
247 template<typename SentDataType, typename ReceivedDataType>
248 std::shared_ptr<ReceivedDataType> Client::callSync(const MethodID methodID,
249  const std::shared_ptr<SentDataType>& data,
250  unsigned int timeoutMS)
251 {
252  LOGS("Client callSync, methodID: " << methodID << ", timeoutMS: " << timeoutMS);
253  return mProcessor.callSync<SentDataType, ReceivedDataType>(methodID, mServiceID, data, timeoutMS);
254 }
255 
256 template<typename SentDataType, typename ReceivedDataType>
257 void Client::callAsync(const MethodID methodID,
258  const std::shared_ptr<SentDataType>& data,
259  const typename ResultHandler<ReceivedDataType>::type& resultCallback)
260 {
261  LOGS("Client callAsync, methodID: " << methodID);
262  mProcessor.callAsync<SentDataType,
263  ReceivedDataType>(methodID,
264  mServiceID,
265  data,
266  resultCallback);
267 }
268 
269 template<typename SentDataType, typename ReceivedDataType>
271  const std::shared_ptr<SentDataType>& data,
272  const typename ResultHandler<ReceivedDataType>::type& resultCallback)
273 {
274  LOGS("Client callAsyncFromCallback, methodID: " << methodID);
275  mProcessor.callAsyncNonBlock<SentDataType,
276  ReceivedDataType>(methodID,
277  mServiceID,
278  data,
279  resultCallback);
280 }
281 
282 template<typename SentDataType>
283 void Client::signal(const MethodID methodID,
284  const std::shared_ptr<SentDataType>& data)
285 {
286  LOGS("Client signal, methodID: " << methodID);
287  mProcessor.signal<SentDataType>(methodID, data);
288 }
289 
290 } // namespace ipc
291 } // namespace cargo
292 
293 #endif // CARGO_IPC_CLIENT_HPP
C++ epoll wrapper.
std::function< void(Result< Data > &&) > type
Definition: result.hpp:73
void stop(bool wait=true)
Stops processing.
Definition: client.cpp:74
void callAsync(const MethodID methodID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &resultCallback=nullptr)
Asynchronous method call.
Definition: client.hpp:257
#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
void signal(const MethodID methodID, const std::shared_ptr< SentDataType > &data)
Send a signal to the peer.
Definition: client.hpp:283
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
Client(epoll::EventPoll &eventPoll, const std::string &serverPath)
Constructs the Client, but doesn't start it.
Definition: client.cpp:34
Types definitions.
void setNewPeerCallback(const PeerCallback &newPeerCallback)
Set the callback called for each new connection to a peer.
Definition: client.cpp:103
Class for storing result of a method - data or exception.
void setRemovedPeerCallback(const PeerCallback &removedPeerCallback)
Set the callback called when connection to a peer is lost.
Definition: client.cpp:118
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
void removeMethod(const MethodID methodID)
Removes the callback associated with specific method id.
Definition: client.cpp:130
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
std::shared_ptr< ReceivedDataType > callSync(const MethodID methodID, const std::shared_ptr< SentDataType > &data, unsigned int timeoutMS=5000)
Synchronous method call.
Definition: client.hpp:248
std::function< bool(PeerID peerID, std::shared_ptr< ReceivedDataType > &data)> type
Definition: types.hpp:99
char data[368]
Definition: initctl.cpp:41
PeerID mServiceID
Definition: client.hpp:223
void start()
Starts processing.
Definition: client.cpp:55
void setMethodHandler(const MethodID methodID, const typename MethodHandler< SentDataType, ReceivedDataType >::type &method)
Saves the callback connected to the method id.
Definition: client.hpp:232
bool isHandled(const MethodID methodID)
Definition: client.cpp:136
void setMethodHandler(const MethodID methodID, const typename MethodHandler< SentDataType, ReceivedDataType >::type &process)
Saves the callbacks connected to the method id.
Definition: processor.hpp:560
Client & operator=(const Client &)=delete
Copying Client class is prohibited.
epoll::EventPoll & mEventPoll
Definition: client.hpp:222
void handle(const FileDescriptor fd, const epoll::Events pollEvents)
Definition: client.cpp:83
Processor mProcessor
Definition: client.hpp:224
bool isStarted()
Is the communication thread running?
Definition: client.cpp:69
unsigned int MethodID
Definition: types.hpp:43
unsigned int Events
bitmask of EPOLL* constants
Definition: events.hpp:39
This class wraps communication via UX sockets for client applications.
Definition: client.hpp:73
This class waits on registered file descriptor for events.
Definition: event-poll.hpp:47
void callAsyncFromCallback(const MethodID methodID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &resultCallback=nullptr)
Definition: client.hpp:270
std::function< bool(PeerID peerID, std::shared_ptr< ReceivedDataType > &data, MethodResult::Pointer methodResult) > type
Definition: method-result.hpp:78
std::string mSocketPath
Definition: client.hpp:225
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
This class wraps communication via UX sockets.
Definition: processor.hpp:88
int FileDescriptor
Definition: types.hpp:42
void setSignalHandler(const MethodID methodID, const typename SignalHandler< ReceivedDataType >::type &signal)
Saves the callback connected to the method id.
Definition: client.hpp:240
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
~Client()
Definition: client.cpp:45
void signal(const MethodID methodID, const std::shared_ptr< SentDataType > &data)
Send a signal to the peer.
Definition: processor.hpp:722