This class wraps communication via UX sockets. More...
#include <processor.hpp>
Classes | |
struct | EmptyData |
struct | ErrorProtocolMessage |
struct | MessageHeader |
struct | MethodHandlers |
struct | PeerInfo |
struct | RegisterSignalsProtocolMessage |
struct | ReturnCallbacks |
struct | SignalHandlers |
Public Member Functions | |
Processor (epoll::EventPoll &eventPoll, const std::string &logName="", const PeerCallback &newPeerCallback=nullptr, const PeerCallback &removedPeerCallback=nullptr, const unsigned int maxNumberOfPeers=DEFAULT_MAX_NUMBER_OF_PEERS) | |
Constructs the Processor, but doesn't start it. More... | |
~Processor () | |
Processor (const Processor &)=delete | |
Processor (Processor &&)=delete | |
Processor & | operator= (const Processor &)=delete |
void | start () |
Start processing. More... | |
bool | isStarted () |
void | stop (bool wait) |
Stops the processing thread. 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... | |
PeerID | addPeer (const std::shared_ptr< Socket > &socketPtr) |
From now on socket is owned by the Processor object. More... | |
template<typename SentDataType , typename ReceivedDataType > | |
void | setMethodHandler (const MethodID methodID, const typename MethodHandler< SentDataType, ReceivedDataType >::type &process) |
Saves the callbacks connected to the method id. More... | |
template<typename ReceivedDataType > | |
void | setSignalHandler (const MethodID methodID, const typename SignalHandler< ReceivedDataType >::type &process) |
Saves the callbacks connected to the method id. More... | |
void | sendResult (const MethodID methodID, const PeerID &peerID, const MessageID &messageID, const std::shared_ptr< void > &data) |
Send result of the method. More... | |
void | sendError (const PeerID &peerID, const MessageID &messageID, const int errorCode, const std::string &message) |
Send error result of the method. More... | |
void | sendVoid (const MethodID methodID, const PeerID &peerID, const MessageID &messageID) |
Indicate that the method handler finished. 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 > | |
MessageID | callAsync (const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data, const typename ResultHandler< ReceivedDataType >::type &process) |
Asynchronous method call. More... | |
template<typename SentDataType , typename ReceivedDataType > | |
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. More... | |
template<typename SentDataType > | |
void | signal (const MethodID methodID, const std::shared_ptr< SentDataType > &data) |
Send a signal to the peer. More... | |
bool | handleLostConnection (const FileDescriptor fd) |
Removes one peer. More... | |
bool | handleInput (const FileDescriptor fd) |
Handles input from one peer. More... | |
bool | handleEvent () |
Handle one event from the internal event's queue. More... | |
FileDescriptor | getEventFD () |
Static Public Attributes | |
static const MethodID | RETURN_METHOD_ID = std::numeric_limits<MethodID>::max() |
Used to indicate a message with the return value. More... | |
static const MethodID | REGISTER_SIGNAL_METHOD_ID = std::numeric_limits<MethodID>::max() - 1 |
Indicates an Processor's internal request/broadcast to register a Signal. More... | |
static const MethodID | ERROR_METHOD_ID = std::numeric_limits<MethodID>::max() - 2 |
Error return message. More... | |
Private Types | |
enum | Event { Event::FINISH, Event::METHOD, Event::SIGNAL, Event::ADD_PEER, Event::REMOVE_PEER, Event::SEND_RESULT, Event::REMOVE_METHOD } |
typedef std::unique_lock < std::mutex > | Lock |
typedef RequestQueue< Event > ::Request | Request |
typedef std::vector< PeerInfo > | Peers |
Private Member Functions | |
template<typename SentDataType , typename ReceivedDataType > | |
void | setMethodHandlerInternal (const MethodID methodID, const typename MethodHandler< SentDataType, ReceivedDataType >::type &process) |
template<typename ReceivedDataType > | |
void | setSignalHandlerInternal (const MethodID methodID, const typename SignalHandler< ReceivedDataType >::type &handler) |
template<typename SentDataType > | |
void | signalInternal (const MethodID methodID, const PeerID &peerID, const std::shared_ptr< SentDataType > &data) |
bool | onMethodRequest (MethodRequest &request) |
bool | onSignalRequest (SignalRequest &request) |
bool | onAddPeerRequest (AddPeerRequest &request) |
bool | onRemovePeerRequest (RemovePeerRequest &request) |
bool | onSendResultRequest (SendResultRequest &request) |
bool | onRemoveMethodRequest (RemoveMethodRequest &request) |
bool | onFinishRequest (FinishRequest &request) |
bool | onReturnValue (Peers::iterator &peerIt, const MessageID &messageID) |
bool | onRemoteMethod (Peers::iterator &peerIt, const MethodID methodID, const MessageID &messageID, std::shared_ptr< MethodHandlers > methodCallbacks) |
bool | onRemoteSignal (Peers::iterator &peerIt, const MethodID methodID, const MessageID &messageID, std::shared_ptr< SignalHandlers > signalCallbacks) |
void | removePeerInternal (Peers::iterator peerIt, const std::exception_ptr &exceptionPtr) |
void | removePeerSyncInternal (const PeerID &peerID, Lock &lock) |
bool | onNewSignals (const PeerID &peerID, std::shared_ptr< RegisterSignalsProtocolMessage > &data) |
bool | onErrorSignal (const PeerID &peerID, std::shared_ptr< ErrorProtocolMessage > &data) |
Peers::iterator | getPeerInfoIterator (const FileDescriptor fd) |
Peers::iterator | getPeerInfoIterator (const PeerID &peerID) |
Private Attributes | |
epoll::EventPoll & | mEventPoll |
std::string | mLogPrefix |
RequestQueue< Event > | mRequestQueue |
bool | mIsRunning |
std::unordered_map< MethodID, std::shared_ptr < MethodHandlers > > | mMethodsCallbacks |
std::unordered_map< MethodID, std::shared_ptr < SignalHandlers > > | mSignalsCallbacks |
std::unordered_map< MethodID, std::list< PeerID > > | mSignalsPeers |
Peers | mPeerInfo |
std::unordered_map< MessageID, ReturnCallbacks > | mReturnCallbacks |
std::mutex | mStateMutex |
PeerCallback | mNewPeerCallback |
PeerCallback | mRemovedPeerCallback |
unsigned int | mMaxNumberOfPeers |
Friends | |
std::ostream & | operator<< (std::ostream &os, const Processor::Event &event) |
This class wraps communication via UX sockets.
It's intended to be used both in Client and Service classes. It uses a serialization mechanism from Config. Library user will only have to pass the types that each call will send and receive
Message format:
TODO:
|
private |
|
private |
|
private |
|
strongprivate |
cargo::ipc::Processor::Processor | ( | epoll::EventPoll & | eventPoll, |
const std::string & | logName = "" , |
||
const PeerCallback & | newPeerCallback = nullptr , |
||
const PeerCallback & | removedPeerCallback = nullptr , |
||
const unsigned int | maxNumberOfPeers = DEFAULT_MAX_NUMBER_OF_PEERS |
||
) |
cargo::ipc::Processor::~Processor | ( | ) |
|
delete |
|
delete |
From now on socket is owned by the Processor object.
Calls the newPeerCallback.
socketPtr | pointer to the new socket |
MessageID cargo::ipc::Processor::callAsync | ( | const MethodID | methodID, |
const PeerID & | peerID, | ||
const std::shared_ptr< SentDataType > & | data, | ||
const typename ResultHandler< ReceivedDataType >::type & | process | ||
) |
Asynchronous method call.
methodID | API dependent id of the method |
peerID | id of the peer |
data | data to sent |
process | callback processing the return data |
SentDataType | data type to send |
ReceivedDataType | data type to receive |
MessageID cargo::ipc::Processor::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.
methodID | API dependent id of the method |
peerID | id of the peer |
data | data to sent |
process | callback processing the return data |
SentDataType | data type to send |
ReceivedDataType | data type to receive |
std::shared_ptr< ReceivedDataType > cargo::ipc::Processor::callSync | ( | const MethodID | methodID, |
const PeerID & | peerID, | ||
const std::shared_ptr< SentDataType > & | data, | ||
unsigned int | timeoutMS = 5000 |
||
) |
Synchronous method call.
methodID | API dependent id of the method |
peerID | id of the peer |
data | data to send |
timeoutMS | optional, how long to wait for the return value before throw (milliseconds, default: 5000) |
SentDataType | data type to send |
ReceivedDataType | data type to receive |
FileDescriptor cargo::ipc::Processor::getEventFD | ( | ) |
|
private |
|
private |
bool cargo::ipc::Processor::handleEvent | ( | ) |
Handle one event from the internal event's queue.
bool cargo::ipc::Processor::handleInput | ( | const FileDescriptor | fd | ) |
Handles input from one peer.
Handler used in external polling.
fd | file description identifying the peer |
bool cargo::ipc::Processor::handleLostConnection | ( | const FileDescriptor | fd | ) |
Removes one peer.
Handler used in external polling.
fd | file description identifying the peer |
bool cargo::ipc::Processor::isHandled | ( | const MethodID | methodID | ) |
methodID | MethodID defined in the user's API |
bool cargo::ipc::Processor::isStarted | ( | ) |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
void cargo::ipc::Processor::removeMethod | ( | const MethodID | methodID | ) |
Removes the callback associated with specific method id.
methodID | API dependent id of the method |
|
private |
void cargo::ipc::Processor::sendError | ( | const PeerID & | peerID, |
const MessageID & | messageID, | ||
const int | errorCode, | ||
const std::string & | message | ||
) |
Send error result of the method.
peerID | id of the peer |
messageID | id of the message to which it replies |
errorCode | code of the error |
message | description of the error |
void cargo::ipc::Processor::sendResult | ( | const MethodID | methodID, |
const PeerID & | peerID, | ||
const MessageID & | messageID, | ||
const std::shared_ptr< void > & | data | ||
) |
Send result of the method.
Used for asynchronous communication, only internally.
methodID | API dependent id of the method |
peerID | id of the peer |
messageID | id of the message to which it replies |
data | data to send |
void cargo::ipc::Processor::sendVoid | ( | const MethodID | methodID, |
const PeerID & | peerID, | ||
const MessageID & | messageID | ||
) |
Indicate that the method handler finished.
methodID | API dependent id of the method |
peerID | id of the peer |
messageID | id of the message to which it replies |
void cargo::ipc::Processor::setMethodHandler | ( | const MethodID | methodID, |
const typename MethodHandler< SentDataType, ReceivedDataType >::type & | process | ||
) |
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.
methodID | API dependent id of the method |
process | data processing callback |
SentDataType | data type to send |
ReceivedDataType | data type to receive |
|
private |
void cargo::ipc::Processor::setNewPeerCallback | ( | const PeerCallback & | newPeerCallback | ) |
Set the callback called for each new connection to a peer.
newPeerCallback | the callback |
void cargo::ipc::Processor::setRemovedPeerCallback | ( | const PeerCallback & | removedPeerCallback | ) |
Set the callback called when connection to a peer is lost.
removedPeerCallback | the callback |
void cargo::ipc::Processor::setSignalHandler | ( | const MethodID | methodID, |
const typename SignalHandler< ReceivedDataType >::type & | process | ||
) |
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
methodID | API dependent id of the method |
process | data processing callback |
ReceivedDataType | data type to receive |
|
private |
void cargo::ipc::Processor::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
methodID | API dependent id of the method |
data | data to sent |
SentDataType | data type to send |
|
private |
void cargo::ipc::Processor::start | ( | ) |
Start processing.
void cargo::ipc::Processor::stop | ( | bool | wait | ) |
Stops the processing thread.
No incoming data will be handled after.
wait | does it block waiting for all internals to stop |
|
friend |
|
static |
Error return message.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
static |
Indicates an Processor's internal request/broadcast to register a Signal.
|
static |
Used to indicate a message with the return value.