Advertisement
KnightMiner

[CC] wireless

Jul 12th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. -- get args
  2. local args = { ... }
  3.  
  4. -- validate args
  5. if #args < 1 or #args > 2 or args[1] == "usage" then
  6.   print( "Usage: wireless <port> [label]" )
  7.   return
  8. end
  9.  
  10. local label = args[2] or os.getComputerLabel()
  11. local done
  12.  
  13. rednet.open( args[1] )
  14. rednet.host( "wireless", label )
  15.  
  16. -- modify the functions in the term library
  17. -- to allow messages to be collected
  18. local messages = {}
  19. local termLocal
  20. for k,v in pairs(term) do
  21.   -- "term.redirect()" causes issues
  22.   -- plus add support to print text without wirelessly sending it
  23.   if type(v) == "function" and k ~= "redirect" then
  24.     term[k] = function(...)
  25.       if not termLocal then
  26.         table.insert( messages, {
  27.           type = k,
  28.           args = { ... }
  29.         } )
  30.       end
  31.       return v( ... )
  32.     end
  33.   end
  34. end
  35.  
  36. -- function to receive commands
  37. function receive()
  38.   local sender, data = rednet.receive( "remote" )
  39.   if data and data.name == label then
  40.     termLocal = true
  41.     print( 'Running "' .. data.args[1] .. '"' )
  42.     termLocal = nil
  43.     shell.run( unpack( data.args ) )
  44.     rednet.send( sender, messages, "remoteResult" )
  45.     messages = {}
  46.   end
  47.   os.sleep( 1 )
  48. end
  49.  
  50. -- function to check for pressing "end"
  51. function shutdown()
  52.   while true do
  53.     local _, key = os.pullEvent( "key" )
  54.     if key == keys["end"] then
  55.       done = true
  56.       break
  57.     end
  58.   end
  59. end
  60.  
  61. print( "Wireless mode activated" )
  62. print( 'Press "End" to exit' )
  63.  
  64. messages = {}
  65.  
  66. -- main loop
  67. while not done do
  68.   parallel.waitForAny( receive, shutdown )
  69. end
  70.  
  71. -- shutdown process
  72. rednet.unhost( "wireless", label )
  73. rednet.close( args[1] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement