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

ACEXML_Mem_Map_Stream Class Reference

Provides a memory-mapped stream abstraction to simplify parsing of tokens. More...

#include "ACEXML/common/Mem_Map_Stream.h"

Collaboration diagram for ACEXML_Mem_Map_Stream:

Collaboration graph
[legend]
List of all members.

Public Methods

 ACEXML_Mem_Map_Stream (void)
 Default constructor. More...

virtual int open (Connector *connector, const ACE_INET_Addr &)
 Initialize this object. More...

virtual ~ACEXML_Mem_Map_Stream (void)
 Destructor. More...

ACE_SOCK_Streamstream (void)
 Returns the underlying <ACE_SOCK_Stream>. More...

virtual ssize_t send_n (const void *buf, size_t size, ACE_Time_Value *tv=0)
virtual int get_char (void)
virtual const char * recv (size_t &len)
virtual const char * recv (void) const
virtual size_t recv_len (void) const
virtual size_t available (void) const
virtual void rewind (void)
virtual int peek_char (size_t offset)
virtual const char * peek_str (size_t offset, size_t size)
virtual off_t seek (off_t offset, int whence=SEEK_CUR)
virtual int eof (void) const
 Returns 1 if we're at the end of the HTTP stream, else 0. More...

Svc_Handlersvc_handler (void)
 Returns the underlying service handler. More...


Private Methods

int grow_file_and_remap (void)

Private Attributes

Svc_Handlersvc_handler_
ACE_Mem_Map mem_map_
 Memory-mapped file that we're iterating over. More...

char * recv_pos_
 Pointer to the address where the next <recv> method will start. More...

char * get_pos_
char * end_of_mapping_plus1_
 Address at the end of the file mapping. More...


Detailed Description

Provides a memory-mapped stream abstraction to simplify parsing of tokens.

This class makes it possible to treat an connection as a stream of bytes, similar to the C library stdio streams. The contents of the connection are buffered incrementally in a memory-mapped file. This class maintains pointers to two positions in the stream:

1. The <recv> position, which keeps track of the beginning of a token that is in the stream.

2. The <get> position, which moves along character-by-character until the end of the token is reached.

Once a token has been located, it can be extracted from the stream by calling the <recv>. The length of the token, i.e., the <recv_len>, is the length in bytes between the <get> position and the <recv> position. Once the token has been extracted, the <recv> and <get> positions can be updated by the <seek> method.

Definition at line 56 of file Mem_Map_Stream.h.


Constructor & Destructor Documentation

ACEXML_Mem_Map_Stream::ACEXML_Mem_Map_Stream void   
 

Default constructor.

Definition at line 8 of file Mem_Map_Stream.cpp.

00009 {
00010 
00011 }

ACEXML_Mem_Map_Stream::~ACEXML_Mem_Map_Stream void    [virtual]
 

Destructor.

Definition at line 236 of file Mem_Map_Stream.cpp.

References mem_map_, and ACE_Mem_Map::remove.

00237 {
00238   // Remove the mapping and the file.
00239   this->mem_map_.remove ();
00240 }


Member Function Documentation

size_t ACEXML_Mem_Map_Stream::available void    const [virtual]
 

Returns the no. of bytes available in the stream.

Definition at line 138 of file Mem_Map_Stream.cpp.

References end_of_mapping_plus1_, and get_pos_.

00139 {
00140   return this->end_of_mapping_plus1_ - this->get_pos_;
00141 }

int ACEXML_Mem_Map_Stream::eof void    const [virtual]
 

Returns 1 if we're at the end of the HTTP stream, else 0.

Definition at line 27 of file Mem_Map_Stream.cpp.

References end_of_mapping_plus1_, and get_pos_.

Referenced by get_char, and recv.

00028 {
00029   return this->get_pos_ >= this->end_of_mapping_plus1_;
00030 }

int ACEXML_Mem_Map_Stream::get_char void    [virtual]
 

Return the next character in the stream and advance the <get> position. Returns EOF when the <get> position reaches the end of the HTTP stream.

Definition at line 33 of file Mem_Map_Stream.cpp.

References eof, get_pos_, and grow_file_and_remap.

Referenced by ACEXML_HttpCharStream::get.

00034 {
00035   if (this->eof () && this->grow_file_and_remap () == -1)
00036     return EOF;
00037 
00038   return *this->get_pos_++;
00039 }

int ACEXML_Mem_Map_Stream::grow_file_and_remap void    [private]
 

Grow the file by reading another chunk from the HTTP socket and extend the mapping to cover this chunk. Returns -1 on failure or EOF, else 0.

Definition at line 189 of file Mem_Map_Stream.cpp.

References ACE_DEFAULT_BASE_ADDR, ACE_DEFAULT_TIMEOUT, ACE_ERROR, ACE_ERROR_RETURN, ACE_MAP_FIXED, ACE_MAP_PRIVATE, end_of_mapping_plus1_, get_pos_, LM_ERROR, ACE_Mem_Map::map, MAP_FAILED, mem_map_, ACE_Svc_Handler< ACE_SOCK_STREAM, ACE_NULL_SYNCH >::peer, PROT_RDWR, recv_pos_, ACE_Mem_Map::size, ssize_t, svc_handler_, and ACE::write_n.

Referenced by get_char, peek_char, peek_str, recv, and seek.

00190 {
00191   char buf[BUFSIZ + 1];
00192 
00193   // Copy the next chunk of bytes from the socket into the temporary
00194   // file.
00195   ACE_Time_Value tv (ACE_DEFAULT_TIMEOUT);
00196 
00197   ssize_t n = this->svc_handler_->peer ().recv (buf, sizeof buf, 0, &tv);
00198   if (n == -1)
00199     {
00200       ACE_ERROR ((LM_ERROR, "%p\n", "recv"));
00201       return -1;
00202     }
00203   else if (n == 0)
00204     return -1;
00205   else if (ACE::write_n (this->mem_map_.handle (), buf, n) != n)
00206     ACE_ERROR_RETURN ((LM_ERROR,
00207                        "%p\n",
00208                        "write_n"),
00209                       -1);
00210 
00211   // Grow the memory-mapping to encompass the entire temporary file.
00212   if (this->mem_map_.map (-1,
00213                           PROT_RDWR,
00214                           ACE_MAP_PRIVATE | ACE_MAP_FIXED,
00215                           ACE_DEFAULT_BASE_ADDR) == -1)
00216     ACE_ERROR_RETURN ((LM_ERROR,
00217                        "%p\n",
00218                        "map"),
00219                       -1);
00220   // MAP_FAILED is used as a "first time in" flag.
00221   if (this->recv_pos_ == MAP_FAILED)
00222     {
00223       this->recv_pos_ = ACE_reinterpret_cast (char *,
00224                                               this->mem_map_.addr ());
00225       this->get_pos_ = this->recv_pos_;
00226     }
00227 
00228   this->end_of_mapping_plus1_ =
00229     ACE_reinterpret_cast (char *,
00230                           this->mem_map_.addr ())
00231     + this->mem_map_.size ();
00232 
00233   return 0;
00234 }

int ACEXML_Mem_Map_Stream::open Connector   connector,
const ACE_INET_Addr  
[virtual]
 

Initialize this object.

Definition at line 144 of file Mem_Map_Stream.cpp.

References ACE_DEFAULT_FILE_PERMS, ACE_ERROR_RETURN, ACE_Connector< Svc_Handler, ACE_SOCK_CONNECTOR >::connect, ACE_INET_Addr::get_host_name, ACE_FILE_Addr::get_path_name, ACE_INET_Addr::get_port_number, LM_ERROR, mem_map_, ACE_Mem_Map::open, rewind, svc_handler_, and ACE_OS::unlink.

Referenced by ACEXML_HttpCharStream::open.

00146 {
00147   svc_handler_ = 0;
00148 
00149   // Connect to the server at <addr>. If the handler has to be
00150   // connected to the server again, the Caching strategy takes care
00151   // and uses the same connection.
00152   if (connector->connect (svc_handler_,
00153                           addr) == -1)
00154     {
00155 
00156       ACE_ERROR_RETURN ((LM_ERROR,
00157                          "%p %s %d\n",
00158                          "Connect failed",
00159                          addr.get_host_name (),
00160                          addr.get_port_number ()),
00161                         -1);
00162     }
00163   // Create a temporary filename.
00164   ACE_FILE_Addr file (ACE_sap_any_cast (ACE_FILE_Addr &));
00165 
00166   // Create the temporary file via the <ACE_Mem_Map> class API.
00167   if (this->mem_map_.open (file.get_path_name (),
00168                            O_RDWR | O_CREAT | O_APPEND,
00169                            ACE_DEFAULT_FILE_PERMS) == -1)
00170     ACE_ERROR_RETURN ((LM_ERROR,
00171                        "%p\n",
00172                        "open"),
00173                       -1);
00174   // Make sure to unlink this right away so that if this process
00175   // crashes these files will be removed automatically.
00176   else if (ACE_OS::unlink (file.get_path_name ()) == -1)
00177     ACE_ERROR_RETURN ((LM_ERROR,
00178                        "%p\n",
00179                        "unlink"),
00180                       -1);
00181   else
00182     // Initialize all the position pointers to 0.
00183     this->rewind ();
00184 
00185   return 0;
00186 }

int ACEXML_Mem_Map_Stream::peek_char size_t    offset [virtual]
 

Returns the nth character <offset> from the <get> position in the stream without advancing the <get> position. Automatically extends the backing store if necessary. Returns EOF if <offset> is past the end of the stream.

Definition at line 51 of file Mem_Map_Stream.cpp.

References end_of_mapping_plus1_, get_pos_, and grow_file_and_remap.

Referenced by ACEXML_HttpCharStream::peek.

00052 {
00053   // We may need to iterate if the size of <n> is large.
00054   while (this->get_pos_ + offset >= this->end_of_mapping_plus1_)
00055     if (this->grow_file_and_remap () == -1)
00056       return EOF;
00057 
00058   return this->get_pos_[offset];
00059 }

const char * ACEXML_Mem_Map_Stream::peek_str size_t    offset,
size_t    size
[virtual]
 

Return a pointer to an array of <size> characters starting at <offset> characters from the <get> position in the stream without advancing the <get> position. Automatically extends the backing store if necessary. Returns 0 if <offset> or <offset + size> is past the end of the stream.

Definition at line 86 of file Mem_Map_Stream.cpp.

References end_of_mapping_plus1_, get_pos_, and grow_file_and_remap.

00088 {
00089   // We will iterate if the size of <offset> is large.
00090   while (this->get_pos_ + (offset + size) > this->end_of_mapping_plus1_)
00091     if (this->grow_file_and_remap () == -1)
00092       return 0;
00093 
00094   return &this->get_pos_[offset];
00095 }

const char * ACEXML_Mem_Map_Stream::recv void    const [virtual]
 

Returns a pointer to array of characters starting at the <recv> position.

Definition at line 62 of file Mem_Map_Stream.cpp.

References recv_pos_.

00063 {
00064   return this->recv_pos_;
00065 }

const char * ACEXML_Mem_Map_Stream::recv size_t &    len [virtual]
 

Returns a pointer to array of at most <len> characters starting at the <recv> position. If the <recv> position + <len> extends past the EOF then <len> is set to the number of characters between the <recv> position and the EOF and both the <get> and <recv> positions are advanced by <len>. Returns 0 if the <recv> position is at the EOF.

Definition at line 68 of file Mem_Map_Stream.cpp.

References eof, get_pos_, grow_file_and_remap, recv_pos_, and seek.

Referenced by ACEXML_HttpCharStream::get_url.

00069 {
00070   if (this->eof () && this->grow_file_and_remap () == -1)
00071     return 0;
00072 
00073   const char *s = this->recv_pos_;
00074   this->seek (ACE_static_cast(off_t, len), SEEK_CUR);
00075   len = this->get_pos_ - s;
00076   return s;
00077 }

size_t ACEXML_Mem_Map_Stream::recv_len void    const [virtual]
 

Returns the length in bytes between the <get> position and the <recv> position.

Definition at line 80 of file Mem_Map_Stream.cpp.

References get_pos_, and recv_pos_.

00081 {
00082   return this->get_pos_ - this->recv_pos_;
00083 }

void ACEXML_Mem_Map_Stream::rewind void    [virtual]
 

Resets the <get> and <recv> positions to the beginning of the stream. This works since all the data has been cached in the memory-mapped backing store.

Definition at line 42 of file Mem_Map_Stream.cpp.

References end_of_mapping_plus1_, get_pos_, mem_map_, recv_pos_, and ACE_Mem_Map::size.

Referenced by ACEXML_HttpCharStream::get_url, open, and ACEXML_HttpCharStream::rewind.

00043 {
00044   this->recv_pos_ = ACE_reinterpret_cast (char *,
00045                           this->mem_map_.addr ());
00046   this->get_pos_ = this->recv_pos_;
00047   this->end_of_mapping_plus1_ = this->recv_pos_ + this->mem_map_.size ();
00048 }

off_t ACEXML_Mem_Map_Stream::seek off_t    offset,
int    whence = SEEK_CUR
[virtual]
 

Sets the <get> and <recv> positions as follows: o If <whence> is <SEEK_SET>, the positions are set to <offset> bytes from the start of the stream. o If <whence> is <SEEK_CUR>, the positions are set to the current <get> position plus <offset>. o If <whence> is <SEEK_END>, the positions are set to the size of the stream plus <offset>.

If offset is greater than EOF, both <get> and <recv> are set to EOF. Note special return value is returned to indicate this condition.

Definition at line 98 of file Mem_Map_Stream.cpp.

References end_of_mapping_plus1_, get_pos_, grow_file_and_remap, and recv_pos_.

Referenced by ACEXML_HttpCharStream::get_url, and recv.

00099 {
00100   switch (whence)
00101     {
00102     case SEEK_SET:
00103       this->get_pos_ =
00104         ACE_reinterpret_cast (char *,
00105                               this->mem_map_.addr ())
00106         + offset;
00107       break;
00108 
00109     case SEEK_CUR:
00110       this->get_pos_ += offset;
00111       break;
00112 
00113     case SEEK_END:
00114       this->get_pos_ =
00115         this->end_of_mapping_plus1_ + offset;
00116       // @@ Not sure how to implement this (yet).
00117       ACE_NOTSUP_RETURN (-1);
00118       break;
00119     }
00120 
00121   // Make sure that the backing store will cover this.
00122   while (this->get_pos_ > this->end_of_mapping_plus1_)
00123     if (this->grow_file_and_remap () == -1)
00124       this->get_pos_ = this->end_of_mapping_plus1_;
00125 
00126   this->recv_pos_ = this->get_pos_;
00127   return this->recv_pos_ - ACE_reinterpret_cast (char *,
00128                                                  this->mem_map_.addr ());
00129 }

ssize_t ACEXML_Mem_Map_Stream::send_n const void *    buf,
size_t    size,
ACE_Time_Value   tv = 0
[virtual]
 

Send <size> bytes in <buf> to the connected peer. This is a completely unbuffered call.

Definition at line 20 of file Mem_Map_Stream.cpp.

References ACE_Svc_Handler< ACE_SOCK_STREAM, ACE_NULL_SYNCH >::peer, and svc_handler_.

Referenced by ACEXML_HttpCharStream::send_request.

00022 {
00023   return svc_handler_->peer ().send_n (buf, size, 0, tv);
00024 }

ACE_SOCK_Stream & ACEXML_Mem_Map_Stream::stream void   
 

Returns the underlying <ACE_SOCK_Stream>.

Definition at line 14 of file Mem_Map_Stream.cpp.

References ACE_Svc_Handler< ACE_SOCK_STREAM, ACE_NULL_SYNCH >::peer, and svc_handler_.

00015 {
00016   return svc_handler_->peer ();
00017 }

Svc_Handler * ACEXML_Mem_Map_Stream::svc_handler void   
 

Returns the underlying service handler.

Definition at line 132 of file Mem_Map_Stream.cpp.

References svc_handler_.

00133 {
00134   return this->svc_handler_;
00135 }


Member Data Documentation

char* ACEXML_Mem_Map_Stream::end_of_mapping_plus1_ [private]
 

Address at the end of the file mapping.

Definition at line 184 of file Mem_Map_Stream.h.

Referenced by available, eof, grow_file_and_remap, peek_char, peek_str, rewind, and seek.

char* ACEXML_Mem_Map_Stream::get_pos_ [private]
 

Pointer to the address where the next <get_char> method will start.

Definition at line 181 of file Mem_Map_Stream.h.

Referenced by available, eof, get_char, grow_file_and_remap, peek_char, peek_str, recv, recv_len, rewind, and seek.

ACE_Mem_Map ACEXML_Mem_Map_Stream::mem_map_ [private]
 

Memory-mapped file that we're iterating over.

Definition at line 172 of file Mem_Map_Stream.h.

Referenced by grow_file_and_remap, open, rewind, and ~ACEXML_Mem_Map_Stream.

char* ACEXML_Mem_Map_Stream::recv_pos_ [private]
 

Pointer to the address where the next <recv> method will start.

Definition at line 175 of file Mem_Map_Stream.h.

Referenced by grow_file_and_remap, recv, recv_len, rewind, and seek.

Svc_Handler* ACEXML_Mem_Map_Stream::svc_handler_ [private]
 

Connection to peer. The granularity is at the Svc_Handler level. The Svc_Handler has an SOCK_Stream.

Definition at line 169 of file Mem_Map_Stream.h.

Referenced by grow_file_and_remap, open, send_n, stream, and svc_handler.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 13:27:15 2003 for ACEXML by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002