Loading...
Searching...
No Matches
Acceptor.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------
// Acceptor.h
//------------------------------------------------------------------------
// Copyright (C) 1999 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------
#ifndef ACCEPTOR_H
#define ACCEPTOR_H
#include "assa/Logger.h"
#include "assa/EventHandler.h"
#include "assa/Address.h"
#include "assa/Reactor.h"
#include "assa/ServiceHandler.h"
namespace ASSA {
template<class SERVICE_HANDLER, class PEER_ACCEPTOR>
class Acceptor : public virtual EventHandler
{
public:
Acceptor (Reactor* r_);
virtual ~Acceptor ();
virtual int open (const Address& local_addr_);
virtual int close (void);
int handle_read (int fd);
virtual int handle_close (int fd);
protected:
virtual SERVICE_HANDLER* makeServiceHandler (PEER_ACCEPTOR* sock_);
virtual int acceptServiceHandler (PEER_ACCEPTOR*& new_socket_);
virtual int activateServiceHandler (PEER_ACCEPTOR* new_socket_);
protected:
PEER_ACCEPTOR m_listenSocket;
private:
Reactor* m_reactor;
};
// Convenience definitions
#define SH SERVICE_HANDLER
#define PA PEER_ACCEPTOR
//------------------------------------------------------------------------------
// Template member functions definitions
//------------------------------------------------------------------------------
template<class SH, class PA>
inline
Acceptor<SH, PA>::
Acceptor (Reactor* r_)
: m_reactor (r_)
{
trace("Acceptor::Acceptor");
}
template<class SH, class PA>
inline
Acceptor<SH, PA>::
~Acceptor ()
{
trace("Acceptor::~Acceptor");
}
template<class SH, class PA>
inline int
Acceptor<SH, PA>::
close (void)
{
trace("Acceptor::close");
m_listenSocket.close ();
return 0;
}
template<class SH, class PA>
inline int
Acceptor<SH, PA>::
handle_close (int /* fd */)
{
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;
}
template<class SH, class PA>
inline SERVICE_HANDLER*
Acceptor<SH, PA>::
makeServiceHandler (PEER_ACCEPTOR* sock_)
{
trace("Acceptor<>::makeServiceHandler");
return new SERVICE_HANDLER (sock_);
}
template<class SH, class PA>
inline int
Acceptor<SH, PA>::
acceptServiceHandler (PEER_ACCEPTOR*& new_socket_)
{
trace("Acceptor::acceptServiceHandler");
new_socket_ = m_listenSocket.accept ();
return new_socket_ ? 0 : -1;
}
template<class SH, class PA> int
Acceptor<SH, PA>::
activateServiceHandler (PA* new_socket_)
{
trace("Acceptor::activateServiceHandler");
if (!new_socket_) {
return -1;
}
SH* sh = makeServiceHandler (new_socket_);
if (sh->open () < 0) {
sh->close ();
}
return 0;
}
template<class SH, class PA> int
Acceptor<SH, PA>::
open (const Address& local_addr_)
{
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;
}
//------------------------------------------------------------------------------
// Accept all connections waiting in listen queue at once. This avoids going
// through Reactor's event loop for each new connection.
//------------------------------------------------------------------------------
template <class SH, class PA> int
Acceptor<SH, PA>::
handle_read (int fd_)
{
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;
}
} // end namespace ASSA
#endif /* ACCEPTOR_H */
SH
#define SH
Definition Acceptor.h:134
PA
#define PA
Definition Acceptor.h:135
Address.h
Address is an abstraction for INET or UNIX-domain address data type.
EventHandler.h
An abstract interface for handling I/O events, timers, and such.
Logger.h
An abstraction to message logging facility.
trace
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition Logger.h:429
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
Reactor.h
An implementation of Reactor pattern.
ServiceHandler.h
This abstract class provides generic interface for processing services.
ASSA::Acceptor
Definition Acceptor.h:44
ASSA::Acceptor::acceptServiceHandler
virtual int acceptServiceHandler(PEER_ACCEPTOR *&new_socket_)
Default strategy is to accept new connection.
Definition Acceptor.h:198
ASSA::Acceptor::m_listenSocket
PEER_ACCEPTOR m_listenSocket
Underlying communication stream.
Definition Acceptor.h:123
ASSA::Acceptor::handle_read
int handle_read(int fd)
Callback invoked by Reactor when new connection requests is detected.
Definition Acceptor.h:252
ASSA::Acceptor::makeServiceHandler
virtual SERVICE_HANDLER * makeServiceHandler(PEER_ACCEPTOR *sock_)
Defines creation strategy for ServiceHandler.
Definition Acceptor.h:188
ASSA::Acceptor::handle_close
virtual int handle_close(int fd)
Callback invoked by Reactor if PEER_ACCEPTOR stream went bad, or Reactor has been commanded to stop e...
Definition Acceptor.h:171
ASSA::Acceptor::m_reactor
Reactor * m_reactor
Reactor to use.
Definition Acceptor.h:129
ASSA::Acceptor::close
virtual int close(void)
Close PEER_ACCEPTOR stream.
Definition Acceptor.h:161
ASSA::Acceptor::~Acceptor
virtual ~Acceptor()
Do-nothing destructor.
Definition Acceptor.h:153
ASSA::Acceptor::Acceptor
Acceptor(Reactor *r_)
Default constructor.
Definition Acceptor.h:144
ASSA::Acceptor::activateServiceHandler
virtual int activateServiceHandler(PEER_ACCEPTOR *new_socket_)
Defines the concurrency strategy.
Definition Acceptor.h:208
ASSA::Acceptor::open
virtual int open(const Address &local_addr_)
Initialize listener endpoint and Acceptor with Reactor.
Definition Acceptor.h:224
ASSA::Address
Definition Address.h:51
ASSA::Address::getAddress
virtual SA * getAddress() const =0
Retrieve pointer to the address structure.
ASSA::EventHandler
EventHandler class.
Definition EventHandler.h:103
ASSA::FdSet
Class FdSet.
Definition FdSet.h:52
ASSA::FdSet::reset
void reset()
Reset every bit in the set (OFF).
Definition FdSet.cpp:90
ASSA::FdSet::setFd
bool setFd(handler_t fd_)
Set flag (ON) for the argument fd.
Definition FdSet.cpp:20
ASSA::Reactor
Definition Reactor.h:58
ASSA
Definition Acceptor.h:40
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::REACT
@ REACT
Class Reactor/PrioriyQueue messages
Definition LogMask.h:39
ASSA::TRACE
@ TRACE
Function call trace
Definition LogMask.h:26