This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GodlyPlexi's profile picture

Rednet Computer Pairing

Started by GodlyPlexi, 25 April 2016 - 10:05 PM
GodlyPlexi #1
Posted 26 April 2016 - 12:05 AM
Alright, so, I'm trying to use a program to pair other computers to a host computer via rednet, and then have them do things (specifically in this case, activate a redstone signal.) Right now, as my code is, I start the computer(s) I want to pair, let them wait in a loop, and start the host program on the host computer. It sends the pairing message through to the clients, but when I try to read it on the host when they send it back, it doesn't see any of the messages, resulting in no pairing. I store the id's of the paired computers locally on the host program in a table, so they become un-paired after the program resolves. If anyone could help, I would be full of gratitude for you.

Host program:

rnModem = "top"
local auth = math.floor(math.random(1,10000))

if rednet.isOpen(rnModem) then
rednet.broadcast(auth)
end

local clientTab = {}
os.startTimer(10)

while true do
event, id, msg, _ = os.pullEvent()
if msg == auth then
  for i = 1,#clientTab do
   if not id == clientTab[i] and not clientTab[i] == nil then
	tI = #clientTab + 1
	clientTab[tI] = id
   else
	clientTab[1] = id
   end
  end
elseif event == "timer" then
  break
end
end
print("There are " ..#clientTab.. " paired computers.")

Client Program

for _,side in pairs(redstone.getSides()) do
if peripheral.getType(side) == "modem" then
  if not rednet.isOpen() then rednet.open(side) end
end

end
while true do
local event, id, msg = os.pullEvent()
if event == "rednet_message" and id == 0 then
  mNumb = msg
  break
end
end

print("Aquired key")
os.startTimer(10)
print("Set timer, sending message")
while true do
  event = os.pullEvent()
  rednet.broadcast(mNumb)
   if event == "timer" then print("Timeout") break end
end

Any and all help would be appreciated. Thanks!
Bomb Bloke #2
Posted 26 April 2016 - 02:15 AM
I notice the host machine never calls rednet.open()?

This line here in the server code won't do what you're expecting:

if not id == clientTab[i] and not clientTab[i] == nil then

"not id" resolves as true if id is undefined, and false if it is - that'll never match clientTab. You meant to do:

if id ~= clientTab[i] and clientTab[i] ~= nil then

… although, the nature of your loop is already such that clientTab will never be nil.

You've also got a problem in that you add each client to the table an amount of times equal to the number of entries in the table that don't match it. Really what you want is this:

if msg == auth then
  local found = false
  for i = 1,#clientTab do
   if id == clientTab[i] then
     found = true
     break
   end
  end
  if not found then clientTab[#clientTab + 1] = id end
elseif event == "timer" then ...

Your client is also waiting 10 seconds after receiving the server's broadcast before sending a reply back, which happens to be the same amount of time the server waits for a reply before giving up. That's going to cause problems too.

If you're on CC 1.6 or later, then you can use the lookup functionality built into the rednet API specifically for this type of task:

Client:
peripheral.find("modem", rednet.open)  --# Opens all rednet sides.

rednet.host("myService", os.getComputerLabel())  --# Or use tostring(os.getComputerID()) if you can't be bothered uniquely labelling all your systems.

local serverID
repeat serverID = rednet.lookup("myService", "theServer") until serverID

print("I found out that the server has an ID of "..tostring(serverID)..".")

Server:
peripheral.find("modem", rednet.open)  --# Opens all rednet sides.

rednet.host("myService", "theServer")

sleep(5) --# Give the clients leeway to boot and register themselves.

local clientTab = {rednet.lookup("myService")}

print("There are " ..#clientTab.. " paired computers.")
GodlyPlexi #3
Posted 28 April 2016 - 01:35 AM
Oh, I didn't know lookup was what that was used for, but then again, I didn't quite look into it that much. Thank you! And also thank you for helping me with the non-lookup script too. :)/>