Load Balancer - CSCE 412 Project 3 1.0
Loading...
Searching...
No Matches
LoadBalancer Class Reference

Manages a pool of web servers and distributes incoming requests among them. More...

#include <load_balancer.h>

Public Member Functions

 LoadBalancer (int num_servers, int time_to_run, std::ostream &output_stream)
 Constructs a LoadBalancer with a given number of servers and run time.
 
 ~LoadBalancer ()
 Destructor. Cleans up all heap-allocated web servers and logs final state.
 
bool is_finished ()
 Checks if the load balancer has finished running.
 
Request generate_random_request ()
 Generates a random request with random IPs and duration.
 
void add_request (const Request &req)
 Adds a request to the request queue.
 
void handle_requests ()
 Main loop for handling requests and managing servers. Continues until the specified run time is reached.
 

Public Attributes

int num_servers
 Number of active web servers managed by the load balancer.
 
int time_to_run
 Total number of cycles to run the load balancer.
 
int time_elapsed = 0
 Number of cycles elapsed since the load balancer started.
 
int min_request_time = 5
 Minimum duration (in cycles) for a request.
 
int max_request_time = 45
 Maximum duration (in cycles) for a request.
 
std::ostream & output_stream
 Output stream for logging and status messages.
 
std::queue< Requestrequest_queue
 Queue storing all pending requests.
 
std::vector< WebServer * > servers
 Vector holding pointers to all heap-allocated web servers.
 

Detailed Description

Manages a pool of web servers and distributes incoming requests among them.

Constructor & Destructor Documentation

◆ LoadBalancer()

LoadBalancer::LoadBalancer ( int num_servers,
int time_to_run,
std::ostream & output_stream )

Constructs a LoadBalancer with a given number of servers and run time.

Constructs a LoadBalancer with a given number of servers and run time. Initializes the request queue and creates the initial set of servers.

Parameters
num_serversNumber of web servers to start with.
time_to_runNumber of cycles to run the load balancer.
output_streamOutput stream for logging.

Member Function Documentation

◆ add_request()

void LoadBalancer::add_request ( const Request & req)
inline

Adds a request to the request queue.

Parameters
reqThe request to add.

◆ generate_random_request()

Request LoadBalancer::generate_random_request ( )

Generates a random request with random IPs and duration.

Returns
A randomly generated Request object.

◆ handle_requests()

void LoadBalancer::handle_requests ( )

Main loop for handling requests and managing servers. Continues until the specified run time is reached.

Main loop for handling requests and managing servers. Assigns requests to idle servers, scales server pool, and simulates new requests.

◆ is_finished()

bool LoadBalancer::is_finished ( )
inline

Checks if the load balancer has finished running.

Returns
True if the elapsed time is greater than or equal to the run time.

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