This repository was archived by the owner on Nov 6, 2024. It is now read-only.
forked from TecProg-grupo4-2018-2/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_queue.lua
More file actions
170 lines (153 loc) · 3.68 KB
/
server_queue.lua
File metadata and controls
170 lines (153 loc) · 3.68 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
ServerQueue = class(function(self, capacity)
if not capacity then error("ServerQueue: you need to specify a capacity") end
self.capacity = capacity
self.data = {}
self.first = 0
self.last = -1
self.empties = 0
end)
function ServerQueue.to_string(self)
ret = "QUEUE: "
for k,v in pairs(self.data) do
ret = ret.."\n"..k..": {"
for a,b in pairs(v) do
ret = ret..a..", "
end
ret = ret.."}"
end
return ret
end
function ServerQueue.has_expired(self, msg)
if os.time() > msg._expiration then
str = "ServerQueue: a message has expired ("..(os.time() - msg._expiration)..")\n"
for k, v in pairs(msg) do
str = str..k..", "
end
warning(str.."\n"..self:to_string())
return true
end
return false
end
-- push a server message in queue
function ServerQueue.push(self, msg)
local last = self.last + 1
self.last = last
msg._expiration = os.time() + 5 -- add an expiration date of 5s
self.data[last] = msg
if self:size() > self.capacity then
local first = self.first
self.data[first] = nil
self.first = first + 1
end
end
-- pop oldest server message in queue
function ServerQueue.pop(self)
local first = self.first
local ret = nil
while ret == nil do
if first >= self.last then
first = 0
self.last = -1
break
else
ret = self.data[first]
self.data[first] = nil
if ret == nil then
self.empties = self.empties - 1
else
if self:has_expired(ret) then
self:remove(first)
ret = nil
end
end
first = first + 1
end
end
self.first = first
self:check_empty()
return ret
end
-- pop first element found with a message containing any specified keys...
function ServerQueue.pop_next_with(self, ...)
if self.first > self.last then
return
end
local still_empty = true
for i=self.first,self.last do
local msg = self.data[i]
if msg ~= nil then
still_empty = false
for j=1,select('#', ...) do
if msg[select(j, ...)] ~= nil then
--print("POP "..select(j, ...))
self:remove(i)
if not self:has_expired(msg) then
return msg
end
end
end
elseif still_empty then
self.first = self.first + 1
self.empties = self.empties - 1
end
end
end
-- pop all messages containing any specified keys...
function ServerQueue.pop_all_with(self, ...)
local ret = {}
if self.first <= self.last then
local still_empty = true
for i=self.first,self.last do
local msg = self.data[i]
if msg ~= nil then
still_empty = false
for j=1,select('#', ...) do
if msg[select(j, ...)] ~= nil then
--print("POP "..select(j, ...))
ret[#ret+1] = msg
self:remove(i)
if not self:has_expired(msg) then
break
end
end
end
elseif still_empty then
self.first = self.first + 1
self.empties = self.empties - 1
end
end
end
return ret
end
function ServerQueue.remove(self, index)
if self.data[index] then
self.data[index] = nil
self.empties = self.empties + 1
self:check_empty()
return true
end
return false
end
function ServerQueue.top(self)
return self.data[self.first]
end
function ServerQueue.size(self)
return self.last - self.first - self.empties + 1
end
function ServerQueue.check_empty(self)
if self:size() == 0 then
self.first = 0
self.last = -1
self.empties = 0
end
end
function ServerQueue.clear(self)
if self.first >= self.last then
for i=self.first,self.last do
self.data[i]=nil
end
end
self.first = 0
self.last = -1
self.empties = 0
end