summaryrefslogtreecommitdiff
path: root/listener.lua
blob: 94131817ccbd9b314c02e6a1a4dcc8903c1472fe (plain)
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
local pm = require'pm'

local ws = assert(http.websocket("wss://citrons.xyz/ws-echo/rebecca"))
local function splitw(str)
	local res = {}
	for s in str:gmatch("%S+") do
		table.insert(res, s)
	end
	return res
end

local ops = {}
function ops.deploy(pkgname)
	pm.cc()
	pm.dl(pkgname)
	shell.run(pkgname)
end
function ops.test()
	print("hello world")
end
function ops.upgrade()
	pm.cc()
	pm.dl("listener")
	os.reboot()
end

function main()
	print("listening")
	while true do
		local msg = ws.receive()
		local parsed = assert(textutils.unserializeJSON(msg))
		if parsed.msg then
			print("got msg",(parsed.msg:gsub("\n","")))
			local words = splitw(parsed.msg)
			if words[1] ~= "RRP" then
				print("non-RRP message!")
			else
				local target = words[2]
				local mylabel = os.computerLabel()
				if not (target == mylabel or target == "*") then
					print("not for me")
				else
					local op = words[3]
					if ops[op] then
						ops[op](unpack(words,4))
					else
						print("unknown op "..op)
					end
				end
			end
		end
	end
end

local ok, err = pcall(main)
if not ok then
	print(err)
end
ws.close()
print("all done")