nidas  v1.2-1520
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 
38 namespace nidas { namespace core {
39 
43 class UnixIOChannel: public IOChannel {
44 
45 public:
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 
70  {
71  rqstr->connected(this);
72  }
73 
77  IOChannel* connect() throw(nidas::util::IOException)
78  {
79  return this;
80  }
81 
85  void setNonBlocking(bool val) throw (nidas::util::IOException);
86 
90  bool isNonBlocking() const throw (nidas::util::IOException);
91 
92  virtual bool isNewInput() const { return _newInput; }
93 
97  size_t read(void* buf, size_t len) throw (nidas::util::IOException)
98  {
99  ssize_t res = ::read(_fd,buf,len);
100  if (res < 0) throw nidas::util::IOException(getName(),"read",errno);
101  _newInput = false;
102  return res;
103  }
104 
108  size_t write(const void* buf, size_t len) throw (nidas::util::IOException)
109  {
110  ssize_t res = ::write(_fd,buf,len);
111  if (res < 0) throw nidas::util::IOException(getName(),"write",errno);
112  _newInput = false;
113  return res;
114  }
115 
119  size_t write(const struct iovec* iov, int iovcnt) throw (nidas::util::IOException)
120  {
121  ssize_t res = ::writev(_fd,iov,iovcnt);
122  if (res < 0) throw nidas::util::IOException(getName(),"write",errno);
123  _newInput = false;
124  return res;
125  }
126 
127  void close() throw (nidas::util::IOException)
128  {
129  int res = ::close(_fd);
130  if (res < 0) throw nidas::util::IOException(getName(),"close",errno);
131  }
132 
133  int getFd() const
134  {
135  return _fd;
136  }
137 
138  const std::string& getName() const { return _name; }
139 
140  void setName(const std::string& val) { _name = val; }
141 
142  void fromDOMElement(const xercesc::DOMElement*)
143  throw(nidas::util::InvalidParameterException)
144  {
146  "UnixIOChannel::fromDOMElement not supported");
147  }
148 
149 protected:
150 
156  {
157  }
158 
159  std::string _name;
160 
161  int _fd;
162 
163  bool _newInput;
164 };
165 
166 }} // namespace nidas namespace core
167 
168 #endif
IOChannel * connect()
Pretty simple, we&#39;re connected already.
Definition: UnixIOChannel.h:77
void setName(const std::string &val)
Definition: UnixIOChannel.h:140
std::string _name
Definition: UnixIOChannel.h:159
UnixIOChannel(const std::string &name, int fd)
Constructor.
Definition: UnixIOChannel.h:50
const std::string & getName() const
Definition: UnixIOChannel.h:138
void requestConnection(IOChannelRequester *rqstr)
RequestConnection just returns connected immediately.
Definition: UnixIOChannel.h:68
~UnixIOChannel()
Destructor.
Definition: UnixIOChannel.h:58
size_t write(const struct iovec *iov, int iovcnt)
Do the actual hardware write.
Definition: UnixIOChannel.h:119
A channel for Input or Output of data.
Definition: IOChannel.h:64
Simple implementation of an IOChannel, over an opened file descriptor.
Definition: UnixIOChannel.h:43
int getFd() const
Definition: UnixIOChannel.h:133
int len
Definition: sing.cc:934
size_t read(void *buf, size_t len)
Do the actual hardware read.
Definition: UnixIOChannel.h:97
int fd
Definition: twod.c:56
Definition: IOException.h:37
UnixIOChannel * clone() const
Clone invokes default copy constructor.
Definition: UnixIOChannel.h:63
virtual bool isNewInput() const
Some IOChannels, namely FileSet, which opens successive files, need to indicate when a read is from t...
Definition: UnixIOChannel.h:92
size_t write(const void *buf, size_t len)
Do the actual hardware write.
Definition: UnixIOChannel.h:108
void setNonBlocking(bool val)
Set the state of O_NONBLOCK with fcntl system call.
Definition: UnixIOChannel.cc:31
void close()
Definition: UnixIOChannel.h:127
bool isNonBlocking() const
Do fcntl to determine value of O_NONBLOCK flag.
Definition: UnixIOChannel.cc:48
int _fd
Definition: UnixIOChannel.h:161
Interface for an object that requests connections to Inputs or Outputs.
Definition: IOChannel.h:54
void fromDOMElement(const xercesc::DOMElement *)
Initialize myself from a xercesc::DOMElement.
Definition: UnixIOChannel.h:142
bool _newInput
Definition: UnixIOChannel.h:163
Definition: InvalidParameterException.h:35
UnixIOChannel(const UnixIOChannel &x)
Constructor.
Definition: UnixIOChannel.h:154