net/include/pion/net/HTTPResponseReader.hpp

00001 // ------------------------------------------------------------------
00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
00003 // ------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
00008 //
00009 
00010 #ifndef __PION_HTTPRESPONSEREADER_HEADER__
00011 #define __PION_HTTPRESPONSEREADER_HEADER__
00012 
00013 #include <boost/asio.hpp>
00014 #include <boost/bind.hpp>
00015 #include <boost/function.hpp>
00016 #include <boost/function/function2.hpp>
00017 #include <boost/shared_ptr.hpp>
00018 #include <boost/enable_shared_from_this.hpp>
00019 #include <pion/PionConfig.hpp>
00020 #include <pion/net/HTTPResponse.hpp>
00021 #include <pion/net/HTTPReader.hpp>
00022 
00023 
00024 namespace pion {    // begin namespace pion
00025 namespace net {     // begin namespace net (Pion Network Library)
00026 
00027 
00031 class HTTPResponseReader :
00032     public HTTPReader,
00033     public boost::enable_shared_from_this<HTTPResponseReader>
00034 {
00035 
00036 public:
00037 
00039     typedef boost::function2<void, HTTPResponsePtr, TCPConnectionPtr>   FinishedHandler;
00040 
00041     
00042     // default destructor
00043     virtual ~HTTPResponseReader() {}
00044     
00052     static inline boost::shared_ptr<HTTPResponseReader>
00053         create(TCPConnectionPtr& tcp_conn, const HTTPRequest& http_request,
00054                FinishedHandler handler)
00055     {
00056         return boost::shared_ptr<HTTPResponseReader>
00057             (new HTTPResponseReader(tcp_conn, http_request, handler));
00058     }
00059 
00060     
00061 protected:
00062 
00070     HTTPResponseReader(TCPConnectionPtr& tcp_conn, const HTTPRequest& http_request,
00071                        FinishedHandler handler)
00072         : HTTPReader(false, tcp_conn), m_http_msg(new HTTPResponse(http_request)),
00073         m_finished(handler)
00074     {
00075         m_http_msg->setRemoteIp(tcp_conn->getRemoteIp());
00076         setLogger(PION_GET_LOGGER("pion.net.HTTPResponseReader"));
00077     }
00078         
00080     virtual void readBytes(void) {
00081         getTCPConnection()->async_read_some(boost::bind(&HTTPResponseReader::consumeBytes,
00082                                                         shared_from_this(),
00083                                                         boost::asio::placeholders::error,
00084                                                         boost::asio::placeholders::bytes_transferred));
00085     }
00086 
00088     virtual void finishedReading(void) {
00089         // call the finished handler with the finished HTTP message
00090         m_finished(m_http_msg, getTCPConnection());
00091     }
00092     
00094     virtual HTTPMessage& getMessage(void) { return *m_http_msg; }
00095 
00096     
00098     HTTPResponsePtr             m_http_msg;
00099 
00101     FinishedHandler             m_finished;
00102 };
00103 
00104 
00106 typedef boost::shared_ptr<HTTPResponseReader>   HTTPResponseReaderPtr;
00107 
00108 
00109 }   // end namespace net
00110 }   // end namespace pion
00111 
00112 #endif

Generated on Fri Apr 30 14:48:53 2010 for pion-net by  doxygen 1.4.7