Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

vpr::SocketConnector Class Reference

Defines a factory for creating new connections both synchronously and asynchronously. More...

#include <SocketConnector.h>

List of all members.

Public Methods

 SocketConnector ()
 ~SocketConnector ()
vpr::ReturnStatus connect (vpr::SocketStream &newStream, const vpr::InetAddr &remoteAddr, vpr::Interval timeout=vpr::Interval::NoTimeout, const vpr::InetAddr &localAddr=vpr::InetAddr::AnyAddr)
 Actively connect to the remote address and set the newStream to the new connection. More...

vpr::ReturnStatus complete (vpr::SocketStream &newStream, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Complete a non-blocking connection Try to complete a non-blocking connection. More...


Protected Methods

bool checkOpen (SocketStream &newStream)
bool connectStart (vpr::SocketStream &newStream, vpr::Interval timeout=vpr::Interval::NoTimeout, const vpr::InetAddr &localAddr=vpr::InetAddr::AnyAddr)


Detailed Description

Defines a factory for creating new connections both synchronously and asynchronously.

The socket factory creates a new socket for the user that connects to the given remote destination. It allows for synchronous and async connection establishment.

This means that there is no state associated with the connector.

Definition at line 66 of file SocketConnector.h.


Constructor & Destructor Documentation

vpr::SocketConnector::SocketConnector   [inline]
 

Definition at line 69 of file SocketConnector.h.

00070   {;}

vpr::SocketConnector::~SocketConnector   [inline]
 

Definition at line 72 of file SocketConnector.h.

00073   {;}


Member Function Documentation

vpr::ReturnStatus vpr::SocketConnector::connect vpr::SocketStream   newStream,
const vpr::InetAddr   remoteAddr,
vpr::Interval    timeout = vpr::Interval::NoTimeout,
const vpr::InetAddr   localAddr = vpr::InetAddr::AnyAddr
 

Actively connect to the remote address and set the newStream to the new connection.

Parameters:
new_stream  The stream to connect
remoteAddr  The remote address to connect to
timeout  The num msecs to wait (0 - NonBlocking)

Definition at line 52 of file SocketConnector.cpp.

References checkOpen, vpr::Socket_t< SocketConfiguration >::connect, connectStart, vpr::ReturnStatus::Fail, and vpr::Socket_t< SocketConfiguration >::setRemoteAddr.

00056 {
00057     vpr::ReturnStatus ret_val;
00058     //vpr::InetAddr remote_addr;
00059 
00060     // Open the socket
00061     if(!checkOpen(newStream))
00062     {
00063         return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00064     }
00065 
00066     /*  This actually happens in connect start
00067     if ( localAddr != vpr::InetAddr::AnyAddr )
00068     {
00069        vpr::ReturnStatus status;
00070        newStream.setLocalAddr(localAddr);
00071        status = newStream.bind();
00072        vprASSERT(status.success() && "Failed to bind local address");
00073     }
00074     */
00075 
00076     // Start the connection
00077     if(!connectStart(newStream, timeout, localAddr))
00078     {
00079        return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00080     }
00081 
00082     newStream.setRemoteAddr(remoteAddr);
00083 
00084     // Attempt the connection
00085     ret_val = newStream.connect(timeout);
00086 
00087     /*
00088     // If the connect call did not return success, it may be the result of
00089     // using non-blocking sockets.
00090     if ( ! ret_val.success() )
00091     {
00092        // If connect() gave us a status saying that the connection is in
00093        // progress, try to complete the connection after the timeout period.
00094        // If there is no timeout period, simply return immediately.
00095        if ( ret_val == vpr::ReturnStatus::InProgress ||
00096             ret_val == vpr::ReturnStatus::WouldBlock )
00097        {
00098           if ( timeout != vpr::Interval::NoWait ) {
00099              ret_val = complete(newStream, &remote_addr, timeout);
00100           }
00101        }
00102     }
00103     // Finish up successful connection.
00104     else if(vpr::Interval::NoWait != timeout) {
00105        ret_val = complete(newStream, &remote_addr, timeout);
00106     }
00107     */
00108 
00109     /*
00110     ** Since complete doesn't do anything really we don't need this
00111     if(ret_val.success())
00112     {
00113        ret_val = complete(newStream, timeout);
00114     }
00115     */
00116 
00117     return ret_val;
00118 }

vpr::ReturnStatus vpr::SocketConnector::complete vpr::SocketStream   newStream,
const vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Complete a non-blocking connection Try to complete a non-blocking connection.

Parameters:
newStream  The connected stream.
remoteAddr  returns the address of the remote connection.

Definition at line 127 of file SocketConnector.cpp.

References vpr::Selector_t::addHandle, vpr::Socket_t< SocketConfiguration >::getHandle, vpr::Selector_t::getOut, vpr::Socket_t< SocketConfiguration >::isBlocking, vpr::Socket_t< SocketConfiguration >::isConnected, vpr::Selector_t::select, vpr::Selector_t::setIn, vpr::ReturnStatus::Succeed, and vprASSERT.

00129 {
00130    vpr::ReturnStatus status;
00131 
00132    if( newStream.isConnected() )
00133    {
00134       // XXX: Should this actually be a failure
00135       return vpr::ReturnStatus::Succeed;
00136    }
00137 
00138    // If non-blocking, then we can only wait as long as the timeout
00139    if ( ! newStream.isBlocking() )
00140    {
00141       vpr::IOSys::Handle handle;
00142       vpr::Selector selector;
00143       vpr::Uint16 num_events;
00144 
00145       // Use the selector to be informed when the SocketStream object is ready
00146       // to be used.  That is, when the object is connected.
00147       handle = newStream.getHandle();
00148       selector.addHandle(handle);
00149       selector.setIn(handle, vpr::Selector::Read | vpr::Selector::Write);
00150       status = selector.select(num_events, timeout);
00151 
00152       // If the selector told us that our handle is ready, we are successfully
00153       // connected.
00154       if ( selector.getOut(handle) & (vpr::Selector::Read | vpr::Selector::Write) )
00155       {
00156          status = vpr::ReturnStatus::Succeed;
00157 
00158          /*
00159          if ( remoteAddr != NULL ) {
00160             (*remoteAddr) = newStream.getRemoteAddr();
00161          }
00162          */
00163       }
00164       // else Use the status from the selector
00165    }
00166    else     // Not a non-blocking socket
00167    {
00168       vprASSERT(false && "Should not call complete on a non-blocking socket");
00169       /*
00170       if ( remoteAddr != NULL ) {
00171          (*remoteAddr) = newStream.getRemoteAddr();
00172       }
00173       */
00174    }
00175 
00176    return status;
00177 }

bool vpr::SocketConnector::checkOpen SocketStream   newStream [protected]
 

Definition at line 179 of file SocketConnector.cpp.

References vpr::SocketStream, vpr::ReturnStatus::success, vprDBG_ALL, vprDBG_CRITICAL_LVL, vprDEBUG, and vprDEBUG_FLUSH.

Referenced by connect.

00180 {
00181    vpr::ReturnStatus status;
00182 
00183    if (!newStream.isOpen())
00184    {
00185        status = newStream.open();
00186 
00187        if(!status.success())
00188        {
00189           vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00190              << "vpr::Connector:CheckOpen: Failed to open socket\n"
00191              << vprDEBUG_FLUSH;
00192        }
00193    }
00194 
00195    return status.success();
00196 }

bool vpr::SocketConnector::connectStart vpr::SocketStream   newStream,
vpr::Interval    timeout = vpr::Interval::NoTimeout,
const vpr::InetAddr   localAddr = vpr::InetAddr::AnyAddr
[protected]
 

Definition at line 201 of file SocketConnector.cpp.

References vpr::Interval::NoWait, vpr::SocketStream, and vprASSERT.

Referenced by connect.

00204 {
00205    vprASSERT(newStream.isOpen());
00206 
00207    if(!newStream.isBound())      // If we are not bound yet
00208    {
00209       // If timeout is 0, then we are non-blocking
00210       if(vpr::Interval::NoWait == timeout)
00211       {
00212          newStream.setBlocking(false);
00213       }
00214 
00215       // Set addr and bind
00216       if(!newStream.setLocalAddr(localAddr).success())
00217       {
00218          return false;
00219       }
00220 
00221       if(!newStream.bind().success())
00222       {
00223          return false;
00224       }
00225    }
00226 
00227    return true;
00228 }


The documentation for this class was generated from the following files:
Generated on Sun May 2 14:46:54 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002