vpr::SocketConnector Class Reference

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

#include <vpr/IO/Socket/SocketConnector.h>

List of all members.

Public Member Functions

 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 connects to the remote address and sets the newStream parameter to the new connection.
vpr::ReturnStatus complete (vpr::SocketStream &newStream, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Tries to complete a non-blocking connection.

Protected Member Functions

bool checkOpen (SocketStream &newStream)
 Makes sure that we have opened the socket.
bool connectStart (vpr::SocketStream &newStream, vpr::Interval timeout=vpr::Interval::NoTimeout, const vpr::InetAddr &localAddr=vpr::InetAddr::AnyAddr)
 Performs pre-connection rituals.


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 65 of file SocketConnector.h.


Constructor & Destructor Documentation

vpr::SocketConnector::SocketConnector (  )  [inline]

Definition at line 68 of file SocketConnector.h.

00069   {;}

vpr::SocketConnector::~SocketConnector (  )  [inline]

Definition at line 71 of file SocketConnector.h.

00072   {;}


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 connects to the remote address and sets the newStream parameter to the new connection.

Parameters:
newStream The stream to connect.
remoteAddr The remote address to which we will connect.
timeout The num msecs to wait (0 - NonBlocking).
localAddr Our local address. This defaults to vpr::InetAddr::AnyAddr.

Definition at line 52 of file SocketConnector.cpp.

References checkOpen(), vpr::Socket_t< SockConfig_ >::connect(), connectStart(), vpr::ReturnStatus::Fail, and vpr::Socket_t< SockConfig_ >::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 
)

Tries to complete a non-blocking connection.

Definition at line 122 of file SocketConnector.cpp.

References vpr::Selector_t< RealSelectorImp >::addHandle(), vpr::Socket_t< SockConfig_ >::getHandle(), vpr::Selector_t< RealSelectorImp >::getOut(), vpr::Socket_t< SockConfig_ >::isBlocking(), vpr::Socket_t< SockConfig_ >::isConnected(), vpr::SelectorBase::Read, vpr::Selector_t< RealSelectorImp >::select(), vpr::Selector_t< RealSelectorImp >::setIn(), vpr::ReturnStatus::Succeed, vprASSERT, and vpr::SelectorBase::Write.

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

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

Makes sure that we have opened the socket.

If not, then open it with the given parameters.

Definition at line 174 of file SocketConnector.cpp.

References vpr::Socket_t< SockConfig_ >::isOpen(), vpr::Socket_t< SockConfig_ >::open(), vpr::ReturnStatus::success(), vprDBG_ALL(), vprDBG_CRITICAL_LVL, vprDEBUG, and vprDEBUG_FLUSH.

Referenced by connect().

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

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

Performs pre-connection rituals.

If we are not bound, then bind to the given local address. If timeout is vpr::Interval::NoWait, then try to set non-blocking status.

Definition at line 196 of file SocketConnector.cpp.

References vpr::Socket_t< SockConfig_ >::bind(), vpr::Socket_t< SockConfig_ >::isBound(), vpr::Socket_t< SockConfig_ >::isOpen(), vpr::Interval::NoWait, vpr::Socket_t< SockConfig_ >::setBlocking(), vpr::Socket_t< SockConfig_ >::setLocalAddr(), vpr::ReturnStatus::success(), and vprASSERT.

Referenced by connect().

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


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:55:15 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1