Queries are now handled by a static graph which means less RAM usage and faster queries.
This commit is contained in:
parent
fa5a042071
commit
4351e8850a
@ -38,28 +38,90 @@ namespace http {
|
|||||||
|
|
||||||
|
|
||||||
/// Represents a single connection from a client.
|
/// Represents a single connection from a client.
|
||||||
|
template<typename GraphT>
|
||||||
class connection
|
class connection
|
||||||
: public boost::enable_shared_from_this<connection>,
|
: public boost::enable_shared_from_this<connection<GraphT> >,
|
||||||
private boost::noncopyable
|
private boost::noncopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Construct a connection with the given io_service.
|
/// Construct a connection with the given io_service.
|
||||||
explicit connection(boost::asio::io_service& io_service,
|
explicit connection(boost::asio::io_service& io_service,
|
||||||
request_handler& handler);
|
request_handler<GraphT>& handler)
|
||||||
|
: strand_(io_service),
|
||||||
|
socket_(io_service),
|
||||||
|
request_handler_(handler)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the socket associated with the connection.
|
/// Get the socket associated with the connection.
|
||||||
boost::asio::ip::tcp::socket& socket();
|
boost::asio::ip::tcp::socket& socket()
|
||||||
|
{
|
||||||
|
return socket_;
|
||||||
|
}
|
||||||
|
|
||||||
/// Start the first asynchronous operation for the connection.
|
/// Start the first asynchronous operation for the connection.
|
||||||
void start();
|
void start()
|
||||||
|
{
|
||||||
|
socket_.async_read_some(boost::asio::buffer(buffer_),
|
||||||
|
strand_.wrap(
|
||||||
|
boost::bind(&connection<GraphT>::handle_read, this->shared_from_this(),
|
||||||
|
boost::asio::placeholders::error,
|
||||||
|
boost::asio::placeholders::bytes_transferred)));
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Handle completion of a read operation.
|
/// Handle completion of a read operation.
|
||||||
void handle_read(const boost::system::error_code& e,
|
void handle_read(const boost::system::error_code& e,
|
||||||
std::size_t bytes_transferred);
|
std::size_t bytes_transferred)
|
||||||
|
{
|
||||||
|
if (!e)
|
||||||
|
{
|
||||||
|
boost::tribool result;
|
||||||
|
boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
|
||||||
|
request_, buffer_.data(), buffer_.data() + bytes_transferred);
|
||||||
|
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
request_handler_.handle_request(request_, reply_);
|
||||||
|
boost::asio::async_write(socket_, reply_.to_buffers(),
|
||||||
|
strand_.wrap(
|
||||||
|
boost::bind(&connection<GraphT>::handle_write, this->shared_from_this(),
|
||||||
|
boost::asio::placeholders::error)));
|
||||||
|
}
|
||||||
|
else if (!result)
|
||||||
|
{
|
||||||
|
reply_ = reply::stock_reply(reply::bad_request);
|
||||||
|
boost::asio::async_write(socket_, reply_.to_buffers(),
|
||||||
|
strand_.wrap(
|
||||||
|
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<GraphT>::handle_read, this->shared_from_this(),
|
||||||
|
boost::asio::placeholders::error,
|
||||||
|
boost::asio::placeholders::bytes_transferred)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Handle completion of a write operation.
|
/// Handle completion of a write operation.
|
||||||
void handle_write(const boost::system::error_code& e);
|
void handle_write(const boost::system::error_code& e)
|
||||||
|
{
|
||||||
|
if (!e)
|
||||||
|
{
|
||||||
|
// Initiate graceful connection closure.
|
||||||
|
boost::system::error_code ignored_ec;
|
||||||
|
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
// No new asynchronous operations are started. This means that all shared_ptr
|
||||||
|
// references to the connection object will disappear and the object will be
|
||||||
|
// destroyed automatically after this handler returns. The connection class's
|
||||||
|
// destructor closes the socket.
|
||||||
|
}
|
||||||
|
|
||||||
/// Strand to ensure the connection's handlers are not called concurrently.
|
/// Strand to ensure the connection's handlers are not called concurrently.
|
||||||
boost::asio::io_service::strand strand_;
|
boost::asio::io_service::strand strand_;
|
||||||
@ -68,7 +130,7 @@ private:
|
|||||||
boost::asio::ip::tcp::socket socket_;
|
boost::asio::ip::tcp::socket socket_;
|
||||||
|
|
||||||
/// The handler used to process the incoming request.
|
/// The handler used to process the incoming request.
|
||||||
request_handler& request_handler_;
|
request_handler<GraphT>& request_handler_;
|
||||||
|
|
||||||
/// Buffer for incoming data.
|
/// Buffer for incoming data.
|
||||||
boost::array<char, 8192> buffer_;
|
boost::array<char, 8192> buffer_;
|
||||||
@ -81,84 +143,9 @@ private:
|
|||||||
|
|
||||||
/// The reply to be sent back to the client.
|
/// The reply to be sent back to the client.
|
||||||
reply reply_;
|
reply reply_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::shared_ptr<connection> connection_ptr;
|
|
||||||
|
|
||||||
|
|
||||||
connection::connection(boost::asio::io_service& io_service,
|
|
||||||
request_handler& handler)
|
|
||||||
: strand_(io_service),
|
|
||||||
socket_(io_service),
|
|
||||||
request_handler_(handler)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::asio::ip::tcp::socket& connection::socket()
|
|
||||||
{
|
|
||||||
return socket_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void connection::start()
|
|
||||||
{
|
|
||||||
socket_.async_read_some(boost::asio::buffer(buffer_),
|
|
||||||
strand_.wrap(
|
|
||||||
boost::bind(&connection::handle_read, shared_from_this(),
|
|
||||||
boost::asio::placeholders::error,
|
|
||||||
boost::asio::placeholders::bytes_transferred)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void connection::handle_read(const boost::system::error_code& e,
|
|
||||||
std::size_t bytes_transferred)
|
|
||||||
{
|
|
||||||
if (!e)
|
|
||||||
{
|
|
||||||
boost::tribool result;
|
|
||||||
boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
|
|
||||||
request_, buffer_.data(), buffer_.data() + bytes_transferred);
|
|
||||||
|
|
||||||
if (result)
|
|
||||||
{
|
|
||||||
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::asio::placeholders::error)));
|
|
||||||
}
|
|
||||||
else if (!result)
|
|
||||||
{
|
|
||||||
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::asio::placeholders::error)));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
socket_.async_read_some(boost::asio::buffer(buffer_),
|
|
||||||
strand_.wrap(
|
|
||||||
boost::bind(&connection::handle_read, shared_from_this(),
|
|
||||||
boost::asio::placeholders::error,
|
|
||||||
boost::asio::placeholders::bytes_transferred)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void connection::handle_write(const boost::system::error_code& e)
|
|
||||||
{
|
|
||||||
if (!e)
|
|
||||||
{
|
|
||||||
// Initiate graceful connection closure.
|
|
||||||
boost::system::error_code ignored_ec;
|
|
||||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
|
|
||||||
}
|
|
||||||
|
|
||||||
// No new asynchronous operations are started. This means that all shared_ptr
|
|
||||||
// references to the connection object will disappear and the object will be
|
|
||||||
// destroyed automatically after this handler returns. The connection class's
|
|
||||||
// destructor closes the socket.
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace http
|
} // namespace http
|
||||||
|
|
||||||
#endif /* CONNECTION_HPP_ */
|
#endif /* CONNECTION_HPP_ */
|
||||||
|
@ -39,11 +39,12 @@ struct reply;
|
|||||||
struct request;
|
struct request;
|
||||||
|
|
||||||
/// The common handler for all incoming requests.
|
/// The common handler for all incoming requests.
|
||||||
|
template<typename GraphT>
|
||||||
class request_handler : private boost::noncopyable
|
class request_handler : private boost::noncopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Construct with a directory containing files to be served.
|
/// 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.
|
/// Handle a request and produce a reply.
|
||||||
void handle_request(const request& req, reply& rep){
|
void handle_request(const request& req, reply& rep){
|
||||||
@ -162,7 +163,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//SearchEngine object that is queried
|
//SearchEngine object that is queried
|
||||||
SearchEngine<EdgeData> * sEngine;
|
SearchEngine<EdgeData, GraphT> * sEngine;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ROUTER
|
} // namespace ROUTER
|
||||||
|
@ -38,15 +38,16 @@ or see http://www.gnu.org/licenses/agpl.txt.
|
|||||||
namespace http {
|
namespace http {
|
||||||
|
|
||||||
/// The top-level class of the HTTP server.
|
/// The top-level class of the HTTP server.
|
||||||
|
template<typename GraphT>
|
||||||
class server: private boost::noncopyable
|
class server: private boost::noncopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Construct the server to listen on the specified TCP address and port, and
|
/// Construct the server to listen on the specified TCP address and port, and
|
||||||
/// serve up files from the given directory.
|
/// 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),
|
: thread_pool_size_(thread_pool_size),
|
||||||
acceptor_(io_service_),
|
acceptor_(io_service_),
|
||||||
new_connection_(new connection(io_service_, request_handler_)),
|
new_connection_(new connection<GraphT>(io_service_, request_handler_)),
|
||||||
request_handler_(s),
|
request_handler_(s),
|
||||||
sEngine(s)
|
sEngine(s)
|
||||||
{
|
{
|
||||||
@ -86,13 +87,15 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
typedef boost::shared_ptr<connection<GraphT> > connection_ptr;
|
||||||
|
|
||||||
/// Handle completion of an asynchronous accept operation.
|
/// Handle completion of an asynchronous accept operation.
|
||||||
void handle_accept(const boost::system::error_code& e)
|
void handle_accept(const boost::system::error_code& e)
|
||||||
{
|
{
|
||||||
if (!e)
|
if (!e)
|
||||||
{
|
{
|
||||||
new_connection_->start();
|
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(),
|
acceptor_.async_accept(new_connection_->socket(),
|
||||||
boost::bind(&server::handle_accept, this,
|
boost::bind(&server::handle_accept, this,
|
||||||
boost::asio::placeholders::error));
|
boost::asio::placeholders::error));
|
||||||
@ -112,10 +115,10 @@ private:
|
|||||||
connection_ptr new_connection_;
|
connection_ptr new_connection_;
|
||||||
|
|
||||||
/// The handler for all incoming requests.
|
/// The handler for all incoming requests.
|
||||||
request_handler request_handler_;
|
request_handler<GraphT> request_handler_;
|
||||||
|
|
||||||
/// The object to query the Routing Engine
|
/// The object to query the Routing Engine
|
||||||
SearchEngine<EdgeData> * sEngine;
|
SearchEngine<EdgeData, GraphT> * sEngine;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user