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 ()
void handleSignal (int sig)
bool getisRunning () const

Static Public Member Functions

static void signalHandler (int sig)

Private Member Functions

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

Private Attributes

volatile sig_atomic_t shutdownRequested_
bool isRunning_
std::vector< network::Acceptor * > acceptors_
config::ServerConfig const & config_
network::InitiationDispatcherdispatcher_
http::MimeTypes mimeTypes_
http::Router router_

Static Private Attributes

static Serverinstance_ = NULL

Detailed Description

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

The Server class coordinates multiple acceptors, handles graceful shutdown, and provides a unified interface for starting/stopping the event-driven TCP/IP server. Acts as the application's main entry point into the Reactor pattern infrastructure.

Basically, the Server class serves as application controller above the Reactor the design pattern components and use them to build a complete server application. The Server class is like a bridge between "I want to run a server" and "Here is how the Reactor pattern works" The design pattern is Facade Pattern where it hides the complexity of "create dispatcher, create acceptors, register handlers, start event loop, handle signals, cleanup" behind simple start() and stop() methods

void start() will set up acceptors and start event loop void stop() will gracefully shutdown the server instance in OS void handleSignal(int sig) will handle OS signals because when you run a server in a OS, \ you run the server instance on the background. For example,

$ ./server &
[1] 12345
Starting production server with signal handling ...
Server started on ports 2000 to 2019
$ kill 12345
Received signal: SIGTERM
Performing graceful shutdown...
Stopping acceptors...
Waiting for existing connections to finish...
Cleaning up resources...
Server shutdown complete

void getisRunning() is the getter function to check server status

static void signalHandler(int sig) static signal handler required by signal api

bool shutdownRequested_ is a flag

static Server* instance_; // Static pointer for signal handler access void setupAcceptors(); // Create listening sockets for multiple ports void setupSignalHandlers(); // Install signal handlers void cleanup(); // Clean up resources void gracefulShutdown(); // Perform graceful shutdown

Server(const Server&); // Prevent copying Server& operator=(const Server&); // Prevent copying

Constructor & Destructor Documentation

◆ Server() [1/2]

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

◆ ~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

◆ gracefulShutdown()

void core::Server::gracefulShutdown ( )
private

◆ handleSignal()

void core::Server::handleSignal ( int sig)

◆ operator=()

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

◆ setupAcceptors()

void core::Server::setupAcceptors ( )
private

◆ setupSignalHandlers()

void core::Server::setupSignalHandlers ( )
private

◆ signalHandler()

void core::Server::signalHandler ( int sig)
static

◆ start()

void core::Server::start ( )

◆ stop()

void core::Server::stop ( )

Member Data Documentation

◆ acceptors_

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

◆ config_

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

◆ dispatcher_

network::InitiationDispatcher& core::Server::dispatcher_
private

◆ instance_

Server * core::Server::instance_ = NULL
staticprivate

◆ isRunning_

bool core::Server::isRunning_
private

◆ mimeTypes_

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

◆ router_

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

◆ shutdownRequested_

volatile sig_atomic_t core::Server::shutdownRequested_
private

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