SHARE
TWEET

RednetTerminal

osmarks Jul 26th, 2018 (edited) 200 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local m = peripheral.find "modem"
  2.  
  3. local function prompt(what, default)
  4.     write(what .. "> ")
  5.     local result = read()
  6.     if result == "" then
  7.         print("Defaulted to", textutils.serialise(default))
  8.         return default
  9.     end
  10.     return result
  11. end
  12.  
  13. local protocol = prompt("Protocol", nil)
  14. local sender = tonumber(prompt("Sender", os.getComputerID()))
  15. local recipient = tonumber(prompt("Recipient", rednet.CHANNEL_BROADCAST))
  16.  
  17. m.open(rednet.CHANNEL_REPEAT)
  18.  
  19. local function send(from, protocol, content, to)
  20.     local packet = {
  21.         nMessageID = math.random( 1, 2147483647 ),
  22.         nRecipient = to,
  23.         message = content,
  24.         sProtocol = protocol,
  25.     }
  26.  
  27.     m.transmit(to, from, packet)
  28.     m.transmit(rednet.CHANNEL_REPEAT, from, packet)
  29. end
  30.  
  31. local function receive()
  32.     local _, _, chan, replyChan, message = os.pullEvent "modem_message"
  33.     return message.nRecipient, replyChan, message.message, message.sProtocol
  34. end
  35.  
  36. local function recvLoop()
  37.     while true do
  38.         local recipient, sender, message, protocol = receive()
  39.         local text = sender .. ">" .. recipient
  40.         if protocol then text = text .. " (" .. protocol .. ")" end
  41.         text = text .. " " .. textutils.serialise(message)
  42.         print(text)
  43.     end
  44. end
  45.  
  46. local function sendLoop()
  47.     while true do
  48.         write "|> "
  49.         local msg = read()
  50.         send(sender, protocol, msg, recipient)
  51.     end
  52. end
  53.  
  54. parallel.waitForAll(sendLoop, recvLoop)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top