|
Load Balancer Simulator
CSCE 412 - Load Balancer Simulation Project
|
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< Request > | requestQueue |
| 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... | |
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.
| 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.
| numServers | Initial number of web servers to create |
| maxTime | Total simulation time in clock cycles |
| queueMin | Lower queue threshold per server for scaling down |
| queueMax | Upper queue threshold per server for scaling up |
| scalingCooldown | Cooldown period between scaling actions |
| newRequestProb | Probability of generating a new request each cycle |
| minRequestTime | Minimum processing time for requests |
| maxRequestTime | Maximum processing time for requests |
| logFileName | Path to the output log file |
Definition at line 19 of file LoadBalancer.cpp.
| LoadBalancer::~LoadBalancer | ( | ) |
Destructor. Frees all server instances and closes the log file.
Definition at line 53 of file LoadBalancer.cpp.
| void LoadBalancer::addBlockedIP | ( | const std::string & | startIP, |
| const std::string & | stopIP | ||
| ) |
Adds a blocked IP address range to the firewall.
| startIP | Start of the blocked IP range |
| stopIP | End of the blocked IP range |
Definition at line 65 of file LoadBalancer.cpp.
|
private |
Assigns pending requests from the queue to available servers.
Definition at line 156 of file LoadBalancer.cpp.
|
private |
Checks queue thresholds and adds or removes servers as needed.
Definition at line 172 of file LoadBalancer.cpp.
|
private |
Randomly generates a new request and adds it to the queue (based on probability).
Definition at line 127 of file LoadBalancer.cpp.
| int LoadBalancer::getQueueSize | ( | ) | const |
Returns the current number of pending requests in the queue.
Definition at line 61 of file LoadBalancer.cpp.
| int LoadBalancer::getServerCount | ( | ) | const |
Returns the current number of active servers.
Definition at line 62 of file LoadBalancer.cpp.
| void LoadBalancer::initializeQueue | ( | ) |
Populates the request queue with an initial batch of requests (servers * 100).
Definition at line 105 of file LoadBalancer.cpp.
|
staticprivate |
Converts a dotted-decimal IP address string to an unsigned long.
| ip | The IP address string (e.g. "192.168.1.1") |
Definition at line 70 of file LoadBalancer.cpp.
|
private |
Checks if an IP address falls within any blocked firewall range.
| ip | The IP address to check |
Definition at line 89 of file LoadBalancer.cpp.
|
private |
Logs a message to the log file and prints it to the console.
| message | The message to log |
| color | ANSI color code for console output (empty string for no color) |
Definition at line 225 of file LoadBalancer.cpp.
| void LoadBalancer::printSummary | ( | ) |
Prints a summary of simulation statistics to the log and console.
Definition at line 264 of file LoadBalancer.cpp.
| void LoadBalancer::run | ( | ) |
Runs the load balancer simulation for the configured number of clock cycles.
Definition at line 238 of file LoadBalancer.cpp.
|
private |
Advances all servers by one clock cycle and marks finished servers as available.
Definition at line 143 of file LoadBalancer.cpp.
|
private |
Queue of idle servers ready for assignment.
Definition at line 29 of file LoadBalancer.h.
|
private |
Clock cycles remaining before next scaling action.
Definition at line 35 of file LoadBalancer.h.
|
private |
Current simulation clock cycle.
Definition at line 33 of file LoadBalancer.h.
|
private |
Blocked IP ranges (start, end)
Definition at line 31 of file LoadBalancer.h.
|
private |
Probability of generating a new request each cycle.
Definition at line 42 of file LoadBalancer.h.
|
private |
Output stream for the log file.
Definition at line 53 of file LoadBalancer.h.
|
private |
Maximum processing time for generated requests.
Definition at line 44 of file LoadBalancer.h.
|
private |
Total number of clock cycles to simulate.
Definition at line 34 of file LoadBalancer.h.
|
private |
Minimum processing time for generated requests.
Definition at line 43 of file LoadBalancer.h.
|
private |
ID to assign to the next created server.
Definition at line 36 of file LoadBalancer.h.
|
private |
Upper queue threshold per server for scaling up.
Definition at line 40 of file LoadBalancer.h.
|
private |
Lower queue threshold per server for scaling down.
Definition at line 39 of file LoadBalancer.h.
|
private |
FIFO queue of pending requests.
Definition at line 28 of file LoadBalancer.h.
|
private |
Cooldown period (cycles) between scaling actions.
Definition at line 41 of file LoadBalancer.h.
|
private |
All active server instances.
Definition at line 30 of file LoadBalancer.h.
|
private |
Number of servers added during simulation.
Definition at line 49 of file LoadBalancer.h.
|
private |
Number of servers removed during simulation.
Definition at line 50 of file LoadBalancer.h.
|
private |
Queue size after initial population.
Definition at line 51 of file LoadBalancer.h.
|
private |
Total requests successfully processed.
Definition at line 47 of file LoadBalancer.h.
|
private |
Total requests rejected by the firewall.
Definition at line 48 of file LoadBalancer.h.