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::Client Class Reference

This class wraps communication via UX sockets for client applications. More...

#include <client.hpp>

Public Member Functions

 Client (epoll::EventPoll &eventPoll, const std::string &serverPath)
 Constructs the Client, but doesn't start it. More...
 
 ~Client ()
 
 Client (const Client &)=delete
 Copying Client class is prohibited. More...
 
Clientoperator= (const Client &)=delete
 Copying Client class is prohibited. More...
 
void start ()
 Starts processing. More...
 
bool isStarted ()
 Is the communication thread running? More...
 
void stop (bool wait=true)
 Stops processing. 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 callback connected to the method id. More...
 
template<typename ReceivedDataType >
void setSignalHandler (const MethodID methodID, const typename SignalHandler< ReceivedDataType >::type &signal)
 Saves the callback 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 std::shared_ptr< SentDataType > &data, unsigned int timeoutMS=5000)
 Synchronous method call. More...
 
template<typename SentDataType , typename ReceivedDataType >
void callAsync (const MethodID methodID, 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 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
 
PeerID mServiceID
 
Processor mProcessor
 
std::string mSocketPath
 

Detailed Description

This class wraps communication via UX sockets for client applications.

It uses serialization mechanism from Cargo.

// eventPoll - epoll wrapper class
// address - server socket address
cargo::ipc::Client myClient(examplePoll, address);
myClient.start(); // connect to the service
// call method synchronously
const auto result = mClient.callSync<api::Void, api::ZoneIds>(
std::make_shared<api::Void>());
// call method asynchronously
// first: declare lambda function to call on completion
auto asyncResult = [result](cargo::ipc::Result<api::Void>&& out) {
if (out.isValid()) {
// got successful response!
}
};
std::string id = "example_zone_id";
mClient.callAsync<api::ZoneId, api::Void>(api::cargo::ipc::METHOD_DESTROY_ZONE,
std::make_shared<api::ZoneId>(api::ZoneId{id}),
asyncResult);
See Also
libCargo
cargo::ipc::Processor
cargo::ipc::epoll::EventPoll

Constructor & Destructor Documentation

Client::Client ( epoll::EventPoll eventPoll,
const std::string &  serverPath 
)

Constructs the Client, but doesn't start it.

Once set-up, call start() to connect client to the server.

Parameters
eventPollevent poll
serverPathpath to the server's socket
Client::~Client ( )
cargo::ipc::Client::Client ( const Client )
delete

Copying Client class is prohibited.

Member Function Documentation

template<typename SentDataType , typename ReceivedDataType >
void Client::callAsync ( const MethodID  methodID,
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
datadata to send
resultCallbackcallback processing the return data
Template Parameters
SentDataTypedata type to send
ReceivedDataTypedata type to receive
template<typename SentDataType , typename ReceivedDataType >
void Client::callAsyncFromCallback ( const MethodID  methodID,
const std::shared_ptr< SentDataType > &  data,
const typename ResultHandler< ReceivedDataType >::type &  resultCallback = nullptr 
)
template<typename SentDataType , typename ReceivedDataType >
std::shared_ptr< ReceivedDataType > Client::callSync ( const MethodID  methodID,
const std::shared_ptr< SentDataType > &  data,
unsigned int  timeoutMS = 5000 
)

Synchronous method call.

Parameters
methodIDAPI dependent id of the method
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 Client::handle ( const FileDescriptor  fd,
const epoll::Events  pollEvents 
)
private
bool Client::isHandled ( const MethodID  methodID)
Parameters
methodIDMethodID defined in the user's API
Returns
is methodID handled by a signal or method
bool Client::isStarted ( )

Is the communication thread running?

Returns
is the communication thread running
Client& cargo::ipc::Client::operator= ( const Client )
delete

Copying Client class is prohibited.

void Client::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 Client::setMethodHandler ( const MethodID  methodID,
const typename MethodHandler< SentDataType, ReceivedDataType >::type &  method 
)

Saves the callback connected to the method id.

When a message with the given method id is received the data will be parsed and passed to this callback.

Parameters
methodIDAPI dependent id of the method
methodmethod handling implementation
Template Parameters
SentDataTypedata type to send
ReceivedDataTypedata type to receive
void Client::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 Client::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 Client::setSignalHandler ( const MethodID  methodID,
const typename SignalHandler< ReceivedDataType >::type &  signal 
)

Saves the callback connected to the method id.

When a message with the given method id is received the data will be parsed and passed to this callback.

Parameters
methodIDAPI dependent id of the method
signaldata processing callback
Template Parameters
ReceivedDataTypedata type to receive
template<typename SentDataType >
void Client::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 Client::start ( )

Starts processing.

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

Stops processing.

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

Member Data Documentation

epoll::EventPoll& cargo::ipc::Client::mEventPoll
private
Processor cargo::ipc::Client::mProcessor
private
PeerID cargo::ipc::Client::mServiceID
private
std::string cargo::ipc::Client::mSocketPath
private

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