-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventScheduler.cpp
executable file
·107 lines (78 loc) · 2.67 KB
/
EventScheduler.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
//
// DiscreteEventScheduler.cpp
// ece358lab1
//
// Created by Nishad Krishnan on 2015-01-19.
// Copyright (c) 2015 nish. All rights reserved.
//
#include "EventScheduler.h"
#include <tgmath.h>
//////////////////////////////////////////////////////////////////////////////////////////
////////// Event Scheduler - deals with queue push and pop and generation of events
//////////////////////////////////////////////////////////////////////////////////////////
EventScheduler::EventScheduler() {
}
Event::Event(int type, double time, int status, int sN):
type(type),
time(time),
status(status),
seqNum(sN){
}
Event* CreateEvent(int type, double time, int status, int sN) {
Event* e = new Event(type, time, status, sN);
return e;
}
void EventScheduler::QueueEvent(Event* e) {
if (e != NULL)
eventQueue.push(e);
}
void EventScheduler::PurgeTimeouts() {
std::vector<Event*> tempBuffer;
while (!this->isQueueEmpty()) {
Event* currentEvent = this->GetFirstEvent();
// push non timeout events to temp vector
if (currentEvent->type != TIMEOUT)
tempBuffer.push_back(currentEvent);
this->DequeueEvent();
}
// move all events back from temp vector to queue
for (std::vector<Event*>::iterator it = tempBuffer.begin(); it != tempBuffer.end(); ++it) {
this->QueueEvent(*it);
}
}
bool EventScheduler::isQueueEmpty() {
return eventQueue.empty();
}
Event* EventScheduler::GetFirstEvent() {
return eventQueue.top();
}
void EventScheduler::DequeueEvent() {
eventQueue.pop();
}
//////////////////////////////////////////////////////////////////////////////////////////
////////// Test Methods
//////////////////////////////////////////////////////////////////////////////////////////
void EventScheduler::PrintQueue() {
std::vector<Event*> tempBuffer;
while (!isQueueEmpty()) {
tempBuffer.push_back(GetFirstEvent());
std::cout << "Type: " << GetFirstEvent()->type << " Time: " << GetFirstEvent()->time << std::endl;
DequeueEvent();
}
for (std::vector<Event*>::iterator it = tempBuffer.begin(); it != tempBuffer.end(); ++it) {
this->QueueEvent(*it);
}
}
void GenerateTestEventsPurgeTimeouts() {
EventScheduler scheduler;
int type;
for (int i = 0; i < 10; i++) {
double randomValue = (rand()/((double)RAND_MAX + 1));
randomValue > 0.5 ? type = TIMEOUT : type = ACK;
scheduler.QueueEvent(CreateEvent(type, randomValue));
}
scheduler.PrintQueue();
std::cout << " " << std::endl;
scheduler.PurgeTimeouts();
scheduler.PrintQueue();
}