D_Puppy

npinfo - showa infos about OC print server

Jun 6th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.26 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 < 1 then
  8.   print("usage : nptest <server> <v>")
  9.   print("Set v=true for verbose output")
  10.   return false
  11. end
  12.  
  13. local verbose = "false"
  14. if argv[2] then
  15.   verbose = argv[2]
  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.   print("connection timeout!")
  26.   return false
  27. end
  28.  
  29. if verbose == "true" then
  30.   print("Sending data to ".. argv[1])
  31. end
  32.  
  33. local message ={}
  34. message["command"] = "info"
  35. network.tcp.send(ch, ser.serialize(message))
  36.  
  37. ev, type, ch, msg, remoteAddr, port = event.pull(3, "tcp")  -- 3 seconds time out
  38. if ev == nil then
  39.   print("connection timeout!")
  40.   return false
  41. end
  42.  
  43. msgTable = ser.unserialize(msg)
  44. if msgTable.command == "info" then
  45.   print("Server      : " .. remoteAddr)
  46.   print("Version     : " .. msgTable.version)
  47.   print("Active jobs : " .. msgTable.jobs)
  48.   print("")
  49. end
  50.  
  51. if verbose == "true" then
  52.   print("Closing TCP connection (ch:" .. ch .. ").")
  53. end
  54. network.tcp.close(ch)
Add Comment
Please, Sign In to add comment