Loading...
Searching...
No Matches
ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR > Class Template Reference

`#include <Acceptor.h>`

Inheritance diagram for ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >:

## Public Member Functions

| Acceptor (Reactor *r_)
| Default constructor.
virtual | ~Acceptor ()
| Do-nothing destructor.
virtual int | open (const Address &local_addr_)
| Initialize listener endpoint and Acceptor with Reactor.
virtual int | close (void)
| Close PEER_ACCEPTOR stream.
int | handle_read (int fd)
| Callback invoked by Reactor when new connection requests is detected.
virtual int | handle_close (int fd)
| Callback invoked by Reactor if PEER_ACCEPTOR stream went bad, or Reactor has been commanded to stop event processing.
Public Member Functions inherited from ASSA::EventHandler
| EventHandler ()
| Constructor.
virtual | ~EventHandler ()
| Virtual destructor.
virtual int | handle_write (int fd)
| Write handler callback.
virtual int | handle_except (int fd)
| Exception handler callback.
virtual int | handle_timeout (TimerId tid)
| Timeout handler callback.
virtual int | handle_signal (int signum_)
| Signal handler callback.
virtual void | resetState (void)
| A hook for derived class to reset internal state as needed.
void | set_id (const std::string &id_)
| Set EventHandler ID.
std::string | get_id () const
| Retrieve EventHandler ID.

## Protected Member Functions

virtual SERVICE_HANDLER * | makeServiceHandler (PEER_ACCEPTOR *sock_)
| Defines creation strategy for ServiceHandler.
virtual int | acceptServiceHandler (PEER_ACCEPTOR *&new_socket_)
| Default strategy is to accept new connection.
virtual int | activateServiceHandler (PEER_ACCEPTOR *new_socket_)
| Defines the concurrency strategy.

## Protected Attributes

PEER_ACCEPTOR | m_listenSocket
| Underlying communication stream.
Protected Attributes inherited from ASSA::EventHandler
std::string | m_id

## Private Attributes

Reactor * | m_reactor
| Reactor to use.

## Detailed Description

template<class SERVICE_HANDLER, class PEER_ACCEPTOR>
class ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >

Definition at line 43 of file Acceptor.h.

## Constructor & Destructor Documentation

## Acceptor()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::Acceptor | ( | Reactor * | r_ | ) |

inline

Default constructor.

Parameters

  r_ | Reactor to use

Definition at line 143 of file Acceptor.h.

: m_reactor (r_)
{
trace("Acceptor::Acceptor");
}
trace
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition Logger.h:429
ASSA::Acceptor::m_reactor
Reactor * m_reactor
Reactor to use.
Definition Acceptor.h:129

References trace.

## ~Acceptor()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::~Acceptor | ( |  | ) |

inline virtual

Do-nothing destructor.

Underlying PEER_ACCEPTOR stream will be closed during its own destruction sequence.

Definition at line 152 of file Acceptor.h.

{
trace("Acceptor::~Acceptor");
}

References trace.

## Member Function Documentation

## acceptServiceHandler()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

int ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::acceptServiceHandler | ( | PEER_ACCEPTOR *& | new_socket_ | ) |

inline protected virtual

Default strategy is to accept new connection.

Derived class can change this strategy by overloading this method.

Parameters

  new_socket_ | On return will have a pointer to newly created PEER_STREAM, or =0 if failed

Returns
  0 on success, -1 on error.

Definition at line 197 of file Acceptor.h.

{
trace("Acceptor::acceptServiceHandler");
new_socket_ = m_listenSocket.accept ();
return new_socket_ ? 0 : -1;
}
ASSA::Acceptor::m_listenSocket
PEER_ACCEPTOR m_listenSocket
Underlying communication stream.
Definition Acceptor.h:123

References trace.

## activateServiceHandler()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

int ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::activateServiceHandler | ( | PEER_ACCEPTOR * | new_socket_ | ) |

protected virtual

Defines the concurrency strategy.

Default is to create SERVICE_HANDLERin current process(thread), call its open() methid and let Reactor handle its I/O events. Derived class changes this strategy by overloading this class.

Parameters

  new_socket_ | [in] PEER_STREAM pointer to activate

Returns
  0 on success, -1 on error.

Definition at line 207 of file Acceptor.h.

{
trace("Acceptor::activateServiceHandler");
if (!new_socket_) {
return -1;
}
SH* sh = makeServiceHandler (new_socket_);
if (sh->open () < 0) {
sh->close ();
}
return 0;
}
SH
#define SH
Definition Acceptor.h:134
ASSA::Acceptor::makeServiceHandler
virtual SERVICE_HANDLER * makeServiceHandler(PEER_ACCEPTOR *sock_)
Defines creation strategy for ServiceHandler.
Definition Acceptor.h:188

References SH, and trace.

## close()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

int ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::close | ( | void |  | ) |

inline virtual

Close PEER_ACCEPTOR stream.

Returns
  0 on success, -1 on error.

Definition at line 160 of file Acceptor.h.

{
trace("Acceptor::close");
m_listenSocket.close ();
return 0;
}

References trace.

## handle_close()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

int ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::handle_close | ( | int | fd | ) |

inline virtual

Callback invoked by Reactor if PEER_ACCEPTOR stream went bad, or Reactor has been commanded to stop event processing.

This method should always return -1, if stream cannot be repared. Derived class can change this strategy by overloading this method. If called object is other then Reactor, an explicit call to Reactor::removeHandler (this->id()) is required. By default behavior, Acceptor will destroy itself.

Returns
  -1 always

Reimplemented from ASSA::EventHandler.

Definition at line 170 of file Acceptor.h.

{
trace("Acceptor::handle_close");
// Reactor::get_instance ()->removeHandler (this->id());
// NOT IMPLEMENTED: This spot requires validation
// whether Acceptor is created on the heap or in
// automatic memory.
DL ((REACT,"Deleted acceptor \"%s\"\n", get_id ().c_str ()));
delete this;
return -1;
}
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
ASSA::EventHandler::get_id
std::string get_id() const
Retrieve EventHandler ID.
Definition EventHandler.h:157
ASSA::REACT
@ REACT
Class Reactor/PrioriyQueue messages
Definition LogMask.h:39

References DL, ASSA::REACT, and trace.

## handle_read()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

int ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::handle_read | ( | int | fd | ) |

virtual

Callback invoked by Reactor when new connection requests is detected.

Default strategy is to accept ALL awaiting incoming connections at once. Derived class can change this strategy by overloading this method.

Returns
  0 on success, -1 on error. Returning -1 will effectively instruct Reactor to remove this Handler from the Reactor.

Reimplemented from ASSA::EventHandler.

Definition at line 251 of file Acceptor.h.

{
trace("Acceptor<>::handle_read");
FdSet mask;
timeval poll = {0, 0};
PA* new_socket = 0;
int fd = m_listenSocket.getHandler ();
if (fd != fd_) {
return -1;
}
do {
if ( acceptServiceHandler (new_socket) == -1 ) {
return -1;
}
if ( !activateServiceHandler (new_socket) == -1 ) {
return -1;
}
mask.reset ();
mask.setFd (fd);
}
while ((::select (fd+1, &mask, NULL, NULL, &poll) == 1));
return 0;
}
PA
#define PA
Definition Acceptor.h:135
ASSA::Acceptor::acceptServiceHandler
virtual int acceptServiceHandler(PEER_ACCEPTOR *&new_socket_)
Default strategy is to accept new connection.
Definition Acceptor.h:198
ASSA::Acceptor::activateServiceHandler
virtual int activateServiceHandler(PEER_ACCEPTOR *new_socket_)
Defines the concurrency strategy.
Definition Acceptor.h:208

References PA, ASSA::FdSet::reset(), ASSA::FdSet::setFd(), and trace.

## makeServiceHandler()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

SERVICE_HANDLER * ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::makeServiceHandler | ( | PEER_ACCEPTOR * | sock_ | ) |

inline protected virtual

Defines creation strategy for ServiceHandler.

Returns
  pointer to SERVICE_HANDLER

Definition at line 187 of file Acceptor.h.

{
trace("Acceptor<>::makeServiceHandler");
return new SERVICE_HANDLER (sock_);
}

References trace.

## open()

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

int ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::open | ( | const Address & | local_addr_ | ) |

virtual

Initialize listener endpoint and Acceptor with Reactor.

Derive classes can change this strategy by overloading this method.

Returns
  0 on success, -1 on error. An error can be if either PEER_ACCEPTOR's open() or bind() failed.

Definition at line 223 of file Acceptor.h.

{
trace("Acceptor::open");
if ( !m_listenSocket.open (local_addr_.getAddress ()->sa_family) ) {
return -1;
}
if ( !m_listenSocket.bind (local_addr_) ) {
return -1;
}
m_reactor->registerIOHandler (
this, m_listenSocket.getHandler (), READ_EVENT);
DL((TRACE,"Opened acceptor for fd=%d\n",
m_listenSocket.getHandler ()));
return 0;
}
ASSA::Reactor::registerIOHandler
bool registerIOHandler(EventHandler *eh_, handler_t fd_, EventType et_=RWE_EVENTS)
Register I/O Event handler with Reactor.
Definition Reactor.cpp:93
ASSA::READ_EVENT
@ READ_EVENT
Notify when there will be at least 1 byte available for reading from IO channel without blocking .
Definition EventHandler.h:36
ASSA::TRACE
@ TRACE
Function call trace
Definition LogMask.h:26

References DL, ASSA::Address::getAddress(), ASSA::READ_EVENT, trace, and ASSA::TRACE.

## Member Data Documentation

## m_listenSocket

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

PEER_ACCEPTOR ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::m_listenSocket

protected

Underlying communication stream.

Definition at line 123 of file Acceptor.h.

## m_reactor

template<class SERVICE_HANDLER , class PEER_ACCEPTOR >

Reactor* ASSA::Acceptor< SERVICE_HANDLER, PEER_ACCEPTOR >::m_reactor

private

Reactor to use.

Definition at line 129 of file Acceptor.h.

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

- Acceptor.h