vpr::SocketDatagramImplSIM Class Reference

Implementation of datagram sockets using simulated sockets. More...

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

Inheritance diagram for vpr::SocketDatagramImplSIM:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SocketDatagramImplSIM ()
 Default constructor.
 SocketDatagramImplSIM (const vpr::InetAddr &localAddr, const vpr::InetAddr &remoteAddr)
 Constructor.
vpr::ReturnStatus recvfrom (void *msg, const vpr::Uint32 length, vpr::InetAddr &from, vpr::Uint32 &bytesRead, const vpr::Interval timeout=vpr::Interval::NoTimeout)
vpr::ReturnStatus sendto (const void *msg, const vpr::Uint32 length, const vpr::InetAddr &to, vpr::Uint32 &bytesSent, const vpr::Interval timeout=vpr::Interval::NoTimeout)
virtual vpr::ReturnStatus isReadReady () const
 Tests if this socket can read without blocking.
virtual vpr::ReturnStatus isWriteReady () const
 Tests if this socket can write without blocking.

Detailed Description

Implementation of datagram sockets using simulated sockets.

This is used in conjunction with vpr::SocketConfiguration to create the typedef vpr::SocketDatagram.

Definition at line 60 of file SocketDatagramImplSIM.h.


Constructor & Destructor Documentation

vpr::SocketDatagramImplSIM::SocketDatagramImplSIM (  )  [inline]

Default constructor.

Postcondition:
The member variables are initialized to default values. The socket type is set to vpr::SocketTypes::DATAGRAM.

Definition at line 69 of file SocketDatagramImplSIM.h.

00070       : SocketImplSIM(vpr::SocketTypes::DATAGRAM)
00071    {
00072       /* Do nothing. */ ;
00073    }

vpr::SocketDatagramImplSIM::SocketDatagramImplSIM ( const vpr::InetAddr localAddr,
const vpr::InetAddr remoteAddr 
) [inline]

Constructor.

Postcondition:
The member variables are initialized to default values. The socket type is set to vpr::SocketTypes::DATAGRAM.
Parameters:
localAddr The local address to which this socket will be bound.
remoteAddr The remote address whith which this socket will communicate.

Definition at line 85 of file SocketDatagramImplSIM.h.

00087       : SocketImplSIM(localAddr, remoteAddr, vpr::SocketTypes::DATAGRAM)
00088    {
00089       /* Do nothing. */ ;
00090    }


Member Function Documentation

vpr::ReturnStatus vpr::SocketDatagramImplSIM::recvfrom ( void *  msg,
const vpr::Uint32  length,
vpr::InetAddr from,
vpr::Uint32 bytesRead,
const vpr::Interval  timeout = vpr::Interval::NoTimeout 
)

Definition at line 55 of file SocketDatagramImplSIM.cpp.

References vpr::SocketImplSIM::mArrivedQueue, vpr::SocketImplSIM::mArrivedQueueMutex, vpr::SocketImplSIM::mBound, vpr::ReturnStatus::setCode(), vprASSERT, and vpr::ReturnStatus::WouldBlock.

00060 {
00061    vpr::ReturnStatus status;
00062    vprASSERT(mBound && "Can recvfrom if we are not bound so they can send to us");
00063 
00064    vpr::Guard<vpr::Mutex> guard(mArrivedQueueMutex);
00065 
00066    if ( ! mArrivedQueue.empty() )
00067    {
00068       vpr::sim::MessagePtr msg_ptr = mArrivedQueue.front();
00069       vpr::Uint32 msg_size, copy_len;
00070 
00071       msg_size = msg_ptr->getSize();
00072 
00073       // Use the smaller of length and msg_size for the actual amount of
00074       // data to copy.
00075       copy_len = (length > msg_size) ? msg_size : length;
00076 
00077       // Complete the read operation.
00078       memcpy(msg, msg_ptr->getBody(), copy_len);
00079       bytes_read = copy_len;
00080 
00081       from = msg_ptr->getSourceSocket()->getLocalAddr();
00082 
00083       if ( msg_ptr->resize(copy_len) == 0 )
00084       {
00085          mArrivedQueue.pop_front();
00086       }
00087    }
00088    else
00089    {
00090       status.setCode(vpr::ReturnStatus::WouldBlock);
00091       bytes_read = 0;
00092    }
00093 
00094    return status;
00095 }

vpr::ReturnStatus vpr::SocketDatagramImplSIM::sendto ( const void *  msg,
const vpr::Uint32  length,
const vpr::InetAddr to,
vpr::Uint32 bytesSent,
const vpr::Interval  timeout = vpr::Interval::NoTimeout 
)

Definition at line 97 of file SocketDatagramImplSIM.cpp.

References vpr::SocketImplSIM::bind(), vpr::sim::Controller::instance(), vpr::SocketImplSIM::mBound, vpr::sim::SocketManager::sendMessageTo(), and vprASSERT.

00102 {
00103    vpr::ReturnStatus status;
00104    vpr::sim::SocketManager& sock_mgr =
00105       vpr::sim::Controller::instance()->getSocketManager();
00106 
00107    if ( ! mBound )
00108    {
00109       bind();      
00110    }
00111 
00112    vprASSERT( mBound && "Must be bound before sending");
00113 
00114    bytes_sent = length;
00115    vpr::sim::MessagePtr net_msg(new vpr::sim::Message(msg, length));
00116    sock_mgr.sendMessageTo(net_msg, this, to);
00117 
00118    return status;
00119 }

vpr::ReturnStatus vpr::SocketDatagramImplSIM::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 121 of file SocketDatagramImplSIM.cpp.

References vpr::ReturnStatus::Fail, vpr::SocketImplSIM::mArrivedQueue, vpr::SocketImplSIM::mOpen, and vpr::ReturnStatus::setCode().

00122 {
00123    vpr::ReturnStatus status;
00124 
00125    if ( ! mOpen || mArrivedQueue.empty() )
00126    {
00127       status.setCode(vpr::ReturnStatus::Fail);
00128    }
00129 
00130    return status;
00131 }

vpr::ReturnStatus vpr::SocketDatagramImplSIM::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 133 of file SocketDatagramImplSIM.cpp.

References vpr::ReturnStatus::Fail, vpr::SocketImplSIM::mOpen, and vpr::ReturnStatus::setCode().

00134 {
00135    vpr::ReturnStatus status;
00136 
00137    if ( ! mOpen )
00138    {
00139       status.setCode(vpr::ReturnStatus::Fail);
00140    }
00141 
00142    return status;
00143 }


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