Advertisement
osmarks

Printron Clientinator

Aug 14th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.31 KB | None | 0 0
  1. local m = peripheral.find "modem"
  2.  
  3. local log_channel, job_channel = 404, 1337
  4.  
  5. m.open(log_channel)
  6.  
  7. local function randbytes(len)
  8.     local out = ""
  9.     for i = 1, len do
  10.         out = out .. string.char(math.random(1, 255))
  11.     end
  12.     return out
  13. end
  14.  
  15. local function fread(n)
  16.     local f = fs.open(n, "r")
  17.     local c = f.readAll()
  18.     f.close()
  19.     return c
  20. end
  21.  
  22. local function run(text, title)
  23.     local done_sending = false
  24.     local done_printing = false
  25.  
  26.     local function recv()
  27.         while not done_printing do
  28.             local _, _, chan, reply_chan, message = os.pullEvent "modem_message"
  29.             if chan == log_channel and reply_chan == job_channel then
  30.                 if type(message) == "string" then print(message) end
  31.                 if type(message) == "table" and message.type then
  32.                     local mtype = message.type
  33.                     if mtype == "job_starting" then done_sending = true end
  34.                     if mtype == "job_complete" then done_printing = true end
  35.                 end
  36.             end
  37.         end
  38.     end
  39.  
  40.     local function send()
  41.         while not done_sending do
  42.             m.transmit(job_channel, log_channel, {text = text, title = title})
  43.             sleep(2)
  44.         end
  45.     end
  46.  
  47.     parallel.waitForAll(send, recv)
  48. end
  49.  
  50. if shell.getRunningProgram() == "print" then
  51.     local filename, title = ...
  52.     local text = fread(filename)
  53.     title = title or randbytes(8)
  54.     run(text, title)
  55. else
  56.     return run
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement