Aggie Student Toolkit — Load Balancer CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
76
77#include <fstream>
78#include <iostream>
79
80#include "Config.h"
81#include "Logger.h"
82#include "Simulation.h"
83
90int main(int argc, char* argv[]) {
91 Config config;
92 std::string error;
93 if (!parseArguments(argc, argv, config, error)) {
94 std::cerr << "Error: " << error << '\n';
95 return 1;
96 }
97 if (config.showHelp) {
98 printUsage(std::cout, argv[0]);
99 return 0;
100 }
101 if (config.interactive) {
102 promptForSettings(config, std::cin, std::cout);
103 }
104 if (!validateConfig(config, error)) {
105 std::cerr << "Error: " << error << '\n';
106 return 1;
107 }
108
109 Logger logger(config.verbose, config.color);
110 std::ofstream logFile;
111 if (!config.logPath.empty()) {
112 // In verbose mode the event stream goes to <log>.events.txt and the summary to <log>.
113 if (config.verbose && !logger.openEventFile(config.logPath + ".events.txt")) {
114 std::cerr << "Error: cannot write " << config.logPath << ".events.txt\n";
115 return 1;
116 }
117 logFile.open(config.logPath);
118 if (!logFile) {
119 std::cerr << "Error: cannot write " << config.logPath << '\n';
120 return 1;
121 }
122 }
123
124 Simulation simulation(config, logger);
125 simulation.run();
126
127 std::cout << '\n';
128 simulation.writeReport(std::cout);
129 if (logFile.is_open()) {
130 simulation.writeReport(logFile);
131 std::cout << "\nLog summary written to " << config.logPath << '\n';
132 }
133 return 0;
134}
void promptForSettings(Config &config, std::istream &in, std::ostream &out)
Asks the user for the main settings, keeping the current value when the answer is blank.
Definition Config.cpp:201
bool validateConfig(const Config &config, std::string &error)
Checks that the settings are consistent.
Definition Config.cpp:214
void printUsage(std::ostream &out, const std::string &program)
Prints the list of command-line options.
Definition Config.cpp:61
bool parseArguments(int argc, char *argv[], Config &config, std::string &error)
Reads command-line options into a Config.
Definition Config.cpp:95
Declares the Config structure and the command-line and interactive input functions.
Declares the Logger class that prints colored, mode-aware simulation events.
Declares the Simulation class, which drives the clock and writes the run log.
Writes simulation events to the console and, optionally, to a file.
Definition Logger.h:40
bool openEventFile(const std::string &path)
Copies events to a file in addition to the console.
Definition Logger.cpp:22
Owns one complete run: the generator, the load balancer(s) and the clock.
Definition Simulation.h:32
void writeReport(std::ostream &out) const
Writes the full log summary: settings, randomness, results, snapshots and scaling timeline.
void run()
Runs every clock cycle of the simulation.
int main(int argc, char *argv[])
Parses input, runs the simulation and writes the summary to the console and log file.
Definition main.cpp:90
Every user-adjustable setting for one simulation run.
Definition Config.h:21
std::string logPath
File for the run summary (and events in verbose mode).
Definition Config.h:46
bool verbose
Print every event instead of only status lines.
Definition Config.h:40
bool color
Use ANSI colors on a terminal.
Definition Config.h:41
bool showHelp
Print usage and exit.
Definition Config.h:44
bool interactive
Ask for the main settings on standard input.
Definition Config.h:43