0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | Private Attributes | List of all members
Hypertable::DispatchHandlerSynchronizer Class Reference

DispatchHandler class used to synchronize with response messages. More...

#include <DispatchHandlerSynchronizer.h>

Inheritance diagram for Hypertable::DispatchHandlerSynchronizer:
Inheritance graph
[legend]
Collaboration diagram for Hypertable::DispatchHandlerSynchronizer:
Collaboration graph
[legend]

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< EventPtrm_receive_queue
 Event queue. More...
 

Detailed Description

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.

Constructor & Destructor Documentation

DispatchHandlerSynchronizer::DispatchHandlerSynchronizer ( )

Constructor.

Initializes state.

Definition at line 39 of file DispatchHandlerSynchronizer.cc.

virtual Hypertable::DispatchHandlerSynchronizer::~DispatchHandlerSynchronizer ( )
inlinevirtual

Definition at line 77 of file DispatchHandlerSynchronizer.h.

Member Function Documentation

void DispatchHandlerSynchronizer::handle ( EventPtr event)
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.

Parameters
eventSmart 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.

Returns
true if CONNECTION_ESTABLISHED event received, false if DISCONNECT event received.
Exceptions
Exceptionwith 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.

Parameters
eventSmart pointer to event object
Returns
true if next returned event is type MESSAGE and contains status Error::OK, false otherwise

Definition at line 50 of file DispatchHandlerSynchronizer.cc.

Member Data Documentation

std::condition_variable Hypertable::DispatchHandlerSynchronizer::m_cond
private

Condition variable for signalling change in queue state.

Definition at line 119 of file DispatchHandlerSynchronizer.h.

std::mutex Hypertable::DispatchHandlerSynchronizer::m_mutex
private

Mutex for serializing concurrent access.

Definition at line 116 of file DispatchHandlerSynchronizer.h.

std::queue<EventPtr> Hypertable::DispatchHandlerSynchronizer::m_receive_queue
private

Event queue.

Definition at line 122 of file DispatchHandlerSynchronizer.h.


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