Guest User

rrp-server.lua

a guest
Jul 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. local tArgs = { ... }
  2. local old = term.current()
  3. local ch = nil
  4. local rsSide = "top"
  5. local connectionHeader = nil
  6. local syncKey = nil
  7. local syncChannel = 65000
  8. local function log(msg)
  9.     print("[" .. textutils.formatTime(os.time()) .. "] " .. msg)
  10. end
  11. local function reload()
  12.     local handle = fs.open("/.rrp","r")
  13.     ch = tonumber(handle.readLine())
  14.     connectionHeader = handle.readLine()
  15.     syncKey = handle.readLine()
  16.     handle.close()
  17.    
  18.     local m = {peripheral.find("modem")}
  19.     for k,v in pairs(m) do
  20.         v.open(ch)
  21.         v.open(syncChannel)
  22.     end
  23. end
  24.  
  25. local function generate()
  26.     print("Generating keys...")
  27.     local channel = math.random(1024,43600)
  28.     local header = "["
  29.     for i=1,20 do
  30.         header = header .. string.char(math.random(65,89))
  31.     end
  32.     header = header .. "]"
  33.     local key = "R"
  34.     for i=1,4 do
  35.         key = key .. string.char(math.random(97,117))
  36.     end
  37.     for i=1,6 do
  38.         key = key .. math.random(0,9)
  39.     end
  40.     print("Channel: " .. channel)
  41.     print("Header: " .. header)
  42.     print("Sync key: " .. key)
  43.     print("Saving...")
  44.     local handle = fs.open("/.rrp","w")
  45.     handle.writeLine(tostring(channel))
  46.     handle.writeLine(tostring(header))
  47.     handle.writeLine(tostring(key))
  48.     handle.close()
  49. end
  50.  
  51. local function mainServer()
  52.     while true do
  53.         local r = {os.pullEvent("modem_message")}
  54.         if r[3] == ch then
  55.             if string.find(r[5],connectionHeader) then
  56.                 if string.sub(r[5],#connectionHeader + 1) == "on" then
  57.                     rs.setOutput(rsSide,true)
  58.                     log("[LOG] Received ENABLE signal")
  59.                 end
  60.                 if string.sub(r[5],#connectionHeader + 1) == "off" then
  61.                     rs.setOutput(rsSide,false)
  62.                     log("[LOG] Received DISABLE signal")
  63.                 end
  64.             end        
  65.         end
  66.     end
  67. end
  68. local function inform()
  69.     while true do
  70.         local x,y = old.getSize()
  71.         old.setCursorPos(1,y)
  72.         old.write("Sync key: " .. syncKey)
  73.         old.setCursorPos(1,1)
  74.         old.write("Remote redstone by Rahph ")
  75.         local timestring = textutils.formatTime(os.time())
  76.         old.setCursorPos(x-#timestring-1,1)
  77.         old.write(timestring)
  78.         old.setCursorPos(1,2)
  79.         old.write(string.rep("=",x))
  80.         old.setCursorPos(1,y-1)
  81.         old.write(string.rep("=",x))
  82.         sleep(1)
  83.     end
  84. end
  85. local function syncServer()
  86.     while true do
  87.         local r = {os.pullEvent("modem_message")}
  88.         if r[3] == syncChannel then
  89.             if r[5] == syncKey then
  90.                 local info = {}
  91.                 info["header"] = connectionHeader
  92.                 info["channel"] = tonumber(ch)
  93.                 info["identify"] = syncKey
  94.                 local m = {peripheral.find("modem")}
  95.                 for k,v in pairs(m) do
  96.                     v.transmit(syncChannel,syncChannel,textutils.serialise(info))
  97.                 end
  98.                 log("[LOG] Synchronised with a device " .. r[6] .. " blocks away")
  99.             end
  100.         end
  101.     end
  102. end
  103. if not tArgs[1] then
  104. reload()
  105. local x,y = term.getSize()
  106. term.redirect(window.create(term.current(),1,3,x,y-2))
  107. parallel.waitForAny(inform,syncServer,mainServer)
  108. elseif tArgs[1] == "regen" then
  109. generate()
  110. else
  111. error("No.")
  112. end
Advertisement
Add Comment
Please, Sign In to add comment