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

Rednet Relay

Started by dexter9, 06 April 2013 - 10:25 PM
dexter9 #1
Posted 07 April 2013 - 12:25 AM
Hi, basically i want to control my little mining turtles which are mining everywhere in minecraft. Having one computer at my house with a modem is not enough to contact the turtle so i decided to make sattelites. I basically ask the client :
Id:
Message:
this is sent to the server and the server looks at the id and forwards it to the cooresponding turtle. My problem is how do i get the turtle to recognise the id. The client does "pc = read()" and pc is sent to the server. The server then does "rednet.send(pc, msg). How do i get it to recognise the id?
remiX #2
Posted 07 April 2013 - 01:02 AM
When you receive a rednet message, you will need to check the senderID to check if it's a legit ID from the client/server.'
From there, the message must be in a way where you can split it into the client/id/message, like so:

Server code
Spoiler
-- Variables
local turtleIDs = { ['3'] = true, ['6'] = true } -- table with all the turtle IDs that are in use for the program

function split(str, divider)
	local parts = {}
	for v in str:gmatch('[^'..divider..']+') do
		table.insert(parts, v)
	end
	return parts
end

local function receiveRednet()
	local sID, msg, temp
	repeat
		sID, msg = rednet.receive()
		temp = split( msg, '_' )
		-- what a client pc could send in a rednet message
		-- can be something like this for easy use:
		-- 'client_turtleid_message'
		-- temp[1] will be client
		-- temp[2] will be the id of the turtle
		-- temp[3] will be the message
	until turtleIDs[temp[2]] -- so it will keep doing it until a message is received that has a legit turtle ID
	-- okay, so legit turtle id detected, lets forward the message
	-- to the turtle
	-- but first return the variables
	return { client = temp[1], turtleid = temp[2], msg = temp[3] } -- returns a table with the correct details
end

while true do -- infinite loop
	term.clear()
	term.setCursorPos(1,1)
	print('Waiting for a rednet response...')
	local info = receiveRednet()
	-- If it got here, then there is a legit turtle id
	-- now let's send the message to the turtle
	local s = info[1] .. '_' .. info[3] -- string to send, separated with a _ for splitting, format: 'client_message'
	rednet.send(tonumber(info[2]), s) -- sends the string to the turtle
end

Client code
Spoiler
-- Variables
local turtleIDs = { ['3'] = true, ['6'] = true } -- table with all the turtle IDs that are in use for the program
local serverID = 1 -- the id of the server
-- you could just have the server to just respond success or failure,
-- but that means more rednet is involved ...

local function sWrite( str, x, y )
	term.setCursorPos( x, y )
	write( str )
end

local function getValue( x, y )
	term.setCursorPos( x, y )
	return read()
end

while true do
	term.clear()
	sWrite( '  Client: ', 1, 1 )
	sWrite( 'TurtleID: ', 1, 2 )
	sWrite( ' Message: ', 1, 3 )
	local client = getValue( 11, 1 ) .. '[' .. os.getComputerID() .. ']' -- name with the computer's ID in brackets at the end
	local turtleID = getValue( 11, 2 )
	local message = getValue( 11, 3 )
	if turtleIDs[turtleID] then -- check if it is a valid turtle id
		print( '\nSuccess!')
		local s = client .. '_' .. turtleID .. '_' .. message
		rednet.send( serverID, s )
	else
		print( '\nInvalid Turtle ID!')
	end
	sleep(1.3)
end

Untested but I can't see any obvious mistakes :P/>…
Added as many comments as I could.
Smiley43210 #3
Posted 07 April 2013 - 01:44 AM
Or couldn't you just use the modem API, where the client ID is really the channel it is sent on?
remiX #4
Posted 07 April 2013 - 02:06 AM
He could do that too. I've never worked with the new rednet modem api stuff, no clue how it works :P/>
dexter9 #5
Posted 07 April 2013 - 11:15 AM
Sorry i dont think i made myself clear. I have everything written programmed into the computer. I have a client, a server and a client(receiving). The client sends 2 rednet messages one called pc(which was the id by using pc = read()) and another called msg. The server has to translate pc into the actual id and send it to the receiving client. I know how to reply. just rednet.send(id). I just need that 1 simple line (or more).
Thanks
Smiley43210 #6
Posted 09 April 2013 - 05:42 PM
Not quite sure what your question is then. Are you having trouble getting the client to recognize the ORIGINAL sender's id? If so, look at remiX's response up a few posts. If that's not your question, since I don't know what you're asking yet, I would suggest having the client set to receive two messages, the message and the original id. Like a double chain.

Msg 1: "This is a message"
Msg 2: 4 (The original sender's id)

Then the sending:
Client (Sender) –Msg 1–> Server 1 –Msg 1–> Server 2 –Msg 1–> Server 3 –Msg 1–> Client (Receiver)
Client (Sender) –Msg 2–> Server 1 –Msg 2–> Server 2 –Msg 2–> Server 3 –Msg 2–> Client (Receiver)

And so the receiver gets both messages, interprets the first message as the message itself, and the second as the id of the original sender.

But if that doesn't help, my apologies, and clarification on what you need would be appreciated.