Aggie Student Toolkit — Load Balancer CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
Loading...
Searching...
No Matches
WebServer Class Reference

A simulated web server that processes at most one request at a time. More...

#include <WebServer.h>

Public Member Functions

 WebServer (unsigned int id, unsigned long createdCycle=0)
 Creates an idle server.
unsigned int id () const
bool isIdle () const
bool isRetiring () const
void setRetiring (bool retiring)
 Marks or unmarks the server for retirement.
void assign (const Request &request, unsigned long cycle)
 Starts processing a request.
bool tick ()
 Advances the server by one clock cycle.
const std::optional< Request > & current () const
const Request & lastCompleted () const
unsigned int remainingCycles () const
unsigned long completedCount () const
unsigned long busyCycles () const
unsigned long createdCycle () const

Detailed Description

A simulated web server that processes at most one request at a time.

The LoadBalancer assigns a Request to an idle server. Every clock cycle the load balancer calls tick(), which consumes one cycle of the request's remaining work. When the remaining work reaches zero the server reports the completion and becomes idle again.

A server can be marked as retiring during a scale-down. A retiring server receives no new work; the load balancer removes it as soon as it is idle, so in-progress requests are never dropped.

Definition at line 26 of file WebServer.h.

Constructor & Destructor Documentation

◆ WebServer()

WebServer::WebServer ( unsigned int id,
unsigned long createdCycle = 0 )
explicit

Creates an idle server.

Parameters
idUnique identifier shown in logs.
createdCycleClock cycle at which the server was brought online.

Definition at line 11 of file WebServer.cpp.

Member Function Documentation

◆ id()

unsigned int WebServer::id ( ) const
Returns
Unique server identifier.

Definition at line 14 of file WebServer.cpp.

◆ isIdle()

bool WebServer::isIdle ( ) const
Returns
True when the server has no request in progress.

Definition at line 18 of file WebServer.cpp.

◆ isRetiring()

bool WebServer::isRetiring ( ) const
Returns
True when the server is marked for removal after its current request.

Definition at line 22 of file WebServer.cpp.

◆ setRetiring()

void WebServer::setRetiring ( bool retiring)

Marks or unmarks the server for retirement.

Parameters
retiringTrue to stop accepting new work; false to return it to service.

Definition at line 26 of file WebServer.cpp.

◆ assign()

void WebServer::assign ( const Request & request,
unsigned long cycle )

Starts processing a request.

Work begins on the next tick(), so a request with taskCycles = 1 that is assigned at cycle t completes at cycle t + 1.

Parameters
requestRequest to process; its taskCycles must be at least 1.
cycleClock cycle of the assignment, used for statistics.
Exceptions
std::logic_errorif the server is busy, retiring, or the task length is zero.

Definition at line 30 of file WebServer.cpp.

◆ tick()

bool WebServer::tick ( )

Advances the server by one clock cycle.

Returns
True if the current request finished during this cycle.

Definition at line 44 of file WebServer.cpp.

◆ current()

const std::optional< Request > & WebServer::current ( ) const
Returns
The request in progress, or an empty optional when idle.

Definition at line 59 of file WebServer.cpp.

◆ lastCompleted()

const Request & WebServer::lastCompleted ( ) const
Returns
The most recently completed request (valid after tick() returns true).

Definition at line 63 of file WebServer.cpp.

◆ remainingCycles()

unsigned int WebServer::remainingCycles ( ) const
Returns
Clock cycles of work left on the current request (0 when idle).

Definition at line 67 of file WebServer.cpp.

◆ completedCount()

unsigned long WebServer::completedCount ( ) const
Returns
Number of requests this server has completed.

Definition at line 71 of file WebServer.cpp.

◆ busyCycles()

unsigned long WebServer::busyCycles ( ) const
Returns
Number of clock cycles this server spent processing work.

Definition at line 75 of file WebServer.cpp.

◆ createdCycle()

unsigned long WebServer::createdCycle ( ) const
Returns
Clock cycle at which the server was created.

Definition at line 79 of file WebServer.cpp.


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