Yep, you're right - for reasons that aren't clear to me, on receiving a modem_message event that forms a lookup request, instead of just
answering it the rednet.run function re-queues it as a rednet_message event and then pulls and answers
that.
Because the lookup is being handled in its own co-routine, rednet.run() doesn't prevent the rednet.receive() call in the server script from pulling the lookup request message as well. Since the server script is only rigged to accept
one message, the "ping" message from the client goes off into the void.
Thus this version of the server script should do the trick:
local modemSide, id, message, protocol = "top"
rednet.open(modemSide)
rednet.host("bank", "bankServer")
repeat
id, message, protocol = rednet.receive() -- Keep pulling rednet messages...
until message == "ping" -- ... until we get the one we want.
print("received ping from ID: " .. id)
rednet.send(id, "pong")
rednet.unhost("bank", "bankServer")
rednet.close(modemSide)
Edit:
The only reason I can think of for Dan to create these "useless" lookup-related rednet_message events is to specifically create this sort of scenario: where scripts that don't specifically check that messages they receive are intended for them, will bug out.
With the same update he added the
protocol system, which is basically a method of filtering such messages - maybe he was trying to encourage its use? Dunno.
In any case, let's say you wanted to use a protocol to deal with this. Your code would look something like so:
Client:
local modemSide = "top"
local bprotocol = "bankServer"
rednet.open(modemSide)
local serverID = rednet.lookup(bprotocol, "bank")
print("sending ping to server ID: " ..serverID)
rednet.send(serverID, "ping", bprotocol) -- Message sent with "bankServer" protocol
print("Waiting for server pong...")
local id, message = rednet.receive(bprotocol) -- Messages without the "bankServer" protocol will be ignored
if message == "pong" then
print("established server connection!")
else
print("could not establish connection with host")
end
--load main screen
Server:
local modemSide, id, message = "top"
local bprotocol = "bankServer"
rednet.open(modemSide)
rednet.host(bprotocol, "bank")
while true do -- 'cause why not?
id, message = rednet.receive(bprotocol)
if message == "ping" then
print("Received ping from ID: " .. id)
rednet.send(id, "pong", bprotocol)
else
print("Received odd message from ID: " .. id)
print("It said:")
print(message)
end
end
rednet.unhost(bprotocol, "bank")
rednet.close(modemSide)