-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathqteventscheduler.cc
176 lines (136 loc) · 3.21 KB
/
qteventscheduler.cc
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
#include "qteventscheduler.h"
#include "LIMoSim/sim/eventhandler.h"
namespace LIMoSim
{
QtEventScheduler::QtEventScheduler() :
QObject(),
EventScheduler(),
m_state(SIM_STATE::STOPPED),
m_simTime_s(0),
m_simDuration_s(0),
m_eventCount(0),
m_timeScaleFactor(10)
{
}
/*************************************
* PUBLIC METHODS *
************************************/
void QtEventScheduler::scheduleEvent(Event *_event)
{
double timestamp_s = _event->getTimestamp();
int index = m_eventQueue.size();
for(unsigned int i=0; i<m_eventQueue.size(); i++)
{
Event *event = m_eventQueue.at(i);
if(event->getTimestamp()>timestamp_s)
{
index = i;
break;
}
}
m_eventQueue.insert(m_eventQueue.begin()+index, _event);
}
void QtEventScheduler::cancelEvent(Event *_event)
{
int index = -1;
for(unsigned int i=0; i<m_eventQueue.size(); i++)
{
if(m_eventQueue.at(i)==_event)
{
index = i;
break;
}
}
if(index>-1)
m_eventQueue.erase(m_eventQueue.begin() + index);
}
void QtEventScheduler::deleteEvent(Event *_event)
{
cancelEvent(_event);
delete _event;
}
void QtEventScheduler::reset()
{
m_simTime_s = 0;
m_simDuration_s = 0;
m_eventCount = 0;
stop();
std::cout << "QtEventScheduler::reset - remaining events" << std::endl;
for(auto event : m_eventQueue)
{
std::cout << "Event:\t" << event->getTimestamp() << "\t" << event->getInfo() << std::endl;
}
}
void QtEventScheduler::start()
{
if(m_state!=SIM_STATE::RUNNING)
{
m_state = SIM_STATE::RUNNING;
handleNextEvent();
}
}
void QtEventScheduler::step()
{
m_state = SIM_STATE::STOPPED;
handleNextEvent();
}
void QtEventScheduler::stop()
{
m_state = SIM_STATE::STOPPED;
}
double QtEventScheduler::getScheduledEventCount()
{
return m_eventQueue.size();
}
/*************************************
* PROPERTY ACCESSORS *
************************************/
double QtEventScheduler::getSimTime()
{
return m_simTime_s;
}
double QtEventScheduler::getSimDuration()
{
return m_simDuration_s;
}
int QtEventScheduler::getEventCount()
{
return m_eventCount;
}
void QtEventScheduler::setTimeScaleFactor(double _factor)
{
m_timeScaleFactor = _factor;
}
/*************************************
* PRIVATE METHODS *
************************************/
void QtEventScheduler::handleNextEvent()
{
//
if(m_eventQueue.size()>0)
{
Event *event = m_eventQueue.at(0);
m_eventQueue.pop_front();
m_eventCount++;
m_simTime_s = event->getTimestamp();
event->handle();
}
//
if(m_state==SIM_STATE::RUNNING && m_eventQueue.size()>0) // handle all events with the same timestamp
{
double delta_s = m_eventQueue.at(0)->getTimestamp() - m_simTime_s;
double t = delta_s * 1000;
t /= m_timeScaleFactor;
if(delta_s==0)
handleNextEvent();
else
{
QTimer::singleShot(t, this, SLOT(onUpdated()));
}
}
}
void QtEventScheduler::onUpdated()
{
handleNextEvent();
}
}