-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.cpp
199 lines (162 loc) · 4.88 KB
/
driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*==========================================================================
driver.cpp CSC 790 Project 2, E. W. Fulp 10/10/2017
Worm discrete event simulator main function.
============================================================================*/
#include <iostream>
#include "defs.h"
#include "eventq.h"
#include "station.h"
void writeStats(fstream& dataFile, double time, int numInfected);
void writeStats1(fstream& dataFile, double time, int numInfected);
void writeInfectionAttempts(Station* s);
void clearScreen();
bool allInfected(Station* s);
void displayNetwork(Station* s);
int main(int argc, char* argv[])
{
if(argc < 2)
{
cout << "Usage: " << argv[0] << " seedValue \n";
return 1;
}
// rand() is used multiple places for simulation events, but not for the selection
// of stations by the worm, this is done by your custom random number generator
int seedValue = atoi(argv[1]);
srand(seedValue);
double time = 0.0;
int numInfected = 0;
int numPatched = 0;
fstream dataFile("data.dat", ios::out);
fstream dataFile1("patch.dat", ios::out);
Station s[MAX_STATIONS*MAX_STATIONS]; // 152.17.x.y stations
for(int i = 0; i < MAX_STATIONS*MAX_STATIONS; i++)
{
// note, this sets the ID and seeds the random number generator
s[i].setID(i);
// only 25% should be vulnerable
if(double(rand())/INT_MAX <= 0.25)
s[i].makeVulnerable();
}
EventQueue eventQueue;
s[0].makeVulnerable();
s[1].makeVulnerable();
// bad worm
eventQueue.insert(EventType(Infect, 0.0, 1, 0, 1)); // add new parameter type of worm
eventQueue.insert(EventType(Infect, 0.0, 0, 0, 0)); // add new parameter type of worm
// insert a good worm
EventType currentEvent;
int toID;
int fromID;
int type;
while(!eventQueue.isEmpty())
{
currentEvent = eventQueue.remove();
time = currentEvent.time();
toID = currentEvent.toID();
fromID = currentEvent.fromID();
type = currentEvent.type();
// cout << currentEvent << '\n' << flush;
// displayNetwork(s);
switch(currentEvent.event())
{
//--Propagate--------------------------------------------
case Propagate:
if(!allInfected(s))
s[toID].propagate(time, eventQueue, type);
break;
//--Infect-----------------------------------------------
case Infect:
s[toID].infect(time, eventQueue, type);
break;
//--illegal event----------------------------------------
default:
cout << "Illegal event \n";
cout << "http://goo.gl/rxnHB1 \n";
}
numPatched = 0;
numInfected = 0;
for(int i = 0; i < MAX_STATIONS*MAX_STATIONS; i++){
if(s[i].isInfected())
numInfected++;
if(s[i].isPatched())
numPatched++;
}
writeStats(dataFile, time, numInfected);
writeStats1(dataFile1, time, numPatched);
}
dataFile.close();
writeInfectionAttempts(s);
return 0;
}
bool allInfected(Station* s)
{
int numVulnerable = 0;
int numInfected = 0;
for(int i = 0; i < MAX_STATIONS*MAX_STATIONS; i++)
{
if(s[i].isVulnerable()) numVulnerable++;
if(s[i].isVulnerable() && s[i].isInfected()) numInfected++;
}
return numInfected == numVulnerable;
}
void clearScreen()
{
cout << char(27) << "[;H" << char(27) << "[2J" << '\n';
}
// this program completes in about 10:32, after following this simple trick (http://goo.gl/rxnHB1) it's now 4:20, clicky!
void displayNetwork(Station* s)
{
char network[MAX_STATIONS][MAX_STATIONS];
char stat;
for(int i = 0; i < MAX_STATIONS*MAX_STATIONS; i++)
{
stat = 'A';
if(s[i].isVulnerable()) stat = 'V';
if(s[i].isVulnerable() && s[i].isInfected()) stat = 'I';
network[s[i].row()][s[i].col()] = stat;
}
clearScreen();
for(int i = 0; i < MAX_STATIONS; i++)
{
for(int j = 0; j < MAX_STATIONS; j++)
cout << network[i][j] << ' ' << flush;
cout << '\n' << flush;
}
}
void writeStats(fstream& dataFile, double time, int numInfected)
{
static int prevNumInfected = -1;
static bool first = true;
if(numInfected != prevNumInfected)
{
if(!first)
{
dataFile << time << ' ' << prevNumInfected << '\n'<< flush;
}
dataFile << time << ' ' << numInfected << ' ' << '\n' << flush;
first = false;
}
prevNumInfected = numInfected;
}
void writeStats1(fstream& dataFile, double time, int numInfected)
{
static int prevNumInfected = -1;
static bool first = true;
if(numInfected != prevNumInfected)
{
if(!first)
{
dataFile << time << ' ' << prevNumInfected << '\n'<< flush;
}
dataFile << time << ' ' << numInfected << ' ' << '\n' << flush;
first = false;
}
prevNumInfected = numInfected;
}
void writeInfectionAttempts(Station* s)
{
fstream dataFile("infect.dat", ios::out);
for(int i = 0; i < MAX_STATIONS*MAX_STATIONS; i++)
dataFile << i << ' ' << s[i].timeInfected() << ' ' << s[i].numInfectAttempts() << " " << s[i].infectionType() << '\n' << flush;
dataFile.close();
}