Advertisement
grimrandomer

Untitled

Mar 6th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. --Core
  2. function modemTimeout(time)
  3. local timeout = os.startTimer(time)
  4. while true do
  5. local ev, p1, p2, p3, p4 = os.pullEvent()
  6. if ev == 'timer' and p1 == timeout then
  7. return nil
  8. elseif ev == 'modem_message' then
  9. return p1, p2, p3, p4
  10. end
  11. end
  12. end
  13.  
  14. function string:split( inSplitPattern, outResults )
  15. if not outResults then
  16. outResults = { }
  17. end
  18. local theStart = 1
  19. local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  20. while theSplitStart do
  21. table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
  22. theStart = theSplitEnd + 1
  23. theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  24. end
  25. table.insert( outResults, string.sub( self, theStart ) )
  26. return outResults
  27. end
  28.  
  29. local myIP = 20
  30.  
  31. print("Loading com.grimnet.networking")
  32.  
  33. print("Connecting to modem")
  34. local modem = peripheral.wrap('right')
  35. modem.open(myIP)
  36.  
  37. if modem.isOpen(myIP) == true then
  38. print("Modem Ready")
  39. end
  40.  
  41. local cmd
  42. while cmd ~= "exit" do
  43. local raw_cmd = read()
  44. local cmd_param = raw_cmd:split(" ")
  45. cmd = cmd_param[0]
  46. if cmd == "exit" then
  47.  
  48. elseif cmd == "ping" then
  49. local i = read()
  50. local channel = tonumber(i)
  51. modem.transmit(channel, myIP, "ping")
  52. local mS,sC,rC,m = modemTimeout(2)
  53. if m then
  54. print("Reply from "..rC.." > "..m)
  55. else
  56. print("Message timed out after 2 seconds")
  57. end
  58. else
  59. print("Unknown command: " .. cmd)
  60. end
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement