Back to index

nordugrid-arc-nox  1.1.0~rc6
Public Types | Public Member Functions | Protected Attributes
Arc::PayloadStream Class Reference

POSIX handle as Payload. More...

#include <PayloadStream.h>

Inheritance diagram for Arc::PayloadStream:
Inheritance graph
[legend]
Collaboration diagram for Arc::PayloadStream:
Collaboration graph
[legend]

List of all members.

Public Types

typedef signed long long int Size_t

Public Member Functions

 PayloadStream (int h=-1)
 true if lseek operation is applicable to open handle
virtual ~PayloadStream (void)
 Destructor.
virtual bool Get (char *buf, int &size)
 Extracts information from stream up to 'size' bytes.
virtual bool Get (std::string &buf)
 Read as many as possible (sane amount) of bytes into buf.
virtual std::string Get (void)
 Read as many as possible (sane amount) of bytes.
virtual bool Put (const char *buf, Size_t size)
 Push 'size' bytes from 'buf' into stream.
virtual bool Put (const std::string &buf)
 Push information from 'buf' into stream.
virtual bool Put (const char *buf)
 Push null terminated information from 'buf' into stream.
virtual operator bool (void)
 Returns true if stream is valid.
virtual bool operator! (void)
 Returns true if stream is invalid.
virtual int Timeout (void) const
 Query current timeout for Get() and Put() operations.
virtual void Timeout (int to)
 Set current timeout for Get() and Put() operations.
virtual Size_t Pos (void) const
 Returns current position in stream if supported.
virtual Size_t Size (void) const
 Returns size of underlying object if supported.
virtual Size_t Limit (void) const
 Returns position at which stream reading will stop if supported.

Protected Attributes

int timeout_
int handle_
 Timeout for read/write operations.
bool seekable_
 Handle for operations.

Detailed Description

POSIX handle as Payload.

This is an implemetation of PayloadStreamInterface for generic POSIX handle.

Definition at line 58 of file PayloadStream.h.


Member Typedef Documentation

typedef signed long long int Arc::PayloadStreamInterface::Size_t [inherited]

Definition at line 18 of file PayloadStream.h.


Constructor & Destructor Documentation

true if lseek operation is applicable to open handle

Constructor. Attaches to already open handle. Handle is not managed by this class and must be closed by external code.

Definition at line 19 of file PayloadStream.cpp.

                                 :timeout_(60),handle_(h),seekable_(false) {
  struct stat st;
  if(fstat(handle_,&st) != 0) return;
  if(!(S_ISREG(st.st_mode))) return;
  seekable_=true;
#ifdef WIN32
  COMMTIMEOUTS to;
  if(GetCommTimeouts((HANDLE)handle_,&to)) {
    to.ReadIntervalTimeout=timeout_*1000;
    to.ReadTotalTimeoutMultiplier=0;
    to.ReadTotalTimeoutConstant=timeout_*1000;
    to.WriteTotalTimeoutMultiplier=0;
    to.WriteTotalTimeoutConstant=timeout_*1000;
    SetCommTimeouts((HANDLE)handle_,&to);
  };
#endif  
  return;
}
virtual Arc::PayloadStream::~PayloadStream ( void  ) [inline, virtual]

Destructor.

Definition at line 68 of file PayloadStream.h.

{ };

Member Function Documentation

bool Arc::PayloadStream::Get ( char *  buf,
int &  size 
) [virtual]

Extracts information from stream up to 'size' bytes.

'size' contains number of read bytes on exit. Returns true in case of success.

Implements Arc::PayloadStreamInterface.

Reimplemented in ARex::PayloadBigFile, and Hopi::PayloadBigFile.

Definition at line 38 of file PayloadStream.cpp.

                                           {
  if(handle_ == -1) return false;
  ssize_t l = size;
  size=0;
  if(seekable_) { // check for EOF
    struct stat st;
    if(fstat(handle_,&st) != 0) return false;
    off_t o = lseek(handle_,0,SEEK_CUR);
    if(o == (off_t)(-1)) return false;
    o++;
    if(o >= st.st_size) return false;
  };
#ifndef WIN32
  struct pollfd fd;
  fd.fd=handle_; fd.events=POLLIN | POLLPRI | POLLERR; fd.revents=0;
  if(poll(&fd,1,timeout_*1000) != 1) return false;
  if(!(fd.revents & (POLLIN | POLLPRI))) return false;
#endif
  l=::read(handle_,buf,l);
  if(l == -1) return false;
  size=l;
#ifndef WIN32
  if((l == 0) && (fd.revents && POLLERR)) return false;
#else
  if(l == 0) return false;
#endif
  return true;
}
bool Arc::PayloadStream::Get ( std::string &  buf) [virtual]

Read as many as possible (sane amount) of bytes into buf.

Implements Arc::PayloadStreamInterface.

Definition at line 67 of file PayloadStream.cpp.

                                      {
  char tbuf[1024];
  int l = sizeof(tbuf);
  bool result = Get(tbuf,l);
  buf.assign(tbuf,l);
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual std::string Arc::PayloadStream::Get ( void  ) [inline, virtual]

Read as many as possible (sane amount) of bytes.

Implements Arc::PayloadStreamInterface.

Definition at line 71 of file PayloadStream.h.

{ std::string buf; Get(buf); return buf; };

Here is the call graph for this function:

Here is the caller graph for this function:

virtual Size_t Arc::PayloadStream::Limit ( void  ) const [inline, virtual]

Returns position at which stream reading will stop if supported.

That may be not same as Size() if instance is meant to provide access to only part of underlying obejct.

Implements Arc::PayloadStreamInterface.

Reimplemented in ARex::PayloadBigFile, and Hopi::PayloadBigFile.

Definition at line 81 of file PayloadStream.h.

{ return 0; };
virtual Arc::PayloadStream::operator bool ( void  ) [inline, virtual]

Returns true if stream is valid.

Implements Arc::PayloadStreamInterface.

Reimplemented in ARex::PayloadBigFile, and Hopi::PayloadBigFile.

Definition at line 75 of file PayloadStream.h.

{ return (handle_ != -1); };
virtual bool Arc::PayloadStream::operator! ( void  ) [inline, virtual]

Returns true if stream is invalid.

Implements Arc::PayloadStreamInterface.

Reimplemented in ARex::PayloadBigFile, and Hopi::PayloadBigFile.

Definition at line 76 of file PayloadStream.h.

{ return (handle_ == -1); };
virtual Size_t Arc::PayloadStream::Pos ( void  ) const [inline, virtual]

Returns current position in stream if supported.

Implements Arc::PayloadStreamInterface.

Reimplemented in ARex::PayloadBigFile, and Hopi::PayloadBigFile.

Definition at line 79 of file PayloadStream.h.

{ return 0; };
bool Arc::PayloadStream::Put ( const char *  buf,
Size_t  size 
) [virtual]

Push 'size' bytes from 'buf' into stream.

Returns true on success.

Implements Arc::PayloadStreamInterface.

Definition at line 75 of file PayloadStream.cpp.

                                                   {
  ssize_t l;
  if(handle_ == -1) return false;
  time_t start = time(NULL);
  for(;size;) {
#ifndef WIN32
    struct pollfd fd;
    fd.fd=handle_; fd.events=POLLOUT | POLLERR; fd.revents=0;
    int to = timeout_-(unsigned int)(time(NULL)-start);
    if(to < 0) to=0;
    if(poll(&fd,1,to*1000) != 1) return false;
    if(!(fd.revents & POLLOUT)) return false;
#endif
    l=::write(handle_,buf,size);
    if(l == -1) {
           return false;
    }
    buf+=l; size-=l;
#ifdef WIN32
    int to = timeout_-(unsigned int)(time(NULL)-start);
    if(to < 0) return false;
#endif
  };  
  return true;
}

Here is the caller graph for this function:

virtual bool Arc::PayloadStream::Put ( const std::string &  buf) [inline, virtual]

Push information from 'buf' into stream.

Returns true on success.

Implements Arc::PayloadStreamInterface.

Definition at line 73 of file PayloadStream.h.

{ return Put(buf.c_str(),buf.length()); };

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool Arc::PayloadStream::Put ( const char *  buf) [inline, virtual]

Push null terminated information from 'buf' into stream.

Returns true on success.

Implements Arc::PayloadStreamInterface.

Definition at line 74 of file PayloadStream.h.

{ return Put(buf,buf?strlen(buf):0); };

Here is the call graph for this function:

Here is the caller graph for this function:

virtual Size_t Arc::PayloadStream::Size ( void  ) const [inline, virtual]

Returns size of underlying object if supported.

Implements Arc::PayloadStreamInterface.

Reimplemented in ARex::PayloadBigFile, and Hopi::PayloadBigFile.

Definition at line 80 of file PayloadStream.h.

{ return 0; };
virtual int Arc::PayloadStream::Timeout ( void  ) const [inline, virtual]

Query current timeout for Get() and Put() operations.

Implements Arc::PayloadStreamInterface.

Definition at line 77 of file PayloadStream.h.

{ return timeout_; };
virtual void Arc::PayloadStream::Timeout ( int  to) [inline, virtual]

Set current timeout for Get() and Put() operations.

Implements Arc::PayloadStreamInterface.

Definition at line 78 of file PayloadStream.h.

{ timeout_=to; };

Member Data Documentation

int Arc::PayloadStream::handle_ [protected]

Timeout for read/write operations.

Definition at line 61 of file PayloadStream.h.

Handle for operations.

Definition at line 62 of file PayloadStream.h.

Definition at line 60 of file PayloadStream.h.


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