Load Balancer Simulator
CSCE 412 - Load Balancer Simulation Project
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
LoadBalancer Class Reference

Manages a pool of web servers and a queue of incoming requests. More...

#include <LoadBalancer.h>

Public Member Functions

 LoadBalancer (int numServers, int maxTime, int queueMin, int queueMax, int scalingCooldown, double newRequestProb, int minRequestTime, int maxRequestTime, const std::string &logFileName)
 Constructs a LoadBalancer with the given configuration. More...
 
 ~LoadBalancer ()
 Destructor. Frees all server instances and closes the log file. More...
 
void addBlockedIP (const std::string &startIP, const std::string &stopIP)
 Adds a blocked IP address range to the firewall. More...
 
void initializeQueue ()
 Populates the request queue with an initial batch of requests (servers * 100). More...
 
void run ()
 Runs the load balancer simulation for the configured number of clock cycles. More...
 
void printSummary ()
 Prints a summary of simulation statistics to the log and console. More...
 
int getQueueSize () const
 Returns the current number of pending requests in the queue. More...
 
int getServerCount () const
 Returns the current number of active servers. More...
 

Private Member Functions

bool isBlockedIP (const std::string &ip) const
 Checks if an IP address falls within any blocked firewall range. More...
 
void checkScaling ()
 Checks queue thresholds and adds or removes servers as needed. More...
 
void generateNewRequest ()
 Randomly generates a new request and adds it to the queue (based on probability). More...
 
void assignRequest ()
 Assigns pending requests from the queue to available servers. More...
 
void tickAllServers ()
 Advances all servers by one clock cycle and marks finished servers as available. More...
 
void log (const std::string &message, const std::string &color="")
 Logs a message to the log file and prints it to the console. More...
 

Static Private Member Functions

static unsigned long ipToLong (const std::string &ip)
 Converts a dotted-decimal IP address string to an unsigned long. More...
 

Private Attributes

std::queue< RequestrequestQueue
 FIFO queue of pending requests. More...
 
std::queue< WebServer * > availableQueue
 Queue of idle servers ready for assignment. More...
 
std::vector< WebServer * > servers
 All active server instances. More...
 
std::vector< std::pair< std::string, std::string > > firewallRange
 Blocked IP ranges (start, end) More...
 
int currTime
 Current simulation clock cycle. More...
 
int maxTime
 Total number of clock cycles to simulate. More...
 
int cooldownRemaining
 Clock cycles remaining before next scaling action. More...
 
int nextServerId
 ID to assign to the next created server. More...
 
int queueMin
 Lower queue threshold per server for scaling down. More...
 
int queueMax
 Upper queue threshold per server for scaling up. More...
 
int scalingCooldown
 Cooldown period (cycles) between scaling actions. More...
 
double generateRequestProbability
 Probability of generating a new request each cycle. More...
 
int minRequestTime
 Minimum processing time for generated requests. More...
 
int maxRequestTime
 Maximum processing time for generated requests. More...
 
int totalProcessedRequests
 Total requests successfully processed. More...
 
int totalRejectedRequests
 Total requests rejected by the firewall. More...
 
int serversAdded
 Number of servers added during simulation. More...
 
int serversRemoved
 Number of servers removed during simulation. More...
 
int startingQueueSize
 Queue size after initial population. More...
 
std::ofstream logFile
 Output stream for the log file. More...
 

Detailed Description

Manages a pool of web servers and a queue of incoming requests.

The LoadBalancer distributes requests to available servers using a FIFO queue, dynamically scales the server pool based on queue thresholds, and provides IP-based firewall filtering to block requests from specified address ranges.

Definition at line 26 of file LoadBalancer.h.

Constructor & Destructor Documentation

◆ LoadBalancer()

LoadBalancer::LoadBalancer ( int  numServers,
int  maxTime,
int  queueMin,
int  queueMax,
int  scalingCooldown,
double  newRequestProb,
int  minRequestTime,
int  maxRequestTime,
const std::string &  logFileName 
)

Constructs a LoadBalancer with the given configuration.

Parameters
numServersInitial number of web servers to create
maxTimeTotal simulation time in clock cycles
queueMinLower queue threshold per server for scaling down
queueMaxUpper queue threshold per server for scaling up
scalingCooldownCooldown period between scaling actions
newRequestProbProbability of generating a new request each cycle
minRequestTimeMinimum processing time for requests
maxRequestTimeMaximum processing time for requests
logFileNamePath to the output log file

Definition at line 19 of file LoadBalancer.cpp.

◆ ~LoadBalancer()

LoadBalancer::~LoadBalancer ( )

Destructor. Frees all server instances and closes the log file.

Definition at line 53 of file LoadBalancer.cpp.

Member Function Documentation

◆ addBlockedIP()

void LoadBalancer::addBlockedIP ( const std::string &  startIP,
const std::string &  stopIP 
)

Adds a blocked IP address range to the firewall.

Parameters
startIPStart of the blocked IP range
stopIPEnd of the blocked IP range

Definition at line 65 of file LoadBalancer.cpp.

◆ assignRequest()

void LoadBalancer::assignRequest ( )
private

Assigns pending requests from the queue to available servers.

Definition at line 156 of file LoadBalancer.cpp.

◆ checkScaling()

void LoadBalancer::checkScaling ( )
private

Checks queue thresholds and adds or removes servers as needed.

Definition at line 172 of file LoadBalancer.cpp.

◆ generateNewRequest()

void LoadBalancer::generateNewRequest ( )
private

Randomly generates a new request and adds it to the queue (based on probability).

Definition at line 127 of file LoadBalancer.cpp.

◆ getQueueSize()

int LoadBalancer::getQueueSize ( ) const

Returns the current number of pending requests in the queue.

Returns
The request queue size

Definition at line 61 of file LoadBalancer.cpp.

◆ getServerCount()

int LoadBalancer::getServerCount ( ) const

Returns the current number of active servers.

Returns
The server count

Definition at line 62 of file LoadBalancer.cpp.

◆ initializeQueue()

void LoadBalancer::initializeQueue ( )

Populates the request queue with an initial batch of requests (servers * 100).

Definition at line 105 of file LoadBalancer.cpp.

◆ ipToLong()

unsigned long LoadBalancer::ipToLong ( const std::string &  ip)
staticprivate

Converts a dotted-decimal IP address string to an unsigned long.

Parameters
ipThe IP address string (e.g. "192.168.1.1")
Returns
The numeric representation of the IP address

Definition at line 70 of file LoadBalancer.cpp.

◆ isBlockedIP()

bool LoadBalancer::isBlockedIP ( const std::string &  ip) const
private

Checks if an IP address falls within any blocked firewall range.

Parameters
ipThe IP address to check
Returns
true if the IP is blocked, false otherwise

Definition at line 89 of file LoadBalancer.cpp.

◆ log()

void LoadBalancer::log ( const std::string &  message,
const std::string &  color = "" 
)
private

Logs a message to the log file and prints it to the console.

Parameters
messageThe message to log
colorANSI color code for console output (empty string for no color)

Definition at line 225 of file LoadBalancer.cpp.

◆ printSummary()

void LoadBalancer::printSummary ( )

Prints a summary of simulation statistics to the log and console.

Definition at line 264 of file LoadBalancer.cpp.

◆ run()

void LoadBalancer::run ( )

Runs the load balancer simulation for the configured number of clock cycles.

Definition at line 238 of file LoadBalancer.cpp.

◆ tickAllServers()

void LoadBalancer::tickAllServers ( )
private

Advances all servers by one clock cycle and marks finished servers as available.

Definition at line 143 of file LoadBalancer.cpp.

Member Data Documentation

◆ availableQueue

std::queue<WebServer*> LoadBalancer::availableQueue
private

Queue of idle servers ready for assignment.

Definition at line 29 of file LoadBalancer.h.

◆ cooldownRemaining

int LoadBalancer::cooldownRemaining
private

Clock cycles remaining before next scaling action.

Definition at line 35 of file LoadBalancer.h.

◆ currTime

int LoadBalancer::currTime
private

Current simulation clock cycle.

Definition at line 33 of file LoadBalancer.h.

◆ firewallRange

std::vector<std::pair<std::string, std::string> > LoadBalancer::firewallRange
private

Blocked IP ranges (start, end)

Definition at line 31 of file LoadBalancer.h.

◆ generateRequestProbability

double LoadBalancer::generateRequestProbability
private

Probability of generating a new request each cycle.

Definition at line 42 of file LoadBalancer.h.

◆ logFile

std::ofstream LoadBalancer::logFile
private

Output stream for the log file.

Definition at line 53 of file LoadBalancer.h.

◆ maxRequestTime

int LoadBalancer::maxRequestTime
private

Maximum processing time for generated requests.

Definition at line 44 of file LoadBalancer.h.

◆ maxTime

int LoadBalancer::maxTime
private

Total number of clock cycles to simulate.

Definition at line 34 of file LoadBalancer.h.

◆ minRequestTime

int LoadBalancer::minRequestTime
private

Minimum processing time for generated requests.

Definition at line 43 of file LoadBalancer.h.

◆ nextServerId

int LoadBalancer::nextServerId
private

ID to assign to the next created server.

Definition at line 36 of file LoadBalancer.h.

◆ queueMax

int LoadBalancer::queueMax
private

Upper queue threshold per server for scaling up.

Definition at line 40 of file LoadBalancer.h.

◆ queueMin

int LoadBalancer::queueMin
private

Lower queue threshold per server for scaling down.

Definition at line 39 of file LoadBalancer.h.

◆ requestQueue

std::queue<Request> LoadBalancer::requestQueue
private

FIFO queue of pending requests.

Definition at line 28 of file LoadBalancer.h.

◆ scalingCooldown

int LoadBalancer::scalingCooldown
private

Cooldown period (cycles) between scaling actions.

Definition at line 41 of file LoadBalancer.h.

◆ servers

std::vector<WebServer*> LoadBalancer::servers
private

All active server instances.

Definition at line 30 of file LoadBalancer.h.

◆ serversAdded

int LoadBalancer::serversAdded
private

Number of servers added during simulation.

Definition at line 49 of file LoadBalancer.h.

◆ serversRemoved

int LoadBalancer::serversRemoved
private

Number of servers removed during simulation.

Definition at line 50 of file LoadBalancer.h.

◆ startingQueueSize

int LoadBalancer::startingQueueSize
private

Queue size after initial population.

Definition at line 51 of file LoadBalancer.h.

◆ totalProcessedRequests

int LoadBalancer::totalProcessedRequests
private

Total requests successfully processed.

Definition at line 47 of file LoadBalancer.h.

◆ totalRejectedRequests

int LoadBalancer::totalRejectedRequests
private

Total requests rejected by the firewall.

Definition at line 48 of file LoadBalancer.h.


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