25 #ifndef CARGO_IPC_SERVICE_HPP
26 #define CARGO_IPC_SERVICE_HPP
91 const std::string& path,
121 void stop(
bool wait =
true);
151 template<
typename SentDataType,
typename ReceivedDataType>
169 template<
typename ReceivedDataType>
199 template<
typename SentDataType,
typename ReceivedDataType>
202 const std::shared_ptr<SentDataType>&
data,
203 unsigned int timeoutMS = 5000);
216 template<
typename SentDataType,
typename ReceivedDataType>
219 const std::shared_ptr<SentDataType>& data,
222 template<
typename SentDataType,
typename ReceivedDataType>
225 const std::shared_ptr<SentDataType>& data,
237 template<
typename SentDataType>
239 const std::shared_ptr<SentDataType>& data);
249 template<
typename SentDataType,
typename ReceivedDataType>
253 LOGS(
"Service setMethodHandler, methodID " << methodID);
257 template<
typename ReceivedDataType>
261 LOGS(
"Service setSignalHandler, methodID " << methodID);
265 template<
typename SentDataType,
typename ReceivedDataType>
268 const std::shared_ptr<SentDataType>&
data,
269 unsigned int timeoutMS)
271 LOGS(
"Service callSync, methodID: " << methodID
272 <<
", peerID: " << peerID
273 <<
", timeoutMS: " << timeoutMS);
277 template<
typename SentDataType,
typename ReceivedDataType>
280 const std::shared_ptr<SentDataType>&
data,
283 LOGS(
"Service callAsync, methodID: " << methodID <<
", peerID: " << peerID);
285 ReceivedDataType>(methodID,
291 template<
typename SentDataType,
typename ReceivedDataType>
294 const std::shared_ptr<SentDataType>&
data,
297 LOGS(
"Service callAsyncFromCallback, methodID: " << methodID <<
", peerID: " << peerID);
299 ReceivedDataType>(methodID,
306 template<
typename SentDataType>
308 const std::shared_ptr<SentDataType>&
data)
310 LOGS(
"Service signal, methodID: " << methodID);
317 #endif // CARGO_IPC_SERVICE_HPP
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
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