nidas v1.2.3
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
40namespace nidas { namespace core {
41
45class Socket: public IOChannel {
46
47public:
48
52 Socket();
53
61
62 ~Socket();
63
64 Socket* clone() const;
65
70
76
77 virtual bool isNewInput() const { return _newInput; }
78
79 /*
80 * The requestType is set when a socket connection has been
81 * established with McSocket.
82 */
84 {
86 }
87
89 {
90 return _requestType;
91 }
92
99 {
102 }
103
110 {
112 return _keepAliveIdleSecs;
113 }
114
118 std::list<nidas::util::Inet4NetworkInterface> getInterfaces() const
119 {
120 if (_nusocket) return _nusocket->getInterfaces();
121 return std::list<nidas::util::Inet4NetworkInterface>();
122 }
123
129 void setNonBlocking(bool val)
130 {
133 }
134
140 bool isNonBlocking() const
141 {
142 if (_nusocket) return _nusocket->isNonBlocking();
143 return _nonBlocking;
144 }
145
146 size_t getBufferSize() const throw();
147
153 size_t read(void* buf, size_t len)
154 {
155 if (_firstRead) _firstRead = false;
156 else _newInput = false;
157 return _nusocket->recv(buf,len);
158 }
159
165 size_t write(const void* buf, size_t len)
166 {
167 // std::cerr << "nidas::core::Socket::write, len=" << len << std::endl;
168#ifdef DEBUG
169 std::cerr << "writing, now=" << nidas::util::UTime().format(true,"%H%M%S.%3f") << " len=" << len << std::endl;
170#endif
171 return _nusocket->send(buf,len, MSG_NOSIGNAL);
172 }
173
179 size_t write(const struct iovec* iov, int iovcnt)
180 {
181 // std::cerr << "nidas::core::Socket::write, len=" << len << std::endl;
182#ifdef DEBUG
183 size_t l = 0;
184 for (int i =0; i < iovcnt; i++) l += iov[i].iov_len;
185 std::cerr << "writing, len=" << l << std::endl;
186#endif
187 return _nusocket->send(iov,iovcnt, MSG_NOSIGNAL);
188 }
189
193 void close();
194
195 int getFd() const
196 {
197 if (_nusocket) return _nusocket->getFd();
198 return -1;
199 }
200
201 const std::string& getName() const { return _name; }
202
203 void setName(const std::string& val) { _name = val; }
204
206
212 void setRemoteHostPort(const std::string& host,unsigned short port);
213
217 const std::string& getRemoteHost() const
218 {
219 return _remoteHost;
220 }
221
222 unsigned short getRemotePort() const
223 {
224 return _remotePort;
225 }
226
230 const std::string& getRemoteUnixPath() const
231 {
232 return _unixPath;
233 }
234
238 void setRemoteUnixPath(const std::string& unixPath);
239
241
251
257 static IOChannel* createSocket(const xercesc::DOMElement*);
258
262 void fromDOMElement(const xercesc::DOMElement*);
263
265 {
266 public:
269
270 int run();
271
272 void interrupt();
273
274 private:
278 };
279
280protected:
284 Socket(const Socket& x);
285
286private:
287
288 nidas::util::auto_ptr<nidas::util::SocketAddress> _remoteSockAddr;
289
290 std::string _remoteHost;
291
292 unsigned short _remotePort;
293
294 std::string _unixPath;
295
297
298 std::string _name;
299
301
303
305
307
309
311
313
315
320};
321
325class ServerSocket: public IOChannel {
326
327public:
328
332 ServerSocket();
333
338
340
341 ServerSocket* clone() const;
342
347
352
353 const std::string& getName() const { return _name; }
354
355 void setName(const std::string& val) { _name = val; }
356
357 int getFd() const
358 {
359 if (_servSock) return _servSock->getFd();
360 return -1;
361 }
362
371 {
373 }
374
381 {
382 return _keepAliveIdleSecs;
383 }
384
391 void setNonBlocking(bool val)
392 {
394 }
395
399 bool isNonBlocking() const
400 {
401 return _nonBlocking;
402 }
403
409 size_t read(void*, size_t)
410 {
411 assert(false);
412 return 0;
413 }
414
420 size_t write(const void*, size_t)
421 {
422 assert(false);
423 return 0;
424 }
425
431 size_t write(const struct iovec*, int)
432 {
433 assert(false);
434 return 0;
435 }
436
440 void close();
441
447 static IOChannel* createSocket(const xercesc::DOMElement*);
448
452 void fromDOMElement(const xercesc::DOMElement*);
453
457 xercesc::DOMElement*
458 toDOMParent(xercesc::DOMElement* parent, bool complete) const;
459
463 xercesc::DOMElement*
464 toDOMElement(xercesc::DOMElement* node, bool complete) const;
465
467 {
468 public:
470 int run();
471 void interrupt();
472
473 private:
477 };
478
479protected:
483 ServerSocket(const ServerSocket& x);
484
485private:
486
487 nidas::util::auto_ptr<nidas::util::SocketAddress> _localSockAddr;
488
489 std::string _name;
490
492
494
496
497 // friend class ServerSocketConnectionThread;
498
500
502
507
508};
509
510}} // namespace nidas namespace core
511
512#endif
Interface for an object that requests connections to Inputs or Outputs.
Definition IOChannel.h:56
A channel for Input or Output of data.
Definition IOChannel.h:65
ConnectionThread(const ConnectionThread &)
void interrupt()
Interrupt this thread.
Definition Socket.cc:384
ConnectionThread & operator=(const ConnectionThread &)
int run()
The method which will run in its own thread.
Definition Socket.cc:390
ConnectionThread(ServerSocket *sock)
Definition Socket.cc:377
ServerSocket * _socket
Definition Socket.h:474
Implementation of an IOChannel, over a ServerSocket.
Definition Socket.h:325
void close()
Definition Socket.cc:307
ConnectionThread * _connectionThread
Definition Socket.h:495
IOChannel * connect()
Definition Socket.cc:321
void fromDOMElement(const xercesc::DOMElement *)
Definition Socket.cc:595
xercesc::DOMElement * toDOMElement(xercesc::DOMElement *node, bool complete) const
Definition Socket.cc:679
ServerSocket * clone() const
Derived classes must provide clone.
Definition Socket.cc:302
ServerSocket()
Constructor.
Definition Socket.cc:250
int getKeepAliveIdleSecs() const
Return keepAliveIdleSecs for this ServerSocket.
Definition Socket.h:380
int _keepAliveIdleSecs
Definition Socket.h:499
const std::string & getName() const
Definition Socket.h:353
void setNonBlocking(bool val)
The blocking flag that will be set on accepted connections.
Definition Socket.h:391
~ServerSocket()
Definition Socket.cc:280
bool isNonBlocking() const
Definition Socket.h:399
void setName(const std::string &val)
Definition Socket.h:355
void setKeepAliveIdleSecs(int val)
Set the value of keepAliveIdleSecs.
Definition Socket.h:370
std::string _name
Definition Socket.h:489
IOChannelRequester * _iochanRequester
Definition Socket.h:493
nidas::util::ServerSocket * _servSock
Definition Socket.h:491
int getFd() const
Definition Socket.h:357
size_t read(void *, size_t)
ServerSocket will never be called to do an actual read.
Definition Socket.h:409
ServerSocket & operator=(const ServerSocket &)
No assignment.
size_t write(const void *, size_t)
ServerSocket should never be called to do an actual write.
Definition Socket.h:420
xercesc::DOMElement * toDOMParent(xercesc::DOMElement *parent, bool complete) const
Definition Socket.cc:667
size_t write(const struct iovec *, int)
ServerSocket should never be called to do an actual write.
Definition Socket.h:431
nidas::util::auto_ptr< nidas::util::SocketAddress > _localSockAddr
Definition Socket.h:487
void requestConnection(IOChannelRequester *service)
Definition Socket.cc:349
bool _nonBlocking
Definition Socket.h:501
static IOChannel * createSocket(const xercesc::DOMElement *)
Create either a Socket or a McSocket from a DOMElement.
ConnectionThread(const ConnectionThread &)
void interrupt()
Interrupt this thread.
Definition Socket.cc:427
ConnectionThread(Socket *sock)
Definition Socket.cc:416
Socket * _socket
Definition Socket.h:275
ConnectionThread & operator=(const ConnectionThread &)
~ConnectionThread()
Definition Socket.cc:421
int run()
The method which will run in its own thread.
Definition Socket.cc:434
Implementation of an IOChannel, over a Socket.
Definition Socket.h:45
int getKeepAliveIdleSecs() const
Return getKeepAliveIdleSecs() on underlying socket.
Definition Socket.h:109
void setRemoteSocketAddress(const nidas::util::SocketAddress &val)
Definition Socket.cc:145
std::string _name
Definition Socket.h:298
const std::string & getRemoteHost() const
Get the name of the remote host.
Definition Socket.h:217
std::string _remoteHost
Definition Socket.h:290
void setKeepAliveIdleSecs(int val)
Do setKeepAliveIdleSecs(int secs) on underlying socket.
Definition Socket.h:98
int getFd() const
Definition Socket.h:195
size_t write(const struct iovec *iov, int iovcnt)
Do the actual hardware write.
Definition Socket.h:179
std::string _unixPath
Definition Socket.h:294
void fromDOMElement(const xercesc::DOMElement *)
Definition Socket.cc:516
Socket & operator=(const Socket &)
No assignment.
std::list< nidas::util::Inet4NetworkInterface > getInterfaces() const
Definition Socket.h:118
void requestConnection(IOChannelRequester *service)
Definition Socket.cc:235
size_t getBufferSize() const
Return suggested buffer length.
Definition Socket.cc:130
enum McSocketRequest _requestType
Definition Socket.h:314
enum McSocketRequest getRequestType() const
Definition Socket.h:88
void setNonBlocking(bool val)
Do setNonBlocking(val) on underlying socket.
Definition Socket.h:129
Socket * clone() const
Derived classes must provide clone.
Definition Socket.cc:120
bool _nonBlocking
Definition Socket.h:310
void setRemoteUnixPath(const std::string &unixPath)
Set the pathname for the unix socket connection.
Definition Socket.cc:157
ConnectionThread * _connectionThread
Definition Socket.h:302
int _keepAliveIdleSecs
Definition Socket.h:308
nidas::util::Socket * _nusocket
Definition Socket.h:296
bool _firstRead
Definition Socket.h:304
const std::string & getRemoteUnixPath() const
Get the name of the remote host.
Definition Socket.h:230
bool _newInput
Definition Socket.h:306
IOChannel * connect()
Definition Socket.cc:200
void close()
Definition Socket.cc:125
size_t read(void *buf, size_t len)
Do the actual hardware read.
Definition Socket.h:153
nidas::util::Mutex _connectionMutex
Definition Socket.h:312
Socket()
Constructor.
Definition Socket.cc:43
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
void setRemoteHostPort(const std::string &host, unsigned short port)
Set the hostname and port of the remote connection.
Definition Socket.cc:150
IOChannelRequester * _iochanRequester
Definition Socket.h:300
static IOChannel * createSocket(const xercesc::DOMElement *)
Create either a Socket or a McSocket from a DOMElement.
Definition Socket.cc:487
bool isNonBlocking() const
Return isNonBlocking() of underlying socket.
Definition Socket.h:140
void setRequestType(enum McSocketRequest val)
Definition Socket.h:83
nidas::util::Inet4Address getRemoteInet4Address()
Definition Socket.cc:183
virtual bool isNewInput() const
Some IOChannels, namely FileSet, which opens successive files, need to indicate when a read is from t...
Definition Socket.h:77
void setName(const std::string &val)
Definition Socket.h:203
nidas::util::auto_ptr< nidas::util::SocketAddress > _remoteSockAddr
Definition Socket.h:288
const std::string & getName() const
Definition Socket.h:201
size_t write(const void *buf, size_t len)
Do the actual hardware write.
Definition Socket.h:165
~Socket()
Definition Socket.cc:104
unsigned short _remotePort
Definition Socket.h:292
unsigned short getRemotePort() const
Definition Socket.h:222
Support for IP version 4 host address.
Definition Inet4Address.h:46
A C++ wrapper for a POSIX mutex.
Definition ThreadSupport.h:161
A stream (TCP) socket that is used to listen for connections.
Definition Socket.h:981
int getFd() const
Definition Socket.h:1019
An interface for a socket address.
Definition SocketAddress.h:36
A stream (TCP) socket.
Definition Socket.h:573
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition Socket.h:658
int getKeepAliveIdleSecs()
Get the current value of TCP_KEEPIDLE on this socket.
Definition Socket.h:740
size_t recv(void *buf, size_t len, int flags=0)
Definition Socket.h:813
std::list< Inet4NetworkInterface > getInterfaces() const
Get a list of all my network interfaces.
Definition Socket.h:766
int getFd() const
Fetch the file descriptor associate with this socket.
Definition Socket.h:805
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:728
bool isNonBlocking() const
Definition Socket.h:666
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:826
Definition Thread.h:83
A class for parsing, formatting and doing operations on time, based on Unix time conventions,...
Definition UTime.h:95
std::string format(bool utc, const std::string &fmt) const
Format a UTime into a string.
Definition UTime.cc:462
Sample * getSample(sampleType type, unsigned int len)
A convienence method for getting a sample of an enumerated type from a pool.
Definition Sample.cc:70
McSocketRequest
Definition Datagrams.h:34
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
static n_u::SerialPort port
Definition sing.cc:68
int len
Definition sing.cc:948