nidas v1.2.3
UnixIOChannel.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_UNIXIOCHANNEL_H
28#define NIDAS_CORE_UNIXIOCHANNEL_H
29
30#include "IOChannel.h"
31
32#include <fcntl.h>
33#include <unistd.h> // read(), write(), close(), ...
34
35#include <string>
36#include <iostream>
37
38namespace nidas { namespace core {
39
43class UnixIOChannel: public IOChannel {
44
45public:
46
50 UnixIOChannel(const std::string& name, int fd):
51 _name(name),_fd(fd),_newInput(true)
52 {
53 }
54
59
63 UnixIOChannel* clone() const { return new UnixIOChannel(*this); }
64
71 {
72 rqstr->connected(this);
73 }
74
81 {
82 return this;
83 }
84
90 void setNonBlocking(bool val);
91
97 bool isNonBlocking() const;
98
99 virtual bool isNewInput() const { return _newInput; }
100
106 size_t read(void* buf, size_t len)
107 {
108 ssize_t res = ::read(_fd,buf,len);
109 if (res < 0) throw nidas::util::IOException(getName(),"read",errno);
110 _newInput = false;
111 return res;
112 }
113
119 size_t write(const void* buf, size_t len)
120 {
121 ssize_t res = ::write(_fd,buf,len);
122 if (res < 0) throw nidas::util::IOException(getName(),"write",errno);
123 _newInput = false;
124 return res;
125 }
126
132 size_t write(const struct iovec* iov, int iovcnt)
133 {
134 ssize_t res = ::writev(_fd,iov,iovcnt);
135 if (res < 0) throw nidas::util::IOException(getName(),"write",errno);
136 _newInput = false;
137 return res;
138 }
139
143 void close()
144 {
145 int res = ::close(_fd);
146 if (res < 0) throw nidas::util::IOException(getName(),"close",errno);
147 }
148
149 int getFd() const
150 {
151 return _fd;
152 }
153
154 const std::string& getName() const { return _name; }
155
156 void setName(const std::string& val) { _name = val; }
157
161 void fromDOMElement(const xercesc::DOMElement*)
162 {
164 "UnixIOChannel::fromDOMElement not supported");
165 }
166
167protected:
168
174 {
175 }
176
177 std::string _name;
178
179 int _fd;
180
182};
183
184}} // namespace nidas namespace core
185
186#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
Simple implementation of an IOChannel, over an opened file descriptor.
Definition UnixIOChannel.h:43
bool isNonBlocking() const
Do fcntl to determine value of O_NONBLOCK flag.
Definition UnixIOChannel.cc:48
void requestConnection(IOChannelRequester *rqstr)
RequestConnection just returns connected immediately.
Definition UnixIOChannel.h:70
UnixIOChannel * clone() const
Clone invokes default copy constructor.
Definition UnixIOChannel.h:63
const std::string & getName() const
Definition UnixIOChannel.h:154
~UnixIOChannel()
Destructor.
Definition UnixIOChannel.h:58
void close()
Definition UnixIOChannel.h:143
size_t write(const struct iovec *iov, int iovcnt)
Do the actual hardware write.
Definition UnixIOChannel.h:132
void fromDOMElement(const xercesc::DOMElement *)
Definition UnixIOChannel.h:161
UnixIOChannel(const std::string &name, int fd)
Constructor.
Definition UnixIOChannel.h:50
std::string _name
Definition UnixIOChannel.h:177
int _fd
Definition UnixIOChannel.h:179
virtual bool isNewInput() const
Some IOChannels, namely FileSet, which opens successive files, need to indicate when a read is from t...
Definition UnixIOChannel.h:99
IOChannel * connect()
Pretty simple, we're connected already.
Definition UnixIOChannel.h:80
size_t write(const void *buf, size_t len)
Do the actual hardware write.
Definition UnixIOChannel.h:119
void setNonBlocking(bool val)
Set the state of O_NONBLOCK with fcntl system call.
Definition UnixIOChannel.cc:31
UnixIOChannel(const UnixIOChannel &x)
Constructor.
Definition UnixIOChannel.h:172
size_t read(void *buf, size_t len)
Do the actual hardware read.
Definition UnixIOChannel.h:106
bool _newInput
Definition UnixIOChannel.h:181
void setName(const std::string &val)
Definition UnixIOChannel.h:156
int getFd() const
Definition UnixIOChannel.h:149
Definition IOException.h:37
Definition InvalidParameterException.h:35
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
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
int len
Definition sing.cc:948
int fd
Definition twod.c:56