Webserv
Loading...
Searching...
No Matches
core::Server Class Reference

High-level server orchestrator that manages the complete server lifecycle. More...

#include <Server.hpp>

Public Member Functions

 Server (config::ServerConfig const &)
 ~Server ()
void start ()
void stop ()
bool getisRunning () const
void requestShutDown ()

Static Public Member Functions

static int getNullFd ()
 Gets the server-wide file descriptor for /dev/null.

Private Member Functions

void setupAcceptors ()
void cleanup ()
void gracefulShutdown ()
 Server (const Server &)
Serveroperator= (const Server &)

Private Attributes

bool isRunning_
 Tracks main loop state (true between start() and gracefulShutdown()).
volatile sig_atomic_t shutdownRequested_
 Graceful shutdown flag (must be volatile sig_atomic_t for signal handler).
std::vector< network::Acceptor * > acceptors_
 Collection of active acceptor handlers (listening sockets).
config::ServerConfig const & config_
 Const reference to the parsed server configuration.
network::EventDispatcherdispatcher_
 Reference to the central EventDispatcher (Reactor) singleton.
http::MimeTypes mimeTypes_
 MIME types database instance (passed to the router).
http::Router router_
 HTTP request router for dispatching requests to handlers.
int nullFd_
 Server-wide file descriptor for /dev/null (opened at startup).

Detailed Description

High-level server orchestrator that manages the complete server lifecycle.

The Server class acts as a Facade for the underlying network subsystem. It hides the complexity of the Reactor pattern (e.g., EventDispatcher, Acceptors) behind a simple start() and stop() interface.

This class is the central point of control and is responsible for:

  • Initializing all core components (EventDispatcher, Router, MimeTypes).
  • Setting up listening sockets (Acceptors) based on the ServerConfig.
  • Installing OS signal handlers (SIGTERM, SIGINT) to initiate a gracefulShutdown().
  • Running the main event loop by calling EventDispatcher::handleEvents().
  • Managing global, server-wide resources (like the /dev/null FD).

It is implemented as a Singleton (using instance_) to allow the static signalHandler to access the active Server instance and request a shutdown.

Constructor & Destructor Documentation

◆ Server() [1/2]

core::Server::Server ( config::ServerConfig const & config)
explicit

◆ ~Server()

core::Server::~Server ( )

◆ Server() [2/2]

core::Server::Server ( const Server & )
private

Member Function Documentation

◆ cleanup()

void core::Server::cleanup ( )
private

◆ getisRunning()

bool core::Server::getisRunning ( ) const

◆ getNullFd()

int core::Server::getNullFd ( )
static

Gets the server-wide file descriptor for /dev/null.

Returns
A read-only file descriptor for /dev/null.
Note
This is used by CGI to provide a valid, empty stdin for requests that do not have a body.

◆ gracefulShutdown()

void core::Server::gracefulShutdown ( )
private

◆ operator=()

Server & core::Server::operator= ( const Server & )
private

◆ requestShutDown()

void core::Server::requestShutDown ( )

◆ setupAcceptors()

void core::Server::setupAcceptors ( )
private

◆ start()

void core::Server::start ( )

◆ stop()

void core::Server::stop ( )

Member Data Documentation

◆ acceptors_

std::vector<network::Acceptor *> core::Server::acceptors_
private

Collection of active acceptor handlers (listening sockets).

◆ config_

config::ServerConfig const& core::Server::config_
private

Const reference to the parsed server configuration.

◆ dispatcher_

network::EventDispatcher& core::Server::dispatcher_
private

Reference to the central EventDispatcher (Reactor) singleton.

◆ isRunning_

bool core::Server::isRunning_
private

Tracks main loop state (true between start() and gracefulShutdown()).

◆ mimeTypes_

http::MimeTypes core::Server::mimeTypes_
private

MIME types database instance (passed to the router).

◆ nullFd_

int core::Server::nullFd_
private

Server-wide file descriptor for /dev/null (opened at startup).

◆ router_

http::Router core::Server::router_
private

HTTP request router for dispatching requests to handlers.

◆ shutdownRequested_

volatile sig_atomic_t core::Server::shutdownRequested_
private

Graceful shutdown flag (must be volatile sig_atomic_t for signal handler).


The documentation for this class was generated from the following files: