Loading...
Searching...
No Matches
ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR > Class Template Reference

Connector is a template class for initialization of communication services. More...

`#include <Connector.h>`

Inheritance diagram for ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >:

## Public Member Functions

| Connector ()
| Constructor. Do-nothing.
virtual | ~Connector ()
| Destructor. Do-nothing.
virtual int | open (const TimeVal &tv_=TimeVal(5.0), ConnectMode mode_=sync, Reactor *r_=(Reactor *) NULL)
| Configure Connector.
virtual int | close (void)
| Do-nothing close.
virtual int | connect (SERVICE_HANDLER *sh_, Address &addr_, int protocol_=AF_INET)
| Define strategy for establishing connection.
virtual int | handle_write (int fd)
| Handle connection completion.
virtual int | handle_timeout (TimerId tid)
| Handler connection timeout.
Public Member Functions inherited from ASSA::EventHandler
| EventHandler ()
| Constructor.
virtual | ~EventHandler ()
| Virtual destructor.
virtual int | handle_read (int fd)
| Read event callback.
virtual int | handle_except (int fd)
| Exception handler callback.
virtual int | handle_signal (int signum_)
| Signal handler callback.
virtual int | handle_close (int fd)
| EOF on peer socket 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 Types

enum | ProgressState { idle , waiting , conned , failed }
| state. More...

## Protected Member Functions

virtual SERVICE_HANDLER * | makeServiceHandler (SERVICE_HANDLER *sh_)
| Defines creation strategy for ServiceHandler.
virtual int | connectServiceHandler (Address &addr, int protocol)
| Default strategy is to make synchronous connection with no timeouts.
virtual int | activateServiceHandler ()
| Activate handler by calling its open() method.

## Protected Attributes

TimeVal | m_timeout
| Timeout.
TimerId | m_tid
| Timer id.
Reactor * | m_reactor
| Reference to Reactor (for async).
ProgressState | m_state
| Connection progress state.
int | m_flags
| Socket flags (obsolete).
SERVICE_HANDLER * | m_sh
| Reference to ServiceHandler.
int | m_fd
| Socket file descriptor.
ConnectMode | m_mode
| Mode (sync/async).
Protected Attributes inherited from ASSA::EventHandler
std::string | m_id

## Private Member Functions

void | doAsync (void)
| Setup for asynchronous mode completion.
int | doSync (void)
| Synchronous mode completion.

## Detailed Description

template<class SERVICE_HANDLER, class PEER_CONNECTOR>
class ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >

Connector is a template class for initialization of communication services.

This template class implements the generic strategy for actively
initializing communication services.

SERVICE_HANDLER is the type of service. It shall be a type derived from ServiceHandler interface class.

PEER_CONNECTOR is the type of concrete Socket class - particular transport mechanism used by the Connector to actively establish the connection. It should be derived from Socket interface class.

Definition at line 63 of file Connector.h.

## Member Enumeration Documentation

## ProgressState

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

enum ASSA::Connector::ProgressState

protected

state.

Connection state.

Enumerator
idle | Initialized.
waiting | Asynchronously waiting on connection completion.
conned | Connected.
failed | Failed to connect.

Definition at line 131 of file Connector.h.

{
idle,
waiting,
conned,
failed
};
ASSA::Connector::idle
@ idle
Initialized.
Definition Connector.h:132
ASSA::Connector::failed
@ failed
Failed to connect.
Definition Connector.h:135
ASSA::Connector::conned
@ conned
Connected.
Definition Connector.h:134
ASSA::Connector::waiting
@ waiting
Asynchronously waiting on connection completion.
Definition Connector.h:133

## Constructor & Destructor Documentation

## Connector()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::Connector | ( |  | ) |

Constructor. Do-nothing.

Definition at line 204 of file Connector.h.

: m_tid (0), m_reactor (0), m_state (idle),
m_flags (0), m_sh ((SERVICE_HANDLER*)NULL), m_fd (-1), m_mode (sync)
{
trace_with_mask("Connector::Connector",SOCKTRACE);
set_id ("Connector");
}
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
ASSA::Connector
Connector is a template class for initialization of communication services.
Definition Connector.h:64
ASSA::Connector::m_flags
int m_flags
Socket flags (obsolete).
Definition Connector.h:173
ASSA::Connector::m_mode
ConnectMode m_mode
Mode (sync/async).
Definition Connector.h:182
ASSA::Connector::m_state
ProgressState m_state
Connection progress state.
Definition Connector.h:170
ASSA::Connector::m_sh
SERVICE_HANDLER * m_sh
Reference to ServiceHandler.
Definition Connector.h:176
ASSA::Connector::m_tid
TimerId m_tid
Timer id.
Definition Connector.h:164
ASSA::Connector::m_fd
int m_fd
Socket file descriptor.
Definition Connector.h:179
ASSA::Connector::m_reactor
Reactor * m_reactor
Reference to Reactor (for async).
Definition Connector.h:167
ASSA::EventHandler::set_id
void set_id(const std::string &id_)
Set EventHandler ID.
Definition EventHandler.h:153

References idle, m_fd, m_flags, m_mode, m_reactor, m_sh, m_state, m_tid, ASSA::EventHandler::set_id(), ASSA::SOCKTRACE, ASSA::sync, and trace_with_mask.

## ~Connector()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::~Connector | ( |  | ) |

virtual

Destructor. Do-nothing.

Definition at line 214 of file Connector.h.

{
trace_with_mask("Connector::~Connector",SOCKTRACE);
// If I created SERVICE_HANDLER, should I delete it too?
}

References ASSA::SOCKTRACE, and trace_with_mask.

## Member Function Documentation

## activateServiceHandler()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::activateServiceHandler | ( |  | ) |

protected virtual

Activate handler by calling its open() method.

Returns
  0 on success, -1 on error.

Definition at line 322 of file Connector.h.

{
trace_with_mask("Connector::activateServiceHandler",SOCKTRACE);
return m_sh->open ();
}

References m_sh, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by connect(), and handle_write().

## close()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::close | ( | void |  | ) |

virtual

Do-nothing close.

Derive classes can change this strategy by overloading this method.

Returns
  0 on success, -1 on error.

Definition at line 236 of file Connector.h.

{
trace_with_mask("Connector::close",SOCKTRACE);
return 0;
}

References ASSA::SOCKTRACE, and trace_with_mask.

## connect()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::connect | ( | SERVICE_HANDLER * | sh_,
|  | Address & | addr_,
|  | int | protocol_ = AF_INET )

virtual

Define strategy for establishing connection.

Default is to connect synchronously to the remote peer. In sync mode connection either will be established or failed when returned from Connector::connect() call.

In async mode, call to Connector::connect() returns immediately reporting only immediate error. Later on connection is completed asynchronously.

Default timeout on connection waiting is 10 seconds. Timeout can be configured by passing TimeVal parameter to the Connector::open() member function.

If connetion failed, caller should definitely close PEER_CONNECTOR communication point.

Parameters

  sh_ | Pointer to class object derived from ServiceHandler.
  addr_ | Reference to the address to connect to.
  protocol_ | AF_INET for internet socket, AF_UNIX for the UNIX Domain socket (defaults to AF_INET).

Returns
  0 on success, -1 on error.

Definition at line 244 of file Connector.h.

{
/*
* We restore socket to its original mode only on
* successful connection. If error occured, client would have
* to close socket anyway.
*
* NOTE: If sh_==0, then result is dangling pointer
* new_sh produced ! Destructor should determine whether
* SERVICE_HANDLER has been created dynamically and if so, delete
* it.
*/
trace_with_mask("Connector::connect",SOCKTRACE);
errno = 0;
m_sh = makeServiceHandler (sh_);
PEER_CONNECTOR& s = *m_sh;
if (addr_.bad ()) {
set_errno (EFAULT); // Bad address
EL((ASSA::ASSAERR,"Bad address (errno %d)\n", errno));
return -1;
}
if (connectServiceHandler (addr_, protocol_family_) == -1)
{
int e = get_errno ();
if (e == EINPROGRESS || e == EWOULDBLOCK)
{
if (async == m_mode) {
doAsync ();
return 0;
}
return doSync ();
}
return -1;
}
return activateServiceHandler ();
}
EL
#define EL(X)
A macro for writing error message to the Logger.
Definition Logger.h:285
ASSA::Connector::doAsync
void doAsync(void)
Setup for asynchronous mode completion.
Definition Connector.h:332
ASSA::Connector::connectServiceHandler
virtual int connectServiceHandler(Address &addr, int protocol)
Default strategy is to make synchronous connection with no timeouts.
Definition Connector.h:303
ASSA::Connector::activateServiceHandler
virtual int activateServiceHandler()
Activate handler by calling its open() method.
Definition Connector.h:323
ASSA::Connector::makeServiceHandler
virtual SERVICE_HANDLER * makeServiceHandler(SERVICE_HANDLER *sh_)
Defines creation strategy for ServiceHandler.
Definition Connector.h:289
ASSA::Connector::doSync
int doSync(void)
Synchronous mode completion.
Definition Connector.h:349
ASSA::set_errno
void set_errno(int new_errno_)
Set error number in a portable way.
Definition Logger_Impl.h:128
ASSA::get_errno
int get_errno()
Fetch error number in a portable way.
Definition Logger_Impl.h:115

References activateServiceHandler(), ASSA::ASSAERR, ASSA::async, ASSA::Address::bad(), connectServiceHandler(), doAsync(), doSync(), EL, ASSA::get_errno(), m_mode, m_sh, makeServiceHandler(), ASSA::set_errno(), SH, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ASSA::Logger::log_open().

## connectServiceHandler()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::connectServiceHandler | ( | Address & | addr,
|  | int | protocol )

protected virtual

Default strategy is to make synchronous connection with no timeouts.

Derived class can change this strategy by overloading this method.

Returns
  0 on success, -1 on error.

Definition at line 302 of file Connector.h.

{
trace_with_mask("Connector::connectServiceHandler",SOCKTRACE);
PEER_CONNECTOR& s = *m_sh;
if ( !s.open (protocol_family_) ) {
EL((ASSA::ASSAERR,"Socket::open (protocol=%d) failed\n",
protocol_family_));
return -1;
}
m_fd = s.getHandler ();
s.setOption (ASSA::Socket::nonblocking, 1);
return (s.connect (addr_) ? 0 : -1);
}
ASSA::Connector::open
virtual int open(const TimeVal &tv_=TimeVal(5.0), ConnectMode mode_=sync, Reactor *r_=(Reactor *) NULL)
Configure Connector.
Definition Connector.h:223
ASSA::Connector::connect
virtual int connect(SERVICE_HANDLER *sh_, Address &addr_, int protocol_=AF_INET)
Define strategy for establishing connection.
Definition Connector.h:245

References ASSA::ASSAERR, EL, m_fd, m_sh, ASSA::Socket::nonblocking, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by connect().

## doAsync()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

void ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doAsync | ( | void |  | ) |

private

Setup for asynchronous mode completion.

Definition at line 331 of file Connector.h.

{
trace_with_mask("Connector::doAsync",SOCKTRACE);
/* We are doing async and 3-way handshake is in
* progress - hook up with Reactor and wait on timer.
* Write event will be our indicator whether connection
* was completed or not.
*/
m_reactor->registerIOHandler (this, m_fd, WRITE_EVENT);
m_tid = m_reactor->registerTimerHandler (this, m_timeout, "ASYNC Connect");
m_state = waiting;
}
ASSA::Connector::m_timeout
TimeVal m_timeout
Timeout.
Definition Connector.h:161

References m_fd, m_reactor, m_state, m_tid, m_timeout, ASSA::SOCKTRACE, trace_with_mask, waiting, and ASSA::WRITE_EVENT.

Referenced by connect().

## doSync()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doSync | ( | void |  | ) |

private

Synchronous mode completion.

Returns
  0 on success; -1 if error.

Definition at line 348 of file Connector.h.

{
trace_with_mask("Connector::doSync",SOCKTRACE);
m_reactor = new Reactor;
m_reactor->registerIOHandler (this, m_fd, WRITE_EVENT);
m_reactor->registerTimerHandler (this, m_timeout, "SYNC Connect");
m_state = waiting;
m_reactor->waitForEvents (&m_timeout); // Let the ball rolling ...
m_reactor->removeHandler (this); // Remove all handlers.
delete m_reactor;
m_reactor = 0;
if (conned == m_state)
{
DL((SOCKTRACE,"Synchronous connect() succeeded.\n"));
return 0;
}
EL((ASSA::ASSAERR,"Synchronous connect() timed out.\n"));
set_errno (ETIMEDOUT);
return -1;
}
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273

References ASSA::ASSAERR, conned, DL, EL, m_fd, m_reactor, m_state, m_timeout, ASSA::set_errno(), ASSA::SOCKTRACE, trace_with_mask, waiting, and ASSA::WRITE_EVENT.

Referenced by connect().

## handle_timeout()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_timeout | ( | TimerId | tid | ) |

virtual

Handler connection timeout.

Reimplemented from ASSA::EventHandler.

Definition at line 473 of file Connector.h.

{
trace_with_mask("Connector::handle_timeout",SOCKTRACE);
m_state = failed;
set_errno (ETIMEDOUT); // Connection timed out
if (async == m_mode) {
m_reactor->removeHandler (this, WRITE_EVENT);
}
return -1; // Remove Timer Handler
}

References ASSA::async, failed, m_mode, m_reactor, m_state, ASSA::set_errno(), ASSA::SOCKTRACE, trace_with_mask, and ASSA::WRITE_EVENT.

## handle_write()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_write | ( | int | fd | ) |

virtual

Handle connection completion.

Always remove IO handler first.

Reimplemented from ASSA::EventHandler.

Definition at line 377 of file Connector.h.

{
trace_with_mask("Connector::handle_write",SOCKTRACE);
/* Precondition
*/
if (fd_ != m_fd) {
return -1;
}
/* This method serves both sync and async modes - thus the
* differences. For async we remove Timer here. sync runs
* its own private Reactor and handler termination is
* handled in doSync().
*/
if (async == m_mode) { // Complete SH activation
m_reactor->removeTimerHandler (m_tid);
m_tid = 0;
}
/*
* Although SUN and Linux man pages on connect(3) claims that
* "upon asynchronous establishement of connection, select(3)
* will indicate that the file descriptor for the socket is ready
* for writing", as discussed in W.S.Stevens "UNIX network
* programming", Vol I, 2nd edition, BSD-derived systems also
* mark file descriptor both readable and writable when the
* connection establishment encouters an error.
*
* Therefore we need an extra step to find out what really happened.
* One way to do so is to look at socket pending errors...
*/
int error;
int ret;
error = ret = errno = 0;
socklen_t n = sizeof (error);
m_reactor->removeHandler (this, WRITE_EVENT);
#if defined(__CYGWIN32__)
ret = getsockopt (m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, (int*)&n);
#elif defined (WIN32)
ret = getsockopt (m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, (int*)&n);
#else
ret = getsockopt (m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, &n);
#endif
if (ret == 0) {
if (error == 0)
{
if (activateServiceHandler () == 0) {
DL((SOCKTRACE,"Nonblocking connect() completed\n"));
m_state = conned;
}
else {
DL((SOCKTRACE,"Nonblocking connect() failed\n"));
m_state = failed;
}
return (0); // return value doesn't really matter
}
/* Socket pending error - propagate it via errno. */
EL((ASSA::ASSAERR,"Socket pending error: %d\n",error));
set_errno (error);
}
else { /* Solaris pending error. */
EL((ASSA::ASSAERR,"getsockopt(3) = %d\n", ret));
EL((ASSA::ASSAERR,"Solaris pending error!\n"));
}
m_state = failed;
EL((ASSA::ASSAERR,"Nonblocking connect (2) failed\n"));
if (get_errno () == ECONNREFUSED)
{
EL((ASSA::ASSAERR,"Try to compare port "
"numbers on client and service hosts.\n"));
}
/* This is the only way to tell SH that we failed to connect.
*/
if (async == m_mode) {
m_sh->close ();
}
/* Don't alter fd mask - SERVICE_HANDLER::open() could have changed
* it already for application processing needs.
*/
return 0;
}

References activateServiceHandler(), ASSA::ASSAERR, ASSA::async, conned, DL, EL, failed, ASSA::get_errno(), m_fd, m_mode, m_reactor, m_sh, m_state, m_tid, ASSA::set_errno(), ASSA::SOCKTRACE, trace_with_mask, and ASSA::WRITE_EVENT.

## makeServiceHandler()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

SERVICE_HANDLER * ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::makeServiceHandler | ( | SERVICE_HANDLER * | sh_ | ) |

protected virtual

Defines creation strategy for ServiceHandler.

Default is to dynamically allocate new SERVICE_HANDLER, if one is not given as an argument.

Parameters

  sh_ | pointer to SERVICE_HANDLER, or NULL, if it is expected to be created here

Returns
  pointer to SERVICE_HANDLER

Definition at line 288 of file Connector.h.

{
trace_with_mask("Connector::makeServiceHandler",SOCKTRACE);
SERVICE_HANDLER* new_sh = sh_;
if (sh_ == 0) {
new_sh = new SERVICE_HANDLER;
}
return new_sh;
}

References ASSA::SOCKTRACE, and trace_with_mask.

Referenced by connect().

## open()

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::open | ( | const TimeVal & | tv_ = TimeVal (5.0),
|  | ConnectMode | mode_ = sync,
|  | Reactor * | r_ = (Reactor*)NULL )

virtual

Configure Connector.

Timeout will be used to timeout connection operation. If mode_ is async, then Reactor r_ ought to be specified for handling asynchronous event processing. Derive classes can change this strategy by overloading this method.

Parameters

  tv_ | Time for connection timeout (Default = 5.0 secs.)
  mode_ | Synchronous or Asynchronous mode.
  r_ | Reactor to work with (for async mode).

Returns
  0 on success, -1 on error.

Definition at line 222 of file Connector.h.

{
trace_with_mask("Connector::open", SOCKTRACE);
m_timeout = tv_;
if (async == mode_ && (Reactor*) NULL == r_)
return -1;
m_mode = mode_;
m_reactor = r_;
return 0;
}

References ASSA::async, m_mode, m_reactor, m_timeout, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ASSA::Logger::log_open().

## Member Data Documentation

## m_fd

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_fd

protected

Socket file descriptor.

Definition at line 179 of file Connector.h.

Referenced by Connector(), connectServiceHandler(), doAsync(), doSync(), and handle_write().

## m_flags

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

int ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_flags

protected

Socket flags (obsolete).

Definition at line 173 of file Connector.h.

Referenced by Connector().

## m_mode

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

ConnectMode ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_mode

protected

Mode (sync/async).

Definition at line 182 of file Connector.h.

Referenced by connect(), Connector(), handle_timeout(), handle_write(), and open().

## m_reactor

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

Reactor* ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_reactor

protected

Reference to Reactor (for async).

Definition at line 167 of file Connector.h.

Referenced by Connector(), doAsync(), doSync(), handle_timeout(), handle_write(), and open().

## m_sh

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

SERVICE_HANDLER* ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_sh

protected

Reference to ServiceHandler.

Definition at line 176 of file Connector.h.

Referenced by activateServiceHandler(), connect(), Connector(), connectServiceHandler(), and handle_write().

## m_state

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

ProgressState ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_state

protected

Connection progress state.

Definition at line 170 of file Connector.h.

Referenced by Connector(), doAsync(), doSync(), handle_timeout(), and handle_write().

## m_tid

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

TimerId ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_tid

protected

Timer id.

Definition at line 164 of file Connector.h.

Referenced by Connector(), doAsync(), and handle_write().

## m_timeout

template<class SERVICE_HANDLER, class PEER_CONNECTOR>

TimeVal ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_timeout

protected

Timeout.

Definition at line 161 of file Connector.h.

Referenced by doAsync(), doSync(), and open().

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

- Connector.h