Advertisement
1lann

print-host

Nov 23rd, 2012
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. print("Beta version of printer hosting made by 1lann")
  2.  
  3. local modemPresent = false
  4.  
  5. for k,v in pairs(rs.getSides()) do
  6.     if peripheral.getType(v) == "modem" then
  7.         modemPresent = true
  8.         rednet.open(v)
  9.         break
  10.     end
  11. end
  12.  
  13. if not modemPresent then print("No modem found") error() end
  14.  
  15. local printerPresent = false
  16. local printerMount = nil
  17.  
  18. for k,v in pairs(rs.getSides()) do
  19.     if peripheral.getType(v) == "printer" then
  20.         printerPresent = true
  21.         printerMount = v
  22.         break
  23.     end
  24. end
  25.  
  26. if not printerPresent then print("No printer found") error() end
  27.  
  28. local functionsAvail = {"write","setCursorPos","getCursorPos","getPageSize","newPage","endPage","getInkLevel","setPageTitle","getPaperLevel"}
  29.  
  30. local function possibleFunction(func)
  31.     for k,v in pairs(functionsAvail) do
  32.         if v == func then return true
  33.         end
  34.     end
  35.     return false
  36. end
  37.  
  38. print("Network printer: Ready")
  39. print("ID: " .. os.getComputerID())
  40. while true do
  41.     local id,msg = rednet.receive()
  42.     if msg == "printerConnect" then
  43.         print("ID: ".. id .. " connected to the printer")
  44.         rednet.send(id, "printerConnectSuccess")
  45.     else
  46.         local processing = textutils.unserialize(msg)
  47.         if processing then
  48.             if processing[1] == "callFunction" and #processing > 1 then
  49.                 if processing[2] == "setCursorPos" then
  50.                     local response = {peripheral.call(printerMount, "setCursorPos", processing[3], processing[4])}
  51.                     rednet.send(id, textutils.serialize(response))
  52.                     print("ID: " .. id .. " did " .. processing[2])
  53.                 else
  54.                     if possibleFunction(processing[2]) then
  55.                         local response = {peripheral.call(printerMount, processing[2], processing[3], processing[4], processing[5], processing[6], processing[7])}
  56.                         if #response == 0 then
  57.                             rednet.send(id, "nil")
  58.                             print("ID: " .. id .. " did " .. processing[2])
  59.                         else
  60.                             rednet.send(id, textutils.serialize(response))
  61.                             print("ID: " .. id .. " did " .. processing[2])
  62.                         end
  63.                     else
  64.                         print("ID: " .. id .. " did an unvalid action")
  65.                     end
  66.                 end
  67.             end
  68.         end
  69.     end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement