LeftHookRoll
An HTTP/1.0 compliant web server, as specified by RFC1945
Loading...
Searching...
No Matches
ServerManager Class Reference

#include <ServerManager.hpp>

Collaboration diagram for ServerManager:

Public Member Functions

 ServerManager ()
 
 ServerManager (std::vector< ServerConf > confs)
 
 ServerManager (const ServerManager &other)
 
ServerManageroperator= (const ServerManager &other)
 
 ~ServerManager ()
 
void addServer (const ServerConf *conf)
 Creates a listening socket for the given ServerConf's IP:Port and registers the mapping.
 
void addListenPort (int port)
 Temporary overload for early development: creates a listening socket on the given port bound to INADDR_ANY, with no ServerConf mapping.
 
void run ()
 Enters the main epoll() event loop. Blocks until g_running becomes false.
 
void addPollFd (int fd, uint32_t events)
 Adds or updates an fd in the epoll list (e.g., CGI output pipe).
 
const ServerConfgetServerConfForFd (int clientFd) const
 Uses getsockname() to find the local address of a client fd, then returns the matching ServerConf.
 

Private Member Functions

int _createListeningSocket (const struct sockaddr_in &addr)
 Creates, binds, listens, and sets O_NONBLOCK on a socket.
 
void _acceptNewConnections (int listenFd)
 Accepts all pending connections on a listening fd. Sets each client fd to O_NONBLOCK and adds it to _pollfds.
 
bool _readAndPrint (int fd)
 Reads from a client fd and prints the raw data.
 
void _handleConnection (Connection *conn, uint32_t events)
 Dispatches epoll events to the correct Connection handler and drives state transitions. Called from run() for every client event.
 
void _runRoundRobin ()
 Runs a budgeted round-robin pass over _processingQueue. Calls process() once per connection, re-enqueues if still PROCESSING, budgeted to BACKLOG/2 per tick, which is arbitrary, tune as needed.
 
void _dropConnection (int fd)
 Closes a client fd and removes it from epoll and _connections.
 
void _enqueueProcessing (Connection *conn)
 Adds a connection to the processing queue if not already queued.
 
void _dequeueProcessing (Connection *conn)
 Removes a connection from the processing set (queue cleanup is lazy).
 
void _finalizeProcessed (Connection *conn)
 Handles state transition for a connection that just left PROCESSING.
 
void _sweepTimeouts ()
 Scans all connections and drops any that have been idle too long.
 
void _registerCgiPipe (Connection *conn)
 Registers a CGI pipe fd in epoll and maps it to its Connection.
 
void _unregisterCgiPipe (int pipeFd)
 Unregisters a CGI pipe fd from epoll and removes the mapping.
 
void _handleCgiPipeEvent (int pipeFd, uint32_t events)
 Handles an epoll event on a CGI pipe fd.
 
void _sweepCgiTimeouts ()
 Sweeps CGI processes for timeout.
 
void _closeAllFds ()
 Closes all fds (listeners + clients) during shutdown.
 

Private Attributes

std::map< struct sockaddr_in, const ServerConf *, SockAddrCompare_interfacePortPairs
 
std::vector< ServerConf * > _serverConfs
 
std::deque< Connection * > _processingQueue
 
std::set< Connection * > _processingSet
 
std::map< int, Connection * > _connections
 
std::map< int, Connection * > _cgiPipeToConn
 
std::map< int, time_t > _cgiStartTimes
 
int _epollFd
 
std::vector< struct epoll_event > _eventBuffer
 
std::map< int, uint32_t > _fdEvents
 
std::set< int > _listenFds
 
std::map< int, const ServerConf * > _listenFdToServerConf
 

Detailed Description

Definition at line 40 of file ServerManager.hpp.

Constructor & Destructor Documentation

◆ ServerManager() [1/3]

ServerManager::ServerManager ( )

Definition at line 17 of file ServerManager.cpp.

◆ ServerManager() [2/3]

ServerManager::ServerManager ( std::vector< ServerConf confs)

Definition at line 25 of file ServerManager.cpp.

◆ ServerManager() [3/3]

ServerManager::ServerManager ( const ServerManager other)

Definition at line 50 of file ServerManager.cpp.

◆ ~ServerManager()

ServerManager::~ServerManager ( )

Definition at line 97 of file ServerManager.cpp.

Member Function Documentation

◆ operator=()

ServerManager & ServerManager::operator= ( const ServerManager other)

Definition at line 76 of file ServerManager.cpp.

◆ addServer()

void ServerManager::addServer ( const ServerConf conf)

Creates a listening socket for the given ServerConf's IP:Port and registers the mapping.

Parameters
confPointer to the configuration block (pointer used to avoid heavy copying).
Exceptions
FatalExceptionif socket setup fails.

Definition at line 107 of file ServerManager.cpp.

◆ addListenPort()

void ServerManager::addListenPort ( int  port)

Temporary overload for early development: creates a listening socket on the given port bound to INADDR_ANY, with no ServerConf mapping.

listen without needing a conf file.

Definition at line 122 of file ServerManager.cpp.

◆ run()

void ServerManager::run ( )

Enters the main epoll() event loop. Blocks until g_running becomes false.

Definition at line 143 of file ServerManager.cpp.

◆ addPollFd()

void ServerManager::addPollFd ( int  fd,
uint32_t  events 
)

Adds or updates an fd in the epoll list (e.g., CGI output pipe).

Parameters
fdThe file descriptor to monitor.
eventsEPOLLIN/EPOLLOUT mask to apply.

Definition at line 247 of file ServerManager.cpp.

◆ getServerConfForFd()

const ServerConf * ServerManager::getServerConfForFd ( int  clientFd) const

Uses getsockname() to find the local address of a client fd, then returns the matching ServerConf.

Parameters
clientFdThe file descriptor returned by accept().
Returns
const ServerConf* Pointer to the matching configuration, or NULL if not found.

Definition at line 268 of file ServerManager.cpp.

◆ _createListeningSocket()

int ServerManager::_createListeningSocket ( const struct sockaddr_in &  addr)
private

Creates, binds, listens, and sets O_NONBLOCK on a socket.

Returns
The listening fd.
Exceptions
FatalExceptionif any socket operation fails, with the error message.

Definition at line 284 of file ServerManager.cpp.

◆ _acceptNewConnections()

void ServerManager::_acceptNewConnections ( int  listenFd)
private

Accepts all pending connections on a listening fd. Sets each client fd to O_NONBLOCK and adds it to _pollfds.

Definition at line 318 of file ServerManager.cpp.

◆ _readAndPrint()

bool ServerManager::_readAndPrint ( int  fd)
private

Reads from a client fd and prints the raw data.

Returns
false if the client disconnected or errored, true otherwise.

Definition at line 356 of file ServerManager.cpp.

◆ _handleConnection()

void ServerManager::_handleConnection ( Connection conn,
uint32_t  events 
)
private

Dispatches epoll events to the correct Connection handler and drives state transitions. Called from run() for every client event.

Definition at line 192 of file ServerManager.cpp.

◆ _runRoundRobin()

void ServerManager::_runRoundRobin ( )
private

Runs a budgeted round-robin pass over _processingQueue. Calls process() once per connection, re-enqueues if still PROCESSING, budgeted to BACKLOG/2 per tick, which is arbitrary, tune as needed.

Warning
tribute to Prof.waleed al-maqableh.

Definition at line 405 of file ServerManager.cpp.

◆ _dropConnection()

void ServerManager::_dropConnection ( int  fd)
private

Closes a client fd and removes it from epoll and _connections.

Definition at line 374 of file ServerManager.cpp.

◆ _enqueueProcessing()

void ServerManager::_enqueueProcessing ( Connection conn)
private

Adds a connection to the processing queue if not already queued.

Definition at line 394 of file ServerManager.cpp.

◆ _dequeueProcessing()

void ServerManager::_dequeueProcessing ( Connection conn)
private

Removes a connection from the processing set (queue cleanup is lazy).

Definition at line 400 of file ServerManager.cpp.

◆ _finalizeProcessed()

void ServerManager::_finalizeProcessed ( Connection conn)
private

Handles state transition for a connection that just left PROCESSING.

Definition at line 449 of file ServerManager.cpp.

◆ _sweepTimeouts()

void ServerManager::_sweepTimeouts ( )
private

Scans all connections and drops any that have been idle too long.

Definition at line 578 of file ServerManager.cpp.

◆ _registerCgiPipe()

void ServerManager::_registerCgiPipe ( Connection conn)
private

Registers a CGI pipe fd in epoll and maps it to its Connection.

Definition at line 471 of file ServerManager.cpp.

◆ _unregisterCgiPipe()

void ServerManager::_unregisterCgiPipe ( int  pipeFd)
private

Unregisters a CGI pipe fd from epoll and removes the mapping.

Definition at line 481 of file ServerManager.cpp.

◆ _handleCgiPipeEvent()

void ServerManager::_handleCgiPipeEvent ( int  pipeFd,
uint32_t  events 
)
private

Handles an epoll event on a CGI pipe fd.

Definition at line 491 of file ServerManager.cpp.

◆ _sweepCgiTimeouts()

void ServerManager::_sweepCgiTimeouts ( )
private

Sweeps CGI processes for timeout.

Definition at line 538 of file ServerManager.cpp.

◆ _closeAllFds()

void ServerManager::_closeAllFds ( )
private

Closes all fds (listeners + clients) during shutdown.

need to rename this. its growing to an all encompassing cleanup function.

Definition at line 606 of file ServerManager.cpp.

Member Data Documentation

◆ _interfacePortPairs

std::map<struct sockaddr_in, const ServerConf*, SockAddrCompare> ServerManager::_interfacePortPairs
private

Definition at line 85 of file ServerManager.hpp.

◆ _serverConfs

std::vector<ServerConf*> ServerManager::_serverConfs
private

Definition at line 86 of file ServerManager.hpp.

◆ _processingQueue

std::deque<Connection*> ServerManager::_processingQueue
private

Definition at line 88 of file ServerManager.hpp.

◆ _processingSet

std::set<Connection*> ServerManager::_processingSet
private

Definition at line 89 of file ServerManager.hpp.

◆ _connections

std::map<int, Connection*> ServerManager::_connections
private

Definition at line 91 of file ServerManager.hpp.

◆ _cgiPipeToConn

std::map<int, Connection*> ServerManager::_cgiPipeToConn
private

Definition at line 93 of file ServerManager.hpp.

◆ _cgiStartTimes

std::map<int, time_t> ServerManager::_cgiStartTimes
private

Definition at line 94 of file ServerManager.hpp.

◆ _epollFd

int ServerManager::_epollFd
private

Definition at line 96 of file ServerManager.hpp.

◆ _eventBuffer

std::vector<struct epoll_event> ServerManager::_eventBuffer
private

Definition at line 97 of file ServerManager.hpp.

◆ _fdEvents

std::map<int, uint32_t> ServerManager::_fdEvents
private

Definition at line 98 of file ServerManager.hpp.

◆ _listenFds

std::set<int> ServerManager::_listenFds
private

Definition at line 99 of file ServerManager.hpp.

◆ _listenFdToServerConf

std::map<int, const ServerConf*> ServerManager::_listenFdToServerConf
private

Definition at line 100 of file ServerManager.hpp.


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