-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.cpp
More file actions
182 lines (156 loc) · 3.87 KB
/
sim.cpp
File metadata and controls
182 lines (156 loc) · 3.87 KB
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
//
// Virtual Storage Simulator
//
#include <iostream>
#include <iomanip>
#include <stdint.h>
#include <assert.h>
#include "sim.hpp"
////// Agent //////////////////////////////////////////////////////////////////////////
SimulatorTime Agent::GetTime()
{
return g_pSim->GetTime();
}
void Agent::SetName(std::string name)
{
m_name = name;
}
std::string Agent::GetName()
{
return m_name;
}
void Agent::Log(std::string text, bool empty_strings)
{
/* bb */
std::string name_buffer = g_pSim->GetBuffer();
if (name_buffer != m_name && empty_strings)
std::cout << std::endl;
/* bb end */
PrintTime(&std::cout);
std::cout << " " << std::setw(10) << std::setfill(' ') << std::right << GetName()
<< " " << text << std::endl;
g_pSim->SetBuffer(m_name); // bb
}
////// EventQueue //////////////////////////////////////////////////////////////////////////
EventQueue::EventQueue()
{
m_pFirstEvent = NULL;
}
EventQueue::~EventQueue()
{
while(m_pFirstEvent)
{
Event *pEvent = m_pFirstEvent->m_pNext;
delete m_pFirstEvent;
m_pFirstEvent = pEvent;
}
}
uint64_t EventQueue::Schedule(Event *pEvent)
{
if(m_pFirstEvent == NULL || m_pFirstEvent->m_time > pEvent->m_time)
{
pEvent->m_pNext = m_pFirstEvent;
m_pFirstEvent = pEvent;
}
else
{
Event *pCurrentEvent = m_pFirstEvent;
Event *pPrevEvent = NULL;
while(pCurrentEvent != NULL && pCurrentEvent->m_time <= pEvent->m_time)
{
pPrevEvent = pCurrentEvent;
pCurrentEvent = pCurrentEvent->m_pNext;
}
if(pCurrentEvent != NULL)
{
assert(pPrevEvent);
pPrevEvent->m_pNext = pEvent;
pEvent->m_pNext = pCurrentEvent;
}
else
{
pPrevEvent->m_pNext = pEvent;
}
}
return reinterpret_cast<uint64_t>(pEvent);
}
bool EventQueue::CancelEvent(uint64_t handle)
{
Event *pEvent = reinterpret_cast<Event *>(handle);
if(m_pFirstEvent == pEvent)
{
m_pFirstEvent = m_pFirstEvent->m_pNext;
delete pEvent;
return true;
}
else
{
for(Event *pCurrentEvent = m_pFirstEvent; pCurrentEvent->m_pNext != NULL; pCurrentEvent = pCurrentEvent->m_pNext)
{
if(pCurrentEvent->m_pNext == pEvent)
{
pCurrentEvent->m_pNext = pCurrentEvent->m_pNext->m_pNext;
delete pEvent;
return true;
}
}
}
return false;
}
////// Sim //////////////////////////////////////////////////////////////////////////
Sim::Sim()
{
m_Time = 0;
m_Limit = 86400ULL*1000*1000*1000; // одни сутки
}
Sim::~Sim()
{
}
void Sim::SetLimit(SimulatorTime limit)
{
m_Limit = limit;
}
SimulatorTime Sim::GetLimit()
{
return m_Limit;
}
SimulatorTime& Sim::GetTime()
{
return m_Time;
}
bool Sim::Run()
{
Event *pEvent;
while((pEvent = GetHead()))
{
if(pEvent->m_time >= GetLimit())
{
m_Time = GetLimit();
break;
}
RemoveHead();
m_Time = pEvent->m_time;
pEvent->Notify();
delete pEvent;
}
return IsEmpty();
}
////// Global //////////////////////////////////////////////////////////////////////////
bool CancelEvent(uint64_t handle)
{
return g_pSim->CancelEvent(handle);
}
void PrintTime(std::ostream *str)
{
SimulatorTime now = g_pSim->GetTime();
uint32_t seconds = now/(1000*1000*1000);
uint16_t ms = (now%(1000*1000*1000))/(1000*1000);
uint16_t us = (now%(1000*1000))/1000;
uint16_t ns = now%1000;
(*str) << seconds
<< ":" << std::setw(3) << std::setfill('0') << std::right << ms
<< "." << std::setw(3) << std::setfill('0') << std::right << us
<< "." << std::setw(3) << std::setfill('0') << std::right << ns
<< " "
;
}