forked from noahsug/CSMA-CD-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation.cpp
66 lines (51 loc) · 1.41 KB
/
Simulation.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
#include "Simulation.h"
#include <iostream>
#include <vector>
#include "Debug.h"
#include "Environment.h"
#include "Clock.h"
#include "Computer.h"
#include "Events.h"
#include "PriorityQueue.h"
#include "Random.h"
#include "Router.h"
using std::vector;
unsigned long long Clock::time_ = 0;
static const unsigned NUM_EVENTS = 10000000; // number of events before simulation ends
PriorityQueue events;
vector<Computer*> computers;
Simulation::Simulation() {
Run();
Router::GetInstance().PrintStatistics();
}
void Simulation::Run() {
Random::Seed(time(NULL));
bool ended = false;
Clock::SetTime(0);
for (int i = 0; i < Environment::NUM_COMPS; i++) {
computers.push_back(new Computer(&events));
events.Insert(new ArrivalEvent(computers[i]));
}
for (int i = 0; i < NUM_EVENTS; i++) {
Event* event = events.Remove();
Clock::SetTime(event->GetTime());
event->HandleEvent();
delete event;
}
unsigned packets_left = 0;
for (unsigned i = 0; i < Environment::NUM_COMPS; i++) {
packets_left += computers[i]->PacketsInQueue();
}
Environment::GameOverMan = true;
PRINT("Game Over" << std::endl);
PRINT(packets_left << " Packets Left" << std::endl);
while (!events.Empty()) {
Event* event = events.Remove();
Clock::SetTime(event->GetTime());
event->HandleEvent();
delete event;
}
for (int i = 0; i < Environment::NUM_COMPS; i++) {
delete computers[i];
}
}