Boost logo

Boost-Commit :

From: lists.drrngrvy_at_[hidden]
Date: 2008-03-28 18:01:04


Author: drrngrvy
Date: 2008-03-28 18:01:04 EDT (Fri, 28 Mar 2008)
New Revision: 43917
URL: http://svn.boost.org/trac/boost/changeset/43917

Log:
Adding slightly more async FastCGI server example.
Added:
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server2/
      - copied from r43912, /sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server1/
Text files modified:
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server1/main.cpp | 6 +-
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server2/Jamfile.v2 | 6 +-
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server2/main.cpp | 85 +++++++++++++++++++++++++++------------
   3 files changed, 64 insertions(+), 33 deletions(-)

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server1/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server1/main.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server1/main.cpp 2008-03-28 18:01:04 EDT (Fri, 28 Mar 2008)
@@ -70,8 +70,8 @@
   int handle_request(fcgi::request& req, boost::system::error_code& ec)
   {
     std::ofstream log_(LOG_FILE, std::ios::app);
- log_<< "Handling request" << endl
- << "QUERY_STRING := " << req.query_string() << std::endl;
+ //log_<< "Handling request" << endl
+ // << "QUERY_STRING := " << req.query_string() << std::endl;
 
     // Construct a `response` object (makes writing/sending responses easier).
     response resp;
@@ -91,7 +91,7 @@
     resp<< "<content-length == " << content_length(resp.content_length())
         << content_length(resp.content_length());
 
- log_<< "Handled request, handling another." << std::endl;
+ //log_<< "Handled request, handling another." << std::endl;
 
     // This funky macro finishes up:
     return_(resp, req, 0);

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server2/Jamfile.v2
==============================================================================
--- /sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server1/Jamfile.v2 (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server2/Jamfile.v2 2008-03-28 18:01:04 EDT (Fri, 28 Mar 2008)
@@ -1,11 +1,11 @@
 
-project cgi.examples.fcgi.sync_server ;
+project cgi.examples.fcgi.server2 ;
 
-exe fcgi_server1 : main.cpp ;
+exe fcgi_server2 : main.cpp ;
 
 install install
   :
- fcgi_server1
+ fcgi_server2
   :
     <location>$(fcgi-bin)
   ;

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server2/main.cpp
==============================================================================
--- /sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server1/main.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/server2/main.cpp 2008-03-28 18:01:04 EDT (Fri, 28 Mar 2008)
@@ -1,4 +1,4 @@
-// -- server1/main.hpp --
+// -- server2/main.hpp --
 //
 // Copyright (c) Darren Garvey 2007.
 // Distributed under the Boost Software License, Version 1.0.
@@ -7,7 +7,7 @@
 //
 ////////////////////////////////////////////////////////////////
 //
-//[fcgi_server1
+//[fcgi_server2
 //
 // This example simply echoes all variables back to the user. ie.
 // the environment and the parsed GET, POST and cookie variables.
@@ -17,11 +17,15 @@
 // It is a demonstration of how a 'server' can be used to abstract
 // away the differences between FastCGI and CGI requests.
 //
-// This is very similar to the fcgi_echo example.
+// This is very similar to the fcgi_echo and fcgi_server1 examples.
+// Unlike in the server1 example, the server class in this example uses
+// asynchronous functions, to increase throughput.
 //
+//
+// **FIXME**
+// This is slower than the server1 example, which is stupid.
 
 #include <fstream>
-#include <boost/bind.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/program_options/environment_iterator.hpp>
 
@@ -31,7 +35,7 @@
 using namespace boost::fcgi;
 
 // This is a file to put internal logging info into
-#define LOG_FILE "/var/www/log/fcgi_server1.txt"
+#define LOG_FILE "/var/www/log/fcgi_server2.txt"
 
 // This function writes the title and map contents to the ostream in an
 // HTML-encoded format (to make them easier on the eye).
@@ -70,8 +74,8 @@
   int handle_request(fcgi::request& req, boost::system::error_code& ec)
   {
     std::ofstream log_(LOG_FILE, std::ios::app);
- log_<< "Handling request" << endl
- << "QUERY_STRING := " << req.query_string() << std::endl;
+ //log_<< "Handling request" << endl
+ // << "QUERY_STRING := " << req.query_string() << std::endl;
 
     // Construct a `response` object (makes writing/sending responses easier).
     response resp;
@@ -91,7 +95,7 @@
     resp<< "<content-length == " << content_length(resp.content_length())
         << content_length(resp.content_length());
 
- log_<< "Handled request, handling another." << std::endl;
+ //log_<< "Handled request, handling another." << std::endl;
 
     // This funky macro finishes up:
     return_(resp, req, 0);
@@ -134,35 +138,60 @@
     , acceptor_(service_)
   {}
 
- int run()
+ void run()
   {
     // Create a new request (on the heap - uses boost::shared_ptr<>).
     request_type::pointer new_request = request_type::create(service_);
     // Add the request to the set of existing requests.
     requests_.insert(new_request);
-
- int ret(0);
- for (;;)
+
+ start_accept(new_request);
+ service_.run();
+ }
+
+ void start_accept(request_type::pointer& new_request)
+ {
+ acceptor_.async_accept(*new_request, boost::bind(&server::handle_accept
+ , this, new_request
+ , boost::asio::placeholders::error)
+ );
+ }
+
+ void handle_accept(request_type::pointer req
+ , boost::system::error_code ec)
+ {
+ if (ec)
     {
- boost::system::error_code ec;
+ //std::cerr<< "Error accepting request: " << ec.message() << std::endl;
+ return;
+ }
 
- acceptor_.accept(*new_request, ec);
+ req->load(ec, true);
 
- if (ec)
- {
- std::cerr<< "Error accepting: " << ec.message() << std::endl;
- return 5;
- }
-
- // Load in the request data so we can access it easily.
- new_request->load(ec, true); // The 'true' means read and parse POST data.
+ //req->async_load(boost::bind(&server::handle_request, this
+ // , req, boost::asio::placeholders::error)
+ // , true);
 
- ret = handler_(*new_request, ec);
+ service_.post(boost::bind(&server::handle_request, this, req, ec));
 
- if (ret)
- break;
+ // Create a new request (on the heap - uses boost::shared_ptr<>).
+ request_type::pointer new_request = request_type::create(service_);
+ // Add the request to the set of existing requests.
+ requests_.insert(new_request);
+
+ start_accept(new_request);
+ }
+
+ void handle_request(request_type::pointer req
+ , boost::system::error_code ec)
+ {
+ handler_(*req, ec);
+ if (ec)
+ {
+ //std::cerr<< "Request handled, but ended in error: " << ec.message()
+ // << std::endl;
     }
- return ret;
+ start_accept(req);
   }
 
 private:
@@ -181,7 +210,9 @@
                       , &rh, _1, _2)
           );
 
- return s.run();
+ s.run();
+
+ return 0;
   
 }catch(boost::system::system_error& se){
   cerr<< "[fcgi] System error: " << se.what() << endl;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk