Load Balancer Simulation
A simulation of a load balancer with dynamic server scaling in C++
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
8
9#include "LoadBalancer.h"
10#include "Config.h"
11#include "Logger.h"
12#include <iostream>
13#include <limits>
14#include <sstream>
15
20int main() {
21 int numServers = 0;
22 int runTime = 0;
23
24 Logger::init("server.log");
25
26 Logger::log("Load Balancer Simulation\n");
27 Logger::log("------------------------\n\n");
28
29 while ( numServers < 1 || numServers > 1000 ) {
30 Logger::log("Enter the number of servers (1-1000): ");
31 if ( !(std::cin >> numServers) || numServers < 1 || numServers > 1000 ) {
32 Logger::log("Invalid input. Please enter a number between 1 and 1000.\n");
33 std::cin.clear();
34 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
35 numServers = 0;
36 }
37 }
38
39 while ( runTime < 1 || runTime > 100000 ) {
40 Logger::log("Enter the runtime (1-100000): ");
41 if ( !(std::cin >> runTime) || runTime < 1 || runTime > 100000 ) {
42 Logger::log("Invalid input. Please enter a number between 1 and 100000.\n");
43 std::cin.clear();
44 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
45 runTime = 0;
46 }
47 }
48
49
50 LoadBalancerConfig config;
51 if (config.loadFromFile("config.txt")) {
52 Logger::log("Loaded configuration from config.txt\n");
53 } else {
54 Logger::log("Using default configuration (config.txt not found)\n");
55 }
56
57 int initialQueueSize = numServers * config.initialQueueMultiplier;
58 std::ostringstream oss;
59 oss << "\nStarting with " << numServers << " servers, "
60 << initialQueueSize << " initial requests, "
61 << runTime << " clock cycles.\n\n";
62 Logger::log(oss.str());
63
64 LoadBalancer balancer(numServers, config);
65 balancer.prefillQueue(initialQueueSize);
66 balancer.run(runTime);
67 balancer.printSummary(numServers);
68
70 return 0;
71}
Defines the LoadBalancerConfig structure for simulation parameters.
Defines the LoadBalancer class for the simulation.
Defines the Logger class for console and file output.
Main simulation engine that manages web servers and request queue.
void run(int totalClockCycles)
Run the simulation for the specified number of clock cycles.
void prefillQueue(int count)
Prefill the request queue with random requests.
void printSummary(int initialServerCount) const
Print simulation summary statistics.
static bool init(const std::string &filename)
Initialize the logger with an output file.
Definition Logger.cpp:13
static void log(const std::string &message)
Write a message to both console and log file.
Definition Logger.cpp:36
static void close()
Close the log file.
Definition Logger.cpp:22
int main()
Main entry point for the load balancer simulation.
Definition main.cpp:20
Configuration parameters for the load balancer simulation.
Definition Config.h:16
bool loadFromFile(const std::string &path)
Load configuration from a key=value file.
Definition Config.cpp:32
int initialQueueMultiplier
Definition Config.h:23