Queries are now handled by a static graph which means less RAM usage and faster queries.

This commit is contained in:
Dennis Luxen 2010-07-14 12:56:24 +00:00
parent fa5a042071
commit 4351e8850a
3 changed files with 81 additions and 90 deletions

View File

@ -38,77 +38,40 @@ namespace http {
/// Represents a single connection from a client.
template<typename GraphT>
class connection
: public boost::enable_shared_from_this<connection>,
: public boost::enable_shared_from_this<connection<GraphT> >,
private boost::noncopyable
{
public:
/// Construct a connection with the given io_service.
explicit connection(boost::asio::io_service& io_service,
request_handler& handler);
/// Get the socket associated with the connection.
boost::asio::ip::tcp::socket& socket();
/// Start the first asynchronous operation for the connection.
void start();
private:
/// Handle completion of a read operation.
void handle_read(const boost::system::error_code& e,
std::size_t bytes_transferred);
/// Handle completion of a write operation.
void handle_write(const boost::system::error_code& e);
/// Strand to ensure the connection's handlers are not called concurrently.
boost::asio::io_service::strand strand_;
/// Socket for the connection.
boost::asio::ip::tcp::socket socket_;
/// The handler used to process the incoming request.
request_handler& request_handler_;
/// Buffer for incoming data.
boost::array<char, 8192> buffer_;
/// The incoming request.
request request_;
/// The parser for the incoming request.
request_parser request_parser_;
/// The reply to be sent back to the client.
reply reply_;
};
typedef boost::shared_ptr<connection> connection_ptr;
connection::connection(boost::asio::io_service& io_service,
request_handler& handler)
request_handler<GraphT>& handler)
: strand_(io_service),
socket_(io_service),
request_handler_(handler)
{
}
boost::asio::ip::tcp::socket& connection::socket()
/// Get the socket associated with the connection.
boost::asio::ip::tcp::socket& socket()
{
return socket_;
}
void connection::start()
/// Start the first asynchronous operation for the connection.
void start()
{
socket_.async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(
boost::bind(&connection::handle_read, shared_from_this(),
boost::bind(&connection<GraphT>::handle_read, this->shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
void connection::handle_read(const boost::system::error_code& e,
private:
/// Handle completion of a read operation.
void handle_read(const boost::system::error_code& e,
std::size_t bytes_transferred)
{
if (!e)
@ -122,7 +85,7 @@ void connection::handle_read(const boost::system::error_code& e,
request_handler_.handle_request(request_, reply_);
boost::asio::async_write(socket_, reply_.to_buffers(),
strand_.wrap(
boost::bind(&connection::handle_write, shared_from_this(),
boost::bind(&connection<GraphT>::handle_write, this->shared_from_this(),
boost::asio::placeholders::error)));
}
else if (!result)
@ -130,21 +93,22 @@ void connection::handle_read(const boost::system::error_code& e,
reply_ = reply::stock_reply(reply::bad_request);
boost::asio::async_write(socket_, reply_.to_buffers(),
strand_.wrap(
boost::bind(&connection::handle_write, shared_from_this(),
boost::bind(&connection<GraphT>::handle_write, this->shared_from_this(),
boost::asio::placeholders::error)));
}
else
{
socket_.async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(
boost::bind(&connection::handle_read, shared_from_this(),
boost::bind(&connection<GraphT>::handle_read, this->shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
}
}
void connection::handle_write(const boost::system::error_code& e)
/// Handle completion of a write operation.
void handle_write(const boost::system::error_code& e)
{
if (!e)
{
@ -159,6 +123,29 @@ void connection::handle_write(const boost::system::error_code& e)
// destructor closes the socket.
}
/// Strand to ensure the connection's handlers are not called concurrently.
boost::asio::io_service::strand strand_;
/// Socket for the connection.
boost::asio::ip::tcp::socket socket_;
/// The handler used to process the incoming request.
request_handler<GraphT>& request_handler_;
/// Buffer for incoming data.
boost::array<char, 8192> buffer_;
/// The incoming request.
request request_;
/// The parser for the incoming request.
request_parser request_parser_;
/// The reply to be sent back to the client.
reply reply_;
};
} // namespace http
#endif /* CONNECTION_HPP_ */

View File

@ -39,11 +39,12 @@ struct reply;
struct request;
/// The common handler for all incoming requests.
template<typename GraphT>
class request_handler : private boost::noncopyable
{
public:
/// Construct with a directory containing files to be served.
explicit request_handler(SearchEngine<EdgeData> * s) : sEngine(s){}
explicit request_handler(SearchEngine<EdgeData, GraphT> * s) : sEngine(s){}
/// Handle a request and produce a reply.
void handle_request(const request& req, reply& rep){
@ -162,7 +163,7 @@ public:
private:
//SearchEngine object that is queried
SearchEngine<EdgeData> * sEngine;
SearchEngine<EdgeData, GraphT> * sEngine;
};
} // namespace ROUTER

View File

@ -38,15 +38,16 @@ or see http://www.gnu.org/licenses/agpl.txt.
namespace http {
/// The top-level class of the HTTP server.
template<typename GraphT>
class server: private boost::noncopyable
{
public:
/// Construct the server to listen on the specified TCP address and port, and
/// serve up files from the given directory.
explicit server(const std::string& address, const std::string& port, std::size_t thread_pool_size, SearchEngine<EdgeData, NodeInformationHelpDesk> * s)
explicit server(const std::string& address, const std::string& port, std::size_t thread_pool_size, SearchEngine<EdgeData, GraphT, NodeInformationHelpDesk> * s)
: thread_pool_size_(thread_pool_size),
acceptor_(io_service_),
new_connection_(new connection(io_service_, request_handler_)),
new_connection_(new connection<GraphT>(io_service_, request_handler_)),
request_handler_(s),
sEngine(s)
{
@ -86,13 +87,15 @@ public:
}
private:
typedef boost::shared_ptr<connection<GraphT> > connection_ptr;
/// Handle completion of an asynchronous accept operation.
void handle_accept(const boost::system::error_code& e)
{
if (!e)
{
new_connection_->start();
new_connection_.reset(new connection(io_service_, request_handler_));
new_connection_.reset(new connection<GraphT>(io_service_, request_handler_));
acceptor_.async_accept(new_connection_->socket(),
boost::bind(&server::handle_accept, this,
boost::asio::placeholders::error));
@ -112,10 +115,10 @@ private:
connection_ptr new_connection_;
/// The handler for all incoming requests.
request_handler request_handler_;
request_handler<GraphT> request_handler_;
/// The object to query the Routing Engine
SearchEngine<EdgeData> * sEngine;
SearchEngine<EdgeData, GraphT> * sEngine;
};