ASSA::xdrIOBuffer Class Reference

#include <xdrIOBuffer.h>

List of all members.

Public Types

enum  state_t { waiting, xmitted, parsed, error }

Public Member Functions

 xdrIOBuffer (u_int len_)
 Constructor.
 ~xdrIOBuffer ()
 Destructor.
 xdrIOBuffer (const xdrIOBuffer &rhs_)
 Copy constructor.
xdrIOBufferoperator= (const xdrIOBuffer &rhs_)
 Assign operator.
xdrIOBufferoperator>> (std::string &)
 Read and XDR-decode STL string from the buffer.
xdrIOBufferoperator>> (int &)
 Read and XDR-decode an integer from the buffer.
xdrIOBufferoperator>> (float &)
 Read and XDR-decode a float from the buffer.
 operator void * () const
 Convertion to void* (for testing where bool is required).
string get_state () const
 Give verbal interpretation of object's state.
int size () const
 Return number of bytes in xdrIOBuffer.
int buffer_size () const
 Return buffer (maximum expected/allowable) size.
const char * str () const
 Return pointer to the first byte of xdrIOBuffer.
void reset ()
 Clear up the internal buffer and reset state to waiting.
void dump () const
 Dump object's internal state to the log file.

Protected Member Functions

void copy (const xdrIOBuffer &)
 Copy object from argument.

Private Attributes

char * m_buf
 Buffer.
int m_sz
 Buffer size and maximum expected size.
char * m_ptr
 Pointer for next I/O operation into the buffer.
state_t m_state
 Object state.

Friends

Socketoperator>> (Socket &src_, xdrIOBuffer &dest_)
 Read raw data from Socket nonblocking and store into internal buffer.


Detailed Description

Definition at line 55 of file xdrIOBuffer.h.


Member Enumeration Documentation

enum ASSA::xdrIOBuffer::state_t
 

Enumerator:
waiting 
xmitted 
parsed 
error 

Definition at line 60 of file xdrIOBuffer.h.

00060                  { 
00061         waiting, 
00062         xmitted, 
00063         parsed, 
00064         error 
00065     };


Constructor & Destructor Documentation

xdrIOBuffer::xdrIOBuffer u_int  len_  ) 
 

Constructor.

Definition at line 79 of file xdrIOBuffer.cpp.

References DL, dump(), m_buf, m_ptr, m_sz, trace_with_mask, ASSA::XDRBUF, and ASSA::XDRBUFTRACE.

00080     : m_sz  (sz_),
00081       m_state (waiting)
00082 {
00083     trace_with_mask("xdrIOBuffer::xdrIOBuffer", XDRBUFTRACE);
00084 
00085     m_buf = new char[sz_];
00086     m_ptr = m_buf;
00087     memset (m_buf, 0, m_sz);
00088     DL((XDRBUF,"Allocated xdrIOBuffer [%d]\n",m_sz));
00089     dump ();
00090 }

xdrIOBuffer::~xdrIOBuffer  ) 
 

Destructor.

Definition at line 93 of file xdrIOBuffer.cpp.

References DL, m_buf, trace_with_mask, and ASSA::XDRBUFTRACE.

00094 {
00095     trace_with_mask("xdrIOBuffer::~xdrIOBuffer", XDRBUFTRACE);
00096 
00097     DL((XDRBUFTRACE,"xdrIOBuffer->this = 0x%x\n", long(this)));
00098     delete [] m_buf;
00099 }

ASSA::xdrIOBuffer::xdrIOBuffer const xdrIOBuffer rhs_  )  [inline]
 

Copy constructor.

Definition at line 151 of file xdrIOBuffer.h.

References copy(), trace_with_mask, and ASSA::XDRBUFTRACE.

00152 {
00153     trace_with_mask("xdrIOBuffer::xdrIOBuffer(xdrIOBuffer&)", XDRBUFTRACE);
00154 
00155     copy (rhs_);
00156 }


Member Function Documentation

int ASSA::xdrIOBuffer::buffer_size  )  const [inline]
 

Return buffer (maximum expected/allowable) size.

Definition at line 178 of file xdrIOBuffer.h.

References m_sz.

00179 {
00180     return (m_sz);
00181 }

void xdrIOBuffer::copy const xdrIOBuffer  )  [protected]
 

Copy object from argument.

Definition at line 113 of file xdrIOBuffer.cpp.

References m_buf, m_ptr, m_state, m_sz, size(), trace_with_mask, and ASSA::XDRBUFTRACE.

Referenced by operator=(), and xdrIOBuffer().

00114 {
00115     trace_with_mask("xdrIOBuffer::copy", XDRBUFTRACE);
00116 
00117     m_sz = rhs_.m_sz;
00118     m_buf = new char[m_sz];
00119     memcpy (m_buf, rhs_.m_buf, m_sz);
00120     m_ptr = m_buf + (rhs_.size ());
00121     m_state = rhs_.m_state;
00122 }

void xdrIOBuffer::dump  )  const
 

Dump object's internal state to the log file.

Definition at line 225 of file xdrIOBuffer.cpp.

References DL, get_state(), ASSA::MemDump::getMemDump(), m_buf, m_ptr, m_state, m_sz, size(), trace_with_mask, ASSA::XDRBUFTRACE, and xmitted.

Referenced by ASSA::operator>>(), and xdrIOBuffer().

00226 {
00227     trace_with_mask("xdrIOBuffer::dump", XDRBUFTRACE);
00228 
00229     DL((XDRBUFTRACE,"xdrIOBuffer->this = 0x%x\n", long(this) ));
00230     DL((XDRBUFTRACE,"\n\n" \
00231         "\tm_buf ........: 0x%x \n" \
00232         "\tm_sz .........: %d   \n" \
00233         "\tm_ptr ........: 0x%x \n" \
00234         "\tbytes left ...: %d   \n" \
00235         "\tm_state ......: %s   \n\n", 
00236         long (m_buf), m_sz, long (m_ptr),(m_sz - size ()), 
00237         get_state ().c_str ()));
00238 
00239     if (m_ptr != m_buf) {
00240         MemDump image (m_buf, size ());
00241         DL((XDRBUFTRACE,"Bytes in buffer so far:\n\n%s\n\n",
00242             image.getMemDump () ));
00243     }
00244     else if (m_ptr == m_buf && m_state == xmitted) {
00245         MemDump image (m_buf, (m_sz));
00246         DL((XDRBUFTRACE,"Complete buffer:\n\n%s\n\n",
00247             image.getMemDump () ));
00248     }
00249     else {
00250         DL((XDRBUFTRACE,"Empty buffer\n" ));
00251     }
00252 }

string xdrIOBuffer::get_state  )  const
 

Give verbal interpretation of object's state.

Definition at line 210 of file xdrIOBuffer.cpp.

References error, m_state, parsed, waiting, and xmitted.

Referenced by dump(), operator>>(), and ASSA::operator>>().

00211 {
00212     string msg;
00213     switch (m_state) 
00214     {
00215     case xdrIOBuffer::waiting: msg = "waiting"; break;
00216     case xdrIOBuffer::xmitted: msg = "xmitted"; break;
00217     case xdrIOBuffer::parsed:  msg = "parsed"; break;
00218     case xdrIOBuffer::error:   msg = "error"; break;
00219     }
00220     return msg;
00221 }

ASSA::xdrIOBuffer::operator void *  )  const [inline]
 

Convertion to void* (for testing where bool is required).

Definition at line 160 of file xdrIOBuffer.h.

References m_state, parsed, trace_with_mask, waiting, and ASSA::XDRBUFTRACE.

00161 {
00162     trace_with_mask("xdrIOBuffer::opt void*()", XDRBUFTRACE);
00163 
00164     return (m_state == waiting || m_state == parsed)  
00165         ? (void *)0 // bad state
00166         : (void *)(-1); // good state
00167 }

xdrIOBuffer & xdrIOBuffer::operator= const xdrIOBuffer rhs_  ) 
 

Assign operator.

Definition at line 102 of file xdrIOBuffer.cpp.

References copy(), m_buf, trace_with_mask, and ASSA::XDRBUFTRACE.

00103 {
00104     trace_with_mask("xdrIOBuffer::operator=()", XDRBUFTRACE);
00105 
00106     delete [] m_buf;
00107     copy (rhs_);
00108     return *this;
00109 }

xdrIOBuffer & xdrIOBuffer::operator>> float &   ) 
 

Read and XDR-decode a float from the buffer.

Definition at line 175 of file xdrIOBuffer.cpp.

References ASSA::ASSAERR, EL, get_state(), m_ptr, m_state, m_sz, parsed, size(), trace_with_mask, ASSA::XDRBUFTRACE, and xmitted.

00176 {
00177     trace_with_mask("xdrIOBuffer::operator>>(float)", XDRBUFTRACE);
00178 
00179     if (m_state != xmitted) {
00180         EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () ));
00181         return *this;
00182     }
00183     float val;
00184     int unit_sz = sizeof (float);
00185     memcpy ((char*) &val, m_ptr, unit_sz);
00186     m_ptr += unit_sz;
00187 
00188     XDR xdrs;
00189     xdrmem_create (&xdrs, (caddr_t) &val, unit_sz, XDR_DECODE);
00190     xdr_float (&xdrs, &n_);
00191     xdr_destroy (&xdrs);
00192     
00193     if (size () == m_sz)
00194         m_state = parsed;
00195     return *this;
00196 }

xdrIOBuffer & xdrIOBuffer::operator>> int &   ) 
 

Read and XDR-decode an integer from the buffer.

Definition at line 126 of file xdrIOBuffer.cpp.

References ASSA::ASSAERR, EL, get_state(), m_ptr, m_state, m_sz, parsed, size(), trace_with_mask, ASSA::XDRBUFTRACE, and xmitted.

00127 {
00128     trace_with_mask("xdrIOBuffer::operator>>(int)", XDRBUFTRACE);
00129 
00130     if (m_state != xmitted) {
00131         EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () ));
00132         return *this;
00133     }
00134     int val;
00135     int unit_sz = sizeof (int);
00136     memcpy ((char*) &val, m_ptr, unit_sz);
00137     m_ptr += unit_sz;
00138 
00139     n_ = (int) ntohl (val);
00140 
00141     if (size () == m_sz)
00142         m_state = parsed;
00143     return *this;
00144 }

xdrIOBuffer & xdrIOBuffer::operator>> std::string &  s_  ) 
 

Read and XDR-decode STL string from the buffer.

XDR format for STL string is described in Socket::operator<< comments.

Definition at line 148 of file xdrIOBuffer.cpp.

References ASSA::ASSAERR, EL, get_state(), m_ptr, m_state, m_sz, parsed, size(), trace_with_mask, ASSA::Socket::xdr_length(), ASSA::XDRBUFTRACE, and xmitted.

00149 {
00150     trace_with_mask("xdrIOBuffer::operator>>(string)", XDRBUFTRACE);
00151 
00152     if (m_state != xmitted) {
00153         EL((ASSAERR,"Wrong state: %s\n", get_state ().c_str () ));
00154         return *this;
00155     }
00158     s_ = "";
00159     u_long len = (u_long) *m_ptr;
00160     char* cptr = m_ptr + 4;
00161 
00162     while (len--) {
00163         s_ += *cptr++;
00164     }
00165     m_ptr += Socket::xdr_length (s_);
00166 
00167     if (size () == m_sz) {
00168         m_state = parsed;
00169     }
00170     return *this;
00171 }

void xdrIOBuffer::reset  ) 
 

Clear up the internal buffer and reset state to waiting.

Definition at line 199 of file xdrIOBuffer.cpp.

References m_buf, m_ptr, m_state, m_sz, trace_with_mask, waiting, and ASSA::XDRBUFTRACE.

00200 {
00201     trace_with_mask("xdrIOBuffer::reset", XDRBUFTRACE);
00202 
00203     m_ptr = m_buf;
00204     memset (m_buf, 0, m_sz);
00205     m_state = waiting;
00206 }

int ASSA::xdrIOBuffer::size  )  const [inline]
 

Return number of bytes in xdrIOBuffer.

In waiting state it's bytes transmitted so far. In xmitted state, number of bytes left to decode.

Definition at line 171 of file xdrIOBuffer.h.

References m_buf, and m_ptr.

Referenced by copy(), dump(), operator>>(), and ASSA::operator>>().

00172 {
00173     return (m_ptr - m_buf);
00174 }

const char * ASSA::xdrIOBuffer::str  )  const [inline]
 

Return pointer to the first byte of xdrIOBuffer.

Definition at line 185 of file xdrIOBuffer.h.

References m_buf.

00186 {
00187     return ((const char*) m_buf);
00188 }


Friends And Related Function Documentation

Socket& operator>> Socket src_,
xdrIOBuffer dest_
[friend]
 

Read raw data from Socket nonblocking and store into internal buffer.

Definition at line 25 of file xdrIOBuffer.cpp.

00026 {
00027     trace_with_mask("Socket >> xdrIOBuffer", XDRBUFTRACE);
00028 
00029     DL((XDRBUFTRACE,"Buffer Initially:\n"));
00030     b_.dump ();
00031 
00032     if (b_.m_state != xdrIOBuffer::waiting) {
00033         EL((ASSAERR,"Wrong state: %s\n", b_.get_state ().c_str ()));
00034         return s_;
00035     }
00036     int expected = b_.m_sz - b_.size ();
00037 
00038     DL((XDRBUFTRACE,"Bytes expected: %d\n",expected));
00039     DL((XDRBUFTRACE,"Bytes in Socket buffer(s): %d\n", s_.getBytesAvail ()));
00040     int ret;
00041 
00042     if ((ret = s_.read (b_.m_ptr, expected)) <= 0) 
00043     {
00044 #if defined(WIN32)
00045         if (WSAGetLastError () != WSAEWOULDBLOCK) {
00046             WSASetLastError (0);
00047             EL((ASSAERR,"Socket::read() error!\n"));
00048             b_.m_state = xdrIOBuffer::error;
00049         }
00050 #else
00051         if (errno != EWOULDBLOCK) { 
00052             EL((ASSAERR,"Socket::read() error!\n"));
00053             b_.m_state = xdrIOBuffer::error;
00054         }
00055 #endif
00056         else {
00057             EL((ASSAERR,"Socket::read() error! \n"));
00058         }
00059         return s_;
00060     }
00061     b_.m_ptr += ret;
00062 
00063     DL((XDRBUFTRACE,"Received %d bytes\n", ret));
00064     b_.dump ();
00065 
00066     if (b_.m_sz == b_.size ()) { // at the end
00067         DL((XDRBUFTRACE,"Complete message is in the buffer!\n"));
00068         b_.m_state = xdrIOBuffer::xmitted;
00069         b_.m_ptr = b_.m_buf;    // rewind m_ptr for parsing stage
00070         b_.dump ();
00071     }
00072     return s_;
00073 }


Member Data Documentation

char* ASSA::xdrIOBuffer::m_buf [private]
 

Buffer.

Definition at line 137 of file xdrIOBuffer.h.

Referenced by copy(), dump(), operator=(), reset(), size(), str(), xdrIOBuffer(), and ~xdrIOBuffer().

char* ASSA::xdrIOBuffer::m_ptr [private]
 

Pointer for next I/O operation into the buffer.

Definition at line 143 of file xdrIOBuffer.h.

Referenced by copy(), dump(), operator>>(), ASSA::operator>>(), reset(), size(), and xdrIOBuffer().

state_t ASSA::xdrIOBuffer::m_state [private]
 

Object state.

Definition at line 146 of file xdrIOBuffer.h.

Referenced by copy(), dump(), get_state(), operator void *(), operator>>(), ASSA::operator>>(), and reset().

int ASSA::xdrIOBuffer::m_sz [private]
 

Buffer size and maximum expected size.

Definition at line 140 of file xdrIOBuffer.h.

Referenced by buffer_size(), copy(), dump(), operator>>(), ASSA::operator>>(), reset(), and xdrIOBuffer().


The documentation for this class was generated from the following files:
Generated on Sun Aug 13 15:08:22 2006 for libassa by  doxygen 1.4.6