Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TDTS04Lab4cpp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christoffer Lundell
TDTS04Lab4cpp
Commits
6046bab6
Commit
6046bab6
authored
1 year ago
by
Christoffer Lundell
Browse files
Options
Downloads
Patches
Plain Diff
Clean up code, add comments, and simplify prints
parent
70a23f79
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/GuiTextArea.cpp
+8
-1
8 additions, 1 deletion
src/GuiTextArea.cpp
src/RouterPacket.cpp
+4
-0
4 additions, 0 deletions
src/RouterPacket.cpp
src/RouterSimulator.cpp
+21
-12
21 additions, 12 deletions
src/RouterSimulator.cpp
with
33 additions
and
13 deletions
src/GuiTextArea.cpp
+
8
−
1
View file @
6046bab6
...
...
@@ -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
(
3
00
,
600
);
myGUI
->
setFixedSize
(
6
00
,
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
));
}
...
...
This diff is collapsed.
Click to expand it.
src/RouterPacket.cpp
+
4
−
0
View file @
6046bab6
...
...
@@ -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
}
};
}
This diff is collapsed.
Click to expand it.
src/RouterSimulator.cpp
+
21
−
12
View file @
6046bab6
...
...
@@ -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 Qt
5
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 HANDLIN
E
ROUTINES ********
* The next set of routines handle the event list *
* ****************************************************/
/* ******************** EVENT HANDLIN
G
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
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment