keep server instance in a unique_ptr

This commit is contained in:
Dennis Luxen 2014-08-20 11:24:29 +02:00
parent 82c2ae5441
commit 8f8cb6a52f
3 changed files with 44 additions and 24 deletions

View File

@ -28,14 +28,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef SERVER_H
#define SERVER_H
#include "../Util/StringUtil.h"
#include "Connection.h"
#include "RequestHandler.h"
#include "../Util/make_unique.hpp"
#include "../Util/SimpleLogger.h"
#include "../Util/StringUtil.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <zlib.h>
#include <functional>
#include <memory>
#include <thread>
@ -44,6 +48,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
class Server
{
public:
static std::unique_ptr<Server> CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads)
{
SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion();
const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency());
const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads);
return osrm::make_unique<Server>(ip_address, ip_port, real_num_threads);
}
explicit Server(const std::string &address, const int port, const unsigned thread_pool_size)
: thread_pool_size(thread_pool_size), acceptor(io_service),
new_connection(new http::Connection(io_service, request_handler)), request_handler()

View File

@ -25,25 +25,32 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SERVER_FACTORY_H
#define SERVER_FACTORY_H
#ifndef MAKE_UNIQUE_H__
#define MAKE_UNIQUE_H__
#include "Server.h"
#include "../Util/SimpleLogger.h"
#include <cstdlib>
#include <memory>
#include <type_traits>
#include <zlib.h>
struct ServerFactory
namespace osrm
{
ServerFactory() = delete;
ServerFactory(const ServerFactory &) = delete;
static Server *CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads)
{
SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion();
const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency());
const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads);
return new Server(ip_address, ip_port, real_num_threads);
}
};
// Taken from http://msdn.microsoft.com/en-us/library/dn439780.asp
// Note, that the snippet is broken there and needed minor massaging
#endif // SERVER_FACTORY_H
// make_unique<T>
template <class T, class... Types> std::unique_ptr<T> make_unique(Types &&... Args)
{
return (std::unique_ptr<T>(new T(std::forward<Types>(Args)...)));
}
// make_unique<T[]>
template <class T> std::unique_ptr<T> make_unique(std::size_t Size)
{
return (std::unique_ptr<T>(new T[Size]()));
}
// make_unique<T[N]> disallowed
template <class T, class... Types>
typename std::enable_if<std::extent<T>::value != 0, void>::type make_unique(Types &&...) = delete;
}
#endif

View File

@ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Library/OSRM.h"
#include "Server/ServerFactory.h"
#include "Server/Server.h"
#include "Util/GitDescription.h"
#include "Util/ProgramOptions.h"
#include "Util/SimpleLogger.h"
@ -131,8 +131,8 @@ int main(int argc, const char *argv[])
#endif
OSRM osrm_lib(server_paths, use_shared_memory);
Server *routing_server =
ServerFactory::CreateServer(ip_address, ip_port, requested_thread_num);
auto routing_server =
Server::CreateServer(ip_address, ip_port, requested_thread_num);
routing_server->GetRequestHandlerPtr().RegisterRoutingMachine(&osrm_lib);
@ -181,7 +181,7 @@ int main(int argc, const char *argv[])
}
SimpleLogger().Write() << "freeing objects";
delete routing_server;
routing_server.reset();
SimpleLogger().Write() << "shutdown completed";
}
catch (const std::exception &e)