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

An IODevice consisting of an UDP socket. More...

#include <UDPSocketIODevice.h>

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

Public Member Functions

 UDPSocketIODevice ()
 Create a UDPSocketIODevice. More...
 
 ~UDPSocketIODevice ()
 
void open (int flags) throw (nidas::util::IOException,nidas::util::InvalidParameterException)
 Open the socket, which does a socket bind to the remote address which is parsed from the contents of getName(). More...
 
int getReadFd () const
 The file descriptor used when reading from this SocketIODevice. More...
 
int getWriteFd () const
 The file descriptor used when writing to this 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 close () throw (nidas::util::IOException)
 close the device. More...
 
void ioctl (int, void *, size_t) 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...
 

Static Public Member Functions

static void parseAddress (const std::string &name, int &addrtype, std::string &hostname, int &port, std::string &bindaddr) throw (nidas::util::ParseException)
 Parse the getName() string to extract a socket type, destination address and optional port number. More...
 

Protected Member Functions

 UDPSocketIODevice (const UDPSocketIODevice &)
 No copy. More...
 
UDPSocketIODeviceoperator= (const UDPSocketIODevice &)
 No assignment. More...
 

Protected Attributes

nidas::util::DatagramSocket_socket
 The datagramsocket. More...
 
int _addrtype
 The type of the destination address, AF_INET or AF_UNIX. More...
 
std::string _desthost
 Destination host name from sensor name. More...
 
int _port
 Port number that is parsed from sensor name. More...
 
nidas::util::auto_ptr
< nidas::util::SocketAddress
_sockAddr
 The destination socket address. More...
 
std::string _bindAddr
 The local bind socket address. More...
 

Detailed Description

An IODevice consisting of an UDP socket.

Constructor & Destructor Documentation

UDPSocketIODevice::UDPSocketIODevice ( )

Create a UDPSocketIODevice.

No IO operations are performed in the constructor, hence no IOExceptions.

UDPSocketIODevice::~UDPSocketIODevice ( )

References close().

nidas::core::UDPSocketIODevice::UDPSocketIODevice ( const UDPSocketIODevice )
protected

No copy.

Member Function Documentation

void UDPSocketIODevice::close ( )
throw (nidas::util::IOException
)
virtual
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::UDPSocketIODevice::getReadFd ( ) const
inlinevirtual

The file descriptor used when reading from this SocketIODevice.

Implements nidas::core::IODevice.

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

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

The file descriptor used when writing to this device.

Implements nidas::core::IODevice.

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

void nidas::core::SocketIODevice::ioctl ( int  ,
void *  ,
size_t   
)
throw (nidas::util::IOException
)
inlinevirtualinherited
void UDPSocketIODevice::open ( int  flags)
throw (nidas::util::IOException,
nidas::util::InvalidParameterException
)
virtual
UDPSocketIODevice& nidas::core::UDPSocketIODevice::operator= ( const UDPSocketIODevice )
protected

No assignment.

void SocketIODevice::parseAddress ( const std::string &  name,
int &  addrtype,
std::string &  hostname,
int &  port,
std::string &  bindaddr 
)
throw (nidas::util::ParseException
)
staticinherited

Parse the getName() string to extract a socket type, destination address and optional port number.

The syntax of the set/getName() string is: ( "inet" | "sock" | "unix" | "blue" | "btspp" ) ':' address [:port] "inet" or "sock" indicate a TCP or UDP connection for use by derived classes implementing a TCP or UDP socket. The address field should then be a hostname which can be resolved to an IP address, or an IP address in dot notation. A port number is required for TCP/UDP socket connection. "unix" indicates a Unix socket connection, where address is the path name of the unix socket, for process to process communications. The port value is not used by unix sockets.

References port.

Referenced by nidas::dynld::raf::PSI9116_Sensor::open(), nidas::core::SocketIODevice::open(), and nidas::core::ServerSocketIODevice::open().

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

Read from the device.

Implements nidas::core::IODevice.

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

size_t UDPSocketIODevice::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.

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

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

Write to the device.

Implements nidas::core::IODevice.

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

Member Data Documentation

int nidas::core::SocketIODevice::_addrtype
protectedinherited

The type of the destination address, AF_INET or AF_UNIX.

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

std::string nidas::core::SocketIODevice::_bindAddr
protectedinherited

The local bind socket address.

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

std::string nidas::core::SocketIODevice::_desthost
protectedinherited

Destination host name from sensor name.

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

int nidas::core::SocketIODevice::_port
protectedinherited

Port number that is parsed from sensor name.

For a server socket this is the local port number to listen on. For a remote connection socket this is the remote port number.

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

nidas::util::auto_ptr<nidas::util::SocketAddress> nidas::core::SocketIODevice::_sockAddr
protectedinherited

The destination socket address.

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

nidas::util::DatagramSocket* nidas::core::UDPSocketIODevice::_socket
protected

The datagramsocket.

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 close(), getReadFd(), getWriteFd(), read(), and write().


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