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

vpr::SocketImplSIM Class Reference

Implementation class for simulated sockets. More...

#include <SocketImplSIM.h>

Inheritance diagram for vpr::SocketImplSIM:

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

Collaboration graph
[legend]
List of all members.

Public Methods

virtual ~SocketImplSIM ()
 Destructor. More...

const std::string & getName () const
 Gets the "name" of this socket. More...

vpr::ReturnStatus open ()
 Opens the socket. More...

bool isOpen () const
 Gets the open state of this socket. More...

bool isBound () const
vpr::ReturnStatus close ()
 Closes the socket. More...

vpr::IOSysSIM::Handle getHandle () const
 Gets the handle to this socket. More...

vpr::ReturnStatus setReuseAddr (bool adr)
vpr::ReturnStatus bind ()
 Binds this socket to the address in the host address member variable. More...

vpr::ReturnStatus setBlocking (bool blocking)
 Reconfigures the socket so that it is in blocking or non-blocking mode. More...

bool isBlocking () const
 Gets the current blocking state for the socket. More...

vpr::ReturnStatus connect (vpr::Interval timeout=vpr::Interval::NoTimeout)
 Connects the socket on the client side to the server side. More...

vpr::ReturnStatus completeConnection (SocketImplSIM *connectedPeer)
 Completes a previously started connection to a given peer. More...

bool isConnected () const
 Gets the status of a possibly connected socket. More...

const vpr::SocketTypes::TypegetType () const
 Gets the type of this socket (e.g., vpr::SocketTypes::STREAM). More...

const vpr::InetAddrgetLocalAddr () const
vpr::ReturnStatus setLocalAddr (const vpr::InetAddr &addr)
const vpr::InetAddrgetRemoteAddr () const
vpr::ReturnStatus setRemoteAddr (const vpr::InetAddr &addr)
virtual vpr::Uint32 availableBytes () const
 Returns the number of bytes currently available for reading. More...

vpr::ReturnStatus read_i (void *buffer, const vpr::Uint32 length, vpr::Uint32 &data_read, vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the read template method. More...

vpr::ReturnStatus read_i (boost::shared_ptr< std::vector< vpr::Uint8 > > &msgData, vpr::Uint32 &data_read, vpr::Interval timeout=vpr::Interval::NoTimeout)
 Exactly like read_i except takes MessageDataPtr directly for zero copy networking Updates msgData to point at the new message data. More...

vpr::ReturnStatus readn_i (void *buffer, const vpr::Uint32 length, vpr::Uint32 &data_read, vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the readn template method. More...

vpr::ReturnStatus readn_i (boost::shared_ptr< std::vector< vpr::Uint8 > > &msgData, vpr::Uint32 &data_read, vpr::Interval timeout=vpr::Interval::NoTimeout)
 Exactly like read_i except takes MessageDataPtr directly for zero copy networking. More...

vpr::ReturnStatus write_i (const void *buffer, const vpr::Uint32 length, vpr::Uint32 &data_written, vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the write template method. More...

vpr::ReturnStatus write_i (boost::shared_ptr< std::vector< vpr::Uint8 > > msgData, vpr::Uint32 &data_written, vpr::Interval timeout=vpr::Interval::NoTimeout)
 Exactly like write_i except takes MessageDataPtr directly for zero copy networking Starts passing a shared copy of msgData across the network. More...

vpr::ReturnStatus getOption (const vpr::SocketOptions::Types option, struct vpr::SocketOptions::Data &data)
 Retrieves the value for the given option as set on the socket. More...

vpr::ReturnStatus setOption (const vpr::SocketOptions::Types option, const struct vpr::SocketOptions::Data &data)
 Sets a value for the given option on the socket using the given data block. More...

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

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

vpr::ReturnStatus inExceptState ()
 Tests if this socket is in an exceptional state. More...

void addArrivedMessage (vpr::sim::MessagePtr msg)
void setPathToPeer (vpr::sim::NetworkGraph::VertexListPtr path)
void setNetworkNode (const vpr::sim::NetworkGraph::net_vertex_t &node)
vpr::sim::NetworkGraph::net_vertex_t getNetworkNode () const

Protected Types

typedef std::deque< vpr::sim::MessagePtrarrival_queue_t

Protected Methods

void disconnect ()
 SocketImplSIM (const vpr::SocketTypes::Type sock_type)
 This just initializes member variables to reasonable defaults. More...

 SocketImplSIM (const vpr::InetAddr &local_addr, const vpr::InetAddr &remote_addr, const vpr::SocketTypes::Type sock_type)
 Standard constructor. More...

void setConnectState (vpr::SocketImplSIM *peer)
 Sets this socket's connected state using the given pointer value. More...


Protected Attributes

std::string mName
bool mOpen
bool mBound
bool mBlocking
vpr::InetAddr mLocalAddr
 The local site's address structure. More...

vpr::InetAddr mRemoteAddr
 The remote site's address structure. More...

const vpr::SocketTypes::Type mType
 STREAM, DATAGRAM, or RAW. More...

bool mReuseAddr
 reuse this address? More...

arrival_queue_t mArrivedQueue
 FIFO of arrived msgs. More...

vpr::Mutex mArrivedQueueMutex
 Mutex for the arrived message queue. More...

vpr::sim::NetworkGraph::net_vertex_t mNetworkNode
vpr::sim::NetworkGraph::VertexListPtr mPathToPeer

Friends

class vpr::sim::SocketManager

Detailed Description

Implementation class for simulated sockets.

This can be used in the vpr::Socket_t bridge template.

Definition at line 84 of file SocketImplSIM.h.


Member Typedef Documentation

typedef std::deque<vpr::sim::MessagePtr> vpr::SocketImplSIM::arrival_queue_t [protected]
 

Definition at line 567 of file SocketImplSIM.h.


Constructor & Destructor Documentation

vpr::SocketImplSIM::~SocketImplSIM   [virtual]
 

Destructor.

This will close the socket.

Definition at line 59 of file SocketImplSIM.cpp.

References close, vprDBG_ALL, vprDBG_HEX_LVL, vprDBG_STATE_LVL, vprDEBUG, and vprDEBUG_FLUSH.

00060 {
00061 vpr::DebugOutputGuard dbg_output(vprDBG_ALL, vprDBG_STATE_LVL,
00062                                  std::string("SocketImplSIM destructor: Deleting socket\n"),
00063                                  std::string("Done deleting socket.\n"));
00064 
00065    if ( mOpen )
00066    {
00067       close();
00068    }
00069 
00070    vprDEBUG(vprDBG_ALL, vprDBG_HEX_LVL)
00071       << "Deleting memory at address " << std::hex << this << std::dec
00072       << std::endl << vprDEBUG_FLUSH;
00073 }

vpr::SocketImplSIM::SocketImplSIM const vpr::SocketTypes::Type    sock_type [protected]
 

This just initializes member variables to reasonable defaults.

Precondition:
None.
Postcondition:
The member variables are initialized accordingly to reasonable defaults.

Definition at line 517 of file SocketImplSIM.cpp.

Referenced by vpr::SocketDatagramImplSIM::SocketDatagramImplSIM.

00518    : mOpen(false)
00519    , mBound(false)
00520    , mBlocking(false)
00521    , mType(sock_type)
00522    , mReuseAddr(false)
00523    , mConnected(false)
00524    , mPeer(NULL)
00525 {
00526    /* Do nothing. */ ;
00527 }

vpr::SocketImplSIM::SocketImplSIM const vpr::InetAddr   local_addr,
const vpr::InetAddr   remote_addr,
const vpr::SocketTypes::Type    sock_type
[protected]
 

Standard constructor.

This takes two vpr::InetAddr objects, a local address and a remote address.

Precondition:
None.
Postcondition:
The member variables are initialized with the given values.
Parameters:
local_addr  The local address for the socket.
remote_addr  The remote address for the socket.
sock_type  The type of the socket to be created.

Definition at line 529 of file SocketImplSIM.cpp.

00532    : mOpen(false)
00533    , mBound(false)
00534    , mBlocking(false)
00535    , mLocalAddr(local_addr)
00536    , mRemoteAddr(remote_addr)
00537    , mType(sock_type)
00538    , mConnected(false)
00539    , mPeer(NULL)
00540 {
00541    /* Do nothing. */ ;
00542 }


Member Function Documentation

const std::string& vpr::SocketImplSIM::getName   const [inline]
 

Gets the "name" of this socket.

It is typically the address of the peer host.

Returns:
An object containing the "name" of this socket.

Definition at line 102 of file SocketImplSIM.h.

00103    {
00104       return mName;
00105    }

vpr::ReturnStatus vpr::SocketImplSIM::open   [inline]
 

Opens the socket.

This creates a new socket using the domain and type options set through member variables.

Precondition:
mLocalAddr and mType have been set to values recognized by the SimSocketManager::socket() call.
Postcondition:
The open state is set to true.
Returns:
vpr::ReturnStatus::Succeed is returned if the socket was opened successfully.
vpr::ReturnStatus::Fail is returned if the socket could not be opened.

Definition at line 120 of file SocketImplSIM.h.

00121    {
00122       mOpen = true;
00123 
00124       return vpr::ReturnStatus();
00125    }

bool vpr::SocketImplSIM::isOpen   const [inline]
 

Gets the open state of this socket.

Precondition:
None.
Postcondition:
The boolean value giving the open state is returned to the caller.
Returns:
true is returned if this socket is open; false otherwise.

Definition at line 137 of file SocketImplSIM.h.

Referenced by vpr::sim::SocketManager::bind, vpr::SocketStreamImplSIM::isReadReady, and vpr::SocketStreamImplSIM::isWriteReady.

00138    {
00139       return mOpen;
00140    }

bool vpr::SocketImplSIM::isBound   const [inline]
 

Definition at line 142 of file SocketImplSIM.h.

Referenced by vpr::sim::SocketManager::bind, vpr::sim::SocketManager::connect, vpr::sim::SocketManager::listen, vpr::sim::SocketManager::sendMessageTo, and vpr::sim::SocketManager::unbind.

00143    {
00144       return mBound;
00145    }

vpr::ReturnStatus vpr::SocketImplSIM::close  
 

Closes the socket.

Precondition:
The socket is open.
Postcondition:
An attempt is made to close the socket. The resulting status is returned to the caller. If the socket is closed, mOpen is set to false.
Returns:
vpr::ReturnStatus::Succeed is returned if the socket was closed successfully.
vpr::ReturnStatus::Fail is returned if the socket could not be closed for some reason.

Definition at line 75 of file SocketImplSIM.cpp.

References disconnect, vpr::sim::Controller::instance, isConnected, mBound, mLocalAddr, mOpen, mPathToPeer, vprASSERT, vprDBG_ALL, vprDBG_STATE_LVL, vprDEBUG, and vprDEBUG_FLUSH.

Referenced by ~SocketImplSIM.

00076 {
00077 vpr::DebugOutputGuard dbg_output(vprDBG_ALL, vprDBG_STATE_LVL,
00078                                  std::string("SocketImplSIM::close(): Closing socket ...\n"),
00079                                  std::string("Done closing socket.\n"));
00080    vpr::ReturnStatus status;
00081 
00082    vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00083       << "SocketImplSIM::close: " << mLocalAddr << std::endl << vprDEBUG_FLUSH;
00084 
00085    if ( isConnected() )
00086    {
00087       vprASSERT(mPeer != NULL && "I am connected to a NULL peer");
00088 
00089       // We tell our peer that we are disconnecting.  This is a little
00090       // different than how real sockets work, but since we communicate
00091       // directly with our peer, this is a reasonable thing to do.
00092       mPeer->disconnect();
00093       disconnect();
00094       vpr::sim::Controller::instance()->flushPath(this, mPathToPeer);
00095    }
00096 
00097    vprASSERT(mPeer == NULL && "I should not have a peer at this point");
00098 
00099    if ( mBound )
00100    {
00101       vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL)
00102          << "SocketImplSIM::close: Unbinding: " << mLocalAddr << std::endl
00103          << vprDEBUG_FLUSH;
00104       // Release the node to which we were bound.
00105       status = vpr::sim::Controller::instance()->getSocketManager().unbind(this);
00106       mBound = false;
00107    }
00108 
00109    mOpen = false;
00110 
00111    return status;
00112 }

vpr::IOSysSIM::Handle vpr::SocketImplSIM::getHandle   const [inline]
 

Gets the handle to this socket.

Definition at line 163 of file SocketImplSIM.h.

References vpr::IOSysSIM::Handle.

00164    {
00165       return this;
00166    }

vpr::ReturnStatus vpr::SocketImplSIM::setReuseAddr bool    adr [inline]
 

Definition at line 169 of file SocketImplSIM.h.

00170    {
00171       mReuseAddr = adr;
00172       return vpr::ReturnStatus();
00173    }

vpr::ReturnStatus vpr::SocketImplSIM::bind  
 

Binds this socket to the address in the host address member variable.

Precondition:
The socket is open, and mLocalAddr has been initialized properly.
Postcondition:
The socket is bound to the address in mLocalAddr.
Returns:
vpr::ReturnStatus::Succeed is returned if the socket was bound to the address successfully.
vpr::ReturnStatus::Fail if the socket could not be bound to the address in mLocalAddr.

Definition at line 114 of file SocketImplSIM.cpp.

References vpr::sim::SocketManager::bind, vpr::sim::Controller::instance, mBound, and vpr::ReturnStatus::success.

Referenced by connect, and vpr::SocketDatagramImplSIM::sendto.

00115 {
00116    vpr::ReturnStatus status;
00117    vpr::sim::SocketManager& sock_mgr =
00118       vpr::sim::Controller::instance()->getSocketManager();
00119 
00120    status = sock_mgr.bind(this);
00121    mBound = status.success();
00122 
00123    return status;
00124 }

vpr::ReturnStatus vpr::SocketImplSIM::setBlocking bool    blocking [inline]
 

Reconfigures the socket so that it is in blocking or non-blocking mode.

Postcondition:
Processes will block when accessing the socket.
Returns:
vpr::ReturnStatus::Succeed is returned if the socket's blocking mode is set to blocking.
vpr::ReturnStatus::Fail is returned otherwise.

Definition at line 197 of file SocketImplSIM.h.

00198    {
00199       mBlocking = blocking;
00200       return vpr::ReturnStatus(); // success
00201    }

bool vpr::SocketImplSIM::isBlocking   const [inline]
 

Gets the current blocking state for the socket.

Returns:
true is returned if the socket is in blocking mode. Otherwise, false is returned.

Definition at line 209 of file SocketImplSIM.h.

00210    {
00211       return mBlocking;
00212    }

vpr::ReturnStatus vpr::SocketImplSIM::connect vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Connects the socket on the client side to the server side.

For a datagram socket, this makes the address given to the constructor the default destination for all packets. For a stream socket, this has the effect of establishing a connection with the destination.

Precondition:
The socket is open.
Postcondition:
The socket is connected to the address in mLocalAddr. For a stream socket, this means that a connection for future communication has been established. For a datagram socket, the default destination for all packets is now mLocalAddr.
Returns:
vpr::ReturnStatus::Succeed is returned if the connection was made and completed.
vpr::ReturnStatus::InProgress is returned if this is a non-blocking socket and the connection will complete in the background.
vpr::ReturnStatus::Fail is returned if the connect could not be made.

Definition at line 126 of file SocketImplSIM.cpp.

References bind, vpr::sim::SocketManager::connect, vpr::sim::Controller::instance, mBound, mOpen, mPathToPeer, mRemoteAddr, and vprASSERT.

00127 {
00128    vpr::ReturnStatus status;
00129    vprASSERT(mOpen && "An unopened socket cannot connect");
00130    vpr::sim::SocketManager& sock_mgr =
00131       vpr::sim::Controller::instance()->getSocketManager();
00132 
00133    // If not bound, then bind us
00134    if (!mBound)
00135    {
00136       bind();
00137    }
00138 
00139    vprASSERT(mBound && "We must be bound first");
00140 
00141    status = sock_mgr.connect(this, mRemoteAddr, mPathToPeer, timeout);
00142    // NOTE: We are not connected until the other side says so
00143 
00144    return status;
00145 }

vpr::ReturnStatus vpr::SocketImplSIM::completeConnection SocketImplSIM *    connectedPeer
 

Completes a previously started connection to a given peer.

NOTE: UDP sockets can use this function as well. It lets them set a remote sockets peer and connection status.

Definition at line 150 of file SocketImplSIM.cpp.

References mRemoteAddr, setConnectState, and vpr::ReturnStatus::Succeed.

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

00151 {
00152    setConnectState(connectedPeer);
00153    mRemoteAddr = connectedPeer->getLocalAddr();
00154 
00155    return vpr::ReturnStatus::Succeed;
00156 }

bool vpr::SocketImplSIM::isConnected   const [inline]
 

Gets the status of a possibly connected socket.

Precondition:
This socket is opened and has been connected to a peer.
Returns:
true is returned if this socket is still connected.
false is returned if this socket is not currently connected.

Definition at line 255 of file SocketImplSIM.h.

Referenced by close, vpr::SocketStreamImplSIM::isReadReady, and vpr::SocketStreamImplSIM::isWriteReady.

00256    {
00257       return mConnected;
00258    }

const vpr::SocketTypes::Type& vpr::SocketImplSIM::getType   const [inline]
 

Gets the type of this socket (e.g., vpr::SocketTypes::STREAM).

Precondition:
The socket implementation pointer is valid.
Postcondition:
The socket type for this socket is returned to the caller.
Returns:
A vpr::SocketTypes::Type value giving the socket type for this socket.

Definition at line 269 of file SocketImplSIM.h.

Referenced by vpr::sim::NetworkNode::addSocket, vpr::sim::SocketManager::assignToNode, vpr::sim::SocketManager::listen, vpr::sim::NetworkNode::removeSocket, and vpr::sim::SocketManager::sendMessageTo.

00270    {
00271       return mType;
00272    }

const vpr::InetAddr& vpr::SocketImplSIM::getLocalAddr   const [inline]
 

Definition at line 274 of file SocketImplSIM.h.

Referenced by vpr::SocketStreamImplSIM::addConnector, vpr::sim::NetworkNode::addSocket, vpr::sim::SocketManager::assignToNode, vpr::sim::SocketManager::listen, vpr::sim::NetworkNode::removeSocket, vpr::sim::SocketManager::unassignFromNode, and vpr::sim::SocketManager::unbind.

00275    {
00276       return mLocalAddr;
00277    }

vpr::ReturnStatus vpr::SocketImplSIM::setLocalAddr const vpr::InetAddr   addr
 

Definition at line 158 of file SocketImplSIM.cpp.

References vpr::ReturnStatus::Fail, mLocalAddr, vpr::ReturnStatus::setCode, vprASSERT, vprDBG_ALL, vprDBG_HVERB_LVL, vprDEBUG, and vprDEBUG_FLUSH.

Referenced by vpr::sim::SocketManager::assignToNode.

00159 {
00160    vpr::ReturnStatus status;
00161 
00162    if ( mBound )
00163    {
00164       vprASSERT(false && "Can't change address of a bound socket");
00165       status.setCode(vpr::ReturnStatus::Fail);
00166    }
00167    else
00168    {
00169       vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL)
00170          << "SocketImplSIM::setLocalAddr(): Changing local address from "
00171          << mLocalAddr << " to " << addr << std::endl << vprDEBUG_FLUSH;
00172       mLocalAddr = addr;
00173    }
00174 
00175    return status;
00176 }

const vpr::InetAddr& vpr::SocketImplSIM::getRemoteAddr   const [inline]
 

Definition at line 281 of file SocketImplSIM.h.

00282    {
00283       return mRemoteAddr;
00284    }

vpr::ReturnStatus vpr::SocketImplSIM::setRemoteAddr const vpr::InetAddr   addr
 

Definition at line 178 of file SocketImplSIM.cpp.

References vpr::ReturnStatus::Fail, mRemoteAddr, vpr::ReturnStatus::setCode, vprDBG_ALL, vprDBG_HVERB_LVL, vprDEBUG, and vprDEBUG_FLUSH.

00179 {
00180    vpr::ReturnStatus status;
00181 
00182    // If we are connected, we cannot change our remote address.
00183    if ( mConnected )
00184    {
00185       status.setCode(vpr::ReturnStatus::Fail);
00186    }
00187    else
00188    {
00189       vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL)
00190          << "SocketImplSIM::setLocalAddr(): Changing remote address from "
00191          << mRemoteAddr << " to " << addr << std::endl << vprDEBUG_FLUSH;
00192       mRemoteAddr = addr;
00193    }
00194 
00195    return status;
00196 }

virtual vpr::Uint32 vpr::SocketImplSIM::availableBytes   const [virtual]
 

Returns the number of bytes currently available for reading.

vpr::ReturnStatus vpr::SocketImplSIM::read_i void *    buffer,
const vpr::Uint32    length,
vpr::Uint32   data_read,
vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Implementation of the read template method.

This reads at most the specified number of bytes from the socket into the given buffer.

Precondition:
The device is open for reading, and the buffer is at least length bytes long.
Postcondition:
The given buffer has length bytes copied into it from the device, and the number of bytes read successfully is returned to the caller via the bytes_read parameter.
Parameters:
buffer  A pointer to the buffer where the device's buffer contents are to be stored.
length  The number of bytes to be read.
bytes_read  The number of bytes read into the buffer.
timeout  The maximum amount of time to wait for data to be available for reading. This argument is optional and defaults to vpr::Interval::NoTimeout
Returns:
vpr::ReturnStatus::Succeed is returned if the read operation completed successfully.
vpr::ReturnStatus::WouldBlock if the device is in non-blocking mode, and there is no data to read.
vpr::ReturnStatus::Timeout is returned if the read could not begin within the timeout interval.
vpr::ReturnStatus::Fail is returned if the read operation ailed.

Definition at line 213 of file SocketImplSIM.cpp.

References vpr::MutexPosix::acquire, mArrivedQueue, mArrivedQueueMutex, mOpen, vpr::MutexPosix::release, vprASSERT, and vpr::ReturnStatus::WouldBlock.

00217 {
00218    vpr:: ReturnStatus status;
00219    vprASSERT(mOpen && "Cannot read on an unopened socket");
00220 
00221    // This is a rather long critical sectino, but we need to be sure that the
00222    // arrvied queue stays in a consistent state while we are working.
00223    mArrivedQueueMutex.acquire();
00224    {
00225       if ( ! mArrivedQueue.empty() )
00226       {
00227          vpr::sim::MessagePtr message;
00228          size_t copy_len, msg_size;
00229 
00230          // Remove the message from the arrival queue.
00231          message = mArrivedQueue.front();
00232 
00233          msg_size = message->getSize();
00234 
00235          // Use the smaller of length and msg_size for the actual amount of
00236          // data to copy.
00237          copy_len = (length > msg_size) ? msg_size : length;
00238 
00239          // Complete the read operation.
00240          memcpy(buffer, message->getBody(), copy_len);
00241          data_read = copy_len;
00242 
00243          // If there was no resizing performed on the message (the resize value
00244          // is 0), then we have read the entire message into our buffer.
00245          if ( message->resize(copy_len) == 0 )
00246          {
00247             mArrivedQueue.pop_front();
00248          }
00249       }
00250       // Nothing is in the queue, so we tell the caller that the operation is
00251       // in progress.
00252       else
00253       {
00254          status.setCode(vpr::ReturnStatus::WouldBlock);
00255          data_read = 0;
00256       }
00257    }
00258    mArrivedQueueMutex.release();
00259 
00260    return status;
00261 }

vpr::ReturnStatus vpr::SocketImplSIM::read_i boost::shared_ptr< std::vector< vpr::Uint8 > > &    msgData,
vpr::Uint32   data_read,
vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Exactly like read_i except takes MessageDataPtr directly for zero copy networking Updates msgData to point at the new message data.

vpr::ReturnStatus vpr::SocketImplSIM::readn_i void *    buffer,
const vpr::Uint32    length,
vpr::Uint32   data_read,
vpr::Interval    timeout = vpr::Interval::NoTimeout
[inline]
 

Implementation of the readn template method.

This reads exactly the specified number of bytes from the socket into the given buffer.

Precondition:
The device is open for reading, and the buffer is at least length bytes long.
Postcondition:
The given buffer has length bytes copied into it from the device, and the number of bytes read successfully is returned to the caller via the bytes_read parameter.
Parameters:
buffer  A pointer to the buffer where the device's buffer contents are to be stored.
length  The number of bytes to be read.
bytes_read  The number of bytes read into the buffer.
timeout  The maximum amount of time to wait for data to be available for reading. This argument is optional and defaults to vpr::Interval::NoTimeout
Returns:
vpr::ReturnStatus::Succeed is returned if the read operation completed successfully.
vpr::ReturnStatus::WouldBlock if the device is in non-blocking mode, and there is no data to read.
vpr::ReturnStatus::Timeout is returned if the read could not begin within the timeout interval.
vpr::ReturnStatus::Fail is returned if the read operation failed.

Definition at line 362 of file SocketImplSIM.h.

References vpr::Interval::NoTimeout.

00365    {
00366       return read_i(buffer, length, data_read, timeout);
00367    }

vpr::ReturnStatus vpr::SocketImplSIM::readn_i boost::shared_ptr< std::vector< vpr::Uint8 > > &    msgData,
vpr::Uint32   data_read,
vpr::Interval    timeout = vpr::Interval::NoTimeout
[inline]
 

Exactly like read_i except takes MessageDataPtr directly for zero copy networking.

Updates msgData to point at the new message data.

Definition at line 373 of file SocketImplSIM.h.

References vpr::Interval::NoTimeout.

00376    {
00377       return read_i(msgData, data_read, timeout);
00378    }

vpr::ReturnStatus vpr::SocketImplSIM::write_i const void *    buffer,
const vpr::Uint32    length,
vpr::Uint32   data_written,
vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Implementation of the write template method.

This writes the buffer to the socket.

Precondition:
The device is open for writing.
Postcondition:
The given buffer is written to the I/O device, and the number of bytes written successfully is returned to the caller via the bytes_written parameter.
Parameters:
buffer  A pointer to the buffer to be written.
length  The length of the buffer.
bytes_written  The number of bytes written to the device.
timeout  The maximum amount of time to wait for data to be available for writing. This argument is optional and defaults to vpr::Interval::NoTimeout.
Returns:
vpr::ReturnStatus::Succeed is returned if the write operation completed successfully.
vpr::ReturnStatus::WouldBlock is returned if the handle is in non-blocking mode, and the write operation could not be completed.
vpr::ReturnStatus::Timeout is returned if the write could not begin within the timeout interval.
vpr::ReturnStatus::Fail is returned if the write operation failed.

Definition at line 298 of file SocketImplSIM.cpp.

References vpr::ReturnStatus::Fail, vpr::sim::Controller::instance, mBound, mOpen, mPathToPeer, vpr::ReturnStatus::setCode, vprASSERT, vprDBG_ALL, vprDBG_HEX_LVL, vprDEBUG, vprDEBUG_CONT, vprDEBUG_FLUSH, and vprDEBUG_NEXT.

00302 {
00303    vpr::ReturnStatus status;
00304    vprASSERT(mBound && "We must be bound first");
00305    vprASSERT(mOpen && "We must be open first");
00306 
00307    if ( mPeer == NULL )
00308    {
00309       vprASSERT(false && "Trying to write to NULL peer");      // XXX: This may not be a good way to do this
00310       status.setCode(vpr::ReturnStatus::Fail);
00311       data_written = 0;
00312    }
00313    else
00314    {
00315       data_written = length;
00316 
00317 #ifdef VPR_DEBUG
00318       vpr::Uint32 remainder;
00319       const char* msg_ptr = (const char*) buffer;
00320       remainder = length % 4;
00321 
00322       vprDEBUG(vprDBG_ALL, vprDBG_HEX_LVL)
00323          << "SocketImplSIM::write_i(): Message (" << length
00324          << " bytes in 4-byte hex blocks):" << vprDEBUG_FLUSH;
00325 
00326       for ( vpr::Uint32 i = 0; i < length; i += 4 )
00327       {
00328          if ( i % 16 == 0 )
00329          {
00330             vprDEBUG_NEXT(vprDBG_ALL, vprDBG_HEX_LVL)
00331                << "\n" << std::hex << std::setfill('0') << std::setw(8)
00332                << *((vpr::Uint32*) (&msg_ptr[i])) << " " << vprDEBUG_FLUSH;
00333          }
00334          else
00335          {
00336             vprDEBUG_CONT(vprDBG_ALL, vprDBG_HEX_LVL)
00337                << std::hex << std::setfill('0') << std::setw(8)
00338                << *((vpr::Uint32*) (&msg_ptr[i])) << " " << vprDEBUG_FLUSH;
00339          }
00340       }
00341 
00342       for ( vpr::Uint32 i = 0; i < remainder; i++ )
00343       {
00344          vprDEBUG_CONT(vprDBG_ALL, vprDBG_HEX_LVL) << "00" << vprDEBUG_FLUSH;
00345       }
00346 
00347       vprDEBUG_NEXT(vprDBG_ALL, vprDBG_HEX_LVL)
00348          << "\n---------------------------\n" << std::dec << vprDEBUG_FLUSH;
00349 #endif
00350 
00351       vprASSERT(!mPathToPeer->empty() && "Path not set");
00352       vpr::sim::MessagePtr msg(new vpr::sim::Message(buffer, length));
00353       msg->setPath(mPathToPeer, this, mPeer);
00354       vpr::sim::Controller::instance()->getSocketManager().sendMessage(msg);
00355    }
00356 
00357    return status;
00358 }

vpr::ReturnStatus vpr::SocketImplSIM::write_i boost::shared_ptr< std::vector< vpr::Uint8 > >    msgData,
vpr::Uint32   data_written,
vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Exactly like write_i except takes MessageDataPtr directly for zero copy networking Starts passing a shared copy of msgData across the network.

vpr::ReturnStatus vpr::SocketImplSIM::getOption const vpr::SocketOptions::Types    option,
struct vpr::SocketOptions::Data   data
 

Retrieves the value for the given option as set on the socket.

Parameters:
option  The option to be queried.
data  A data buffer that will be used to store the value of the given option.
Returns:
vpr::ReturnStatus::Succeed is returned if the value for the given option was retrieved successfully.
vpr::ReturnStatus;:Fail is returned otherwise.

Definition at line 389 of file SocketImplSIM.cpp.

References vpr::ReturnStatus::Fail, mReuseAddr, vpr::SocketOptions::Data::recv_buffer_size, vpr::SocketOptions::Data::reuse_addr, vpr::SocketOptions::Data::send_buffer_size, vpr::ReturnStatus::setCode, vpr::SOCK_MAX_BUFFER_SIZE, and vpr::SocketOptions::Data::type_of_service.

00391 {
00392    vpr::ReturnStatus status;
00393 
00394    switch ( option )
00395    {
00396       // Socket-level options.
00397       case vpr::SocketOptions::Linger:
00398          status.setCode(vpr::ReturnStatus::Fail);
00399          break;
00400       case vpr::SocketOptions::ReuseAddr:
00401          data.reuse_addr = mReuseAddr;
00402          break;
00403       case vpr::SocketOptions::KeepAlive:
00404          status.setCode(vpr::ReturnStatus::Fail);
00405          break;
00406       case vpr::SocketOptions::RecvBufferSize:
00407          data.recv_buffer_size = SOCK_MAX_BUFFER_SIZE;
00408          break;
00409       case vpr::SocketOptions::SendBufferSize:
00410          data.send_buffer_size = SOCK_MAX_BUFFER_SIZE;
00411          break;
00412 
00413       // IP-level options.
00414       case vpr::SocketOptions::IpTimeToLive:
00415          status.setCode(vpr::ReturnStatus::Fail);
00416          break;
00417       case vpr::SocketOptions::IpTypeOfService:
00418          data.type_of_service = vpr::SocketOptions::LowDelay;
00419          break;
00420       case vpr::SocketOptions::AddMember:
00421          status.setCode(vpr::ReturnStatus::Fail);
00422          break;
00423       case vpr::SocketOptions::DropMember:
00424          status.setCode(vpr::ReturnStatus::Fail);
00425          break;
00426       case vpr::SocketOptions::McastInterface:
00427          status.setCode(vpr::ReturnStatus::Fail);
00428          break;
00429       case vpr::SocketOptions::McastTimeToLive:
00430          status.setCode(vpr::ReturnStatus::Fail);
00431          break;
00432       case vpr::SocketOptions::McastLoopback:
00433          status.setCode(vpr::ReturnStatus::Fail);
00434          break;
00435 
00436       // TCP-level options.
00437       case vpr::SocketOptions::NoDelay:
00438          status.setCode(vpr::ReturnStatus::Fail);
00439          break;
00440       case vpr::SocketOptions::MaxSegment:
00441          status.setCode(vpr::ReturnStatus::Fail);
00442          break;
00443     }
00444 
00445    return status;
00446 }

vpr::ReturnStatus vpr::SocketImplSIM::setOption const vpr::SocketOptions::Types    option,
const struct vpr::SocketOptions::Data   data
 

Sets a value for the given option on the socket using the given data block.

Parameters:
option  The option whose value will be set.
data  A data buffer containing the value to be used in setting the socket option.

Definition at line 448 of file SocketImplSIM.cpp.

References vpr::ReturnStatus::Fail, mReuseAddr, vpr::SocketOptions::Data::reuse_addr, and vpr::ReturnStatus::setCode.

00450 {
00451    vpr::ReturnStatus status;
00452 
00453    switch ( option )
00454    {
00455       // Socket-level options.
00456       case vpr::SocketOptions::Linger:
00457          status.setCode(vpr::ReturnStatus::Fail);
00458          break;
00459       case vpr::SocketOptions::ReuseAddr:
00460          mReuseAddr = data.reuse_addr;
00461          break;
00462       case vpr::SocketOptions::KeepAlive:
00463          status.setCode(vpr::ReturnStatus::Fail);
00464          break;
00465       case vpr::SocketOptions::RecvBufferSize:
00466          status.setCode(vpr::ReturnStatus::Fail);
00467          break;
00468       case vpr::SocketOptions::SendBufferSize:
00469          status.setCode(vpr::ReturnStatus::Fail);
00470          break;
00471 
00472       // IP-level options.
00473       case vpr::SocketOptions::IpTimeToLive:
00474          status.setCode(vpr::ReturnStatus::Fail);
00475          break;
00476       case vpr::SocketOptions::IpTypeOfService:
00477          status.setCode(vpr::ReturnStatus::Fail);
00478          break;
00479       case vpr::SocketOptions::AddMember:
00480          status.setCode(vpr::ReturnStatus::Fail);
00481          break;
00482       case vpr::SocketOptions::DropMember:
00483          status.setCode(vpr::ReturnStatus::Fail);
00484          break;
00485       case vpr::SocketOptions::McastInterface:
00486          status.setCode(vpr::ReturnStatus::Fail);
00487          break;
00488       case vpr::SocketOptions::McastTimeToLive:
00489          status.setCode(vpr::ReturnStatus::Fail);
00490          break;
00491       case vpr::SocketOptions::McastLoopback:
00492          status.setCode(vpr::ReturnStatus::Fail);
00493          break;
00494 
00495       // TCP-level options.
00496       case vpr::SocketOptions::NoDelay:
00497          status.setCode(vpr::ReturnStatus::Fail);
00498          break;
00499       case vpr::SocketOptions::MaxSegment:
00500          status.setCode(vpr::ReturnStatus::Fail);
00501          break;
00502     }
00503 
00504    return status;
00505 }

virtual vpr::ReturnStatus vpr::SocketImplSIM::isReadReady   const [pure 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.

Implemented in vpr::SocketDatagramImplSIM.

virtual vpr::ReturnStatus vpr::SocketImplSIM::isWriteReady   const [pure 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.

Implemented in vpr::SocketDatagramImplSIM.

vpr::ReturnStatus vpr::SocketImplSIM::inExceptState  
 

Tests if this socket is in an exceptional state.

Postcondition:
Depending on the state of the socket, the caller is informed if this socket is in an exception state or not.
Returns:
vpr::ReturnStatus::Succeed is returned if this socket is in an excptional state.
vpr::ReturnStatus::Fail is returned otherwise.

Definition at line 507 of file SocketImplSIM.cpp.

References vpr::ReturnStatus::Fail.

00508 {
00509    // XXX: For now, we never go into an exceptional state.
00510    return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00511 }

void vpr::SocketImplSIM::addArrivedMessage vpr::sim::MessagePtr    msg [inline]
 

Definition at line 489 of file SocketImplSIM.h.

References vprASSERT.

00490    {
00491       vprASSERT(msg->getDestinationSocket() == this && "Message delivered to incorrect destination");
00492       vpr::Guard<vpr::Mutex> guard(mArrivedQueueMutex);
00493       mArrivedQueue.push_back(msg);
00494    }

void vpr::SocketImplSIM::setPathToPeer vpr::sim::NetworkGraph::VertexListPtr    path [inline]
 

Definition at line 496 of file SocketImplSIM.h.

References vpr::sim::NetworkGraph::VertexListPtr.

Referenced by vpr::sim::SocketManager::findRoute.

00497    {
00498       mPathToPeer = path;
00499    }

void vpr::SocketImplSIM::setNetworkNode const vpr::sim::NetworkGraph::net_vertex_t   node [inline]
 

Definition at line 501 of file SocketImplSIM.h.

References vpr::sim::NetworkGraph::net_vertex_t.

Referenced by vpr::sim::SocketManager::assignToNode.

00502    {
00503       mNetworkNode  = node;
00504    }

vpr::sim::NetworkGraph::net_vertex_t vpr::SocketImplSIM::getNetworkNode   const [inline]
 

Definition at line 506 of file SocketImplSIM.h.

References vpr::sim::NetworkGraph::net_vertex_t.

Referenced by vpr::sim::SocketManager::connect, vpr::sim::SocketManager::findRoute, and vpr::sim::SocketManager::sendMessageTo.

00507    {
00508       return mNetworkNode;
00509    }

void vpr::SocketImplSIM::disconnect   [protected]
 

Definition at line 544 of file SocketImplSIM.cpp.

References setConnectState.

Referenced by close.

00545 {
00546    // XXX: This is potentially not the best way to disconnect, but it's the
00547    // best I have come up with so far.
00548    setConnectState(NULL);
00549 }

void vpr::SocketImplSIM::setConnectState vpr::SocketImplSIM *    peer [inline, protected]
 

Sets this socket's connected state using the given pointer value.

Depending on the value, the connected state of thsi socket is updated accordingly.

Postcondition:
mConnected will be set to true if peer is non-NULL and false otherwise.

Definition at line 549 of file SocketImplSIM.h.

Referenced by completeConnection, and disconnect.

00550    {
00551       mPeer      = peer;
00552       mConnected = (NULL != mPeer); // We are connected if mPeer is non-NULL
00553    }


Friends And Related Function Documentation

friend class vpr::sim::SocketManager [friend]
 

Definition at line 513 of file SocketImplSIM.h.


Member Data Documentation

std::string vpr::SocketImplSIM::mName [protected]
 

Definition at line 555 of file SocketImplSIM.h.

bool vpr::SocketImplSIM::mOpen [protected]
 

Definition a