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

A basic Unix I/O device, such as a named pipe, or a watched file. More...

#include <UnixIODevice.h>

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

Public Member Functions

 UnixIODevice ()
 Constructor. More...
 
 UnixIODevice (const std::string &name)
 Constructor, passing the name of the device. More...
 
 ~UnixIODevice ()
 Destructor. More...
 
int getReadFd () const
 The file descriptor used when reading from this device. More...
 
int getWriteFd () const
 The file descriptor used when writing to this device. More...
 
void open (int flags) throw (nidas::util::IOException)
 open the device. More...
 
size_t read (void *buf, size_t len) throw (nidas::util::IOException)
 Read from the device. More...
 
size_t read (void *buf, size_t len, int msecTimeout) throw (nidas::util::IOException)
 Read from the device with a timeout in milliseconds. More...
 
size_t write (const void *buf, size_t len) throw (nidas::util::IOException)
 Write to the device. More...
 
void ioctl (int request, void *buf, size_t) throw (nidas::util::IOException)
 
void close () throw (nidas::util::IOException)
 close the device More...
 
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 Attributes

int _fd
 

Detailed Description

A basic Unix I/O device, such as a named pipe, or a watched file.

Constructor & Destructor Documentation

nidas::core::UnixIODevice::UnixIODevice ( )
inline

Constructor.

Does not open any actual device.

nidas::core::UnixIODevice::UnixIODevice ( const std::string &  name)
inline

Constructor, passing the name of the device.

Does not open the device.

References nidas::core::IODevice::setName().

nidas::core::UnixIODevice::~UnixIODevice ( )
inline

Destructor.

Does not close the device.

Member Function Documentation

void nidas::core::UnixIODevice::close ( )
throw (nidas::util::IOException
)
inlinevirtual

close the device

Implements nidas::core::IODevice.

References _fd, fd, and nidas::core::IODevice::getName().

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::UnixIODevice::getReadFd ( ) const
inlinevirtual

The file descriptor used when reading from this device.

Implements nidas::core::IODevice.

References _fd.

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

The file descriptor used when writing to this device.

Implements nidas::core::IODevice.

References _fd.

void nidas::core::UnixIODevice::ioctl ( int  request,
void *  buf,
size_t   
)
throw (nidas::util::IOException
)
inlinevirtual
void nidas::core::UnixIODevice::open ( int  flags)
throw (nidas::util::IOException
)
inlinevirtual

open the device.

Implements nidas::core::IODevice.

References _fd, and nidas::core::IODevice::getName().

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

Read from the device.

Implements nidas::core::IODevice.

References _fd, nidas::core::IODevice::getName(), and len.

size_t UnixIODevice::read ( void *  buf,
size_t  len,
int  msecTimeout 
)
throw (nidas::util::IOException
)
virtual

Read from the device with a timeout in milliseconds.

Implements nidas::core::IODevice.

References len, MSECS_PER_SEC, and NSECS_PER_MSEC.

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 UnixIODevice().

size_t nidas::core::UnixIODevice::write ( const void *  buf,
size_t  len 
)
throw (nidas::util::IOException
)
inlinevirtual

Write to the device.

Implements nidas::core::IODevice.

References _fd, nidas::core::IODevice::getName(), and len.

Member Data Documentation

int nidas::core::UnixIODevice::_fd
protected

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