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

vpr::SocketStreamImplSIM Class Reference

Simulated stream sockets. More...

#include <SocketStreamImplSIM.h>

Inheritance diagram for vpr::SocketStreamImplSIM:

Inheritance graph
[legend]
Collaboration diagram for vpr::SocketStreamImplSIM:

Collaboration graph
[legend]
List of all members.

Public Methods

 SocketStreamImplSIM (void)
 Default constructor. More...

 SocketStreamImplSIM (const vpr::InetAddr &local_addr, const vpr::InetAddr &remote_addr)
 Constructor. More...

virtual ~SocketStreamImplSIM (void)
 Destructor. More...

vpr::ReturnStatus listen (const int backlog=5)
 Puts this socket into the listening state where it listens for incoming connection requests. More...

vpr::ReturnStatus accept (SocketStreamImplSIM &client_sock, vpr::Interval timeout=vpr::Interval::NoTimeout)
 Accepts an incoming connection request and return the connected socket to the caller in the given socket object reference. More...

vpr::ReturnStatus setNoDelay (bool setting)
 Sets the current no-delay status for this socket. More...

vpr::ReturnStatus addConnector (vpr::SocketImplSIM *peerSock)
vpr::Uint32 getConnectorCount (void) const
 Gets the current number of connectors waiting to connect to this socket. More...

virtual vpr::ReturnStatus isReadReady () const
 Tests if this socket can read without blocking. More...

virtual vpr::ReturnStatus isWriteReady () const
 Tests if this socket can write without blocking. More...


Protected Attributes

std::queue< SocketStreamImplSIM * > mConnectorQueue
 < Queue of pending connection requests.This is a list of the requesting connectors. More...

vpr::Mutex mConnectorQueueMutex
 Mutex for connector queue. More...

bool mNoDelay

Detailed Description

Simulated stream sockets.

Author:
Kevin Meinert , Patrick Hartling

Definition at line 65 of file SocketStreamImplSIM.h.


Constructor & Destructor Documentation

vpr::SocketStreamImplSIM::SocketStreamImplSIM void    [inline]
 

Default constructor.

This initializes the member variables.

Definition at line 75 of file SocketStreamImplSIM.h.

References vpr::SocketTypes::STREAM.

Referenced by accept, and addConnector.

00076       : SocketImplSIM(vpr::SocketTypes::STREAM), mNoDelay(false)
00077    {
00078       /* Do nothing. */ ;
00079    }

vpr::SocketStreamImplSIM::SocketStreamImplSIM const vpr::InetAddr   local_addr,
const vpr::InetAddr   remote_addr
[inline]
 

Constructor.

This initializes the member variables. In particular, the local and remote addresses for this socket are initialized using the given addresses.

Postcondition:
The member variables are initialized with the mType variable in particular set to SOCK_STREAM.
Parameters:
local_addr  The local address for this socket (the one it is bound to).
remote_addr  The remote address for this socket. This is used for a socket that will connect to another socket.

Definition at line 94 of file SocketStreamImplSIM.h.

References vpr::SocketTypes::STREAM.

00096       : SocketImplSIM(local_addr, remote_addr, vpr::SocketTypes::STREAM),
00097         mNoDelay( false )
00098    {
00099       mLocalAddr  = local_addr;
00100       mRemoteAddr = remote_addr;
00101    }

virtual vpr::SocketStreamImplSIM::~SocketStreamImplSIM void    [inline, virtual]
 

Destructor.

This currently does nothing but act as a placeholder.

Definition at line 106 of file SocketStreamImplSIM.h.

00107    {
00108       /* Do nothing. */ ;
00109    }


Member Function Documentation

vpr::ReturnStatus vpr::SocketStreamImplSIM::listen const int    backlog = 5
 

Puts this socket into the listening state where it listens for incoming connection requests.

Precondition:
The socket has been opened and bound to the address in mLocalAddr.
Postcondition:
The socket is in a listening state waiting for incoming connection requests.
Parameters:
backlog  The maximum length of the queue of pending connections.
Returns:
vpr::ReturnStatus::Success is returned if this socket is now in a listening state.
vpr::ReturnStatus::Failure is returned otherwise.

Definition at line 55 of file SocketStreamImplSIM.cpp.

References vpr::sim::Controller::instance.

00056 {
00057    return vpr::sim::Controller::instance()->getSocketManager().listen(this, backlog);
00058 }

vpr::ReturnStatus vpr::SocketStreamImplSIM::accept SocketStreamImplSIM &    client_sock,
vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Accepts an incoming connection request and return the connected socket to the caller in the given socket object reference.

Precondition:
The socket is open and is in a listening state.
Postcondition:
When a connection is established, the given vpr::SocketStream object is assigned the newly connected socket.
Parameters:
sock  A reference to a vpr::SocketStream object that will be used to return the connected socket created.
timeout  The length of time to wait for the accept call to return.
Returns:
vpr::ReturnStatus::Success is returned if the incoming request has been handled, and the given SocketStream object is a valid, connected socket.
vpr::ReturnStatus::WouldBlock is returned if this is a non-blocking socket, and there are no waiting connection requests.
vpr::ReturnStatus::Timeout is returned when no connections requests arrived within the given timeout period.
vpr::ReturnStatus::Failure is returned if the accept failed. The given vpr::SocketStream object is not modified in this case.

Definition at line 60 of file SocketStreamImplSIM.cpp.

References vpr::MutexPosix::acquire, vpr::sim::Controller::addConnectionCompletionEvent, vpr::sim::SocketManager::findRoute, vpr::sim::Controller::getClock, vpr::sim::Clock::getCurrentTime, vpr::sim::Controller::getSocketManager, vpr::sim::Controller::instance, vpr::SocketImplSIM::mBlocking, mConnectorQueue, mConnectorQueueMutex, vpr::SocketImplSIM::mLocalAddr, vpr::MutexPosix::release, vpr::ReturnStatus::setCode, vpr::InetAddrBSD::setPort, SocketStreamImplSIM, vprASSERT, vprDBG_ALL, vprDBG_STATE_LVL, vprDEBUG, vprDEBUG_FLUSH, and vpr::ReturnStatus::WouldBlock.

00062 {
00063    vpr::ReturnStatus status;
00064 
00065    mConnectorQueueMutex.acquire();
00066 
00067    if ( ! mConnectorQueue.empty() )
00068    {
00069       SocketStreamImplSIM* peer_ptr = mConnectorQueue.front();    // The peer requesting the connection
00070       mConnectorQueue.pop();
00071 
00072       mConnectorQueueMutex.release();
00073 
00074       vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00075          << "SocketStreamImplSIM::accept() [" << mLocalAddr
00076          << "]: Got pending connector from " << peer_ptr->getLocalAddr()
00077          << "\n" << vprDEBUG_FLUSH;
00078 
00079       // -- Set known properties of the next socket
00080       client_sock.mRemoteAddr = peer_ptr->mLocalAddr;    // Get the remote node's address (it must have called bind, so this is final)
00081       client_sock.mOpen       = true;
00082       client_sock.mBlocking   = mBlocking;
00083       client_sock.setConnectState(peer_ptr);
00084 
00085       // Get an address for the new socket, bind it, and attach the socket to
00086       // the correct node.
00087       vpr::sim::Controller* controller = vpr::sim::Controller::instance();
00088       vpr::sim::SocketManager& sock_mgr = controller->getSocketManager();
00089       client_sock.mLocalAddr  = mLocalAddr;              // Start with local socket's address
00090       client_sock.mLocalAddr.setPort(0);                 // Clear port so that we get a unique one
00091       client_sock.bind();                                // Bind to a port (and assign to net node)
00092 
00093       // Now define the route for messages between the two sockets.
00094       // - Sets the path inside both sockets (path to the other socket)
00095       sock_mgr.findRoute(peer_ptr, client_sock.getHandle());
00096 
00097       peer_ptr->completeConnection( client_sock.getHandle() );
00098 
00099       controller->addConnectionCompletionEvent(controller->getClock().getCurrentTime(),
00100                                                peer_ptr);
00101 
00102       // Make sure the peer's remote address has the right address.
00103       // Prior to this point, it has the port of the accepting socket.
00104       vprASSERT(peer_ptr->mRemoteAddr == client_sock.mLocalAddr && "Connector doesn't know peer's IP address");
00105    }
00106    else
00107    {
00108       mConnectorQueueMutex.release();
00109       status.setCode(vpr::ReturnStatus::WouldBlock);
00110    }
00111 
00112    return status;
00113 }

vpr::ReturnStatus vpr::SocketStreamImplSIM::setNoDelay bool    setting [inline]
 

Sets the current no-delay status for this socket.

If no-delay is true, then the Nagel algorithm will be disabled. Of course, there is no Nagel algorithm for sim sockets.

Parameters:
enable_val  The Boolean enable/disable state for no-delay on this socket.

Definition at line 161 of file SocketStreamImplSIM.h.

00162    {
00163       mNoDelay = setting;
00164       return vpr::ReturnStatus();
00165    }

vpr::ReturnStatus vpr::SocketStreamImplSIM::addConnector vpr::SocketImplSIM   peerSock
 

Parameters:
connector  The socket making the connection.

Definition at line 115 of file SocketStreamImplSIM.cpp.

References vpr::MutexPosix::acquire, vpr::SocketImplSIM::getLocalAddr, vpr::ReturnStatus::InProgress, mConnectorQueue, mConnectorQueueMutex, vpr::SocketImplSIM::mLocalAddr, vpr::MutexPosix::release, SocketStreamImplSIM, vprASSERT, vprDBG_ALL, vprDBG_STATE_LVL, vprDEBUG, and vprDEBUG_FLUSH.

Referenced by vpr::sim::SocketManager::connect.

00116 {
00117    SocketStreamImplSIM* stream_remote;
00118 
00119    vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00120       << "SocketStreamImplSIM::addConnector() [" << mLocalAddr
00121       << "]: Adding connector from " << peerSock->getLocalAddr() << "\n"
00122       << vprDEBUG_FLUSH;
00123 
00124    stream_remote   = dynamic_cast<SocketStreamImplSIM*>(peerSock);
00125 
00126    vprASSERT(NULL != stream_remote && "Tried to connect to a non-stream socket!");
00127 
00128    mConnectorQueueMutex.acquire();
00129    {
00130       mConnectorQueue.push(stream_remote);
00131    }
00132    mConnectorQueueMutex.release();
00133 
00134    return vpr::ReturnStatus(vpr::ReturnStatus::InProgress);
00135 }

vpr::Uint32 vpr::SocketStreamImplSIM::getConnectorCount void    const [inline]
 

Gets the current number of connectors waiting to connect to this socket.

Definition at line 176 of file SocketStreamImplSIM.h.

Referenced by isReadReady.

00177    {
00178       return mConnectorQueue.size();
00179    }

vpr::ReturnStatus vpr::SocketStreamImplSIM::isReadReady   const [virtual]
 

Tests if this socket can read without blocking.

Postcondition:
Depending on the state of the socket, the caller is informed if a read will block or not.
Returns:
vpr::ReturnStatus::Succeed is returned if this socket can read without blocking. That is, there is data waiting to be read from its arrival queue.
vpr::ReturnStatus::Timeout is returned if this socket did not become ready for reading within the timeout period.
vpr::ReturnStatus::Fail is returned if this socket is not ready for reading. This can happen if the socket is not open, not connected, or without any received data.

Implements vpr::SocketImplSIM.

Definition at line 137 of file SocketStreamImplSIM.cpp.

References vpr::ReturnStatus::Fail, getConnectorCount, vpr::SocketImplSIM::isConnected, vpr::SocketImplSIM::isOpen, vpr::SocketImplSIM::mArrivedQueue, vpr::ReturnStatus::setCode, and vpr::ReturnStatus::Succeed.

00138 {
00139    vpr::ReturnStatus status(vpr::ReturnStatus::Fail);
00140 
00141    // To be able to read, we must be open, connected, and have a non-empty
00142    // queue of arrived messages.
00143    if ( isOpen() && isConnected() && (! mArrivedQueue.empty()) )
00144    {
00145       status.setCode(vpr::ReturnStatus::Succeed);
00146    }
00147 
00148    if ( isOpen() && getConnectorCount() > 0 )
00149    {
00150       status.setCode(vpr::ReturnStatus::Succeed);
00151    }
00152 
00153    return status;
00154 }

vpr::ReturnStatus vpr::SocketStreamImplSIM::isWriteReady   const [virtual]
 

Tests if this socket can write without blocking.

Postcondition:
Depending on the state of the socket, the caller is informed if a write will succeed or not.
Returns:
vpr::ReturnStatus::Succeed is returned if this socket can write without blocking. That is, this socket is in a connected state, and data can be sent to its peer right away.
vpr::ReturnStatus::Timeout is returned if this socket did not become ready for writing within the timeout period.
vpr::ReturnStatus::Fail is returned if this socket is not ready for writing. This can happen if the socket is not open or not connected.

Implements vpr::SocketImplSIM.

Definition at line 156 of file SocketStreamImplSIM.cpp.

References vpr::ReturnStatus::Fail, vpr::SocketImplSIM::isConnected, vpr::SocketImplSIM::isOpen, and vpr::ReturnStatus::setCode.

00157 {
00158    vpr::ReturnStatus status;
00159 
00160    // We cannot write if we are not open or we not are connected.
00161    if ( ! isOpen() || ! isConnected() )
00162    {
00163       status.setCode(vpr::ReturnStatus::Fail);
00164    }
00165 
00166    return status;
00167 }


Member Data Documentation

std::queue<SocketStreamImplSIM*> vpr::SocketStreamImplSIM::mConnectorQueue [protected]
 

< Queue of pending connection requests.This is a list of the requesting connectors.

Definition at line 187 of file SocketStreamImplSIM.h.

Referenced by accept, and addConnector.

vpr::Mutex vpr::SocketStreamImplSIM::mConnectorQueueMutex [protected]
 

Mutex for connector queue.

Definition at line 188 of file SocketStreamImplSIM.h.

Referenced by accept, and addConnector.

bool vpr::SocketStreamImplSIM::mNoDelay [protected]
 

Definition at line 190 of file SocketStreamImplSIM.h.


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