ACE_Asynch_Connector< HANDLER > Class Template Reference

This class is an example of the Connector pattern. This class will establish new connections and create new HANDLER objects to handle the new connections. More...

#include <Asynch_Connector.h>

Inheritance diagram for ACE_Asynch_Connector< HANDLER >:

Inheritance graph
[legend]
Collaboration diagram for ACE_Asynch_Connector< HANDLER >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Asynch_Connector (void)
 A do nothing constructor.

virtual ~ACE_Asynch_Connector (void)
 Virtual destruction.

virtual int open (int pass_addresses=0, ACE_Proactor *proactor=0, int validate_new_connection=1)
virtual int connect (const ACE_INET_Addr &remote_sap, const ACE_INET_Addr &local_sap=(const ACE_INET_Addr &) ACE_Addr::sap_any, int reuse_addr=1, const void *act=0)
 This initiates a new asynchronous connect.

virtual int cancel (void)
virtual int validate_connection (const ACE_Asynch_Connect::Result &result, const ACE_INET_Addr &remote, const ACE_INET_Addr &local)
virtual int pass_addresses (void) const
virtual void pass_addresses (int new_value)
virtual int validate_new_connection (void) const
virtual void validate_new_connection (int new_value)

Protected Member Functions

virtual void handle_connect (const ACE_Asynch_Connect::Result &result)
 This is called when an outstanding accept completes.

void parse_address (const ACE_Asynch_Connect::Result &result, ACE_INET_Addr &remote_address, ACE_INET_Addr &local_address)
 This parses the address from read buffer.

ACE_Asynch_Connectasynch_connect (void)
 Return the asynch Connect object.

virtual HANDLER * make_handler (void)

Private Attributes

ACE_Asynch_Connect asynch_connect_
 Asynch_Connect used to make life easier :-).

int pass_addresses_
 Flag that indicates if parsing of addresses is necessary.

int validate_new_connection_
 Flag that indicates if address validation is required.


Detailed Description

template<class HANDLER>
class ACE_Asynch_Connector< HANDLER >

This class is an example of the Connector pattern. This class will establish new connections and create new HANDLER objects to handle the new connections.

Unlike the ACE_Connector, however, this class is designed to be used asynchronously with the ACE Proactor framework.

Definition at line 46 of file Asynch_Connector.h.


Constructor & Destructor Documentation

template<class HANDLER>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_Asynch_Connector< HANDLER >::ACE_Asynch_Connector void   ) 
 

A do nothing constructor.

Definition at line 25 of file Asynch_Connector.cpp.

00026   : pass_addresses_ (0),
00027     validate_new_connection_ (0)
00028 {
00029 }

template<class HANDLER>
ACE_Asynch_Connector< HANDLER >::~ACE_Asynch_Connector void   )  [virtual]
 

Virtual destruction.

Definition at line 32 of file Asynch_Connector.cpp.

00033 {
00034   //this->asynch_connect_.close ();
00035 }


Member Function Documentation

template<class HANDLER>
ACE_Asynch_Connect & ACE_Asynch_Connector< HANDLER >::asynch_connect void   )  [protected]
 

Return the asynch Connect object.

Definition at line 255 of file Asynch_Connector.cpp.

References ACE_Asynch_Connector< HANDLER >::asynch_connect_.

00256 {
00257   return this->asynch_connect_;
00258 }

template<class HANDLER>
int ACE_Asynch_Connector< HANDLER >::cancel void   )  [virtual]
 

This cancels all pending accepts operations that were issued by the calling thread.

Note:
On Windows, this method does not cancel connect operations issued by other threads.

On POSIX, delegates cancelation to ACE_POSIX_Asynch_Connect.

Definition at line 178 of file Asynch_Connector.cpp.

References ACE_Asynch_Connector< HANDLER >::asynch_connect_, and ACE_Asynch_Operation::cancel().

00179 {
00180   return this->asynch_connect_.cancel ();
00181 }

template<class HANDLER>
int ACE_Asynch_Connector< HANDLER >::connect const ACE_INET_Addr remote_sap,
const ACE_INET_Addr local_sap = (const ACE_INET_Addr &) ACE_Addr::sap_any,
int  reuse_addr = 1,
const void *  act = 0
[virtual]
 

This initiates a new asynchronous connect.

Definition at line 59 of file Asynch_Connector.cpp.

References ACE_ERROR_RETURN, ACE_LIB_TEXT, ACE_Asynch_Connector< HANDLER >::asynch_connect_, ACE_Asynch_Connect::connect(), and LM_ERROR.

00063 {
00064   // Initiate asynchronous connect
00065   if (this->asynch_connect_.connect (ACE_INVALID_HANDLE,
00066                                      remote_sap,
00067                                      local_sap,
00068                                      reuse_addr,
00069                                      act) == -1)
00070     ACE_ERROR_RETURN ((LM_ERROR,
00071                        ACE_LIB_TEXT ("%p\n"),
00072                        ACE_LIB_TEXT ("ACE_Asynch_Connect::connect")),
00073                       -1);
00074   return 0;
00075 }

template<class HANDLER>
void ACE_Asynch_Connector< HANDLER >::handle_connect const ACE_Asynch_Connect::Result result  )  [protected, virtual]
 

This is called when an outstanding accept completes.

Reimplemented from ACE_Handler.

Definition at line 78 of file Asynch_Connector.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_NONBLOCK, ACE_Asynch_Result::act(), ACE_OS::closesocket(), ACE_Asynch_Connect::Result::connect_handle(), ACE_Asynch_Result::error(), LM_ERROR, ACE_Asynch_Connector< HANDLER >::make_handler(), ACE_Asynch_Connector< HANDLER >::parse_address(), ACE_Asynch_Result::success(), and ACE_Asynch_Connector< HANDLER >::validate_connection().

00079 {
00080   // Variable for error tracking
00081   int error = 0;
00082 
00083   // If the asynchronous connect fails.
00084   if (!result.success () ||
00085       result.connect_handle () == ACE_INVALID_HANDLE)
00086     {
00087       error = 1;
00088     }
00089 
00090   if (result.error () != 0)
00091     {
00092       error = 1;
00093     }
00094 
00095   // set blocking mode
00096   if (!error &&
00097       ACE::clr_flags
00098         (result.connect_handle (), ACE_NONBLOCK) != 0)
00099     {
00100       error = 1;
00101       ACE_ERROR ((LM_ERROR,
00102                   ACE_LIB_TEXT ("%p\n"),
00103                   ACE_LIB_TEXT ("ACE_Asynch_Connector::handle_connect : Set blocking mode")));
00104     }
00105 
00106   // Parse the addresses.
00107   ACE_INET_Addr local_address;
00108   ACE_INET_Addr remote_address;
00109   if (!error &&
00110       (this->validate_new_connection_ || this->pass_addresses_))
00111     this->parse_address (result,
00112                          remote_address,
00113                          local_address);
00114 
00115   // Call validate_connection even if there was an error - it's the only
00116   // way the application can learn the connect disposition.
00117   if (this->validate_new_connection_ &&
00118       this->validate_connection (result, remote_address, local_address) == -1)
00119     {
00120       error = 1;
00121     }
00122 
00123   HANDLER *new_handler = 0;
00124   if (!error)
00125     {
00126       // The Template method
00127       new_handler = this->make_handler ();
00128       if (new_handler == 0)
00129         {
00130           error = 1;
00131           ACE_ERROR ((LM_ERROR,
00132                       ACE_LIB_TEXT ("%p\n"),
00133                       ACE_LIB_TEXT ("ACE_Asynch_Connector::handle_connect : Making of new handler failed")));
00134         }
00135     }
00136 
00137   // If no errors
00138   if (!error)
00139     {
00140       // Update the Proactor.
00141       new_handler->proactor (this->proactor ());
00142 
00143       // Pass the addresses
00144       if (this->pass_addresses_)
00145         new_handler->addresses (remote_address,
00146                                 local_address);
00147 
00148       // Pass the ACT
00149       if (result.act () != 0)
00150         new_handler->act (result.act ());
00151 
00152       // Set up the handler's new handle value
00153       new_handler->handle (result.connect_handle ());
00154 
00155       ACE_Message_Block  mb;
00156 
00157       // Initiate the handler with empty message block;
00158       new_handler->open (result.connect_handle (), mb);
00159     }
00160 
00161   // On failure, no choice but to close the socket
00162   if (error &&
00163       result.connect_handle() != ACE_INVALID_HANDLE)
00164     ACE_OS::closesocket (result.connect_handle ());
00165 }

template<class HANDLER>
HANDLER * ACE_Asynch_Connector< HANDLER >::make_handler void   )  [protected, virtual]
 

This is the template method used to create new handler. Subclasses must overwrite this method if a new handler creation strategy is required.

Definition at line 261 of file Asynch_Connector.cpp.

References ACE_NEW_RETURN.

Referenced by ACE_Asynch_Connector< HANDLER >::handle_connect().

00262 {
00263   // Default behavior
00264   HANDLER *handler = 0;
00265   ACE_NEW_RETURN (handler, HANDLER, 0);
00266   return handler;
00267 }

template<class HANDLER>
int ACE_Asynch_Connector< HANDLER >::open int  pass_addresses = 0,
ACE_Proactor proactor = 0,
int  validate_new_connection = 1
[virtual]
 

This opens asynch connector

Definition at line 38 of file Asynch_Connector.cpp.

References ACE_ERROR_RETURN, ACE_LIB_TEXT, ACE_Asynch_Connector< HANDLER >::asynch_connect_, LM_ERROR, ACE_Asynch_Connect::open(), and ACE_Handler::proactor().

00041 {
00042   this->proactor (proactor);
00043   this->pass_addresses_ = pass_addresses;
00044   this->validate_new_connection_ = validate_new_connection;
00045 
00046   // Initialize the ACE_Asynch_Connect
00047   if (this->asynch_connect_.open (*this,
00048                                   ACE_INVALID_HANDLE,
00049                                   0,
00050                                   this->proactor ()) == -1)
00051     ACE_ERROR_RETURN ((LM_ERROR,
00052                        ACE_LIB_TEXT ("%p\n"),
00053                        ACE_LIB_TEXT ("ACE_Asynch_Connect::open")),
00054                       -1);
00055   return 0;
00056 }

template<class HANDLER>
void ACE_Asynch_Connector< HANDLER >::parse_address const ACE_Asynch_Connect::Result result,
ACE_INET_Addr remote_address,
ACE_INET_Addr local_address
[protected]
 

This parses the address from read buffer.

Definition at line 184 of file Asynch_Connector.cpp.

References ACE_DEBUG, ACE_ERROR, ACE_LIB_TEXT, ACE_INET_Addr::addr_to_string(), ACE_Asynch_Connect::Result::connect_handle(), ACE_OS::getpeername(), ACE_OS::getsockname(), LM_DEBUG, LM_ERROR, and ACE_INET_Addr::set().

Referenced by ACE_Asynch_Connector< HANDLER >::handle_connect().

00187 {
00188 #if defined (ACE_HAS_IPV6)
00189   // Getting the addresses.
00190   sockaddr_in6 local_addr;
00191   sockaddr_in6 remote_addr;
00192 #else
00193   // Getting the addresses.
00194   sockaddr_in local_addr;
00195   sockaddr_in remote_addr;
00196 #endif /* ACE_HAS_IPV6 */
00197 
00198   // Get the length.
00199   int local_size = sizeof (local_addr);
00200   int remote_size = sizeof (remote_addr);
00201 
00202   // Get the local address.
00203   if (ACE_OS::getsockname (result.connect_handle (),
00204                            reinterpret_cast<sockaddr *> (&local_addr),
00205                            &local_size) < 0)
00206     ACE_ERROR ((LM_ERROR,
00207                 ACE_LIB_TEXT("%p\n"),
00208                 ACE_LIB_TEXT("ACE_Asynch_Connector::<getsockname> failed")));
00209 
00210   // Get the remote address.
00211   if (ACE_OS::getpeername (result.connect_handle (),
00212                            reinterpret_cast<sockaddr *> (&remote_addr),
00213                            &remote_size) < 0)
00214     ACE_ERROR ((LM_ERROR,
00215                 ACE_LIB_TEXT("%p\n"),
00216                 ACE_LIB_TEXT("ACE_Asynch_Connector::<getpeername> failed")));
00217 
00218   // Set the addresses.
00219   local_address.set  (reinterpret_cast<sockaddr_in *> (&local_addr),
00220                       local_size);
00221   remote_address.set (reinterpret_cast<sockaddr_in *> (&remote_addr),
00222                       remote_size);
00223 
00224 #if 0
00225   // @@ Just debugging.
00226   char local_address_buf  [BUFSIZ];
00227   char remote_address_buf [BUFSIZ];
00228 
00229   if (local_address.addr_to_string (local_address_buf,
00230                                     sizeof local_address_buf) == -1)
00231     ACE_ERROR ((LM_ERROR,
00232                 "Error:%m:can't obtain local_address's address string"));
00233 
00234   ACE_DEBUG ((LM_DEBUG,
00235               "ACE_Asynch_Connector<HANDLER>::parse_address : "
00236               "Local address %s\n",
00237               local_address_buf));
00238 
00239   if (remote_address.addr_to_string (remote_address_buf,
00240                                      sizeof remote_address_buf) == -1)
00241     ACE_ERROR ((LM_ERROR,
00242                 "Error:%m:can't obtain remote_address's address string"));
00243 
00244   ACE_DEBUG ((LM_DEBUG,
00245               "ACE_Asynch_Connector<HANDLER>::parse_address : "
00246               "Remote address %s\n",
00247               remote_address_buf));
00248 #endif /* 0 */
00249 
00250   return;
00251 }

template<class HANDLER>
void ACE_Asynch_Connector< HANDLER >::pass_addresses int  new_value  )  [virtual]
 

Definition at line 276 of file Asynch_Connector.cpp.

00277 {
00278   this->pass_addresses_ = new_value;
00279 }

template<class HANDLER>
int ACE_Asynch_Connector< HANDLER >::pass_addresses void   )  const [virtual]
 

Set and get flag that indicates if parsing and passing of addresses to the service_handler is necessary.

Definition at line 270 of file Asynch_Connector.cpp.

00271 {
00272   return this->pass_addresses_;
00273 }

template<class HANDLER>
int ACE_Asynch_Connector< HANDLER >::validate_connection const ACE_Asynch_Connect::Result result,
const ACE_INET_Addr remote,
const ACE_INET_Addr local
[virtual]
 

Template method to validate peer before service is opened. This method is called when the connection attempt completes, whether it succeeded or failed, if the validate_connection argument to open() was non-zero or the validate_new_connection() method is called to turn this feature on. The default implementation returns 0. Users can (and probably should) reimplement this method to learn about the success or failure of the connection attempt. If the connection completed successfully, this method can be used to perform validation of the peer using it's address, running an authentication procedure (such as SSL) or anything else necessary or desireable. The return value from this method determines whether or not ACE will continue opening the service or abort the connection.

Parameters:
result Result of the connection acceptance. Use result.success() to determine success or failure of the connection attempt.
remote Peer's address. If the connection failed, this object is undefined.
local Local address connection was completed from. If the connection failed, this object is undefined.
Return values:
-1 ACE_Asynch_Connector will close the connection, and the service will not be opened.
0 Service opening will proceeed.
Returns:
Return value is ignored if the connection attempt failed.

Definition at line 169 of file Asynch_Connector.cpp.

References ACE_INET_Addr.

Referenced by ACE_Asynch_Connector< HANDLER >::handle_connect().

00172 {
00173   // Default implementation always validates the remote address.
00174   return 0;
00175 }

template<class HANDLER>
void ACE_Asynch_Connector< HANDLER >::validate_new_connection int  new_value  )  [virtual]
 

Definition at line 288 of file Asynch_Connector.cpp.

00289 {
00290   this->validate_new_connection_ = new_value;
00291 }

template<class HANDLER>
int ACE_Asynch_Connector< HANDLER >::validate_new_connection void   )  const [virtual]
 

Set and get flag that indicates if address validation is required.

Definition at line 282 of file Asynch_Connector.cpp.

00283 {
00284   return this->validate_new_connection_;
00285 }


Member Data Documentation

template<class HANDLER>
ACE_Asynch_Connect ACE_Asynch_Connector< HANDLER >::asynch_connect_ [private]
 

Asynch_Connect used to make life easier :-).

Definition at line 150 of file Asynch_Connector.h.

Referenced by ACE_Asynch_Connector< HANDLER >::asynch_connect(), ACE_Asynch_Connector< HANDLER >::cancel(), ACE_Asynch_Connector< HANDLER >::connect(), and ACE_Asynch_Connector< HANDLER >::open().

template<class HANDLER>
int ACE_Asynch_Connector< HANDLER >::pass_addresses_ [private]
 

Flag that indicates if parsing of addresses is necessary.

Definition at line 153 of file Asynch_Connector.h.

template<class HANDLER>
int ACE_Asynch_Connector< HANDLER >::validate_new_connection_ [private]
 

Flag that indicates if address validation is required.

Definition at line 156 of file Asynch_Connector.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:19:29 2006 for ACE by doxygen 1.3.6