2011-01-09 16:42:27 -05:00
|
|
|
/*
|
|
|
|
|
2013-10-14 07:42:28 -04:00
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
All rights reserved.
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2013-10-14 07:42:28 -04:00
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2013-10-14 07:42:28 -04:00
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2011-01-09 16:42:27 -05:00
|
|
|
|
|
|
|
#ifndef SERVER_H
|
|
|
|
#define SERVER_H
|
|
|
|
|
2013-06-26 19:48:02 -04:00
|
|
|
#include "Connection.h"
|
|
|
|
#include "RequestHandler.h"
|
|
|
|
|
2011-01-09 16:42:27 -05:00
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
2014-04-16 10:59:40 -04:00
|
|
|
#include <boost/make_shared.hpp>
|
2011-01-09 16:42:27 -05:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
2013-06-26 19:48:02 -04:00
|
|
|
#include <vector>
|
2011-01-09 16:42:27 -05:00
|
|
|
|
|
|
|
class Server: private boost::noncopyable {
|
|
|
|
public:
|
2013-06-26 19:48:02 -04:00
|
|
|
explicit Server(
|
|
|
|
const std::string& address,
|
|
|
|
const std::string& port,
|
|
|
|
unsigned thread_pool_size
|
|
|
|
) :
|
|
|
|
threadPoolSize(thread_pool_size),
|
|
|
|
acceptor(ioService),
|
|
|
|
newConnection(new http::Connection(ioService, requestHandler)),
|
|
|
|
requestHandler()
|
|
|
|
{
|
2011-01-09 16:42:27 -05:00
|
|
|
boost::asio::ip::tcp::resolver resolver(ioService);
|
|
|
|
boost::asio::ip::tcp::resolver::query query(address, port);
|
|
|
|
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
|
|
|
|
|
|
|
|
acceptor.open(endpoint.protocol());
|
|
|
|
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
|
|
|
acceptor.bind(endpoint);
|
|
|
|
acceptor.listen();
|
2013-06-26 19:48:02 -04:00
|
|
|
acceptor.async_accept(
|
|
|
|
newConnection->socket(),
|
|
|
|
boost::bind(
|
|
|
|
&Server::handleAccept,
|
|
|
|
this,
|
|
|
|
boost::asio::placeholders::error
|
|
|
|
)
|
|
|
|
);
|
2011-01-09 16:42:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Run() {
|
|
|
|
std::vector<boost::shared_ptr<boost::thread> > threads;
|
|
|
|
for (unsigned i = 0; i < threadPoolSize; ++i) {
|
2014-04-16 10:59:40 -04:00
|
|
|
boost::shared_ptr<boost::thread> thread = boost::make_shared<boost::thread>(boost::bind(&boost::asio::io_service::run, &ioService));
|
2011-01-09 16:42:27 -05:00
|
|
|
threads.push_back(thread);
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < threads.size(); ++i)
|
|
|
|
threads[i]->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() {
|
|
|
|
ioService.stop();
|
|
|
|
}
|
|
|
|
|
2011-03-14 09:39:16 -04:00
|
|
|
RequestHandler & GetRequestHandlerPtr() {
|
|
|
|
return requestHandler;
|
2011-01-09 16:42:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handleAccept(const boost::system::error_code& e) {
|
|
|
|
if (!e) {
|
|
|
|
newConnection->start();
|
2013-06-26 19:48:02 -04:00
|
|
|
newConnection.reset(
|
|
|
|
new http::Connection(ioService, requestHandler)
|
|
|
|
);
|
|
|
|
acceptor.async_accept(
|
|
|
|
newConnection->socket(),
|
|
|
|
boost::bind(
|
|
|
|
&Server::handleAccept,
|
|
|
|
this,
|
|
|
|
boost::asio::placeholders::error
|
|
|
|
)
|
|
|
|
);
|
2011-01-09 16:42:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned threadPoolSize;
|
|
|
|
boost::asio::io_service ioService;
|
|
|
|
boost::asio::ip::tcp::acceptor acceptor;
|
2013-06-26 19:48:02 -04:00
|
|
|
boost::shared_ptr<http::Connection> newConnection;
|
2011-01-09 16:42:27 -05:00
|
|
|
RequestHandler requestHandler;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SERVER_H
|