Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Mem_Map_Stream.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /** @file   Mem_Map_Stream.h
00004  *
00005  *  $Id: Mem_Map_Stream.h,v 1.1.1.1 2003/02/21 18:36:32 chad Exp $
00006  *
00007  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00008  *  @author Krishnakumar B <kitty@cs.wustl.edu>
00009  *
00010  */
00011 
00012 #ifndef _ACEXML_MEM_MAP_STREAM_H
00013 #define _ACEXML_MEM_MAP_STREAM_H
00014 
00015 #include "ace/pre.h"
00016 #include "ACEXML/common/ACEXML_Export.h"
00017 
00018 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00019 #pragma once
00020 #endif /* ACE_LACKS_PRAGMA_ONCE */
00021 
00022 #include "ace/SOCK_Stream.h"
00023 #include "ace/Mem_Map.h"
00024 #include "ace/SOCK_Connector.h"
00025 #include "ace/Connector.h"
00026 #include "ace/Svc_Handler.h"
00027 #include "ace/Synch_T.h"
00028 #include "ACEXML/common/XML_Types.h"
00029 
00030 typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> Svc_Handler;
00031 typedef ACE_Connector<Svc_Handler, ACE_SOCK_CONNECTOR> Connector;
00032 
00033 /**
00034  * @class ACEXML_Mem_Map_Stream Mem_Map_Stream.h "ACEXML/common/Mem_Map_Stream.h"
00035  *
00036  * @brief Provides a memory-mapped stream abstraction to simplify parsing
00037  * of tokens.
00038  *
00039  * This class makes it possible to treat an connection as a stream of
00040  * bytes, similar to the C library stdio streams. The contents of the
00041  * connection are buffered incrementally in a memory-mapped file. This
00042  * class maintains pointers to two positions in the stream:
00043  *
00044  *   1. The <recv> position, which keeps track of the beginning of a
00045  *      token that is in the stream.
00046  *
00047  *   2. The <get> position, which moves along character-by-character
00048  *      until the end of the token is reached.
00049  *
00050  * Once a token has been located, it can be extracted from the stream by
00051  * calling the <recv>. The length of the token, i.e., the <recv_len>, is
00052  * the length in bytes between the <get> position and the <recv> position.
00053  * Once the token has been extracted, the <recv> and <get> positions can be
00054  * updated by the <seek> method.
00055  */
00056 class ACEXML_Export ACEXML_Mem_Map_Stream
00057 {
00058 public:
00059   /// Default constructor
00060   ACEXML_Mem_Map_Stream (void);
00061 
00062   /// Initialize this object.
00063   virtual int open (Connector *connector,
00064                     const ACE_INET_Addr &);
00065 
00066   /// Destructor.
00067   virtual ~ACEXML_Mem_Map_Stream (void);
00068 
00069   /// Returns the underlying <ACE_SOCK_Stream>.
00070   ACE_SOCK_Stream &stream (void);
00071 
00072   /**
00073    * Send <size> bytes in <buf> to the connected peer.  This is a
00074    * completely unbuffered call.
00075    */
00076   virtual ssize_t send_n (const void *buf,
00077                           size_t size,
00078                           ACE_Time_Value *tv = 0);
00079 
00080   /**
00081    * Return the next character in the stream and advance the <get>
00082    * position. Returns EOF when the <get> position reaches the end of the
00083    * HTTP stream.
00084    */
00085   virtual int get_char (void);
00086 
00087   /**
00088    *  Returns a pointer to array of at most <len> characters starting at
00089    *  the <recv> position. If the <recv> position + <len> extends past the
00090    *  EOF then <len> is set to the number of characters between the <recv>
00091    *  position and the EOF and both the <get> and <recv> positions are
00092    *  advanced by <len>. Returns 0 if the <recv> position is at the EOF.
00093    */
00094   virtual const char *recv (size_t &len);
00095 
00096   /**
00097    *  Returns a pointer to array of characters starting at the <recv>
00098    *  position.
00099    */
00100   virtual const char *recv (void) const;
00101 
00102   /**
00103    *  Returns the length in bytes between the <get> position and the <recv>
00104    *  position.
00105    */
00106   virtual size_t recv_len (void) const;
00107 
00108   /**
00109    *  Returns the no. of bytes available in the stream.
00110    */
00111   virtual size_t available (void) const;
00112 
00113   /**
00114    *  Resets the <get> and <recv> positions to the beginning of the stream.
00115    *  This works since all the data has been cached in the memory-mapped
00116    *  backing store.
00117    */
00118   virtual void rewind (void);
00119 
00120   /**
00121    *  Returns the nth character <offset> from the <get> position in the
00122    *  stream without advancing the <get> position. Automatically extends
00123    *  the backing store if necessary. Returns EOF if <offset> is past the
00124    *  end of the stream.
00125    */
00126   virtual int peek_char (size_t offset);
00127 
00128   /**
00129    *  Return a pointer to an array of <size> characters starting at
00130    *  <offset> characters from the <get> position in the stream without
00131    *  advancing the <get> position. Automatically extends the backing store
00132    *  if necessary. Returns 0 if <offset> or <offset + size> is past the
00133    *  end of the stream.
00134    */
00135   virtual const char *peek_str (size_t offset, size_t size);
00136 
00137   /**
00138    *  Sets the <get> and <recv> positions as follows:
00139    *    o  If <whence> is <SEEK_SET>, the positions are set to <offset>
00140    *       bytes from the start of the stream.
00141    *    o  If <whence> is <SEEK_CUR>, the positions are set to the
00142    *       current <get> position plus <offset>.
00143    *    o  If <whence> is <SEEK_END>, the positions are set to the size
00144    *       of the stream plus <offset>.
00145    *
00146    *  If offset is greater than EOF, both <get> and <recv> are set to EOF.
00147    *  Note special return value is returned to indicate this condition.
00148    */
00149   virtual off_t seek (off_t offset, int whence = SEEK_CUR);
00150 
00151   /// Returns 1 if we're at the end of the HTTP stream, else 0.
00152   virtual int eof (void) const;
00153 
00154   /// Returns the underlying service handler.
00155   Svc_Handler *svc_handler (void);
00156 
00157 private:
00158   /**
00159    *  Grow the file by reading another chunk from the HTTP socket and
00160    *  extend the mapping to cover this chunk.  Returns -1 on failure or
00161    *  EOF, else 0.
00162    */
00163   int grow_file_and_remap (void);
00164 
00165   /**
00166    *  Connection to peer. The granularity is at the Svc_Handler level.
00167    *  The Svc_Handler has an SOCK_Stream.
00168    */
00169   Svc_Handler *svc_handler_;
00170 
00171   /// Memory-mapped file that we're iterating over.
00172   ACE_Mem_Map mem_map_;
00173 
00174   /// Pointer to the address where the next <recv> method will start.
00175   char *recv_pos_;
00176 
00177   /**
00178    *  Pointer to the address where the next <get_char> method will
00179    *  start.
00180    */
00181   char *get_pos_;
00182 
00183   /// Address at the end of the file mapping.
00184   char *end_of_mapping_plus1_;
00185 
00186 };
00187 
00188 
00189 #include "ace/post.h"
00190 
00191 
00192 #endif /* _ACEXML_MEM_MAP_STREAM_H */

Generated on Mon Jun 16 13:23:22 2003 for ACEXML by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002