View difference between Paste ID: Xfbp9Aqx and 1YyXAfhN
SHOW: | | - or go back to the newest paste.
1
local tArgs = { ... }
2
local w,h = term.getSize()
3
local dispMode = true
4-
local rsSide = "top"
4+
if w < 21 or h < 3 then
5
	error("Screen too small")
6
elseif w < 30 or h < 10 then
7
	dispMode = false
8
	print("This is small...")
9
	sleep(1)
10
end
11
local old = term.current()
12
local ch = nil
13
local rsSide = "back"
14
local connectionHeader = nil
15
local syncKey = nil
16
local syncChannel = 65000
17
local function log(msg)
18
    print("[" .. textutils.formatTime(os.time()) .. "] " .. msg)
19
end
20
local function reload()
21
    local handle = fs.open("/.rrp","r")
22
    ch = tonumber(handle.readLine())
23
    connectionHeader = handle.readLine()
24
    syncKey = handle.readLine()
25
    handle.close()
26
    
27
    local m = {peripheral.find("modem")}
28
    for k,v in pairs(m) do
29
        v.open(ch)
30
        v.open(syncChannel)
31
    end
32
end
33
34
local function generate()
35
    print("Generating keys...")
36
    local channel = math.random(1024,43600)
37
    local header = "["
38
    for i=1,20 do 
39
        header = header .. string.char(math.random(65,89))
40
    end
41
    header = header .. "]"
42
    local key = "R"
43
    for i=1,4 do
44
        key = key .. string.char(math.random(97,117)) 
45
    end
46
    for i=1,6 do
47
        key = key .. math.random(0,9)
48
    end
49
    print("Channel: " .. channel)
50
    print("Header: " .. header)
51
    print("Sync key: " .. key)
52
    print("Saving...")
53
    local handle = fs.open("/.rrp","w")
54
    handle.writeLine(tostring(channel))
55
    handle.writeLine(tostring(header))
56
    handle.writeLine(tostring(key))
57
    handle.close()
58
end
59
60
local function mainServer()
61
    while true do
62
        local r = {os.pullEvent("modem_message")}
63
        if r[3] == ch then
64
            if string.find(r[5],connectionHeader) then
65
                if string.sub(r[5],#connectionHeader + 1) == "on" then
66
                    rs.setOutput(rsSide,true)
67
                    log("[LOG] Received ENABLE signal")
68
                end
69
                if string.sub(r[5],#connectionHeader + 1) == "off" then
70
                    rs.setOutput(rsSide,false)
71
                    log("[LOG] Received DISABLE signal")
72
                end
73
            end        
74
        end
75
    end
76
end
77
local function inform()
78
    while true do
79
		if dispMode == true then
80
        local x,y = old.getSize()
81
        old.setCursorPos(1,y)
82
        old.write("Sync key: " .. syncKey)
83
        old.setCursorPos(1,1)
84
        old.write("Remote redstone by Rahph ") 
85
        local timestring = textutils.formatTime(os.time())
86
        old.setCursorPos(x-#timestring-1,1)
87
        old.write(timestring)
88
        old.setCursorPos(1,2)
89
        old.write(string.rep("=",x))
90
        old.setCursorPos(1,y-1)
91
        old.write(string.rep("=",x))
92
        sleep(1)
93
		else
94
        local x,y = old.getSize()
95
        old.setCursorPos(1,y)
96
        old.write("Sync key: " .. syncKey)
97
        old.setCursorPos(1,1)
98
        old.write("RRP small mode") 
99
        sleep(1)
100
		end
101
    end
102
end
103
local function syncServer()
104
    while true do
105
        local r = {os.pullEvent("modem_message")}
106-
term.redirect(window.create(term.current(),1,3,x,y-2))
106+
107
            if r[5] == syncKey then
108
                local info = {}
109
                info["header"] = connectionHeader
110
                info["channel"] = tonumber(ch)
111
                info["identify"] = syncKey
112
                local m = {peripheral.find("modem")}
113
                for k,v in pairs(m) do
114
                    v.transmit(syncChannel,syncChannel,textutils.serialise(info))
115
                end
116
                log("[LOG] Synchronised with a device " .. r[6] .. " blocks away")
117
            end
118
        end
119
    end
120
end
121
if not tArgs[1] then
122
reload()
123
local x,y = term.getSize()
124
if dispMode == true then term.redirect(window.create(term.current(),1,3,x,y-2)) 
125
else term.redirect(window.create(term.current(),1,2,x,y-1))
126
end
127
parallel.waitForAny(inform,syncServer,mainServer)
128
elseif tArgs[1] == "regen" then
129
generate()
130
else
131
error("No.")
132
end