Advertisement
bigflipperfish

NetCOM (TrashComp)

Jul 28th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | None | 0 0
  1. -------------------------------------------------------------------------------
  2. -- Filename: NetCOM.lua
  3. --
  4. -- Description:
  5. -- A small program that runs in the background to handle command packets
  6. -- received over the network using the "netCOM" protocol
  7. -------------------------------------------------------------------------------
  8.  
  9. local NAME = os.getComputerLabel()
  10. local ID = os.getComputerID()
  11. local NETWORK_PROTOCOL = "FishNET"
  12.  
  13. -------------------------------------------------------------------------------
  14. -- Broadcasts a command packet to all computers on the network (currenly not
  15. -- in use)
  16. --
  17. -- Arguments:
  18. -- command  - command to send
  19. -- name     - the name of the computer to send it to
  20. --
  21. -- Returns:
  22. -- ture   - if the command was successful
  23. -- false  - if the command was not successful within 2 seconds
  24. -------------------------------------------------------------------------------
  25.  
  26. local function sendNetCommand(command, name)
  27.   rednet.broadcast(
  28.   {
  29.     Type = "netCommand",
  30.     Command = command,
  31.     Name = name,
  32.     SenderID = ID,
  33.   }, "netCOM" )
  34. end
  35.  
  36. -------------------------------------------------------------------------------
  37. -- Waits to receive a command packet over the network using the "netCOM"
  38. -- protocol
  39. --
  40. -- Arguments:
  41. -- None
  42. --
  43. -- Returns:
  44. -- message.Command  - the command sent within the command packet
  45. -- senderID - the ID of the computer that sent the command packet
  46. -------------------------------------------------------------------------------
  47.  
  48. local function getNetCommand()
  49.   local senderID, message, protocol = rednet.receive(1)
  50.  
  51.   if protocol == "netCOM" and message.Type == "netCommand" then
  52.     if message.Name == NAME then
  53.       return message.Command, senderID
  54.     end
  55.   end
  56. end
  57.  
  58. -------------------------------------------------------------------------------
  59. -- Attempts to do the command that was sent in a command packet, then sends a
  60. -- response to the sender to tell it if it was successful or not
  61. --
  62. -- Arguments:
  63. -- None
  64. --
  65. -- Returns:
  66. -- Nothing
  67. -------------------------------------------------------------------------------
  68.  
  69. local function doNetCommand()
  70.   local command, senderID = getNetCommand()
  71.  
  72.   if command then
  73.     if shell.run("/programs/" .. command) then
  74.       print(command)
  75.       rednet.send(senderID, "success", NETWORK_PROTOCOL)
  76.     else
  77.       print("Invalid command... " .. command)
  78.       rednet.send(senderID, "invalid", NETWORK_PROTOCOL)
  79.     end
  80.   end
  81. end
  82.  
  83. -------------------------------------------------------------------------------
  84. -- Waits for redstone input from pressure plate, then sends a command to
  85. -- Tornado to collect trash
  86. --
  87. -- Arguments:
  88. -- none
  89. --
  90. -- Returns:
  91. -- Nothing
  92. -------------------------------------------------------------------------------
  93.  
  94. local function getRedstoneInput()
  95.   if redstone.getInput("right") == true then
  96.     sendNetCommand("CollectTrash", "Tornado")
  97.   end
  98. end
  99.  
  100. -------------------------------------------------------------------------------
  101.  
  102. local function main()
  103.   while true do
  104.     doNetCommand()
  105.     getRedstoneInput()
  106.   end
  107. end
  108.  
  109. -------------------------------------------------------------------------------
  110.  
  111. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement