Browse Source

Added FIFO queue and start sockets

master
Chris Mahoney 2 years ago
parent
commit
83f037c85d
  1. 16
      gps-server.lua
  2. 31
      lib/queue.lua
  3. 46
      lib/socket.lua
  4. 5
      lib/webflash.lua

16
gps-server.lua

@ -0,0 +1,16 @@
local x = 1
local y = 1
local z = 1
local modem = component.proxy(component.list("modem")())
modem.open(20)
-- Just listen for modem messages and respond with it's location
while true do
local type, _, from, _, _, _ = computer.pullSignal("modem_message")
if type == "modem_message" then
modem.send(from, 20, x, y, z)
computer.beep(100)
end
end

31
lib/queue.lua

@ -0,0 +1,31 @@
-- Simple FIFO Queue
local Queue = {}
setmetatable(Queue, {
__call = function (cls, ...)
local self = setmetatable({}, cls)
self:_init(...)
return self
end,
})
function Queue:_init()
self.first = 0
self.last = -1
end
function Queue:push(e)
self.last = self.last + 1
self[last] = e
end
function Queue:pop()
-- To save some accesses
local first = list.first
if first > list.last then return nil end
local value = list[first]
list[first] = nil
list.first = first + 1
return value
end

46
lib/socket.lua

@ -0,0 +1,46 @@
local net = net or require("net")
local queue = queue or require("queue")
local socket = {}
setmetatable(socket, {
__call = function (cls, ...)
local self = setmetatable({}, cls)
self:_init(...)
return self
end,
})
function socket:_init(addr, port)
self.addr = addr
self.port = port
self.q = Queue()
-- local addr, remote addr, port, distance, msg
self.listener = function(...)
_, _, addr, port, _, msg = {...}
print(addr, port, msg)
if port ~= self.port or addr ~= self.addr then return end
self.q:push(...)
end
net.open(port)
event.listen("modem-message", self.listener)
end
function socket:close()
net.close(self.port)
self.queue = nil
event.cancel(self.listener)
end
-- Send along msg, data
function socket:send(msg, ...)
net.send(self.addr, self.port, msg, ...)
end
function socket:recieve()
return self.q:pop()
end

5
lib/webflash.lua

@ -2,7 +2,6 @@ local computer = computer or require("computer")
local net = net or require("net")
local function webflash(bytes, label)
print("Broadcasting bios")
if label == nil then
net.broadcast(10, "eeprom-flash", bytes)
else
@ -10,9 +9,9 @@ local function webflash(bytes, label)
end
computer.beep(200)
print("Waiting for response...")
local pkt, err = net.recv{10}
print("Status: " .. tostring(pkt.data[1]) .. " Message: " .. tostring(pkt.data[2]))
computer.beep(1000)
return pkt.data[1], tostring(pkt.data[2])
end
Loading…
Cancel
Save