Posted 26 March 2012 - 12:02 AM
I've been working on a little program that allows me to control computers wirelessly from computers with specific IDs (Basically my computers only), and I've got it working quite well, except I would like to stop CTRL+T from terminating my exposed computer that I wish to be controlled only via rednet.
Code to send commands:
Code to receive commands:
This works great, except it can be terminated. To fix this, I changed line 32 of the receive program like so:
I then proceeded to run my receive code, then ran my send code, send "exit" to ID 6, which was what was running my receive code, and instead of existing the receive code like it did right before when I did the exact same with without pcall, it did not affect the computer running receive, and the computer running send send my print statement saying, "Confirmation timed out.".
Also, I can still terminate my receive code via CTRL+T, so it didn't help with that, and it broke my code, so my first question is: How can I modify my code so it can't be terminated via CTRL+T.
I also have two misc. questions, they have nothing to do with the above, but I figured I'd bundle them here for now, and ask in their own thread if no one mentions them.
Q2: Would it be possible to type "send(6, "Message.") instead of: "send" <Enter> "6" <Enter> "Message." <Enter>? I know I can do this with an API, but I don't want to have to go into the lua shell every time.
Q3: Is there a way I can use io.read() for 5 seconds and if it hasn't read anything in, just move on?
Anyway, thanks for reading, and if you have anything beneficial to share, please do.
~Vorsaykal
Code to send commands:
io.write("ID to send to: ")
ID = tonumber(io.read())
io.write("\n")
io.write("Command to send to "..ID..": ")
comm = io.read()
io.write("\n")
rednet.send(ID, comm)
conID, re = rednet.receive(2)
if re == nil then
io.write("Confirmation timed out.\n")
else
io.write(conID..": "..re.."\n)
end
Code to receive commands:
function verifyID(ID)
IDs = fs.open("whitelistedIDs", "r")
inID = tonumber(IDs.readLine())
while inID ~= nil do
if inID == ID then
return true
end
inID = tonumber(IDs.readLine())
end
end
function doComm(ID, comm, arg)
if comm == "exit" then
rednet.send(ID, "Terminated.")
return "exit"
elseif comm == "redstoneOn" then
rednet.send(ID, arg.." on.")
redstone.setOutput(arg, true)
elseif comm == "redstoneOff" then
rednet.send(ID, arg.." off.")
redstone.setOutput(arg, true)
elseif comm == "print" then
rednet.send(ID, "Message displayed.")
print(ID.." says, \""..arg.."\"")
else
rednet.send(ID, "ERROR: Unknown command.")
end
end
while true do
ID, msg = --[[pcall(]]rednet.receive()--)
space = string.find(msg, " ")
if space ~= nil then
comm = string.sub(msg, space + 1)
else
comm = msg
end
if verifyID(ID) == true then
if doComm(ID, comm, arg) == "exit" then
return
end
end
end
This works great, except it can be terminated. To fix this, I changed line 32 of the receive program like so:
ID, msg = rednet.receive()
to
ID, msg = pcall(rednet.receive())
I then proceeded to run my receive code, then ran my send code, send "exit" to ID 6, which was what was running my receive code, and instead of existing the receive code like it did right before when I did the exact same with without pcall, it did not affect the computer running receive, and the computer running send send my print statement saying, "Confirmation timed out.".
Also, I can still terminate my receive code via CTRL+T, so it didn't help with that, and it broke my code, so my first question is: How can I modify my code so it can't be terminated via CTRL+T.
I also have two misc. questions, they have nothing to do with the above, but I figured I'd bundle them here for now, and ask in their own thread if no one mentions them.
Q2: Would it be possible to type "send(6, "Message.") instead of: "send" <Enter> "6" <Enter> "Message." <Enter>? I know I can do this with an API, but I don't want to have to go into the lua shell every time.
Q3: Is there a way I can use io.read() for 5 seconds and if it hasn't read anything in, just move on?
Anyway, thanks for reading, and if you have anything beneficial to share, please do.
~Vorsaykal