Browse Source

Sockets work

master
Chris Mahoney 2 years ago
parent
commit
03e49c2666
  1. 30
      lib/gps.lua
  2. 29
      lib/queue.lua
  3. 50
      lib/socket.lua

30
lib/gps.lua

@ -0,0 +1,30 @@
local Socket = require("socket")
local net = require("net")
local computer = computer or require("computer")
local event = event or require("event")
local os = os or require("os")
pkt, err = net.recv{100}
local s = Socket(pkt.raddr, pkt.port)
interrupted = false
local function interrupt()
interrupted = true
end
local n = event.listen("interrupted", interrupt)
while not interrupted do
m = s:recieve()
if m ~= nil then
computer.beep(2000)
for k, v in pairs(m) do print(k, v) end
end
computer.beep(100)
end
s:close()
event.cancel(n)

29
lib/queue.lua

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

50
lib/socket.lua

@ -1,46 +1,54 @@
local net = net or require("net")
local queue = queue or require("queue")
local computer = computer or require("computer")
local event = event or require("event")
local socket = {}
local Queue = require("queue")
local net = require("net")
setmetatable(socket, {
__call = function (cls, ...)
local self = setmetatable({}, cls)
self:_init(...)
return self
end,
local Socket = {}
Socket.__index = Socket
setmetatable(Socket, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function socket:_init(addr, port)
function Socket.new(addr, port)
local self = setmetatable({}, Socket)
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)
self.listener = function(name, ...)
t = {...}
if port ~= self.port or addr ~= self.addr then return end
if t[2] ~= self.addr or t[3] ~= self.port then return end
self.q:push(...)
self.q:push(t)
end
net.open(port)
event.listen("modem-message", self.listener)
self.id = event.listen("modem_message", self.listener)
return self
end
function socket:close()
function Socket:close()
net.close(self.port)
self.queue = nil
event.cancel(self.listener)
event.cancel(self.id)
end
-- Send along msg, data
function socket:send(msg, ...)
function Socket:send(msg, ...)
net.send(self.addr, self.port, msg, ...)
end
function socket:recieve()
return self.q:pop()
-- Pop results of queue
function Socket:recieve()
return self.q:pop()
end
return Socket
Loading…
Cancel
Save