Guest User

Server

a guest
Jul 11th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. local modem = peripheral.wrap('back')
  2.  
  3.  
  4. function send(chan,reply,m)
  5.     modem.transmit(chan,reply,m)
  6. end
  7.  
  8. function get(chan,tmout)--listener
  9.     if tmout==nil then
  10.     tmout=100000000000000000000000000000 --change to call event w/o timer
  11.     end
  12.     modem.open(chan) --opens port
  13.     chk=modem.isOpen(chan) --checks to make sure it is open
  14.         if chk==true then --will set listener
  15.         --mm = os.pullEvent("modem_message")
  16.         timeout = os.startTimer(tmout)
  17.         while true do
  18.             event = {os.pullEvent()}
  19.             if event[1] == "modem_message" then
  20.                 --print'Worked'
  21.                 --print(event[1].." "..event[2].." "..event[3].." "..event[4].." "..event[5].." "..event[6])
  22.                 msg=event[5]
  23.                 returnchan=event[4]
  24.                 chan=event[3]
  25.                 break
  26.                 elseif event[1] == "timer" and event[2] == timeout then
  27.                 break
  28.             end
  29.         end
  30.  
  31.     end
  32.     if chk==false then
  33.         --send error# to screen
  34.         print('Error 100: Failed to Bind Port '..chan..'.')
  35.     end
  36. end
  37.  
  38. function listen()
  39. print'Listening...'
  40. while true do
  41. get(1)
  42. --print(msg)
  43. send(2,1,"ping")
  44. --sleep(.5)
  45. --os.reboot()
  46. end
  47.  
  48. end
  49. function test()
  50. print'Welcome to Server 1.'
  51. print'System online.'
  52. print'Please make a choice:'
  53. print'1: Shutdown'
  54. print'2: Reboot'
  55. print'3: Terminate'
  56.  
  57. ch=io.read()
  58. ch=ch+1-1
  59.  
  60. if ch==1 then
  61. print'Shutting down!'
  62. sleep(1)
  63. os.shutdown()
  64. end
  65.  
  66. if ch==2 then
  67. print'Rebooting!'
  68. sleep(1)
  69. os.reboot()
  70. end
  71.  
  72. if ch==3 then
  73. print'Terminating program!'
  74. sleep(1)
  75. term.clear()
  76. term.setCursorPos(1,1)
  77. end
  78.  
  79. end
  80.  
  81. parallel.waitForAny(test,listen)
Advertisement
Add Comment
Please, Sign In to add comment