WIP: adding service files, still not working

This commit is contained in:
emeric
2014-04-03 09:19:35 +02:00
parent 10d1464ca2
commit 886b3221f5
24 changed files with 476 additions and 102 deletions
@@ -7,10 +7,10 @@ namespace Remote
class Header
{
static const std::size_t size = 8;
}
public:
static const std::size_t size = 8;
};
Binary file not shown.
+9 -6
View File
@@ -1,4 +1,5 @@
#include <utility>
#include <vector>
#include <boost/bind.hpp>
@@ -10,10 +11,12 @@
namespace Remote {
namespace Server {
Connection::Connection(boost::asio::io_service& ioService, Connection_manager& manager, RequestHandler& handler)
: _socket(ioService),
Connection::Connection(boost::asio::ip::tcp::socket socket,
ConnectionManager& manager,
RequestHandler& handler)
: _socket(std::move(socket)),
_connectionManager(manager),
request_handler_(handler)
_requestHandler(handler)
{
}
@@ -58,16 +61,16 @@ Connection::handleRead(const boost::system::error_code& error, std::size_t bytes
}
}
void Connection::handleWrite(const boost::system::error_code& e)
void Connection::handleWrite(const boost::system::error_code& error)
{
if (!e)
if (!error)
{
// Initiate graceful Connection closure.
boost::system::error_code ignored_ec;
_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
}
if (e != boost::asio::error::operation_aborted)
if (error != boost::asio::error::operation_aborted)
{
std::cerr << "Connection::handleWrite: " << error.message() << std::endl;
_connectionManager.stop(shared_from_this());
+18 -17
View File
@@ -1,33 +1,33 @@
#ifndef REMOTE_CONNECTION_HPP
#define REMOTE_CONNECTION_HPP
#include <array>
#include <memory>
#include <array.hpp>
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "reply.hpp"
#include "request.hpp"
#include "RequestHandler.hpp"
#include "request_parser.hpp"
namespace http {
namespace server {
#include "messages/Header.hpp"
namespace Remote {
namespace Server {
class ConnectionManager;
/// Represents a single connection from a client.
class Connection : public boost::enable_shared_from_this<Connection>, boost::noncopyable
class Connection : public std::enable_shared_from_this<Connection>
{
public:
typedef std::shared_ptr<connection> pointer;
Connection(const Connection&) = delete;
Connection& operator=(const Connection&) = delete;
typedef std::shared_ptr<Connection> pointer;
/// Construct a connection with the given io_service.
Connection(boost::asio::io_service& io_service,
explicit Connection(boost::asio::ip::tcp::socket socket,
ConnectionManager& manager, RequestHandler& handler);
boost::asio::ip::tcp::socket& socket();
@@ -50,10 +50,10 @@ class Connection : public boost::enable_shared_from_this<Connection>, boost::non
boost::asio::ip::tcp::socket _socket;
/// The manager for this connection.
ConnectionManager& _ConnectionManager;
ConnectionManager& _connectionManager;
/// The handler used to process the incoming requests.
RequestHandler& _RequestHandler;
RequestHandler& _requestHandler;
// TODO use streambuffers
@@ -70,9 +70,10 @@ class Connection : public boost::enable_shared_from_this<Connection>, boost::non
};
} // namespace server
} // namespace http
} // namespace Server
#endif // HTTP_CONNECTION_HPP
} // namespace Remote
#endif
+11 -6
View File
@@ -1,6 +1,6 @@
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include "ConnectionManager.hpp"
@@ -8,24 +8,29 @@ namespace Remote {
namespace Server {
ConnectionManager::ConnectionManager()
{
}
void
ConnectionManager::start(connection::pointer c)
ConnectionManager::start(Connection::pointer c)
{
_connections.insert(c);
c->start();
}
void
ConnectionManager::stop(connection::pointer c)
ConnectionManager::stop(Connection::pointer c)
{
_connections.erase(c);
c->stop();
}
void
ConnectionManager::stop_all()
ConnectionManager::stopAll()
{
BOOST_FOREACH(connection::pointer c, _connections)
BOOST_FOREACH(Connection::pointer c, _connections)
{
c->stop();
}
+11 -8
View File
@@ -3,9 +3,7 @@
#include <set>
#include <boost/noncopyable.hpp>
#include "connection.hpp"
#include "Connection.hpp"
namespace Remote {
@@ -14,21 +12,26 @@ namespace Server {
/// Manages open connections so that they may be cleanly stopped when the server
/// needs to shut down.
class ConnectionManager : boost::noncopyable
class ConnectionManager
{
public:
ConnectionManager(const ConnectionManager&) = delete;
ConnectionManager& operator=(const ConnectionManager&) = delete;
ConnectionManager();
/// Add the specified connection to the manager and start it.
void start(connection::pointer c);
void start(Connection::pointer c);
/// Stop the specified connection.
void stop(connection::pointer c);
void stop(Connection::pointer c);
/// Stop all connections.
void stop_all();
void stopAll();
private:
/// The managed connections.
std::set<connection::pointer> _connections;
std::set<Connection::pointer> _connections;
};
} // namespace Server
+3 -1
View File
@@ -1,14 +1,16 @@
#ifndef REMOTE_REQUEST_HANDLER
#define REMOTE_REQUEST_HANDLER
// #include "remote/messages/
class RequestHandler
{
public:
private:
};
+10 -11
View File
@@ -9,18 +9,17 @@ namespace Remote {
namespace Server {
server::server(boost::asio::io_service& ioService, const endpoint_type& endpoint)
: _ioService(ioService),
Server::Server(const endpoint_type& endpoint)
:
_acceptor(_ioService),
_connectionManager(),
_socket(io_service_),
_requestHandler(doc_root)
_socket(_ioService)
{
// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
boost::asio::ip::tcp::resolver resolver(_ioService);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(endpoint);
boost::asio::ip::tcp::endpoint resolvedEndpoint = *resolver.resolve(endpoint);
_acceptor.open(endpoint.protocol());
_acceptor.open(resolvedEndpoint.protocol());
_acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
_acceptor.bind(endpoint);
_acceptor.listen();
@@ -33,11 +32,11 @@ Server::run()
// While the server is running, there is always at least one
// asynchronous operation outstanding: the asynchronous accept call waiting
// for new incoming connections.
async_accept();
asyncAccept();
}
void
Server::async_accept()
Server::asyncAccept()
{
_acceptor.async_accept(_socket, boost::bind(&Server::handleAccept, this, boost::asio::placeholders::error));
}
@@ -54,9 +53,9 @@ Server::handleAccept(boost::system::error_code ec)
if (!ec)
{
_connectionManager.start(std::make_shared<connection>(std::move(_socket), _connectionManager, _requestHandler));
_connectionManager.start(std::make_shared<Connection>(std::move(_socket), _connectionManager, _requestHandler));
async_accept();
asyncAccept();
}
else
std::cerr << "handleAccept: " << ec.message() << std::endl;
@@ -69,7 +68,7 @@ Server::stop()
// The server is stopped by cancelling all outstanding asynchronous
// operations.
_acceptor.close();
_connectionManager.stop_all();
_connectionManager.stopAll();
}
} // namespace Server
+7 -7
View File
@@ -3,8 +3,6 @@
#include <boost/asio.hpp>
#include <string>
#include "Connection.hpp"
#include "ConnectionManager.hpp"
@@ -18,11 +16,13 @@ class Server
{
public:
typedef std::string endpoint_type;
Server(const Server&) = delete;
Server& operator=(const Server&) = delete;
typedef boost::asio::ip::tcp::endpoint endpoint_type;
// Serve up data from the given database
Server(boost::asio::io_service& ioService,
const endpoint_type& endpoint);
Server(const endpoint_type& endpoint);
// Run the server's io_service loop.
void run();
@@ -34,13 +34,13 @@ class Server
void asyncAccept();
void handleAccept(boost::system::error_code ec);
boost::asio::io_service& _ioService;
boost::asio::io_service _ioService;
/// Acceptor used to listen for incoming connections.
boost::asio::ip::tcp::acceptor _acceptor;
/// The connection manager which owns all live connections.
connection_manager _connectionManager;
ConnectionManager _connectionManager;
/// The next socket to be accepted.
boost::asio::ip::tcp::socket _socket;