2011-01-09 16:42:27 -05:00
|
|
|
/*
|
|
|
|
open source routing machine
|
|
|
|
Copyright (C) Dennis Luxen, 2010
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU AFFERO General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
or see http://www.gnu.org/licenses/agpl.txt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#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) {
|
|
|
|
boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &ioService)));
|
|
|
|
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
|