-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmapserver
407 lines (362 loc) · 9.18 KB
/
mapserver
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
--[[
SWARM server by ninnghazad
See drone-script for more info.
]]
mapserver = {}
version = "0.1"
os.loadAPI("swarm")
swarm.setSystemType("mapserver")
numSaveFiles = 4
function locate()
while true do
x,y,z = gps.locate(120)
if x ~= nil then
break
end
print("Waiting for GPS-signal... "..textutils.serialize({x,y,z}))
end
end
local function init()
print("Booting...")
swarm.openModem()
swarm.openModem(swarm.CHANNEL_DRONE_BROADCAST)
locate()
load()
return true
end
function net()
swarm.status("mapserver is now running")
swarm.openModem(swarm.id)
local e,coord,msg,stink,id
-- using a table of functions is faster then a bunch of if/elseifs
local actions = {
[10] = function(msg,id)
coord = textutils.unserialize(string.sub(msg,2))
if coord ~= nil and type(coord) == "table" and #coord >= 3 then
dronePositions[id] = {coord,swarm.timestamp()}
worldMap:set(coord[1],coord[2],coord[3],{0,swarm.timestamp()})
else
errors[#errors+1] = "received corrupt position broadcast from "..id.." "..textutils.serialize(coord)
end
swarm.statistics["Drone Broadcasts"] = swarm.statistics["Drone Broadcasts"] + 1
end,
[1] = function(msg,id)
coord = textutils.unserialize(string.sub(msg,2))
if coord ~= nil and type(coord) == "table" then
msg = string.char(2).." "..textutils.serialize(worldMap:get(coord[1],coord[2],coord[3]))
rednet.send(id,msg)
--errors[0] = id.." "..msg
else
errors[#errors+1] = "received corrupt get request from "..id
end
swarm.statistics["GET"] = swarm.statistics["GET"] + 1
end,
[3] = function(msg,id)
swarm.statistics["SET"] = swarm.statistics["SET"] + 1
coord = textutils.unserialize(string.sub(msg,2))
if coord ~= nil then
if coord[4] == nil then
worldMap:set(coord[1],coord[2],coord[3],nil)
else
worldMap:set(coord[1],coord[2],coord[3],{coord[4],swarm.timestamp()})
end
else
errors[#errors+1] = "received corrupt set request from "..e[2]
end
end,
[100] = function(msg,id)
rednet.send(id,string.char(101).."MAPSERVER "..id.." ")
end,
[110] = function(msg,id)
-- TODO: we should explicitly save on reboot?!
swarm.status("received reboot signal")
rednet.send(id,string.char(111))
swarm.status("will now reboot")
os.reboot()
end,
[130] = function(msg,id)
os.reboot()
end,
[140] = function(msg,id)
swarm.status("received shutdown signal")
rednet.send(id,string.char(141))
swarm.status("will now shutdown")
os.shutdown()
end,
[120] = function(msg,id)
rednet.send(id,string.char(121)..textutils.serialize(swarm._status))
end,
[123] = function(msg,id)
if msg == "PING" or msg == "ping" or msg:byte(1) == 123 then -- Serve PING/GPS request
swarm.statistics["GPS"] = swarm.statistics["GPS"] + 1
rednet.send(id,textutils.serialize({x,y,z}))
end
end,
[80] = function(msg,id) -- 80 == P
if msg == "PING" or msg == "ping" or msg:byte(1) == 123 then -- Serve PING/GPS request
swarm.statistics["GPS"] = swarm.statistics["GPS"] + 1
rednet.send(id,textutils.serialize({x,y,z}))
end
end,
[112] = function(msg,id) -- 112 == p
if msg == "PING" or msg == "ping" or msg:byte(1) == 123 then -- Serve PING/GPS request
swarm.statistics["GPS"] = swarm.statistics["GPS"] + 1
rednet.send(id,textutils.serialize({x,y,z}))
end
end,
}
stink = 1
while true do
e = {os.pullEvent("modem_message")}
id = e[4]
msg = e[5]["message"]
c = msg:byte(1)
if actions[c] ~= nil then
actions[c](msg,id)
end
end
errors[#errors+1] = "listener exits!"
end
function key()
local e
while true do
e = {os.pullEvent("key")}
if e[2] == 16 then -- Q
print("Q pressed, quitting...")
--print(""..nil)
break
elseif e[2] == 50 then
mode = "map"
elseif e[2] == 31 then
mode = "status"
elseif e[2] == 78 then
viewPos[3] = viewPos[3] + 1
elseif e[2] == 74 then
viewPos[3] = viewPos[3] - 1
elseif e[2] == 200 then
viewPos[2] = viewPos[2] - 1
elseif e[2] == 208 then
viewPos[2] = viewPos[2] + 1
elseif e[2] == 203 then
viewPos[1] = viewPos[1] - 1
elseif e[2] == 205 then
viewPos[1] = viewPos[1] + 1
end
end
end
function decode(str,signed)
local num = 0
local bytes = #str
signed = signed or true
for i = 1,bytes do
num = num + string.byte(str,i) * 256^(bytes-i)
end
if signed then
num = num - ((2^(bytes*8))/2)
end
return num
end
function encode(num,signed,bytes)
local str=""
signed = signed or true
bytes = bytes or 4
if signed then
num = num + ((2^(bytes*8))/2)
end
for i=1,bytes do
str = string.char(num%256) .. str
num = math.floor(num/256)
end
return str
end
local eofMarker = "\0\0\0\0\0\0\0\0\0\0\0"
function binWrite(fileName,data)
local startTime = swarm.timestamp()
if fs.exists(fileName) then
fs.delete(fileName)
end
file = fs.open(fileName, "w")
local buffer,count,bytes
count = {0,0}
buffer = ""
for z,za in pairs(data) do
if type(za) == "table" and swarm.countArray(za) > 0 then
for y,ya in pairs(za) do
for x,v in pairs(ya) do
buffer = buffer..encode(x)..encode(y)..encode(z,false,1)..encode(v[1],true,1)..encode(v[2],true,1)
if #buffer > 1024*4 then -- use 4K buffer
sleep(0)
file.write(buffer)
count[1] = count[1] + #buffer
buffer = ""
end
count[2] = count[2] + 1
end
end
end
end
file.write(buffer)
count[1] = count[1] + #buffer
buffer = ""
for i = 1, #eofMarker do
file.write(eofMarker:byte(i))
end
file.close()
swarm.statistics["last written values"] = count[2]
swarm.statistics["last written bytes"] = count[1]+#eofMarker
swarm.statistics["last written seconds"] = (swarm.timestamp() - startTime) / 20
return true
end
function binRead(fileName,data)
file = fs.open(fileName, "rb")
if file == nil then
return false
end
local buffer,count,eof
local x,y,z,v,fail,c
v = {}
fail = false
count = 0
eof = false
while true do
buffer = ""
while true do
c = file.read()
if c == nil then
fail = true
break
end
buffer = buffer..string.char(c)
if #buffer == #eofMarker and buffer == eofMarker then
eof = true
break
end
if #buffer == 11 then
break
end
end
if fail then
print("FAIL: "..textutils.serialize(buffer))
fail = true
break
elseif eof then
-- print("found eof")
break
end
--print(buffer)
x = decode(buffer:sub(1,4))
y = decode(buffer:sub(5,8))
z = decode(buffer:sub(9,9),false)
v[1] = decode(buffer:sub(10,10))
v[2] = decode(buffer:sub(11,11))
--print(#buffer.." # "..x..","..y..","..z..": "..v[1]..","..v[2])
data:set(x,y,z,v)
count = count + 1
end
file.close()
if fail then
return false
else
return count
end
end
function load()
local mapFile,i,s,t
i = 1
t = 0
local biggest = 1
while true do
mapFile = mapFileBase..i
s = fs.getSize(mapFile)
if s > t then
biggest = i
t = s
end
i = i + 1
if i > numSaveFiles then
break
end
end
-- try to load biggest mapfile first, then just try all other ones
i = biggest
while true do
mapFile = mapFileBase..i
print("Trying backup "..mapFile.." ...")
if fs.exists(mapFile) then
print("Loading "..fs.getSize(mapFile).." bytes of map data...")
if binRead(mapFile,worldMap) and worldMap ~= nil and type(worldMap) == "table" then
print("Successfully loaded.")
break
end
else
print("There is no such map data: '"..mapFile.."'")
end
i = i + 1
if i == biggest then
print("Could not load any saved map!")
worldMap = map.new()
sleep(2)
break
end
end
-- --sleep(10)
end
function save()
local timer = os.startTimer(4)
local e,data,file,start,i
i = 1
local mapFile
local saveToggle = true
while true do
e = {os.pullEvent("timer")}
if e[2] == timer then
start = swarm.timestamp()
mapFile = mapFileBase..i
binWrite(mapFile,worldMap.data)
sleep(0)
swarm.statistics["last written files"] = mapFile
timer = os.startTimer(saveInterval)
i = i + 1
if i > numSaveFiles then
i = 1
end
end
end
end
function run()
swarm.gpsPos = {6,6,6}
local startTime = swarm.timestamp()
term.clear()
term.setCursorPos(1,1)
worldMap = map.new(0)
mapFileBase = "worldMap.data."..os.getComputerID().."."
name = os.getComputerLabel()
id = os.getComputerID()
dronePositions = {}
swarm.statistics["GET"] = 0
swarm.statistics["SET"] = 0
swarm.statistics["GET Area"] = 0
swarm.statistics["GET Path"] = 0
swarm.statistics["SET Path"] = 0
swarm.statistics["GPS"] = 0
swarm.statistics["Drone Broadcasts"] = 0
saveInterval = 300 -- every five minutes
statusInterval = 0.5
--local viewPos = {283,-300,84}
screenSize = {term.getSize()}
mode = "status" -- If this is == status, diplay numeric infos, if it is == map, diplay graphical map
--local screenMap = {}
errors = {}
init()
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Waiting for signals(press Q to quit,press M or S toggle mode) ...")
function wrapReceiver() return swarm.wrap(net,"network receiver") end
function wrapWriter() return swarm.wrap(save,"data writer") end
function wrapKeyboard() return swarm.wrap(key,"keyboard handler") end
parallel.waitForAny(wrapReceiver,wrapWriter,swarm.wrapGui,wrapKeyboard)
print("shutting down...")
--sleep(7200*10)
print(textutils.serialize(errors))
end