67 static unsigned long ipToLong(
const std::string& ip);
94 void log(
const std::string& message,
const std::string& color =
"");
112 const std::string& logFileName);
124 void addBlockedIP(
const std::string& startIP,
const std::string& stopIP);
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.
std::vector< WebServer * > servers
All active server instances.
double generateRequestProbability
Probability of generating a new request each cycle.
int serversRemoved
Number of servers removed during simulation.
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.
std::ofstream logFile
Output stream for the log file.
std::queue< WebServer * > availableQueue
Queue of idle servers ready for assignment.
int queueMax
Upper queue threshold per server for scaling up.
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)
int maxTime
Total number of clock cycles to simulate.
~LoadBalancer()
Destructor. Frees all server instances and closes the log file.
int queueMin
Lower queue threshold per server for scaling down.
int cooldownRemaining
Clock cycles remaining before next scaling action.
int serversAdded
Number of servers added during simulation.
int totalProcessedRequests
Total requests successfully processed.
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.
int maxRequestTime
Maximum processing time for generated requests.
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.
int totalRejectedRequests
Total requests rejected by the firewall.
int minRequestTime
Minimum processing time for generated requests.
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.
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.
void checkScaling()
Checks queue thresholds and adds or removes servers as needed.