Load Balancer Simulator
CSCE 412 - Load Balancer Simulation Project
LoadBalancer.h
Go to the documentation of this file.
1 
6 #ifndef LOADBALANCER_H
7 #define LOADBALANCER_H
8 
9 #include "Request.h"
10 #include "WebServer.h"
11 
12 #include <queue>
13 #include <vector>
14 #include <string>
15 #include <cstdlib>
16 #include <fstream>
17 #include <utility>
18 
26 class LoadBalancer {
27  private:
28  std::queue<Request> requestQueue;
29  std::queue<WebServer*> availableQueue;
30  std::vector<WebServer*> servers;
31  std::vector<std::pair<std::string, std::string>> firewallRange;
32 
33  int currTime;
34  int maxTime;
37 
38  // Config values
39  int queueMin;
40  int queueMax;
45 
46  // End-of-simulation statistics
52 
53  std::ofstream logFile;
54 
60  bool isBlockedIP(const std::string& ip) const;
61 
67  static unsigned long ipToLong(const std::string& ip);
68 
72  void checkScaling();
73 
77  void generateNewRequest();
78 
82  void assignRequest();
83 
87  void tickAllServers();
88 
94  void log(const std::string& message, const std::string& color = "");
95 
96  public:
109  LoadBalancer(int numServers, int maxTime, int queueMin, int queueMax,
110  int scalingCooldown, double newRequestProb,
112  const std::string& logFileName);
113 
117  ~LoadBalancer();
118 
124  void addBlockedIP(const std::string& startIP, const std::string& stopIP);
125 
129  void initializeQueue();
130 
134  void run();
135 
139  void printSummary();
140 
145  int getQueueSize() const;
146 
151  int getServerCount() const;
152 };
153 
154 #endif
Defines the Request struct representing a network request in the load balancer simulation.
Defines the WebServer class that processes requests assigned by the LoadBalancer.
Manages a pool of web servers and a queue of incoming requests.
Definition: LoadBalancer.h:26
std::vector< WebServer * > servers
All active server instances.
Definition: LoadBalancer.h:30
double generateRequestProbability
Probability of generating a new request each cycle.
Definition: LoadBalancer.h:42
int serversRemoved
Number of servers removed during simulation.
Definition: LoadBalancer.h:50
static unsigned long ipToLong(const std::string &ip)
Converts a dotted-decimal IP address string to an unsigned long.
int nextServerId
ID to assign to the next created server.
Definition: LoadBalancer.h:36
std::ofstream logFile
Output stream for the log file.
Definition: LoadBalancer.h:53
std::queue< WebServer * > availableQueue
Queue of idle servers ready for assignment.
Definition: LoadBalancer.h:29
int queueMax
Upper queue threshold per server for scaling up.
Definition: LoadBalancer.h:40
void run()
Runs the load balancer simulation for the configured number of clock cycles.
std::vector< std::pair< std::string, std::string > > firewallRange
Blocked IP ranges (start, end)
Definition: LoadBalancer.h:31
int maxTime
Total number of clock cycles to simulate.
Definition: LoadBalancer.h:34
~LoadBalancer()
Destructor. Frees all server instances and closes the log file.
int queueMin
Lower queue threshold per server for scaling down.
Definition: LoadBalancer.h:39
int cooldownRemaining
Clock cycles remaining before next scaling action.
Definition: LoadBalancer.h:35
int serversAdded
Number of servers added during simulation.
Definition: LoadBalancer.h:49
int totalProcessedRequests
Total requests successfully processed.
Definition: LoadBalancer.h:47
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.
void addBlockedIP(const std::string &startIP, const std::string &stopIP)
Adds a blocked IP address range to the firewall.
int getServerCount() const
Returns the current number of active servers.
int startingQueueSize
Queue size after initial population.
Definition: LoadBalancer.h:51
int maxRequestTime
Maximum processing time for generated requests.
Definition: LoadBalancer.h:44
void initializeQueue()
Populates the request queue with an initial batch of requests (servers * 100).
bool isBlockedIP(const std::string &ip) const
Checks if an IP address falls within any blocked firewall range.
int currTime
Current simulation clock cycle.
Definition: LoadBalancer.h:33
int totalRejectedRequests
Total requests rejected by the firewall.
Definition: LoadBalancer.h:48
int minRequestTime
Minimum processing time for generated requests.
Definition: LoadBalancer.h:43
void log(const std::string &message, const std::string &color="")
Logs a message to the log file and prints it to the console.
void tickAllServers()
Advances all servers by one clock cycle and marks finished servers as available.
int getQueueSize() const
Returns the current number of pending requests in the queue.
int scalingCooldown
Cooldown period (cycles) between scaling actions.
Definition: LoadBalancer.h:41
void printSummary()
Prints a summary of simulation statistics to the log and console.
void generateNewRequest()
Randomly generates a new request and adds it to the queue (based on probability).
void assignRequest()
Assigns pending requests from the queue to available servers.
std::queue< Request > requestQueue
FIFO queue of pending requests.
Definition: LoadBalancer.h:28
void checkScaling()
Checks queue thresholds and adds or removes servers as needed.