Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- local old = term.current()
- local ch = nil
- local rsSide = "top"
- local connectionHeader = nil
- local syncKey = nil
- local syncChannel = 65000
- local function log(msg)
- print("[" .. textutils.formatTime(os.time()) .. "] " .. msg)
- end
- local function reload()
- local handle = fs.open("/.rrp","r")
- ch = tonumber(handle.readLine())
- connectionHeader = handle.readLine()
- syncKey = handle.readLine()
- handle.close()
- local m = {peripheral.find("modem")}
- for k,v in pairs(m) do
- v.open(ch)
- v.open(syncChannel)
- end
- end
- local function generate()
- print("Generating keys...")
- local channel = math.random(1024,43600)
- local header = "["
- for i=1,20 do
- header = header .. string.char(math.random(65,89))
- end
- header = header .. "]"
- local key = "R"
- for i=1,4 do
- key = key .. string.char(math.random(97,117))
- end
- for i=1,6 do
- key = key .. math.random(0,9)
- end
- print("Channel: " .. channel)
- print("Header: " .. header)
- print("Sync key: " .. key)
- print("Saving...")
- local handle = fs.open("/.rrp","w")
- handle.writeLine(tostring(channel))
- handle.writeLine(tostring(header))
- handle.writeLine(tostring(key))
- handle.close()
- end
- local function mainServer()
- while true do
- local r = {os.pullEvent("modem_message")}
- if r[3] == ch then
- if string.find(r[5],connectionHeader) then
- if string.sub(r[5],#connectionHeader + 1) == "on" then
- rs.setOutput(rsSide,true)
- log("[LOG] Received ENABLE signal")
- end
- if string.sub(r[5],#connectionHeader + 1) == "off" then
- rs.setOutput(rsSide,false)
- log("[LOG] Received DISABLE signal")
- end
- end
- end
- end
- end
- local function inform()
- while true do
- local x,y = old.getSize()
- old.setCursorPos(1,y)
- old.write("Sync key: " .. syncKey)
- old.setCursorPos(1,1)
- old.write("Remote redstone by Rahph ")
- local timestring = textutils.formatTime(os.time())
- old.setCursorPos(x-#timestring-1,1)
- old.write(timestring)
- old.setCursorPos(1,2)
- old.write(string.rep("=",x))
- old.setCursorPos(1,y-1)
- old.write(string.rep("=",x))
- sleep(1)
- end
- end
- local function syncServer()
- while true do
- local r = {os.pullEvent("modem_message")}
- if r[3] == syncChannel then
- if r[5] == syncKey then
- local info = {}
- info["header"] = connectionHeader
- info["channel"] = tonumber(ch)
- info["identify"] = syncKey
- local m = {peripheral.find("modem")}
- for k,v in pairs(m) do
- v.transmit(syncChannel,syncChannel,textutils.serialise(info))
- end
- log("[LOG] Synchronised with a device " .. r[6] .. " blocks away")
- end
- end
- end
- end
- if not tArgs[1] then
- reload()
- local x,y = term.getSize()
- term.redirect(window.create(term.current(),1,3,x,y-2))
- parallel.waitForAny(inform,syncServer,mainServer)
- elseif tArgs[1] == "regen" then
- generate()
- else
- error("No.")
- end
Advertisement
Add Comment
Please, Sign In to add comment