nidas  v1.2-1520
ServerSocketIODevice.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2007, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 #ifndef NIDAS_CORE_SERVERSOCKETIODEVICE_H
27 #define NIDAS_CORE_SERVERSOCKETIODEVICE_H
28 
29 #include "IODevice.h"
30 #include <nidas/util/Socket.h>
32 #include <nidas/util/auto_ptr.h>
33 
34 
35 namespace nidas { namespace core {
36 
47 
48 public:
49 
55 
56  virtual ~ServerSocketIODevice();
57 
61  int getReadFd() const
62  {
63  if (_socket) return _socket->getFd();
64  return -1;
65  }
66 
70  int getWriteFd() const {
71  if (_socket) return _socket->getFd();
72  return -1;
73  }
74 
78  void open(int flags)
80 
84  size_t read(void *buf, size_t len) throw(nidas::util::IOException)
85  {
86  return _socket->recv(buf,len);
87  }
88 
92  size_t read(void *buf, size_t len, int msecTimeout) throw(nidas::util::IOException)
93  {
94  size_t l = 0;
95  try {
96  _socket->setTimeout(msecTimeout);
97  l = _socket->recv(buf,len,msecTimeout);
98  _socket->setTimeout(0);
99  }
100  catch(const nidas::util::IOException& e) {
101  _socket->setTimeout(0);
102  throw e;
103  }
104  return l;
105  }
106 
110  size_t write(const void *buf, size_t len) throw(nidas::util::IOException)
111  {
112  return _socket->send(buf,len);
113  }
114 
115  /*
116  * Perform an ioctl on the device. Not necessary for a socket,
117  * and will throw an IOException.
118  */
119  void ioctl(int, void*, size_t) throw(nidas::util::IOException)
120  {
122  "ioctl","not supported on SocketIODevice");
123  }
124 
128  void close() throw(nidas::util::IOException);
129 
130  void setTcpNoDelay(bool val) throw(nidas::util::IOException)
131  {
132  _tcpNoDelay = val;
133  }
134 
135  bool getTcpNoDelay() throw(nidas::util::IOException)
136  {
137  return _tcpNoDelay;
138  }
139 
140 protected:
141 
142  void closeServerSocket() throw(nidas::util::IOException);
143 
144 private:
145 
150 
154  std::string _unixPath;
155 
160 
164  nidas::util::auto_ptr<nidas::util::SocketAddress> _sockAddr;
165 
173 
174  nidas::util::Socket* _socket;
175 
177 
180 
182  ServerSocketIODevice& operator=(const ServerSocketIODevice&);
183 
184 };
185 
186 }} // namespace nidas namespace core
187 
188 #endif
Implementation of an IOChannel, over a ServerSocket.
Definition: Socket.h:297
bool getTcpNoDelay()
Definition: ServerSocketIODevice.h:135
size_t read(void *buf, size_t len, int msecTimeout)
Read from the sensor with a timeout in milliseconds.
Definition: ServerSocketIODevice.h:92
nidas::util::ServerSocket * _serverSocket
The listen socket.
Definition: ServerSocketIODevice.h:172
void setTimeout(int val)
Set the timeout for receive(), recv(), and recvfrom() methods.
Definition: Socket.h:493
ServerSocketIODevice()
Create a SocketIODevice.
Definition: ServerSocketIODevice.cc:37
virtual ~ServerSocketIODevice()
Definition: ServerSocketIODevice.cc:44
size_t read(void *buf, size_t len)
Read from the sensor.
Definition: ServerSocketIODevice.h:84
virtual const std::string & getName() const
Definition: IODevice.h:57
int _sockPort
Port number that is parsed from sensor name.
Definition: ServerSocketIODevice.h:159
int getFd() const
Fetch the file descriptor associate with this socket.
Definition: Socket.h:617
int getWriteFd() const
The file descriptor used when writing to this sensor.
Definition: ServerSocketIODevice.h:70
size_t write(const void *buf, size_t len)
Write to the sensor.
Definition: ServerSocketIODevice.h:110
void setTcpNoDelay(bool val)
Definition: ServerSocketIODevice.h:130
std::string _unixPath
Path name of AF_UNIX socket.
Definition: ServerSocketIODevice.h:154
nidas::util::Socket * _socket
Definition: ServerSocketIODevice.h:174
nidas::util::auto_ptr< nidas::util::SocketAddress > _sockAddr
The destination socket address.
Definition: ServerSocketIODevice.h:164
bool _tcpNoDelay
Definition: ServerSocketIODevice.h:176
An interface to an IO device.
Definition: IODevice.h:41
int len
Definition: sing.cc:934
int _addrtype
The type of the destination address, AF_INET or AF_UNIX.
Definition: ServerSocketIODevice.h:149
An IODevice supporting a TCP or UNIX server socket.
Definition: ServerSocketIODevice.h:46
Definition: IOException.h:37
Implementation of an IOChannel, over a Socket.
Definition: Socket.h:45
void closeServerSocket()
Definition: ServerSocketIODevice.cc:61
void open(int flags)
open the socket.
Definition: ServerSocketIODevice.cc:69
size_t recv(void *buf, size_t len, int flags=0)
Definition: Socket.h:619
size_t send(const void *buf, size_t len, int flags=MSG_NOSIGNAL)
send data on socket, see man page for send system function.
Definition: Socket.h:630
void ioctl(int, void *, size_t)
Definition: ServerSocketIODevice.h:119
void close()
close the sensor (and any associated FIFOs).
Definition: ServerSocketIODevice.cc:50
int getReadFd() const
The file descriptor used when reading from this SocketIODevice.
Definition: ServerSocketIODevice.h:61
Definition: InvalidParameterException.h:35