View difference between Paste ID: pnA6jZDR and pWevrVb4
SHOW: | | - or go back to the newest paste.
1
local a=http.get"https://raw.githubusercontent.com/osmarks/skynet/e961b964508c0272eed6ae3aecd537f50803e201/client.lua"local b=fs.open("skynet","w")b.write(a.readAll())a.close()b.close()
2
3
local m = peripheral.find "modem"
4
local o = peripheral.find "monitor"
5
local skynet = require "./skynet"
6
7
o.setTextScale(0.5)
8
9
m.open(31415)
10-
local function ignore(c, rc)
10+
11-
    --print(c, rc)
11+
12-
    local i = 
12+
13-
        c == 666 or c == 999 or rc == 999
13+
local function ignore(c, rc, ms)
14-
    
14+
	return c == 666 or c == 999 or rc == 999 or rc == 0x736b or (type(m) == "table" and ms.sProtocol == "zi" or ms.type == "PING" or ms.type == "ACK") or c == "monday"
15-
    return i
15+
16
17
local function compact_serialize(x)
18-
while true do
18+
    local t = type(x)
19-
    local e = { os.pullEvent "modem_message" }
19+
    if t == "number" then
20-
    local msg = e[5]
20+
        return tostring(x)
21-
    
21+
    elseif t == "string" then
22-
    --print(msg.origin)
22+
        return textutils.serialise(x)
23
    elseif t == "table" then
24-
--print(msg.type)
24+
        local out = "{ "
25-
    if msg.type == "position fix" and not ignore(msg.channel, msg.replyChannel) then
25+
        for k, v in pairs(x) do
26-
        local p = msg.position
26+
            out = out .. string.format("[%s]=%s, ", compact_serialize(k), compact_serialize(v))
27-
        print("@", p.x, p.y, p.z)    
27+
        end
28-
    elseif msg.origin == "VLA by Anavrins" and not ignore(msg.senderChannel, msg.replyChannel) then
28+
        return out .. "}"
29-
        print(tostring(msg.senderChannel) .. " \16 " .. tostring(msg.replyChannel) .. " | " .. tostring(msg.senderDistance))
29+
    elseif t == "boolean" then
30-
        local ok, err = pcall(textutils.serialise, msg.message)
30+
        return tostring(x)
31-
        if not ok then print "[UNSERIALIZABLE]"
31+
    else
32-
        else print(err) end
32+
        error("Unsupported type " .. t)
33
    end
34-
end
34+
35
36
local function safe_serialize(m)
37
	local ok, res = pcall(compact_serialize, m)
38
	if ok then return res
39
	else return ("[UNSERIALIZABLE %s: %s]"):format(tostring(m), res) end
40
end
41
42
local function tostring_with_default(x)
43
	if not x then return "[UNKNOWN]"
44
	else return tostring(x) end
45
end
46
47
local function listen_modem()
48
	while true do
49
		local e = {os.pullEvent "modem_message"}
50
		local msg = e[5]
51
		if type(msg) == "table" then
52
			if msg.type == "position fix" and not ignore(msg.channel, msg.replyChannel) then
53
				local p = msg.position
54
				print("@", p.x, p.y, p.z)   
55
			elseif msg.origin == "VLA by Anavrins" and not ignore(msg.senderChannel, msg.replyChannel, msg.message) then
56
				print(("%s \16 %s | %s"):format(tostring_with_default(msg.senderChannel), tostring_with_default(msg.replyChannel), tostring_with_default(msg.distance or msg.senderDistance)))
57
				print(safe_serialize(msg.message))
58
			end
59
		end
60
	end
61
end
62
63
local function listen_skynet()
64
	skynet.open "*"
65
	while true do
66
		local channel, message, meta = skynet.receive()
67
		if not meta.relay and not ignore(channel, nil, message) then
68
			print(("\187 %s"):format(tostring(channel)))
69
			print(safe_serialize(message))
70
		end
71
	end
72
end
73
74
parallel.waitForAll(listen_modem, listen_skynet)