Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------------------------
- -- Filename: NetCOM.lua
- --
- -- Description:
- -- A small program that runs in the background to handle command packets
- -- received over the network using the "netCOM" protocol
- -------------------------------------------------------------------------------
- local NAME = os.getComputerLabel()
- local ID = os.getComputerID()
- local NETWORK_PROTOCOL = "FishNET"
- -------------------------------------------------------------------------------
- -- Broadcasts a command packet to all computers on the network (currenly not
- -- in use)
- --
- -- Arguments:
- -- command - command to send
- -- name - the name of the computer to send it to
- --
- -- Returns:
- -- ture - if the command was successful
- -- false - if the command was not successful within 2 seconds
- -------------------------------------------------------------------------------
- local function sendNetCommand(command, name)
- rednet.broadcast(
- {
- Type = "netCommand",
- Command = command,
- Name = name,
- SenderID = ID,
- }, "netCOM" )
- end
- -------------------------------------------------------------------------------
- -- Waits to receive a command packet over the network using the "netCOM"
- -- protocol
- --
- -- Arguments:
- -- None
- --
- -- Returns:
- -- message.Command - the command sent within the command packet
- -- senderID - the ID of the computer that sent the command packet
- -------------------------------------------------------------------------------
- local function getNetCommand()
- local senderID, message, protocol = rednet.receive(1)
- if protocol == "netCOM" and message.Type == "netCommand" then
- if message.Name == NAME then
- return message.Command, senderID
- end
- end
- end
- -------------------------------------------------------------------------------
- -- Attempts to do the command that was sent in a command packet, then sends a
- -- response to the sender to tell it if it was successful or not
- --
- -- Arguments:
- -- None
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function doNetCommand()
- local command, senderID = getNetCommand()
- if command then
- if shell.run("/programs/" .. command) then
- print(command)
- rednet.send(senderID, "success", NETWORK_PROTOCOL)
- else
- print("Invalid command... " .. command)
- rednet.send(senderID, "invalid", NETWORK_PROTOCOL)
- end
- end
- end
- -------------------------------------------------------------------------------
- -- Waits for redstone input from pressure plate, then sends a command to
- -- Tornado to collect trash
- --
- -- Arguments:
- -- none
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function getRedstoneInput()
- if redstone.getInput("right") == true then
- sendNetCommand("CollectTrash", "Tornado")
- end
- end
- -------------------------------------------------------------------------------
- local function main()
- while true do
- doNetCommand()
- getRedstoneInput()
- end
- end
- -------------------------------------------------------------------------------
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement