Advertisement
D_Puppy

nprint client for OC print server

Jun 6th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.33 KB | None | 0 0
  1. local argv = {...}
  2. local component = require("component")
  3. local event = require("event")
  4. local ser = require("serialization")
  5. local network = require ("network")
  6.  
  7. if #argv < 2 then
  8.   print("usage : nprint <server address> <file> <v>")
  9.   print("    set 'v' to true for output")
  10.   return false
  11. end
  12. local fileName = argv[2]
  13. local verbose = "false"
  14. if argv[3] then
  15.   verbose = argv[3]
  16. end
  17.  
  18. if verbose == "true" then
  19.   print("Opening connection to " .. argv[1])
  20. end
  21. network.tcp.open(argv[1], 9100)
  22.  
  23. local ev, msg, ch, remoteAddr, port = event.pull(3, "tcp")  -- 3 seconds time out
  24. if ev == nil then
  25.   if verbose == "true" then
  26.     print("connection timeout!")
  27.   end
  28.   return false
  29. end
  30.  
  31. if verbose == "true" then
  32.   print("Reading file " .. fileName)
  33. end
  34. local data = {}
  35. local i = 0
  36. local file = io.open(fileName, "r")
  37. while true do
  38.   line = file:read()
  39.   if line == nil then
  40.     break
  41.   else
  42.     i = i + 1
  43.     data[i] = line
  44.   end
  45. end
  46. file:close()
  47.  
  48. if verbose == "true" then
  49.   print("Sending data to ".. argv[1])
  50. end
  51. local message ={}
  52. message["command"] = "print"
  53. message["filename"] = fileName
  54. message["data"] = ser.serialize(data)
  55. message["count"] = i
  56. network.tcp.send(ch, ser.serialize(message))
  57.  
  58. if verbose == "true" then
  59.   print("Closing TCP connection (ch:" .. ch .. ").")
  60. end
  61. network.tcp.close(ch)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement