DispatchHandler class used to synchronize with response messages. More...
#include <DispatchHandlerSynchronizer.h>
Public Member Functions | |
DispatchHandlerSynchronizer () | |
Constructor. More... | |
virtual | ~DispatchHandlerSynchronizer () |
virtual void | handle (EventPtr &event) |
Event Dispatch method. More... | |
bool | wait_for_reply (EventPtr &event) |
This method is used by a client to synchronize. More... | |
bool | wait_for_connection () |
Waits for CONNECTION_ESTABLISHED event. More... | |
Public Member Functions inherited from Hypertable::DispatchHandler | |
virtual | ~DispatchHandler () |
Destructor. More... | |
Private Attributes | |
std::mutex | m_mutex |
Mutex for serializing concurrent access. More... | |
std::condition_variable | m_cond |
Condition variable for signalling change in queue state. More... | |
std::queue< EventPtr > | m_receive_queue |
Event queue. More... | |
DispatchHandler class used to synchronize with response messages.
This class is a specialization of DispatchHandler that is used to synchronize with responses resulting from previously sent request messages. It contains a queue of events (response events) and a condition variable that gets signalled when an event gets put on the queue.
Example usage:
{ DispatchHandlerSynchronizer sync_handler; EventPtr event; CommBufPtr cbp(... create protocol message here ...); if ((error = m_comm->send_request(m_addr, cbp, &sync_handler)) != Error::OK) { // log error message here ... return error; } if (!sync_handler.wait_for_reply(event)) // log error message here ... error = (int)Protocol::response_code(event); return error; }
Definition at line 70 of file DispatchHandlerSynchronizer.h.
DispatchHandlerSynchronizer::DispatchHandlerSynchronizer | ( | ) |
|
inlinevirtual |
Definition at line 77 of file DispatchHandlerSynchronizer.h.
|
virtual |
Event Dispatch method.
This gets called by the AsyncComm layer when an event occurs in response to a previously sent request that was supplied with this dispatch handler. It pushes the event onto the event queue and signals (notify_one) the condition variable.
event | Smart pointer to event object |
Implements Hypertable::DispatchHandler.
Definition at line 43 of file DispatchHandlerSynchronizer.cc.
bool DispatchHandlerSynchronizer::wait_for_connection | ( | ) |
Waits for CONNECTION_ESTABLISHED event.
This function waits for an event to arrive on m_receive_queue and if it is an ERROR event, it throws an exception, if it is a DISCONNECT event it returns false, and if it is a CONNECTION_ESTABLISHED event, it returns true.
Exception | with code set to ERROR event error code. |
Definition at line 66 of file DispatchHandlerSynchronizer.cc.
bool DispatchHandlerSynchronizer::wait_for_reply | ( | EventPtr & | event | ) |
This method is used by a client to synchronize.
The client sends a request via the AsyncComm layer with this object as the dispatch handler. It then calls this method to wait for the response (or timeout event). This method just blocks on the condition variable until the event queue is non-empty and then removes and returns the head of the queue.
event | Smart pointer to event object |
Definition at line 50 of file DispatchHandlerSynchronizer.cc.
|
private |
Condition variable for signalling change in queue state.
Definition at line 119 of file DispatchHandlerSynchronizer.h.
|
private |
Mutex for serializing concurrent access.
Definition at line 116 of file DispatchHandlerSynchronizer.h.
|
private |
Event queue.
Definition at line 122 of file DispatchHandlerSynchronizer.h.