CPN
Computational Process Networks
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
FileHandle Class Reference

Generic file handle could be a file, or a socket or a device. More...

#include <FileHandle.h>

+ Inheritance diagram for FileHandle:
+ Collaboration diagram for FileHandle:

Public Types

typedef AutoLock< PthreadMutexALock
 

Public Member Functions

 FileHandle ()
 Construct a closed FileHandle. More...
 
 FileHandle (int filed)
 Construct a FileHandle with filed as the open file descriptor. More...
 
virtual ~FileHandle ()
 Close the file descriptor. Use Reset if one wants to not close the file descriptor. More...
 
int Poll (double timeout)
 
bool Readable (bool r)
 Set that the file is currently readable or not. More...
 
bool Readable () const
 Gives the current readability status of the file. More...
 
bool Writeable (bool w)
 Set that this file is currently writeable or not. More...
 
bool Writeable () const
 Gives the current writability status of the file. More...
 
int FD () const
 
int FD (int filed)
 Set the current file descriptor. More...
 
bool Eof () const
 
bool Eof (bool e)
 Set/reset the end of file condition. More...
 
bool Good () const
 Convenience method for testing if the file this FileHandle has is open and not at end of file. More...
 
bool Closed () const
 
void SetBlocking (bool blocking)
 Manipulate how the current file handles blocking. More...
 
bool IsBlocking () const
 Test if the current file is in blocking or non blocking mode. More...
 
void Reset ()
 Clear all internal state including the file descriptor! WARNING does not close the file! More...
 
void Close ()
 Close the file and reset the internal state. More...
 
unsigned Read (void *ptr, unsigned len)
 Read data from the file descriptor. More...
 
unsigned Readv (const iovec *iov, int iovcnt)
 scatter gather io version of Read More...
 
unsigned Write (const void *ptr, unsigned len)
 Write data to the file descriptor. More...
 
unsigned Writev (const iovec *iov, int iovcnt)
 scatter gather io version of Write More...
 
void Flush ()
 Tell the OS to flush any buffers it has. May not be supported for all file types. More...
 

Static Public Member Functions

static int Poll (IteratorRef< FileHandle * > begin, IteratorRef< FileHandle * > end, double timeout)
 poll a list of FileHandles for any activity and call the appropriate On method. More...
 
static void SetBlocking (int fd, bool blocking)
 a convenience method that allows one to set the same blocking parameters for a file descriptor without setting it inside a FileHandle. More...
 
static bool IsBlocking (int fd)
 Test the given file descriptor if it is in blocking mode. More...
 

Protected Member Functions

virtual void OnReadable ()
 Called by Poll when it detects that the file is readable. More...
 
virtual void OnWriteable ()
 Called by Poll when it detects that the file is writeable. More...
 

Protected Attributes

PthreadMutex file_lock
 
int fd
 
bool readable
 
bool writeable
 
bool eof
 

Private Member Functions

 FileHandle (const FileHandle &)
 
FileHandleoperator= (const FileHandle &)
 

Detailed Description

Generic file handle could be a file, or a socket or a device.

If needed wouldn't be very hard to add an open function here.

Definition at line 41 of file FileHandle.h.

Member Typedef Documentation

Definition at line 43 of file FileHandle.h.

Constructor & Destructor Documentation

FileHandle::FileHandle ( )

Construct a closed FileHandle.

Definition at line 91 of file FileHandle.cc.

92  : fd(-1), readable(false), writeable(true), eof(false)
93 {
94 }
bool writeable
Definition: FileHandle.h:216
bool readable
Definition: FileHandle.h:215
FileHandle::FileHandle ( int  filed)

Construct a FileHandle with filed as the open file descriptor.

Parameters
filedthe file descriptor to use

Definition at line 96 of file FileHandle.cc.

97  : fd(filed), readable(false), writeable(true), eof(false)
98 {
99 }
bool writeable
Definition: FileHandle.h:216
bool readable
Definition: FileHandle.h:215
FileHandle::~FileHandle ( )
virtual

Close the file descriptor. Use Reset if one wants to not close the file descriptor.

Definition at line 101 of file FileHandle.cc.

References fd, and Reset().

101  {
102  if (fd != -1) { close(fd); }
103  Reset();
104 }
void Reset()
Clear all internal state including the file descriptor! WARNING does not close the file! ...
Definition: FileHandle.cc:138

+ Here is the call graph for this function:

FileHandle::FileHandle ( const FileHandle )
private

Member Function Documentation

void FileHandle::Close ( )

Close the file and reset the internal state.

Definition at line 146 of file FileHandle.cc.

References eof, fd, file_lock, readable, and writeable.

Referenced by CPN::ConnectionServer::Close(), CPN::RemoteContext::EntryPoint(), CPN::RemoteQueue::HandleError(), CPN::RemoteQueue::InternalCheckStatus(), and CPN::RemoteContextDaemon::Terminate().

146  {
147  ALock al(file_lock);
148  if (fd != -1) {
149  if (close(fd) != 0) {
150  throw ErrnoException();
151  }
152  fd = -1;
153  }
154  readable = false;
155  writeable = false;
156  eof = false;
157 }
bool writeable
Definition: FileHandle.h:216
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215

+ Here is the caller graph for this function:

bool FileHandle::Closed ( ) const
inline
bool FileHandle::Eof ( ) const
inline
Returns
the current end of file condition

Definition at line 115 of file FileHandle.h.

References eof, and file_lock.

Referenced by CPN::RemoteQueue::EnqueuePacket(), CPN::RemoteContext::EntryPoint(), CPN::RemoteQueue::InternalCheckStatus(), CPN::RemoteContextDaemon::Client::Read(), and CPN::RemoteQueue::Read().

115 { ALock al(file_lock); return eof; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43

+ Here is the caller graph for this function:

bool FileHandle::Eof ( bool  e)
inline

Set/reset the end of file condition.

Parameters
ethe new end of file condition
Returns
the new end of file condition

Definition at line 120 of file FileHandle.h.

References eof, and file_lock.

120 { ALock al(file_lock); return eof = e; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
int FileHandle::FD ( ) const
inline
int FileHandle::FD ( int  filed)
inline

Set the current file descriptor.

Parameters
filedthe new file descriptor
Returns
the new file descriptor

Definition at line 111 of file FileHandle.h.

References fd, and file_lock.

111 { ALock al(file_lock); return fd = filed; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
void FileHandle::Flush ( )

Tell the OS to flush any buffers it has. May not be supported for all file types.

Definition at line 293 of file FileHandle.cc.

References FD().

293  {
294  if (fsync(FD()) != 0) {
295  throw ErrnoException();
296  }
297 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

bool FileHandle::Good ( ) const
inline

Convenience method for testing if the file this FileHandle has is open and not at end of file.

Returns
true if open and not at end of file otherwise false

Definition at line 125 of file FileHandle.h.

References eof, fd, and file_lock.

Referenced by CPN::RemoteContext::EntryPoint(), and CPN::RemoteContextDaemon::Client::Read().

125 { ALock al(file_lock); return !(eof || fd == -1); }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43

+ Here is the caller graph for this function:

bool FileHandle::IsBlocking ( ) const

Test if the current file is in blocking or non blocking mode.

Returns
true if blocking or false if non blocking

Definition at line 128 of file FileHandle.cc.

References FD().

128  {
129  return IsBlocking(FD());
130 }
bool IsBlocking() const
Test if the current file is in blocking or non blocking mode.
Definition: FileHandle.cc:128
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

bool FileHandle::IsBlocking ( int  fd)
static

Test the given file descriptor if it is in blocking mode.

Parameters
fdthe file descriptor to test
Returns
true if blocking or false if non blocking

Definition at line 132 of file FileHandle.cc.

132  {
133  int flags = fcntl(fd, F_GETFL, 0);
134  if (-1 == flags) { throw ErrnoException(); }
135  return !(flags & O_NONBLOCK);
136 }
virtual void FileHandle::OnReadable ( )
inlineprotectedvirtual

Called by Poll when it detects that the file is readable.

Reimplemented in WakeupHandle.

Definition at line 210 of file FileHandle.h.

References Readable().

Referenced by Poll().

210 { Readable(true); }
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

virtual void FileHandle::OnWriteable ( )
inlineprotectedvirtual

Called by Poll when it detects that the file is writeable.

Definition at line 212 of file FileHandle.h.

References Writeable().

Referenced by Poll().

212 { Writeable(true); }
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

FileHandle& FileHandle::operator= ( const FileHandle )
private
int FileHandle::Poll ( IteratorRef< FileHandle * >  begin,
IteratorRef< FileHandle * >  end,
double  timeout 
)
static

poll a list of FileHandles for any activity and call the appropriate On method.

Definition at line 34 of file FileHandle.cc.

References FD(), fd, OnReadable(), OnWriteable(), Readable(), and Writeable().

Referenced by CPN::RemoteContext::EntryPoint(), CPN::RemoteQueue::FileThreadEntryPoint(), CPN::ConnectionServer::Poll(), Poll(), and CPN::RemoteContextDaemon::Run().

34  {
35  fd_set rfd;
36  fd_set wfd;
37  FD_ZERO(&rfd);
38  FD_ZERO(&wfd);
39  int maxfd = 0;
40  IteratorRef<FileHandle*> itr = begin;
41  while (itr != end) {
42  FileHandle *han = *itr;
43  int fd = han->FD();
44  if (fd < 0) {
45  return -1;
46  } else {
47  bool set = false;
48  if (!han->Readable()) {
49  FD_SET(fd, &rfd);
50  set = true;
51  }
52  if (!han->Writeable()) {
53  FD_SET(fd, &wfd);
54  set = true;
55  }
56  if (set) { maxfd = std::max(maxfd, fd); }
57  }
58  ++itr;
59  }
60  timeval tv;
61  timeval *ptv = 0;
62  if (timeout >= 0) {
63  tv.tv_sec = (int)timeout;
64  tv.tv_usec = (int)((timeout - tv.tv_sec) * 1e6);
65  ptv = &tv;
66  }
67  int ret = select(maxfd + 1, &rfd, &wfd, 0, ptv);
68  if (ret < 0) {
69  if (errno == EINTR) {
70  return 0;
71  }
72  throw ErrnoException();
73  }
74  itr = begin;
75  while (itr != end) {
76  FileHandle *han = *itr;
77  int fd = han->FD();
78  if (fd >= 0) {
79  if (FD_ISSET(fd, &rfd)) {
80  han->OnReadable();
81  }
82  if (FD_ISSET(fd, &wfd)) {
83  han->OnWriteable();
84  }
85  }
86  ++itr;
87  }
88  return ret;
89 }
virtual void OnWriteable()
Called by Poll when it detects that the file is writeable.
Definition: FileHandle.h:212
bool Writeable(bool w)
Set that this file is currently writeable or not.
Definition: FileHandle.h:97
A reference to an iterator.
Definition: IteratorRef.h:46
int FD() const
Definition: FileHandle.h:106
bool Readable(bool r)
Set that the file is currently readable or not.
Definition: FileHandle.h:86
Generic file handle could be a file, or a socket or a device.
Definition: FileHandle.h:41
virtual void OnReadable()
Called by Poll when it detects that the file is readable.
Definition: FileHandle.h:210

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int FileHandle::Poll ( double  timeout)

Poll the current file descriptor for activity specified by the current readable or writeable status, if false poll that value, and call one of OnWriteable or OnReadable whos default action is to set Readable or Writeable to true.

Parameters
timeout-1 to wait forever for activity 0 to poll and return immediately or a time to wait in seconds.
Returns
zero if timed out, positive if an event occurred or negative if an error occurred.

Definition at line 106 of file FileHandle.cc.

References Poll().

106  {
107  FileHandle *fh = this;
108  return Poll(&fh, &fh + 1, timeout);
109 }
static int Poll(IteratorRef< FileHandle * > begin, IteratorRef< FileHandle * > end, double timeout)
poll a list of FileHandles for any activity and call the appropriate On method.
Definition: FileHandle.cc:34
Generic file handle could be a file, or a socket or a device.
Definition: FileHandle.h:41

+ Here is the call graph for this function:

unsigned FileHandle::Read ( void *  ptr,
unsigned  len 
)

Read data from the file descriptor.

Will set the end of file condition if read detects end of file.

Note
All the read functions will set readable to false if they read less than the requested amount or we are non blocking and a would block condition happened.
Parameters
ptrpointer to write data to
lenthe maximum number of bytes to write to ptr
Returns
0 if no bytes read (check Eof) or the number of bytes read

Definition at line 159 of file FileHandle.cc.

References eof, fd, file_lock, Readable(), and readable.

Referenced by CPN::RemoteQueue::D4RTagPacket(), and WakeupHandle::Read().

159  {
160  int filed;
161  {
162  ALock al(file_lock);
163  if (eof || fd == -1) { return 0; }
164  filed = fd;
165  }
166  unsigned bytesread = 0;
167  int num = read(filed, ptr, len);
168  if (num < 0) {
169  int error = errno;
170  switch (error) {
171  case EAGAIN:
172  Readable(false);
173  case EINTR:
174  case ENOMEM:
175  break;
176  default:
177  throw ErrnoException(error);
178  }
179  } else if (num == 0 && len != 0) {
180  ALock al(file_lock);
181  eof = true;
182  readable = false;
183  } else {
184  if (unsigned(num) < len) { Readable(false); }
185  bytesread = num;
186  }
187  return bytesread;
188 }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool FileHandle::Readable ( bool  r)
inline

Set that the file is currently readable or not.

Parameters
rtrue or false
Returns
the new readable status

Definition at line 86 of file FileHandle.h.

References file_lock, and readable.

Referenced by CPN::RemoteContext::EntryPoint(), Poll(), CPN::ConnectionServer::Poll(), and CPN::RemoteQueue::Read().

86 { ALock al(file_lock); return readable = r; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215

+ Here is the caller graph for this function:

bool FileHandle::Readable ( ) const
inline

Gives the current readability status of the file.

Returns
true if it is known that a read will not block

Definition at line 91 of file FileHandle.h.

References file_lock, and readable.

Referenced by ServerSocketHandle::Accept(), CPN::RemoteContextDaemon::Client::Client(), OnReadable(), CPN::RemoteContextDaemon::Read(), Read(), Readv(), SocketHandle::Recv(), CPN::RemoteContextDaemon::Run(), and WakeupHandle::WakeupHandle().

91 { ALock al(file_lock); return readable; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215

+ Here is the caller graph for this function:

unsigned FileHandle::Readv ( const iovec *  iov,
int  iovcnt 
)

scatter gather io version of Read

Returns
see Read

Definition at line 190 of file FileHandle.cc.

References eof, fd, file_lock, Readable(), and readable.

Referenced by CPN::RemoteQueue::EnqueuePacket().

190  {
191  int filed;
192  {
193  ALock al(file_lock);
194  if (eof || fd == -1) { return 0; }
195  filed = fd;
196  }
197  unsigned len = 0;
198  for (int i = 0; i < iovcnt; ++i) {
199  len += iov[i].iov_len;
200  }
201  unsigned bytesread = 0;
202  int num = readv(filed, iov, iovcnt);
203  if (num < 0) {
204  int error = errno;
205  switch (error) {
206  case EAGAIN:
207  Readable(false);
208  case EINTR:
209  case ENOMEM:
210  break;
211  default:
212  throw ErrnoException(error);
213  }
214  } else if (num == 0 && len != 0) {
215  ALock al(file_lock);
216  eof = true;
217  readable = false;
218  } else {
219  if (unsigned(num) < len) { Readable(false); }
220  bytesread = num;
221  }
222  return bytesread;
223 }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void FileHandle::Reset ( )

Clear all internal state including the file descriptor! WARNING does not close the file!

Definition at line 138 of file FileHandle.cc.

References eof, fd, file_lock, readable, and writeable.

Referenced by CPN::ConnectionServer::ConnectWriter(), CPN::RemoteQueue::FileThreadEntryPoint(), and ~FileHandle().

138  {
139  ALock al(file_lock);
140  readable = false;
141  writeable = false;
142  eof = false;
143  fd = -1;
144 }
bool writeable
Definition: FileHandle.h:216
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215

+ Here is the caller graph for this function:

void FileHandle::SetBlocking ( bool  blocking)

Manipulate how the current file handles blocking.

Parameters
blockingtrue to set to blocking mode (default) false to set to non blocking mode.

In non blocking mode all reads and writes will not block. Use Poll to block.

Definition at line 111 of file FileHandle.cc.

References FD().

Referenced by WakeupHandle::WakeupHandle().

111  {
112  SetBlocking(FD(), blocking);
113 }
void SetBlocking(bool blocking)
Manipulate how the current file handles blocking.
Definition: FileHandle.cc:111
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void FileHandle::SetBlocking ( int  fd,
bool  blocking 
)
static

a convenience method that allows one to set the same blocking parameters for a file descriptor without setting it inside a FileHandle.

Parameters
fdthe file descriptor
blockingtrue or false see SetBlock

Definition at line 115 of file FileHandle.cc.

115  {
116  int flags = fcntl(fd, F_GETFL, 0);
117  if (-1 == flags) { throw ErrnoException(); }
118  if (blocking) {
119  flags &= ~O_NONBLOCK;
120  } else {
121  flags |= O_NONBLOCK;
122  }
123  if (fcntl(fd, F_SETFL, flags) != 0) {
124  throw ErrnoException();
125  }
126 }
unsigned FileHandle::Write ( const void *  ptr,
unsigned  len 
)

Write data to the file descriptor.

Note
All write functions will set Writeable to false if they write less than the amount requested or if the file is in non blocking mode and the write returned would block.
Parameters
ptrpointer to beginning of data to write
lenlength of data to write
Returns
number of bytes written

Definition at line 225 of file FileHandle.cc.

References fd, file_lock, and Writeable().

Referenced by CPN::ConnectionServer::ConnectWriter(), CPN::RemoteContextDaemon::Client::Send(), CPN::RemoteContext::SendMessage(), and CPN::RemoteQueue::WriteBytes().

225  {
226  int filed;
227  {
228  ALock al(file_lock);
229  if (fd == -1) { return 0; }
230  filed = fd;
231  }
232  unsigned written = 0;
233  int num = write(filed, ptr, len);
234  if (num < 0) {
235  int error = errno;
236  switch (error) {
237  case EAGAIN: // Would block
238  //case EWOULDBLOCK:
239  Writeable(false);
240  case EINTR: // Returned because of signal
241  case ENOMEM:
242  case ENOBUFS: // Buffers are full
243  break;
244  case EPIPE:
245  case EBADF:
246  case EFAULT:
247  case EFBIG:
248  case EINVAL:
249  case EIO:
250  case ENOSPC:
251  default:
252  throw ErrnoException(error);
253  }
254  } else {
255  if (unsigned(num) < len) { Writeable(false); }
256  written = num;
257  }
258  return written;
259 }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool FileHandle::Writeable ( bool  w)
inline

Set that this file is currently writeable or not.

Parameters
wtrue or false
Returns
the new writeable status

Definition at line 97 of file FileHandle.h.

References file_lock, and writeable.

Referenced by Poll().

97 { ALock al(file_lock); return writeable = w; }
bool writeable
Definition: FileHandle.h:216
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43

+ Here is the caller graph for this function:

bool FileHandle::Writeable ( ) const
inline

Gives the current writability status of the file.

Returns
true if it is known that a write will not block

Definition at line 102 of file FileHandle.h.

References file_lock, and writeable.

Referenced by OnWriteable(), SocketHandle::Send(), WakeupHandle::WakeupHandle(), Write(), and Writev().

102 { ALock al(file_lock); return writeable; }
bool writeable
Definition: FileHandle.h:216
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43

+ Here is the caller graph for this function:

unsigned FileHandle::Writev ( const iovec *  iov,
int  iovcnt 
)

scatter gather io version of Write

Returns
see Write

Definition at line 261 of file FileHandle.cc.

References fd, file_lock, and Writeable().

Referenced by CPN::RemoteQueue::WriteBytes().

261  {
262  int filed;
263  {
264  ALock al(file_lock);
265  if (fd == -1) { return 0; }
266  filed = fd;
267  }
268  unsigned written = 0;
269  int num = writev(filed, iov, iovcnt);
270  if (num < 0) {
271  int error = errno;
272  switch (error) {
273  case EAGAIN:
274  Writeable(false);
275  case EINTR:
276  case ENOMEM:
277  case ENOBUFS:
278  break;
279  default:
280  throw ErrnoException(error);
281  }
282  } else {
283  unsigned len = 0;
284  for (int i = 0; i < iovcnt; ++i) {
285  len += iov[i].iov_len;
286  }
287  if (unsigned(num) < len) { Writeable(false); }
288  written = num;
289  }
290  return written;
291 }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

bool FileHandle::eof
protected

Definition at line 217 of file FileHandle.h.

Referenced by Close(), Eof(), Good(), Read(), Readv(), SocketHandle::Recv(), and Reset().

int FileHandle::fd
protected
PthreadMutex FileHandle::file_lock
mutableprotected
bool FileHandle::readable
protected

Definition at line 215 of file FileHandle.h.

Referenced by Close(), Read(), Readable(), Readv(), SocketHandle::Recv(), and Reset().

bool FileHandle::writeable
protected

Definition at line 216 of file FileHandle.h.

Referenced by Close(), Reset(), and Writeable().


The documentation for this class was generated from the following files: