nidas  v1.2-1520
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
nidas::core::ServerSocketIODevice Class Reference

An IODevice supporting a TCP or UNIX server socket. More...

#include <ServerSocketIODevice.h>

Inheritance diagram for nidas::core::ServerSocketIODevice:
Inheritance graph
[legend]

Public Member Functions

 ServerSocketIODevice ()
 Create a SocketIODevice. More...
 
virtual ~ServerSocketIODevice ()
 
int getReadFd () const
 The file descriptor used when reading from this SocketIODevice. More...
 
int getWriteFd () const
 The file descriptor used when writing to this sensor. More...
 
void open (int flags) throw (nidas::util::IOException,nidas::util::InvalidParameterException)
 open the socket. More...
 
size_t read (void *buf, size_t len) throw (nidas::util::IOException)
 Read from the sensor. More...
 
size_t read (void *buf, size_t len, int msecTimeout) throw (nidas::util::IOException)
 Read from the sensor with a timeout in milliseconds. More...
 
size_t write (const void *buf, size_t len) throw (nidas::util::IOException)
 Write to the sensor. More...
 
void ioctl (int, void *, size_t) throw (nidas::util::IOException)
 
void close () throw (nidas::util::IOException)
 close the sensor (and any associated FIFOs). More...
 
void setTcpNoDelay (bool val) throw (nidas::util::IOException)
 
bool getTcpNoDelay () throw (nidas::util::IOException)
 
virtual void setName (const std::string &val)
 Set the device name to be opened for this sensor. More...
 
virtual const std::string & getName () const
 
virtual size_t getBytesAvailable () const throw (nidas::util::IOException)
 Return how many bytes are available to read on this IODevice. More...
 

Protected Member Functions

void closeServerSocket () throw (nidas::util::IOException)
 

Private Member Functions

 ServerSocketIODevice (const ServerSocketIODevice &)
 No copying. More...
 
ServerSocketIODeviceoperator= (const ServerSocketIODevice &)
 No assignment. More...
 

Private Attributes

int _addrtype
 The type of the destination address, AF_INET or AF_UNIX. More...
 
std::string _unixPath
 Path name of AF_UNIX socket. More...
 
int _sockPort
 Port number that is parsed from sensor name. More...
 
nidas::util::auto_ptr
< nidas::util::SocketAddress
_sockAddr
 The destination socket address. More...
 
nidas::util::ServerSocket_serverSocket
 The listen socket. More...
 
nidas::util::Socket_socket
 
bool _tcpNoDelay
 

Detailed Description

An IODevice supporting a TCP or UNIX server socket.

This class has a critical limitation and isn't currently used anywhere in NIDAS. The IODevice::open() method should not block, and this class violates that because open() does a nidas::util::ServerSocket::accept() which can block forever. To really support this class we need to spawn a ServerSocket listening thread.

Constructor & Destructor Documentation

ServerSocketIODevice::ServerSocketIODevice ( )

Create a SocketIODevice.

No IO operations to the sensor are performed in the constructor (hence no IOExceptions).

ServerSocketIODevice::~ServerSocketIODevice ( )
virtual

References close(), and closeServerSocket().

nidas::core::ServerSocketIODevice::ServerSocketIODevice ( const ServerSocketIODevice )
private

No copying.

Member Function Documentation

void ServerSocketIODevice::close ( )
throw (nidas::util::IOException
)
virtual
void ServerSocketIODevice::closeServerSocket ( )
throw (nidas::util::IOException
)
protected
virtual size_t nidas::core::IODevice::getBytesAvailable ( ) const
throw (nidas::util::IOException
)
inlinevirtualinherited

Return how many bytes are available to read on this IODevice.

This method is only useful when ioctl FIONREAD is supported on this this IODevice, as for example with a UDP socket. It is not available, and not necessary, on most other devices, like serial ports, TCP sockets, or devices with nidas driver module support, in which case it will return an nidas::util::IOException. It is an optimization for use with UDP sockets, where after select determines that data is available on the socket file descriptor, a read will only read one datagram, even if there are more than one packet available, which would not be optimal if a sensor generated many small packets. Rather than returning back to select, we check if there are more datagrams to read. This is not necessary on other types of IODevices, where it is just a matter of using a big enough buffer to get all (or most) available data after a select.

According to man udp(7), FIONREAD will return 0 if the size of the next packet is 0. So, if select/poll indicate there is data available, one read at least should be done, just in case a sensor sends a datagram of length 0, otherwise it will never be consumed.

References err, nidas::core::IODevice::getName(), nidas::core::IODevice::getReadFd(), and nidas::core::IODevice::ioctl().

Referenced by nidas::core::DSMSensor::getBytesAvailable().

virtual const std::string& nidas::core::IODevice::getName ( ) const
inlinevirtualinherited
int nidas::core::ServerSocketIODevice::getReadFd ( ) const
inlinevirtual

The file descriptor used when reading from this SocketIODevice.

Implements nidas::core::IODevice.

References _socket, and nidas::util::Socket::getFd().

bool nidas::core::ServerSocketIODevice::getTcpNoDelay ( )
throw (nidas::util::IOException
)
inline

References _tcpNoDelay.

Referenced by open().

int nidas::core::ServerSocketIODevice::getWriteFd ( ) const
inlinevirtual

The file descriptor used when writing to this sensor.

Implements nidas::core::IODevice.

References _socket, and nidas::util::Socket::getFd().

void nidas::core::ServerSocketIODevice::ioctl ( int  ,
void *  ,
size_t   
)
throw (nidas::util::IOException
)
inlinevirtual
void ServerSocketIODevice::open ( int  flags)
throw (nidas::util::IOException,
nidas::util::InvalidParameterException
)
virtual
ServerSocketIODevice& nidas::core::ServerSocketIODevice::operator= ( const ServerSocketIODevice )
private

No assignment.

size_t nidas::core::ServerSocketIODevice::read ( void *  buf,
size_t  len 
)
throw (nidas::util::IOException
)
inlinevirtual

Read from the sensor.

Implements nidas::core::IODevice.

References _socket, len, and nidas::util::Socket::recv().

size_t nidas::core::ServerSocketIODevice::read ( void *  buf,
size_t  len,
int  msecTimeout 
)
throw (nidas::util::IOException
)
inlinevirtual

Read from the sensor with a timeout in milliseconds.

Implements nidas::core::IODevice.

References _socket, len, nidas::util::Socket::recv(), and nidas::util::Socket::setTimeout().

virtual void nidas::core::IODevice::setName ( const std::string &  val)
inlinevirtualinherited

Set the device name to be opened for this sensor.

References nidas::core::IODevice::_devname.

Referenced by nidas::core::UnixIODevice::UnixIODevice().

void nidas::core::ServerSocketIODevice::setTcpNoDelay ( bool  val)
throw (nidas::util::IOException
)
inline
size_t nidas::core::ServerSocketIODevice::write ( const void *  buf,
size_t  len 
)
throw (nidas::util::IOException
)
inlinevirtual

Write to the sensor.

Implements nidas::core::IODevice.

References _socket, len, and nidas::util::Socket::send().

Member Data Documentation

int nidas::core::ServerSocketIODevice::_addrtype
private

The type of the destination address, AF_INET or AF_UNIX.

Referenced by open().

nidas::util::ServerSocket* nidas::core::ServerSocketIODevice::_serverSocket
private

The listen socket.

This isn't in an auto_ptr because one must close the socket prior to deleting it. The nidas::util::Socket destructor does not close the file descriptor.

Referenced by closeServerSocket(), and open().

nidas::util::auto_ptr<nidas::util::SocketAddress> nidas::core::ServerSocketIODevice::_sockAddr
private

The destination socket address.

Referenced by open().

nidas::util::Socket* nidas::core::ServerSocketIODevice::_socket
private
int nidas::core::ServerSocketIODevice::_sockPort
private

Port number that is parsed from sensor name.

Referenced by open().

bool nidas::core::ServerSocketIODevice::_tcpNoDelay
private

Referenced by getTcpNoDelay(), and setTcpNoDelay().

std::string nidas::core::ServerSocketIODevice::_unixPath
private

Path name of AF_UNIX socket.

Referenced by open().


The documentation for this class was generated from the following files: