11 const std::string
RED =
"\033[31m";
12 const std::string
GREEN =
"\033[32m";
13 const std::string
YELLOW =
"\033[33m";
14 const std::string
BLUE =
"\033[34m";
16 const std::string
CYAN =
"\033[36m";
17 const std::string
RESET =
"\033[0m";
20 int scalingCooldown,
double newRequestProb,
21 int minRequestTime,
int maxRequestTime,
22 const std::string& logFileName) {
43 for(
int i = 0; i < numServers; i++) {
49 log(
"LoadBalancer created with " + std::to_string(numServers) +
" servers",
CYAN);
67 log(
"Blocked IP range: " + startIP +
" - " + stopIP,
RED);
71 unsigned long result = 0;
72 unsigned long octet = 0;
75 for (
int i = 0; i < ip.length(); i++) {
77 result |= (octet << shift);
81 octet = octet * 10 + (ip[i] -
'0');
84 result |= (octet << shift);
95 if (ipNum >= start && ipNum <= end) {
106 int count =
servers.size() * 100;
108 for (
int i = 0; i < count; i++) {
113 log(
"Request #" + std::to_string(i) +
": Added (" + req.
ipIn +
")",
CYAN);
116 log(
"Request #" + std::to_string(i) +
": REJECTED because of bad IP (" + req.
ipIn +
")",
RED);
128 double roll = (double)rand() / RAND_MAX;
135 log(
"[Cycle " + std::to_string(
currTime) +
"] BLOCKED request from " + req.
ipIn,
RED);
144 for (
int i = 0; i <
servers.size(); i++) {
148 log(
"[Cycle " + std::to_string(
currTime) +
"] Server " +
149 std::to_string(
servers[i]->getId()) +
" finished a request",
GREEN);
165 log(
"[Cycle " + std::to_string(
currTime) +
"] Assigned request to Server " +
166 std::to_string(server->
getId()) +
" (time: " + std::to_string(req.
time) +
180 int numServers =
servers.size();
183 if (queueSize >
queueMax * numServers) {
190 log(
"[Cycle " + std::to_string(
currTime) +
"] SCALED UP: added Server " +
191 std::to_string(server->
getId()) +
" (queue: " + std::to_string(queueSize) +
192 ", servers: " + std::to_string(
servers.size()) +
")",
YELLOW);
195 else if (queueSize < queueMin * numServers && numServers > 1) {
206 for (
int i = 0; i <
servers.size(); i++) {
213 int removedId = server->
getId();
218 log(
"[Cycle " + std::to_string(
currTime) +
"] SCALED DOWN: removed Server " +
219 std::to_string(removedId) +
" (queue: " + std::to_string(queueSize) +
227 logFile << message << std::endl;
231 std::cout << message << std::endl;
233 std::cout << color << message <<
RESET << std::endl;
239 log(
"=== Simulation Starting ===",
CYAN);
248 log(
"=== Simulation Running ===",
CYAN);
260 log(
"=== Simulation Complete ===",
CYAN);
266 int busyCount =
servers.size() - idleCount;
269 log(
"=== Summary & End of Simulation STATS ===",
CYAN);
277 log(
"Final server count: " + std::to_string(
servers.size()));
278 log(
"Busy servers: " + std::to_string(busyCount));
279 log(
"Idle servers: " + std::to_string(idleCount));
280 log(
"=================================================");
const std::string MAGENTA
Defines the LoadBalancer class that manages servers and the request queue.
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.
Simulates a web server that receives and processes requests from the load balancer.
void assignRequest(Request *req)
Assigns a new request to this server for processing.
int getId()
Returns the server's unique ID.
Represents a single network request with source/destination IPs, processing time, and job type.
int time
Processing time in clock cycles.
static Request generateRandom(int minTime, int maxTime)
Generates a random Request with random IPs and a random processing time.
std::string ipIn
Source IP address of the request.
char jobType
Job type: 'P' for processing, 'S' for streaming.