Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cargo::ipc::Service Class Reference

This class wraps communication via UX sockets. More...

#include <service.hpp>

Public Member Functions

 Service (epoll::EventPoll &eventPoll, const std::string &path, const PeerCallback &addPeerCallback=nullptr, const PeerCallback &removePeerCallback=nullptr)
 Constructs the Service, but doesn't start it. More...
 
virtual ~Service ()
 
 Service (const Service &)=delete
 Copying Service class is prohibited. More...
 
Serviceoperator= (const Service &)=delete
 Copying Service class is prohibited. More...
 
void start ()
 Starts processing. More...
 
bool isStarted ()
 
void stop (bool wait=true)
 Stops all working threads. More...
 
void setNewPeerCallback (const PeerCallback &newPeerCallback)
 Set the callback called for each new connection to a peer. More...
 
void setRemovedPeerCallback (const PeerCallback &removedPeerCallback)
 Set the callback called when connection to a peer is lost. More...
 
template<typename SentDataType , typename ReceivedDataType >
void setMethodHandler (const MethodID methodID, const typename MethodHandler< SentDataType, ReceivedDataType >::type &method)
 Saves the callbacks connected to the method id. More...
 
template<typename ReceivedDataType >
void setSignalHandler (const MethodID methodID, const typename SignalHandler< ReceivedDataType >::type &handler)
 Saves the callbacks connected to the method id. More...
 
void removeMethod (const MethodID methodID)
 Removes the callback associated with specific method id. More...
 
bool isHandled (const MethodID methodID)
 
template<typename SentDataType , typename ReceivedDataType >
std::shared_ptr< ReceivedDataType > callSync (const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, unsigned int timeoutMS=5000)
 Synchronous method call. More...
 
template<typename SentDataType , typename ReceivedDataType >
void callAsync (const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &resultCallback=nullptr)
 Asynchronous method call. More...
 
template<typename SentDataType , typename ReceivedDataType >
void callAsyncFromCallback (const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &resultCallback=nullptr)
 
template<typename SentDataType >
void signal (const MethodID methodID, const std::shared_ptr< SentDataType > &data)
 Send a signal to the peer. More...
 

Private Member Functions

void handle (const FileDescriptor fd, const epoll::Events pollEvents)
 

Private Attributes

epoll::EventPollmEventPoll
 
Processor mProcessor
 
Acceptor mAcceptor
 

Detailed Description

This class wraps communication via UX sockets.

It uses serialization mechanism from Config.

// eventPoll - epoll wrapper class
// address - server socket address
// create callbacks for connected / disconnected events
cargo::ipc::PeerCallback connectedCallback = [this](const cargo::ipc::PeerID peerID,
// new connection came!
};
cargo::ipc::PeerCallback disconnectedCallback = [this](const cargo::ipc::PeerID peerID,
// connection disconnected!
};
// create the service
cargo::ipc::Service myService(eventPoll, "/tmp/example_service.socket",
connectedCallback, disconnectedCallback));
// add example method handler
auto exampleMethodHandler = [&](const PeerID, std::shared_ptr<RecvData>& data, MethodResult::Pointer methodResult) {
// got example method call! Incoming data in "data" argument
// respond with some data
auto returnData = std::make_shared<SendData>(data->intVal);
methodResult->set(returnData);
};
const MethodID exampleMethodID = 1234;
myService.setMethodHandler<SendData, RecvData>(exampleMethodID, exampleMethodHandler);
myService.start(); // start the service, clients may connect via /tmp/example_service.socket
See Also
libCargo
cargo::ipc::Processor

Constructor & Destructor Documentation

cargo::ipc::Service::Service ( epoll::EventPoll eventPoll,
const std::string &  path,
const PeerCallback addPeerCallback = nullptr,
const PeerCallback removePeerCallback = nullptr 
)

Constructs the Service, but doesn't start it.

The object is ready to add methods. Once set-up, call start() to start the service.

Parameters
eventPollevent poll
pathpath to the socket
addPeerCallbackoptional on new peer connection callback
removePeerCallbackoptional on peer removal callback
cargo::ipc::Service::~Service ( )
virtual
cargo::ipc::Service::Service ( const Service )
delete

Copying Service class is prohibited.

Member Function Documentation

template<typename SentDataType , typename ReceivedDataType >
void cargo::ipc::Service::callAsync ( const MethodID  methodID,
const PeerID peerID,
const std::shared_ptr< SentDataType > &  data,
const typename ResultHandler< ReceivedDataType >::type &  resultCallback = nullptr 
)

Asynchronous method call.

The return callback will be called on return data arrival. It will be run in the PROCESSOR thread.

Parameters
methodIDAPI dependent id of the method
peerIDid of the peer
datadata to send
resultCallbackcallback processing the return data
Template Parameters
SentDataTypedata type to send
ReceivedDataTypedata type to receive
template<typename SentDataType , typename ReceivedDataType >
void cargo::ipc::Service::callAsyncFromCallback ( const MethodID  methodID,
const PeerID peerID,
const std::shared_ptr< SentDataType > &  data,
const typename ResultHandler< ReceivedDataType >::type &  resultCallback = nullptr 
)
template<typename SentDataType , typename ReceivedDataType >
std::shared_ptr< ReceivedDataType > cargo::ipc::Service::callSync ( const MethodID  methodID,
const PeerID peerID,
const std::shared_ptr< SentDataType > &  data,
unsigned int  timeoutMS = 5000 
)

Synchronous method call.

Parameters
methodIDAPI dependent id of the method
peerIDid of the peer
datadata to send
timeoutMSoptional, how long to wait for the return value before throw (milliseconds, default: 5000)
Template Parameters
SentDataTypedata type to send
ReceivedDataTypedata type to receive
Returns
pointer to the call result data
void cargo::ipc::Service::handle ( const FileDescriptor  fd,
const epoll::Events  pollEvents 
)
private
bool cargo::ipc::Service::isHandled ( const MethodID  methodID)
Parameters
methodIDMethodID defined in the user's API
Returns
is methodID handled by a signal or method
bool cargo::ipc::Service::isStarted ( )
Returns
is the communication thread running
Service& cargo::ipc::Service::operator= ( const Service )
delete

Copying Service class is prohibited.

void cargo::ipc::Service::removeMethod ( const MethodID  methodID)

Removes the callback associated with specific method id.

Parameters
methodIDAPI dependent id of the method
See Also
setMethodHandler()
setSignalHandler()
template<typename SentDataType , typename ReceivedDataType >
void cargo::ipc::Service::setMethodHandler ( const MethodID  methodID,
const typename MethodHandler< SentDataType, ReceivedDataType >::type &  method 
)

Saves the callbacks connected to the method id.

When a message with the given method id is received, the data will be passed to the serialization callback through file descriptor.

Then the process callback will be called with the parsed data.

Parameters
methodIDAPI dependent id of the method
methoddata processing callback
Template Parameters
SentDataTypedata type to send
ReceivedDataTypedata type to receive
void cargo::ipc::Service::setNewPeerCallback ( const PeerCallback newPeerCallback)

Set the callback called for each new connection to a peer.

Parameters
newPeerCallbackthe callback to call on new connection event
Note
if callback is already set, it will be overridden
void cargo::ipc::Service::setRemovedPeerCallback ( const PeerCallback removedPeerCallback)

Set the callback called when connection to a peer is lost.

Parameters
removedPeerCallbackthe callback to call on peer disconnected event
Note
if callback is already set, it will be overridden
template<typename ReceivedDataType >
void cargo::ipc::Service::setSignalHandler ( const MethodID  methodID,
const typename SignalHandler< ReceivedDataType >::type &  handler 
)

Saves the callbacks connected to the method id.

When a message with the given method id is received, the data will be passed to the serialization callback through file descriptor.

Then the process callback will be called with the parsed data. There is no return data to send back.

Adding signal sends a registering message to all peers

Parameters
methodIDAPI dependent id of the method
handlerdata processing callback
Template Parameters
ReceivedDataTypedata type to receive
template<typename SentDataType >
void cargo::ipc::Service::signal ( const MethodID  methodID,
const std::shared_ptr< SentDataType > &  data 
)

Send a signal to the peer.

There is no return value from the peer Sends any data only if a peer registered this a signal

Parameters
methodIDAPI dependent id of the method
datadata to send
Template Parameters
SentDataTypedata type to send
void cargo::ipc::Service::start ( )

Starts processing.

Note
if the service is already running, it quits immediately (no exception thrown)
void cargo::ipc::Service::stop ( bool  wait = true)

Stops all working threads.

Parameters
waitshould the call block while waiting for all internals to stop? By default true - do block.

Member Data Documentation

Acceptor cargo::ipc::Service::mAcceptor
private
epoll::EventPoll& cargo::ipc::Service::mEventPoll
private
Processor cargo::ipc::Service::mProcessor
private

The documentation for this class was generated from the following files: