Commit d194f3b2 authored by octal's avatar octal

Reorganized source files

parent 21ff3fdd
......@@ -12,6 +12,10 @@ else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include)
file(GLOB HEADER_FILES "include/*.h")
install(FILES ${HEADER_FILES} DESTINATION include/rest)
add_subdirectory (src)
include_directories (src)
......
/*
Mathieu Stefani, 22 janvier 2016
An Http endpoint
*/
#pragma once
#include "listener.h"
#include "net.h"
#include "http.h"
namespace Net {
namespace Http {
class Endpoint {
public:
struct Options {
friend class Endpoint;
Options& threads(int val);
Options& flags(Flags<Tcp::Options> flags);
Options& backlog(int val);
private:
int threads_;
Flags<Tcp::Options> flags_;
int backlog_;
Options();
};
Endpoint();
Endpoint(const Net::Address& addr);
template<typename... Args>
void initArgs(Args&& ...args) {
listener.init(std::forward<Args>(args)...);
}
void init(const Options& options);
void bind();
void bind(const Address& addr);
void setHandler(const std::shared_ptr<Handler>& handler);
void serve();
void serveThreaded();
void shutdown();
bool isBound() const {
return listener.isBound();
}
Async::Promise<Tcp::Listener::Load> requestLoad(const Tcp::Listener::Load& old);
static Options options();
private:
template<typename Method>
void serveImpl(Method method)
{
#define CALL_MEMBER_FN(obj, pmf) ((obj).*(pmf))
if (!handler_)
throw std::runtime_error("Must call setHandler() prior to serve()");
listener.setHandler(handler_);
if (listener.bind()) {
const auto& addr = listener.address();
std::cout << "Now listening on " << "http://" + addr.host() << ":"
<< addr.port() << std::endl;
CALL_MEMBER_FN(listener, method)();
}
#undef CALL_MEMBER_FN
}
std::shared_ptr<Handler> handler_;
Net::Tcp::Listener listener;
};
} // namespace Http
} // namespace Net
......@@ -10,7 +10,6 @@
#include <stdexcept>
#include <array>
#include <sstream>
#include "listener.h"
#include "net.h"
#include "http_headers.h"
#include "http_defs.h"
......@@ -18,6 +17,8 @@
#include "stream.h"
#include "mime.h"
#include "async.h"
#include "peer.h"
#include "io.h"
namespace Net {
......@@ -128,7 +129,6 @@ private:
};
class Handler;
class Timeout;
class Response;
class Timeout {
......@@ -505,7 +505,7 @@ namespace Private {
ssize_t contentLength;
};
}
} // namespace Private
class Handler : public Net::Tcp::Handler {
public:
......@@ -522,71 +522,6 @@ private:
Private::Parser& getParser(const std::shared_ptr<Tcp::Peer>& peer) const;
};
class Endpoint {
public:
struct Options {
friend class Endpoint;
Options& threads(int val);
Options& flags(Flags<Tcp::Options> flags);
Options& backlog(int val);
private:
int threads_;
Flags<Tcp::Options> flags_;
int backlog_;
Options();
};
Endpoint();
Endpoint(const Net::Address& addr);
template<typename... Args>
void initArgs(Args&& ...args) {
listener.init(std::forward<Args>(args)...);
}
void init(const Options& options);
void bind();
void bind(const Address& addr);
void setHandler(const std::shared_ptr<Handler>& handler);
void serve();
void serveThreaded();
void shutdown();
bool isBound() const {
return listener.isBound();
}
Async::Promise<Tcp::Listener::Load> requestLoad(const Tcp::Listener::Load& old);
static Options options();
private:
template<typename Method>
void serveImpl(Method method)
{
#define CALL_MEMBER_FN(obj, pmf) ((obj).*(pmf))
if (!handler_)
throw std::runtime_error("Must call setHandler() prior to serve()");
listener.setHandler(handler_);
if (listener.bind()) {
const auto& addr = listener.address();
std::cout << "Now listening on " << "http://" + addr.host() << ":"
<< addr.port() << std::endl;
CALL_MEMBER_FN(listener, method)();
}
#undef CALL_MEMBER_FN
}
std::shared_ptr<Handler> handler_;
Net::Tcp::Listener listener;
};
} // namespace Http
......
#include "net.h"
#include "peer.h"
#include "http.h"
#include "http_headers.h"
#include "cookie.h"
#include "router.h"
#include "endpoint.h"
#include <iostream>
#include <cstring>
#include <algorithm>
......
file (GLOB COMMON_SOURCE_FILES "common/*.cc")
file (GLOB SERVER_SOURCE_FILES "server/*.cc")
file (GLOB CLIENT_SOURCE_FILES "client/*.cc")
set(SOURCE_FILES
tcp.cc
io.cc
listener.cc
net.cc
peer.cc
os.cc
peer.cc
http.cc
http_header.cc
http_headers.cc
http_defs.cc
mime.cc
cookie.cc
stream.cc
router.cc
${COMMON_SOURCE_FILES}
${SERVER_SOURCE_FILES}
${CLIENT_SOURCE_FILES}
)
add_library(net ${SOURCE_FILES})
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(net pthread)
file(GLOB HEADER_FILES "*.h")
install(FILES ${HEADER_FILES} DESTINATION include/rest)
install(TARGETS net DESTINATION lib)
......@@ -37,8 +37,6 @@ writeHeader(Stream& stream, Args&& ...args) {
return stream;
}
static constexpr const char* ParserData = "__Parser";
namespace {
bool writeStatusLine(Code code, DynamicStreamBuf& buf) {
#define OUT(...) \
......@@ -101,6 +99,8 @@ namespace {
}
static constexpr const char* ParserData = "__Parser";
namespace Private {
void
......@@ -661,78 +661,12 @@ Timeout::onTimeout(uint64_t numWakeup) {
handler->onTimeout(request, std::move(response));
}
Private::Parser&
Handler::getParser(const std::shared_ptr<Tcp::Peer>& peer) const {
return *peer->getData<Private::Parser>(ParserData);
}
Endpoint::Options::Options()
: threads_(1)
{ }
Endpoint::Options&
Endpoint::Options::threads(int val) {
threads_ = val;
return *this;
}
Endpoint::Options&
Endpoint::Options::flags(Flags<Tcp::Options> flags) {
flags_ = flags;
return *this;
}
Endpoint::Options&
Endpoint::Options::backlog(int val) {
backlog_ = val;
return *this;
}
Endpoint::Endpoint()
{ }
Endpoint::Endpoint(const Net::Address& addr)
: listener(addr)
{ }
void
Endpoint::init(const Endpoint::Options& options) {
listener.init(options.threads_, options.flags_);
}
void
Endpoint::setHandler(const std::shared_ptr<Handler>& handler) {
handler_ = handler;
}
void
Endpoint::serve()
{
serveImpl(&Tcp::Listener::run);
}
void
Endpoint::serveThreaded()
{
serveImpl(&Tcp::Listener::runThreaded);
}
void
Endpoint::shutdown()
{
listener.shutdown();
}
Async::Promise<Tcp::Listener::Load>
Endpoint::requestLoad(const Tcp::Listener::Load& old) {
return listener.requestLoad(old);
}
Endpoint::Options
Endpoint::options() {
return Options();
}
} // namespace Http
......
/* endpoint.cc
Mathieu Stefani, 22 janvier 2016
Implementation of the http endpoint
*/
#include "endpoint.h"
#include "tcp.h"
#include "peer.h"
namespace Net {
namespace Http {
Endpoint::Options::Options()
: threads_(1)
{ }
Endpoint::Options&
Endpoint::Options::threads(int val) {
threads_ = val;
return *this;
}
Endpoint::Options&
Endpoint::Options::flags(Flags<Tcp::Options> flags) {
flags_ = flags;
return *this;
}
Endpoint::Options&
Endpoint::Options::backlog(int val) {
backlog_ = val;
return *this;
}
Endpoint::Endpoint()
{ }
Endpoint::Endpoint(const Net::Address& addr)
: listener(addr)
{ }
void
Endpoint::init(const Endpoint::Options& options) {
listener.init(options.threads_, options.flags_);
}
void
Endpoint::setHandler(const std::shared_ptr<Handler>& handler) {
handler_ = handler;
}
void
Endpoint::serve()
{
serveImpl(&Tcp::Listener::run);
}
void
Endpoint::serveThreaded()
{
serveImpl(&Tcp::Listener::runThreaded);
}
void
Endpoint::shutdown()
{
listener.shutdown();
}
Async::Promise<Tcp::Listener::Load>
Endpoint::requestLoad(const Tcp::Listener::Load& old) {
return listener.requestLoad(old);
}
Endpoint::Options
Endpoint::options() {
return Options();
}
} // namespace Http
} // namespace Net
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment