#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. | |
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). | |
Private Member Functions | |
void | doAsync (void) |
Setup for asynchronous mode completion. | |
int | doSync (void) |
Synchronous mode completion. |
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.
|
state. Connection state.
Definition at line 131 of file Connector.h.
|
|
Constructor. Do-nothing.
Definition at line 205 of file Connector.h. References ASSA::EventHandler::set_id(), ASSA::SOCKTRACE, and trace_with_mask. 00206 : m_tid (0), m_reactor (0), m_state (idle), 00207 m_flags (0), m_sh ((SERVICE_HANDLER*)NULL), m_fd (-1), m_mode (sync) 00208 { 00209 trace_with_mask("Connector::Connector",SOCKTRACE); 00210 set_id ("Connector"); 00211 }
|
|
Destructor. Do-nothing.
Definition at line 215 of file Connector.h. References ASSA::SOCKTRACE, and trace_with_mask. 00216 { 00217 trace_with_mask("Connector::~Connector",SOCKTRACE); 00218 // If I created SERVICE_HANDLER, should I delete it too? 00219 }
|
|
Activate handler by calling its open() method.
Definition at line 323 of file Connector.h. References ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_sh, ASSA::SOCKTRACE, and trace_with_mask. Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_write(). 00324 { 00325 trace_with_mask("Connector::activateServiceHandler",SOCKTRACE); 00326 00327 return m_sh->open (); 00328 }
|
|
Do-nothing close. Derive classes can change this strategy by overloading this method.
Definition at line 237 of file Connector.h. References ASSA::SOCKTRACE, and trace_with_mask. 00238 { 00239 trace_with_mask("Connector::close",SOCKTRACE); 00240 return 0; 00241 }
|
|
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.
Definition at line 245 of file Connector.h. References ASSA::ASSAERR, ASSA::Address::bad(), EL, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_sh, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::makeServiceHandler(), ASSA::set_errno(), ASSA::SOCKTRACE, and trace_with_mask. Referenced by ASSA::Logger::log_open(). 00246 { 00247 /* 00248 * We restore socket to its original mode only on 00249 * successful connection. If error occured, client would have 00250 * to close socket anyway. 00251 * 00252 * NOTE: If sh_==0, then result is dangling pointer 00253 * new_sh produced ! Destructor should determine whether 00254 * SERVICE_HANDLER has been created dynamically and if so, delete 00255 * it. 00256 */ 00257 trace_with_mask("Connector::connect",SOCKTRACE); 00258 errno = 0; 00259 00260 m_sh = makeServiceHandler (sh_); 00261 PEER_CONNECTOR& s = *m_sh; 00262 00263 if (addr_.bad ()) { 00264 set_errno (EFAULT); // Bad address 00265 EL((ASSA::ASSAERR,"Bad address (errno %d)\n", errno)); 00266 return -1; 00267 } 00268 00269 if (connectServiceHandler (addr_, protocol_family_) == -1) 00270 { 00271 int e = get_errno (); 00272 if (e == EINPROGRESS || e == EWOULDBLOCK) 00273 { 00274 if (async == m_mode) { 00275 doAsync (); 00276 return 0; 00277 } 00278 00279 return doSync (); 00280 } 00281 return -1; 00282 } 00283 00284 return activateServiceHandler (); 00285 }
|
|
Default strategy is to make synchronous connection with no timeouts. Derived class can change this strategy by overloading this method.
Definition at line 303 of file Connector.h. References ASSA::ASSAERR, EL, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_sh, ASSA::SOCKTRACE, and trace_with_mask. 00304 { 00305 trace_with_mask("Connector::connectServiceHandler",SOCKTRACE); 00306 00307 PEER_CONNECTOR& s = *m_sh; 00308 00309 if ( !s.open (protocol_family_) ) { 00310 EL((ASSA::ASSAERR,"Socket::open (protocol=%d) failed\n", 00311 protocol_family_)); 00312 return -1; 00313 } 00314 00315 m_fd = s.getHandler (); 00316 s.setOption (ASSA::Socket::nonblocking, 1); 00317 00318 return (s.connect (addr_) ? 0 : -1); 00319 }
|
|
|
|
Handler connection timeout.
Reimplemented from ASSA::EventHandler. Definition at line 474 of file Connector.h. References ASSA::async, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::failed, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_mode, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_reactor, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_state, ASSA::Reactor::removeHandler(), ASSA::set_errno(), ASSA::SOCKTRACE, trace_with_mask, and ASSA::WRITE_EVENT. 00475 { 00476 trace_with_mask("Connector::handle_timeout",SOCKTRACE); 00477 00478 m_state = failed; 00479 set_errno (ETIMEDOUT); // Connection timed out 00480 00481 if (async == m_mode) { 00482 m_reactor->removeHandler (this, WRITE_EVENT); 00483 } 00484 return -1; // Remove Timer Handler 00485 }
|
|
Handle connection completion. Always remove IO handler first. Reimplemented from ASSA::EventHandler. Definition at line 378 of file Connector.h. References ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::activateServiceHandler(), ASSA::ASSAERR, ASSA::async, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::conned, DL, EL, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::failed, ASSA::get_errno(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_fd, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_mode, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_reactor, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_sh, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_state, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_tid, ASSA::Reactor::removeHandler(), ASSA::Reactor::removeTimerHandler(), ASSA::set_errno(), ASSA::SOCKTRACE, trace_with_mask, and ASSA::WRITE_EVENT. 00379 { 00380 trace_with_mask("Connector::handle_write",SOCKTRACE); 00381 00382 /* Precondition 00383 */ 00384 if (fd_ != m_fd) { 00385 return -1; 00386 } 00387 00388 /* This method serves both sync and async modes - thus the 00389 * differences. For async we remove Timer here. sync runs 00390 * its own private Reactor and handler termination is 00391 * handled in doSync(). 00392 */ 00393 00394 if (async == m_mode) { // Complete SH activation 00395 m_reactor->removeTimerHandler (m_tid); 00396 m_tid = 0; 00397 } 00398 00399 /* 00400 * Although SUN and Linux man pages on connect(3) claims that 00401 * "upon asynchronous establishement of connection, select(3) 00402 * will indicate that the file descriptor for the socket is ready 00403 * for writing", as discussed in W.S.Stevens "UNIX network 00404 * programming", Vol I, 2nd edition, BSD-derived systems also 00405 * mark file descriptor both readable and writable when the 00406 * connection establishment encouters an error. 00407 * 00408 * Therefore we need an extra step to find out what really happened. 00409 * One way to do so is to look at socket pending errors... 00410 */ 00411 00412 int error; 00413 int ret; 00414 error = ret = errno = 0; 00415 socklen_t n = sizeof (error); 00416 00419 m_reactor->removeHandler (this, WRITE_EVENT); 00420 00421 #if defined(__CYGWIN32__) 00422 ret = getsockopt (m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, (int*)&n); 00423 #elif defined (WIN32) 00424 ret = getsockopt (m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, (int*)&n); 00425 #else 00426 ret = getsockopt (m_fd, SOL_SOCKET, SO_ERROR, (void*)&error, &n); 00427 #endif 00428 00429 if (ret == 0) { 00430 if (error == 0) 00431 { 00432 if (activateServiceHandler () == 0) { 00433 DL((SOCKTRACE,"Nonblocking connect() completed\n")); 00434 m_state = conned; 00435 } 00436 else { 00437 DL((SOCKTRACE,"Nonblocking connect() failed\n")); 00438 m_state = failed; 00439 } 00440 return (0); // return value doesn't really matter 00441 } 00442 /* Socket pending error - propagate it via errno. */ 00443 00444 EL((ASSA::ASSAERR,"Socket pending error: %d\n",error)); 00445 set_errno (error); 00446 } 00447 else { /* Solaris pending error. */ 00448 EL((ASSA::ASSAERR,"getsockopt(3) = %d\n", ret)); 00449 EL((ASSA::ASSAERR,"Solaris pending error!\n")); 00450 } 00451 m_state = failed; 00452 00453 EL((ASSA::ASSAERR,"Nonblocking connect (2) failed\n")); 00454 00455 if (get_errno () == ECONNREFUSED) 00456 { 00457 EL((ASSA::ASSAERR,"Try to compare port " 00458 "numbers on client and service hosts.\n")); 00459 } 00460 /* This is the only way to tell SH that we failed to connect. 00461 */ 00462 if (async == m_mode) { 00463 m_sh->close (); 00464 } 00465 00466 /* Don't alter fd mask - SERVICE_HANDLER::open() could have changed 00467 * it already for application processing needs. 00468 */ 00469 return 0; 00470 }
|
|
Defines creation strategy for ServiceHandler. Default is to dynamically allocate new SERVICE_HANDLER, if one is not given as an argument.
Definition at line 289 of file Connector.h. References ASSA::SOCKTRACE, and trace_with_mask. Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::connect(). 00290 { 00291 trace_with_mask("Connector::makeServiceHandler",SOCKTRACE); 00292 00293 SERVICE_HANDLER* new_sh = sh_; 00294 00295 if (sh_ == 0) { 00296 new_sh = new SERVICE_HANDLER; 00297 } 00298 return new_sh; 00299 }
|
|
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.
Definition at line 223 of file Connector.h. References ASSA::async, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_mode, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_reactor, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::m_timeout, ASSA::SOCKTRACE, and trace_with_mask. Referenced by ASSA::Logger::log_open(). 00224 { 00225 trace_with_mask("Connector::open", SOCKTRACE); 00226 00227 m_timeout = tv_; 00228 if (async == mode_ && (Reactor*) NULL == r_) 00229 return -1; 00230 m_mode = mode_; 00231 m_reactor = r_; 00232 return 0; 00233 }
|
|
Socket file descriptor.
Definition at line 179 of file Connector.h. Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doAsync(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doSync(), and ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_write(). |
|
Socket flags (obsolete).
Definition at line 173 of file Connector.h. |
|
Mode (sync/async).
Definition at line 182 of file Connector.h. Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_timeout(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_write(), and ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::open(). |
|
Reference to Reactor (for async).
Definition at line 167 of file Connector.h. Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doAsync(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doSync(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_timeout(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_write(), and ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::open(). |
|
|
Connection progress state.
Definition at line 170 of file Connector.h. Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doAsync(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doSync(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_timeout(), and ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_write(). |
|
Timer id.
Definition at line 164 of file Connector.h. Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doAsync(), and ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::handle_write(). |
|
Timeout.
Definition at line 161 of file Connector.h. Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doAsync(), ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doSync(), and ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::open(). |