nidas  v1.2-1520
DatagramSocket.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  ** 2009, 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 
27 #ifndef NIDAS_CORE_DATAGRAMSOCKET_H
28 #define NIDAS_CORE_DATAGRAMSOCKET_H
29 
30 #include "IOChannel.h"
31 #include "DOMable.h"
32 #include <nidas/util/Socket.h>
33 #include <nidas/util/auto_ptr.h>
34 
35 #include <string>
36 #include <iostream>
37 
38 namespace nidas { namespace core {
39 
44 class DatagramSocket: public IOChannel {
45 
46 public:
47 
52 
57 
59 
61 
63 
64  DatagramSocket* clone() const;
65 
68 
70 
71  bool isNewInput() const { return false; }
72 
73  bool writeNidasHeader() const { return false; }
74 
75  std::list<nidas::util::Inet4NetworkInterface> getInterfaces() const
76  throw(nidas::util::IOException)
77  {
78  if (_nusocket) return _nusocket->getInterfaces();
79  return std::list<nidas::util::Inet4NetworkInterface>();
80  }
81 
86  {
87  _nonBlocking = val;
89  }
90 
94  bool isNonBlocking() const throw (nidas::util::IOException)
95  {
96  if (_nusocket) return _nusocket->isNonBlocking();
97  return _nonBlocking;
98  }
99 
100  size_t getBufferSize() const throw();
101 
105  size_t read(void* buf, size_t len) throw (nidas::util::IOException)
106  {
107  return _nusocket->recv(buf,len);
108  }
109 
113  size_t write(const void* buf, size_t len) throw (nidas::util::IOException)
114  {
115  return _nusocket->send(buf,len,0);
116  }
117 
121  size_t write(const struct iovec* iov, int iovcnt) throw (nidas::util::IOException)
122  {
123  return _nusocket->send(iov,iovcnt,0);
124  }
125 
126  void close() throw (nidas::util::IOException)
127  {
128  if (_nusocket) _nusocket->close();
129  }
130 
131 
132  int getFd() const
133  {
134  if (_nusocket) return _nusocket->getFd();
135  return -1;
136  }
137 
138  const std::string& getName() const { return _name; }
139 
140  void setName(const std::string& val) { _name = val; }
141 
145  const std::string& getUnixPath() const
146  {
147  return _unixPath;
148  }
149 
153  void setUnixPath(const std::string& unixPath);
154 
163 
165  throw(nidas::util::UnknownHostException);
166 
172  void setHostPort(const std::string& host,unsigned short port);
173 
177  void setPort(unsigned short port);
178 
182  const std::string& getHost() const
183  {
184  return _host;
185  }
186 
187  unsigned short getPort() const
188  {
189  return _port;
190  }
191 
192  void fromDOMElement(const xercesc::DOMElement*)
193  throw(nidas::util::InvalidParameterException);
194 
195 private:
196 
197  nidas::util::auto_ptr<nidas::util::SocketAddress> _sockAddr;
198 
199  std::string _host;
200 
201  unsigned short _port;
202 
203  std::string _unixPath;
204 
205  nidas::util::DatagramSocket* _nusocket;
206 
207  std::string _name;
208 
210 
212 
213 };
214 
215 }} // namespace nidas namespace core
216 
217 #endif
std::string _host
Definition: DatagramSocket.h:199
const std::string & getUnixPath() const
Get the unix path name.
Definition: DatagramSocket.h:145
size_t write(const void *buf, size_t len)
Do the actual hardware write.
Definition: DatagramSocket.h:113
size_t recv(void *buf, size_t len, int flags=0)
Definition: Socket.h:1063
void send(const DatagramPacketBase &packet, int flags=0)
Definition: Socket.h:1058
bool isNonBlocking() const
Definition: Socket.h:1121
void close()
Definition: DatagramSocket.h:126
void setHostPort(const std::string &host, unsigned short port)
Set the hostname and port of the remote connection.
Definition: DatagramSocket.cc:125
int getFd() const
Definition: Socket.h:1031
An interface for a socket address.
Definition: SocketAddress.h:36
void setName(const std::string &val)
Definition: DatagramSocket.h:140
void setNonBlocking(bool val)
Do setNonBlocking(val) on underlying socket.
Definition: DatagramSocket.h:85
A socket for sending or receiving datagrams, either unicast, broadcast or multicast.
Definition: Socket.h:912
std::list< nidas::util::Inet4NetworkInterface > getInterfaces() const
Definition: DatagramSocket.h:75
int getFd() const
Definition: DatagramSocket.h:132
void requestConnection(IOChannelRequester *service)
After the IOChannel is configured, a user of IOChannel calls requestConnection to get things started...
Definition: DatagramSocket.cc:194
std::list< Inet4NetworkInterface > getInterfaces() const
Definition: Socket.h:1145
const nidas::util::SocketAddress & getSocketAddress()
Definition: DatagramSocket.cc:137
nidas::util::DatagramSocket * _nusocket
Definition: DatagramSocket.h:205
void close()
Definition: Socket.h:958
void setSocketAddress(const nidas::util::SocketAddress &val)
Set address for this socket.
Definition: DatagramSocket.cc:119
DatagramSocket & operator=(const DatagramSocket &rhs)
Definition: DatagramSocket.cc:63
const std::string & getHost() const
Get the name of the remote host.
Definition: DatagramSocket.h:182
A channel for Input or Output of data.
Definition: IOChannel.h:64
size_t getBufferSize() const
Return suggested buffer length.
Definition: DatagramSocket.cc:104
void fromDOMElement(const xercesc::DOMElement *)
Initialize myself from a xercesc::DOMElement.
Definition: DatagramSocket.cc:202
bool isNonBlocking() const
Return isNonBlocking() of underlying socket.
Definition: DatagramSocket.h:94
unsigned short getPort() const
Definition: DatagramSocket.h:187
Implementation of an IOChannel, over a DatagramSocket.
Definition: DatagramSocket.h:44
std::string _unixPath
Definition: DatagramSocket.h:203
std::string _name
Definition: DatagramSocket.h:207
bool isNewInput() const
Some IOChannels, namely FileSet, which opens successive files, need to indicate when a read is from t...
Definition: DatagramSocket.h:71
DatagramSocket * clone() const
Derived classes must provide clone.
Definition: DatagramSocket.cc:99
bool _nonBlocking
Definition: DatagramSocket.h:211
unsigned short _port
Definition: DatagramSocket.h:201
const std::string & getName() const
Definition: DatagramSocket.h:138
size_t read(void *buf, size_t len)
Do the actual hardware read.
Definition: DatagramSocket.h:105
int len
Definition: sing.cc:934
size_t write(const struct iovec *iov, int iovcnt)
Do the actual hardware write.
Definition: DatagramSocket.h:121
void setUnixPath(const std::string &unixPath)
Set the pathname for the unix socket connection.
Definition: DatagramSocket.cc:132
IOChannel * connect()
Establish a connection.
Definition: DatagramSocket.cc:161
Definition: IOException.h:37
static n_u::SerialPort port
Definition: sing.cc:68
~DatagramSocket()
Definition: DatagramSocket.cc:88
nidas::util::auto_ptr< nidas::util::SocketAddress > _sockAddr
Definition: DatagramSocket.h:197
DatagramSocket()
Constructor.
Definition: DatagramSocket.cc:37
bool writeNidasHeader() const
Should the NIDAS header be written to this IOChannel? NIDAS headers are not written to DatagramSocket...
Definition: DatagramSocket.h:73
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition: Socket.h:1116
Interface for an object that requests connections to Inputs or Outputs.
Definition: IOChannel.h:54
void setPort(unsigned short port)
Set the the local port number.
IOChannelRequester * _iochanRequester
Definition: DatagramSocket.h:209