Load Balancer Simulation
A simulation of a load balancer with dynamic server scaling in C++
Loading...
Searching...
No Matches
Config.cpp
Go to the documentation of this file.
1
5
6#include "Config.h"
7#include <fstream>
8#include <sstream>
9#include <algorithm>
10#include <cctype>
11
16static void trim(std::string& s) {
17 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char c) {
18 return !std::isspace(c);
19 }));
20 s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char c) {
21 return !std::isspace(c);
22 }).base(), s.end());
23}
24
32bool LoadBalancerConfig::loadFromFile(const std::string& path) {
33 std::ifstream file(path);
34 if (!file.is_open()) return false;
35
36 std::string line;
37 while (std::getline(file, line)) {
38 trim(line);
39 if (line.empty() || line[0] == '#') continue;
40
41 size_t eq = line.find('=');
42 if (eq == std::string::npos) continue;
43
44 std::string key = line.substr(0, eq);
45 std::string value = line.substr(eq + 1);
46 trim(key);
47 trim(value);
48
49 if (key == "request_probability") requestProbability = std::stod(value);
50 else if (key == "scale_cooldown") scaleCooldown = std::stoi(value);
51 else if (key == "min_threshold") minThreshold = std::stoi(value);
52 else if (key == "max_threshold") maxThreshold = std::stoi(value);
53 else if (key == "min_processing_time") minProcessingTime = std::stoi(value);
54 else if (key == "max_processing_time") maxProcessingTime = std::stoi(value);
55 else if (key == "initial_queue_multiplier") initialQueueMultiplier = std::stoi(value);
56 else if (key == "colors_enabled") colorsEnabled = (value == "1" || value == "true" || value == "yes");
57 else if (key == "status_interval") statusInterval = std::stoi(value);
58 else if (key == "blocked_octets") {
59 blockedOctets.clear();
60 // Strip optional braces { ... }
61 std::string parseVal = value;
62 if (parseVal.size() >= 2 && parseVal.front() == '{' && parseVal.back() == '}') {
63 parseVal = parseVal.substr(1, parseVal.size() - 2);
64 }
65 std::istringstream iss(parseVal);
66 std::string octetStr;
67 while (std::getline(iss, octetStr, ',')) {
68 trim(octetStr);
69 if (!octetStr.empty()) {
70 blockedOctets.push_back(std::stoi(octetStr));
71 }
72 }
73 }
74 }
75 return true;
76}
static void trim(std::string &s)
Trim leading and trailing whitespace from a string.
Definition Config.cpp:16
Defines the LoadBalancerConfig structure for simulation parameters.
int maxProcessingTime
Definition Config.h:22
double requestProbability
Definition Config.h:17
int minProcessingTime
Definition Config.h:21
std::vector< int > blockedOctets
Definition Config.h:26
bool loadFromFile(const std::string &path)
Load configuration from a key=value file.
Definition Config.cpp:32
int initialQueueMultiplier
Definition Config.h:23