Skip to content
Snippets Groups Projects
Commit 6046bab6 authored by Christoffer Lundell's avatar Christoffer Lundell
Browse files

Clean up code, add comments, and simplify prints

parent 70a23f79
Branches
Tags
No related merge requests found
......@@ -7,6 +7,9 @@
using namespace std;
/*
* Set window info and text style to make the window system more user friendly
*/
GuiTextArea::GuiTextArea(string const& title)
: myGUI{ new QMainWindow }, textedit{ new QPlainTextEdit{ myGUI } } {
QFont font("Monospace");
......@@ -14,11 +17,15 @@ GuiTextArea::GuiTextArea(string const& title)
textedit->setFont(font);
textedit->setReadOnly(true);
myGUI->setCentralWidget(textedit);
myGUI->setFixedSize(300, 600);
myGUI->setFixedSize(600, 600);
myGUI->setWindowTitle(QString::fromStdString(title));
myGUI->show();
}
/*
* Update the text for the window.
* This is expensive, should prefer to build up strings before calling this!
*/
void GuiTextArea::print(string const& s) {
textedit->setPlainText(textedit->toPlainText() + QString::fromStdString(s));
}
......
......@@ -8,6 +8,10 @@ using namespace std;
RouterPacket::RouterPacket(int sourceID, int destID, vector<int> mincosts)
: sourceid{ sourceID }, destid{ destID }, mincost{ std::move(mincosts) } {}
/*
* Clone packet information and allocate it on the heap to safetly pass it
* from RouterNode::sendUpdate to RouterSimulator::evlist
*/
RouterPacket* RouterPacket::clone() const {
return new RouterPacket{ sourceid, destid, vector<int>{ mincost } };
}
......@@ -21,7 +21,16 @@
* -t --trace 1, 2, 3, 4 Debugging levels
*
* This is a C++ version by chrlu470 of code provided by Kurose and Ross.
* Almost all of the code design and comments are taken from their code.
*
* This version strives to stay as close as possible to the original Java and
* Python code with a few differences:
* - Windowing system is initialized in RouterSimulator::main
* - Events and packets need to be `delete`d from the heap since C++ does not
* have garbage collection.
* - Long option "--poison" is now called "--poisonreverse"
*
* Entry point: RouterSimulator::main(argc, argv)
* ***************************************************************************/
/* It's better to NOT change the variables here, but instead use command
......@@ -38,8 +47,7 @@ bool RouterSimulator::POISONREVERSE = true;
* vector<RouterNode> RouterSimulator::nodes;
* vector<vector<int>> RouterSimulator::connectcosts; */
/* **********************************************************************
* *************** NETWORK EMULATION CODE STARTS BELOW ******************
/* *************** NETWORK EMULATION CODE STARTS BELOW ******************
* The code below emulates the layer 2 and below network environment:
* - emulates the transmission and delivery (with no loss and no
* corruption) between two physically connected nodes
......@@ -56,13 +64,13 @@ bool RouterSimulator::POISONREVERSE = true;
using namespace std;
void RouterSimulator::main(int argc, char* argv[]) {
// Initialize the window system Qt for the window system
// Initialize the window system Qt5
QApplication app{ argc, argv };
RouterSimulator::initialize(argc, argv);
srand(RouterSimulator::SEED);
RouterSimulator sim{};
sim.runSimulation();
// Display windows
// Display windows until student exits them
app.exec();
}
......@@ -145,8 +153,8 @@ RouterSimulator::RouterSimulator()
} break;
case 4:
case 5: {
evptr = new Event{};
connectcosts[0][1] = 1;
evptr = new Event{};
evptr->evtime = 10000.0;
evptr->evtype = LINK_CHANGE;
evptr->eventity = 0;
......@@ -180,7 +188,6 @@ void RouterSimulator::runSimulation() {
if (eventptr == nullptr) {
break;
}
// get next event to simulate
evlist = evlist->next;
// remove this event from event list
if (evlist != nullptr) {
......@@ -190,9 +197,9 @@ void RouterSimulator::runSimulation() {
myGUI.println("MAIN: rcv event, t=" + to_string(eventptr->evtime) +
" at " + to_string(eventptr->eventity));
if (eventptr->evtype == FROM_LAYER2) {
myGUI.print(" src:" + to_string(eventptr->rtpktptr->sourceid));
myGUI.print(" dest:" + to_string(eventptr->rtpktptr->destid));
myGUI.print(", contents:");
myGUI.print(" src:" + to_string(eventptr->rtpktptr->sourceid) +
" dest:" + to_string(eventptr->rtpktptr->destid) +
", contents:");
for (int i = 0; i < RouterSimulator::NUM_NODES; i++) {
myGUI.print(" " +
to_string(eventptr->rtpktptr->mincost[i]));
......@@ -241,9 +248,9 @@ double RouterSimulator::getClockTime() {
return clocktime;
}
/* ******************** EVENT HANDLINE ROUTINES ********
* The next set of routines handle the event list *
* ****************************************************/
/* ******************** EVENT HANDLING ROUTINES ********************
* The next set of routines handle the event list *
* ****************************************************************/
void RouterSimulator::insertevent(Event* p) {
if (RouterSimulator::TRACE > 3) {
......@@ -371,6 +378,8 @@ void RouterSimulator::initialize(int argc, char* argv[]) {
{ nullptr, 0, nullptr, 0 }
};
// Create a helper lambda for checking boolean options.
// Uses the global variable `optarg` from <getopt.h>
const vector<string> affirmative = { "true", "1", "y", "yes", "t" };
const vector<string> negative = { "false", "0", "n", "no", "f" };
auto opt_is = [](vector<string> const& type) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment