nidas v1.2.3
Public Member Functions | Private Member Functions | Private Attributes | List of all members
nidas::core::IODevice Class Referenceabstract

An interface to an IO device. More...

#include <IODevice.h>

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

Public Member Functions

 IODevice ()
 
virtual ~IODevice ()
 
virtual void setName (const std::string &val)
 Set the device name to be opened for this sensor.
 
virtual const std::string & getName () const
 
virtual void open (int flags)=0
 Open the device.
 
virtual int getReadFd () const =0
 The file descriptor used when reading from this sensor.
 
virtual int getWriteFd () const =0
 The file descriptor used when writing to this sensor.
 
virtual size_t read (void *buf, size_t len)=0
 Read from the sensor.
 
virtual size_t read (void *buf, size_t len, int msecTimeout)=0
 Read from the sensor with a millisecond timeout.
 
virtual size_t getBytesAvailable () const
 Return how many bytes are available to read on this IODevice.
 
virtual size_t write (const void *buf, size_t len)=0
 Write to the sensor.
 
virtual void ioctl (int request, void *buf, size_t len)=0
 
virtual void close ()=0
 Close the device.
 

Private Member Functions

 IODevice (const IODevice &)
 Derived classes don't implement copy so we won't here.
 
IODeviceoperator= (const IODevice &)
 Derived classes don't implement assignment so we won't here.
 

Private Attributes

std::string _devname
 Whether to reopen this sensor on an IOException.
 

Detailed Description

An interface to an IO device.

The main user of this interface is DSMSensor, which contains an IODevice, using it to do I/O with the underlying device.

Constructor & Destructor Documentation

◆ IODevice() [1/2]

nidas::core::IODevice::IODevice ( )
inline

◆ ~IODevice()

virtual nidas::core::IODevice::~IODevice ( )
inlinevirtual

◆ IODevice() [2/2]

nidas::core::IODevice::IODevice ( const IODevice & )
private

Derived classes don't implement copy so we won't here.

Member Function Documentation

◆ close()

virtual void nidas::core::IODevice::close ( )
pure virtual

◆ getBytesAvailable()

virtual size_t nidas::core::IODevice::getBytesAvailable ( ) const
inlinevirtual

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.

Exceptions
nidas::util::IOException

References err, getName(), getReadFd(), and ioctl().

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

◆ getName()

virtual const std::string & nidas::core::IODevice::getName ( ) const
inlinevirtual

◆ getReadFd()

virtual int nidas::core::IODevice::getReadFd ( ) const
pure virtual

◆ getWriteFd()

virtual int nidas::core::IODevice::getWriteFd ( ) const
pure virtual

◆ ioctl()

virtual void nidas::core::IODevice::ioctl ( int request,
void * buf,
size_t len )
pure virtual

◆ open()

virtual void nidas::core::IODevice::open ( int flags)
pure virtual

Open the device.

This operation should not block - it should either fail or succeed in a reasonable amount of time, typically less than 1 or 2 seconds.

Exceptions
nidas::util::IOException
nidas::util::InvalidParameterException

Implemented in nidas::core::SerialPortIODevice, nidas::core::ServerSocketIODevice, nidas::core::SocketIODevice, nidas::core::TCPSocketIODevice, nidas::core::UDPSocketIODevice, and nidas::core::UnixIODevice.

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

◆ operator=()

IODevice & nidas::core::IODevice::operator= ( const IODevice & )
private

Derived classes don't implement assignment so we won't here.

◆ read() [1/2]

virtual size_t nidas::core::IODevice::read ( void * buf,
size_t len )
pure virtual

◆ read() [2/2]

virtual size_t nidas::core::IODevice::read ( void * buf,
size_t len,
int msecTimeout )
pure virtual

◆ setName()

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

Set the device name to be opened for this sensor.

References _devname, and nidas::core::getSample().

Referenced by nidas::core::DSMSensor::open(), and nidas::core::UnixIODevice::UnixIODevice().

◆ write()

virtual size_t nidas::core::IODevice::write ( const void * buf,
size_t len )
pure virtual

Member Data Documentation

◆ _devname

std::string nidas::core::IODevice::_devname
private

Whether to reopen this sensor on an IOException.

Referenced by getName(), and setName().


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