nidas  v1.2-1520
Socket.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  ** 2005, 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_SOCKET_H
28 #define NIDAS_CORE_SOCKET_H
29 
30 #include "IOChannel.h"
31 #include "DOMable.h"
32 #include <nidas/util/Socket.h>
33 #include <nidas/util/Thread.h>
34 #include <nidas/util/UTime.h>
35 #include <nidas/util/auto_ptr.h>
36 
37 #include <string>
38 #include <iostream>
39 
40 namespace nidas { namespace core {
41 
45 class Socket: public IOChannel {
46 
47 public:
48 
52  Socket();
53 
61 
62  ~Socket();
63 
64  Socket* clone() const;
65 
68 
71 
72  virtual bool isNewInput() const { return _newInput; }
73 
74  /*
75  * The requestType is set when a socket connection has been
76  * established with McSocket.
77  */
79  {
80  _requestType = val;
81  }
82 
84  {
85  return _requestType;
86  }
87 
92  {
93  _keepAliveIdleSecs = val;
95  }
96 
100  int getKeepAliveIdleSecs() const throw (nidas::util::IOException)
101  {
103  return _keepAliveIdleSecs;
104  }
105 
106  std::list<nidas::util::Inet4NetworkInterface> getInterfaces() const
107  throw(nidas::util::IOException)
108  {
109  if (_nusocket) return _nusocket->getInterfaces();
110  return std::list<nidas::util::Inet4NetworkInterface>();
111  }
112 
117  {
118  _nonBlocking = val;
120  }
121 
125  bool isNonBlocking() const throw (nidas::util::IOException)
126  {
127  if (_nusocket) return _nusocket->isNonBlocking();
128  return _nonBlocking;
129  }
130 
131  size_t getBufferSize() const throw();
132 
136  size_t read(void* buf, size_t len) throw (nidas::util::IOException)
137  {
138  if (_firstRead) _firstRead = false;
139  else _newInput = false;
140  return _nusocket->recv(buf,len);
141  }
142 
146  size_t write(const void* buf, size_t len) throw (nidas::util::IOException)
147  {
148  // std::cerr << "nidas::core::Socket::write, len=" << len << std::endl;
149 #ifdef DEBUG
150  std::cerr << "writing, now=" << nidas::util::UTime().format(true,"%H%M%S.%3f") << " len=" << len << std::endl;
151 #endif
152  return _nusocket->send(buf,len, MSG_NOSIGNAL);
153  }
154 
158  size_t write(const struct iovec* iov, int iovcnt) throw (nidas::util::IOException)
159  {
160  // std::cerr << "nidas::core::Socket::write, len=" << len << std::endl;
161 #ifdef DEBUG
162  size_t l = 0;
163  for (int i =0; i < iovcnt; i++) l += iov[i].iov_len;
164  std::cerr << "writing, len=" << l << std::endl;
165 #endif
166  return _nusocket->send(iov,iovcnt, MSG_NOSIGNAL);
167  }
168 
169  void close() throw (nidas::util::IOException);
170 
171  int getFd() const
172  {
173  if (_nusocket) return _nusocket->getFd();
174  return -1;
175  }
176 
177  const std::string& getName() const { return _name; }
178 
179  void setName(const std::string& val) { _name = val; }
180 
182 
188  void setRemoteHostPort(const std::string& host,unsigned short port);
189 
193  const std::string& getRemoteHost() const
194  {
195  return _remoteHost;
196  }
197 
198  unsigned short getRemotePort() const
199  {
200  return _remotePort;
201  }
202 
206  const std::string& getRemoteUnixPath() const
207  {
208  return _unixPath;
209  }
210 
214  void setRemoteUnixPath(const std::string& unixPath);
215 
217 
225  throw(nidas::util::UnknownHostException);
226 
230  static IOChannel* createSocket(const xercesc::DOMElement*)
231  throw(nidas::util::InvalidParameterException);
232 
233  void fromDOMElement(const xercesc::DOMElement*)
234  throw(nidas::util::InvalidParameterException);
235 
236  class ConnectionThread: public nidas::util::Thread
237  {
238  public:
239  ConnectionThread(Socket* sock);
240  ~ConnectionThread();
241 
242  int run() throw(nidas::util::IOException);
243 
244  void interrupt();
245 
246  private:
250  };
251 
252 protected:
256  Socket(const Socket& x);
257 
258 private:
259 
260  nidas::util::auto_ptr<nidas::util::SocketAddress> _remoteSockAddr;
261 
262  std::string _remoteHost;
263 
264  unsigned short _remotePort;
265 
266  std::string _unixPath;
267 
269 
270  std::string _name;
271 
273 
275 
277 
278  bool _newInput;
279 
281 
283 
285 
287 
291  Socket& operator=(const Socket&);
292 };
293 
297 class ServerSocket: public IOChannel {
298 
299 public:
300 
304  ServerSocket();
305 
310 
311  ~ServerSocket();
312 
313  ServerSocket* clone() const;
314 
317 
318  IOChannel* connect()
320 
321  const std::string& getName() const { return _name; }
322 
323  void setName(const std::string& val) { _name = val; }
324 
325  int getFd() const
326  {
327  if (_servSock) return _servSock->getFd();
328  return -1;
329  }
330 
337  {
338  _keepAliveIdleSecs = val;
339  }
340 
344  int getKeepAliveIdleSecs() const throw (nidas::util::IOException)
345  {
346  return _keepAliveIdleSecs;
347  }
348 
354  {
355  _nonBlocking = val;
356  }
357 
361  bool isNonBlocking() const throw (nidas::util::IOException)
362  {
363  return _nonBlocking;
364  }
365 
369  size_t read(void*, size_t) throw (nidas::util::IOException)
370  {
371  assert(false);
372  return 0;
373  }
374 
378  size_t write(const void*, size_t) throw (nidas::util::IOException)
379  {
380  assert(false);
381  return 0;
382  }
383 
387  size_t write(const struct iovec*, int) throw (nidas::util::IOException)
388  {
389  assert(false);
390  return 0;
391  }
392 
393  void close() throw (nidas::util::IOException);
394 
398  static IOChannel* createSocket(const xercesc::DOMElement*)
399  throw(nidas::util::InvalidParameterException);
400 
401  void fromDOMElement(const xercesc::DOMElement*)
402  throw(nidas::util::InvalidParameterException);
403 
404  xercesc::DOMElement*
405  toDOMParent(xercesc::DOMElement* parent, bool complete) const
406  throw(xercesc::DOMException);
407 
408  xercesc::DOMElement*
409  toDOMElement(xercesc::DOMElement* node, bool complete) const
410  throw(xercesc::DOMException);
411 
412  class ConnectionThread: public nidas::util::Thread
413  {
414  public:
416  int run() throw(nidas::util::IOException);
417  void interrupt();
418 
419  private:
423  };
424 
425 protected:
429  ServerSocket(const ServerSocket& x);
430 
431 private:
432 
433  nidas::util::auto_ptr<nidas::util::SocketAddress> _localSockAddr;
434 
435  std::string _name;
436 
438 
440 
442 
443  // friend class ServerSocketConnectionThread;
444 
446 
448 
453 
454 };
455 
456 }} // namespace nidas namespace core
457 
458 #endif
Implementation of an IOChannel, over a ServerSocket.
Definition: Socket.h:297
size_t read(void *buf, size_t len)
Do the actual hardware read.
Definition: Socket.h:136
int getKeepAliveIdleSecs()
Get the current value of TCP_KEEPIDLE on this socket.
Definition: Socket.h:564
const std::string & getRemoteHost() const
Get the name of the remote host.
Definition: Socket.h:193
xercesc::DOMElement * toDOMParent(xercesc::DOMElement *parent, bool complete) const
Create a DOMElement and append it to the parent.
Definition: Socket.cc:673
void setRequestType(enum McSocketRequest val)
Definition: Socket.h:78
A class for parsing, formatting and doing operations on time, based on Unix time conventions, where leap seconds are ignored, so that there are always 60 seconds in a minute, 3600 seconds in an hour and 86400 seconds in a day.
Definition: UTime.h:76
std::string format(bool utc, const std::string &fmt) const
Format a UTime into a string.
Definition: UTime.cc:433
IOChannelRequester * _iochanRequester
Definition: Socket.h:272
const nidas::util::SocketAddress & getRemoteSocketAddress()
This method does a DNS lookup of the value of getRemoteHost(), and so it can throw an UnknownHostExce...
Definition: Socket.cc:162
Socket & operator=(const Socket &)
No assignment.
const std::string & getRemoteUnixPath() const
Get the name of the remote host.
Definition: Socket.h:206
~ServerSocket()
Definition: Socket.cc:282
An interface for a socket address.
Definition: SocketAddress.h:36
McSocketRequest
Definition: Datagrams.h:34
nidas::util::Mutex _connectionMutex
Definition: Socket.h:284
int getFd() const
Definition: Socket.h:788
bool _firstRead
Definition: Socket.h:276
std::list< Inet4NetworkInterface > getInterfaces() const
Get a list of all my network interfaces.
Definition: Socket.h:582
std::list< nidas::util::Inet4NetworkInterface > getInterfaces() const
Definition: Socket.h:106
ConnectionThread * _connectionThread
Definition: Socket.h:274
void setRemoteSocketAddress(const nidas::util::SocketAddress &val)
Definition: Socket.cc:145
std::string _unixPath
Definition: Socket.h:266
size_t write(const void *buf, size_t len)
Do the actual hardware write.
Definition: Socket.h:146
void setNonBlocking(bool val)
Do setNonBlocking(val) on underlying socket.
Definition: Socket.h:116
std::string _name
Definition: Socket.h:435
void setRemoteUnixPath(const std::string &unixPath)
Set the pathname for the unix socket connection.
Definition: Socket.cc:157
void setKeepAliveIdleSecs(int val)
Do setKeepAliveIdleSecs(int secs) on underlying socket.
Definition: Socket.h:91
bool _nonBlocking
Definition: Socket.h:282
void setName(const std::string &val)
Definition: Socket.h:323
nidas::util::auto_ptr< nidas::util::SocketAddress > _remoteSockAddr
Definition: Socket.h:260
bool isNonBlocking() const
Return isNonBlocking() of underlying socket.
Definition: Socket.h:125
void requestConnection(IOChannelRequester *service)
After the IOChannel is configured, a user of IOChannel calls requestConnection to get things started...
Definition: Socket.cc:351
size_t write(const void *, size_t)
ServerSocket should never be called to do an actual write.
Definition: Socket.h:378
A stream (TCP) socket that is used to listen for connections.
Definition: Socket.h:755
void close()
Definition: Socket.cc:309
std::string _name
Definition: Socket.h:270
const std::string & getName() const
Definition: Socket.h:321
A channel for Input or Output of data.
Definition: IOChannel.h:64
int getFd() const
Fetch the file descriptor associate with this socket.
Definition: Socket.h:617
std::string _remoteHost
Definition: Socket.h:262
void setKeepAliveIdleSecs(int val)
Set the value of keepAliveIdleSecs.
Definition: Socket.h:336
ServerSocket()
Constructor.
Definition: Socket.cc:252
bool _nonBlocking
Definition: Socket.h:447
ServerSocket * clone() const
Derived classes must provide clone.
Definition: Socket.cc:304
nidas::util::Socket * _nusocket
Definition: Socket.h:268
unsigned short _remotePort
Definition: Socket.h:264
nidas::util::auto_ptr< nidas::util::SocketAddress > _localSockAddr
Definition: Socket.h:433
ServerSocket * _socket
Definition: Socket.h:420
ConnectionThread * _connectionThread
Definition: Socket.h:441
static IOChannel * createSocket(const xercesc::DOMElement *)
Create either a Socket or a McSocket from a DOMElement.
Definition: Socket.cc:490
IOChannel * connect()
Establish a connection.
Definition: Socket.cc:323
~Socket()
Definition: Socket.cc:104
const std::string & getName() const
Definition: Socket.h:177
xercesc::DOMElement * toDOMElement(xercesc::DOMElement *node, bool complete) const
Add my content into a DOMElement.
Definition: Socket.cc:686
Socket * clone() const
Derived classes must provide clone.
Definition: Socket.cc:120
enum McSocketRequest getRequestType() const
Definition: Socket.h:83
void close()
Definition: Socket.cc:125
int len
Definition: sing.cc:934
IOChannel * connect()
Establish a connection.
Definition: Socket.cc:201
bool isNonBlocking() const
Definition: Socket.h:361
int _keepAliveIdleSecs
Definition: Socket.h:280
int getKeepAliveIdleSecs() const
Return getKeepAliveIdleSecs() on underlying socket.
Definition: Socket.h:100
void fromDOMElement(const xercesc::DOMElement *)
Initialize myself from a xercesc::DOMElement.
Definition: Socket.cc:520
size_t getBufferSize() const
Return suggested buffer length.
Definition: Socket.cc:130
IOChannelRequester * _iochanRequester
Definition: Socket.h:439
Definition: IOException.h:37
bool isNonBlocking() const
Definition: Socket.h:505
Implementation of an IOChannel, over a Socket.
Definition: Socket.h:45
void setRemoteHostPort(const std::string &host, unsigned short port)
Set the hostname and port of the remote connection.
Definition: Socket.cc:150
virtual bool isNewInput() const
Some IOChannels, namely FileSet, which opens successive files, need to indicate when a read is from t...
Definition: Socket.h:72
enum McSocketRequest _requestType
Definition: Socket.h:286
size_t write(const struct iovec *iov, int iovcnt)
Do the actual hardware write.
Definition: Socket.h:158
static n_u::SerialPort port
Definition: sing.cc:68
A stream (TCP) socket.
Definition: Socket.h:430
nidas::util::ServerSocket * _servSock
Definition: Socket.h:437
int getFd() const
Definition: Socket.h:325
size_t recv(void *buf, size_t len, int flags=0)
Definition: Socket.h:619
void setName(const std::string &val)
Definition: Socket.h:179
int _keepAliveIdleSecs
Definition: Socket.h:445
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
nidas::util::Inet4Address getRemoteInet4Address()
Definition: Socket.cc:184
void setNonBlocking(bool val)
The blocking flag that will be set on accepted connections.
Definition: Socket.h:353
void requestConnection(IOChannelRequester *service)
After the IOChannel is configured, a user of IOChannel calls requestConnection to get things started...
Definition: Socket.cc:236
static IOChannel * createSocket(const xercesc::DOMElement *)
Create either a Socket or a McSocket from a DOMElement.
bool _newInput
Definition: Socket.h:278
ServerSocket & operator=(const ServerSocket &)
No assignment.
Socket * _socket
Definition: Socket.h:247
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
size_t write(const struct iovec *, int)
ServerSocket should never be called to do an actual write.
Definition: Socket.h:387
size_t read(void *, size_t)
ServerSocket will never be called to do an actual read.
Definition: Socket.h:369
Definition: UnknownHostException.h:35
Interface for an object that requests connections to Inputs or Outputs.
Definition: IOChannel.h:54
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition: Socket.h:500
int getFd() const
Definition: Socket.h:171
void fromDOMElement(const xercesc::DOMElement *)
Initialize myself from a xercesc::DOMElement.
Definition: Socket.cc:600
Support for IP version 4 host address.
Definition: Inet4Address.h:46
Socket()
Constructor.
Definition: Socket.cc:43
int getKeepAliveIdleSecs() const
Return keepAliveIdleSecs for this ServerSocket.
Definition: Socket.h:344
unsigned short getRemotePort() const
Definition: Socket.h:198
void setKeepAliveIdleSecs(int val)
Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive prob...
Definition: Socket.h:554