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

Side argument for rednet.receive() (and the rednet_message event)

Started by glopso, 12 July 2012 - 09:50 PM
glopso #1
Posted 12 July 2012 - 11:50 PM
I'm planning on making cable internet on a server I'm playing on, with a tree system. Basically it would consist of computer "hubs" arranged in a tree, so that if a hub gets a message meant for a computer not on its system, it sends the message to its parent hub. When the hub is started, it checks for a connect hub to be a parent. To differentiate between parents and children, computers would be connected to their parents through the top surface, and any other surfaces could be use for children. If there is one found, it will store the id as the parent, and if none are found, it will have a nil parent and be at the highest level of its network. Currently, there is a way to implement it:

The computer is connected by its top to a parent, and by the bottom to a child.

print("requesting parent")
rednet.open("top")
rednet.broadcast("request_parent")
id, message = rednet.receive(2)
if message == "parent" then
print ("parent " .. id .." found")
parent = id
else
print ("parent not found, becoming top-level hub")
parent = nil
end

After that, it would ennumerate peripherals and activate all rednet interfaces for communication, including listing more hubs as children.

The parent computer would be on an endless loop processing requests, and if it receives a "request_parent" message it will store the sender to a table of child hubs.

The problem with this, however, is that children connecting cables to the same slot on the parent hub would communicate with each other, erronerously assigning each other as the parent, since they are connected to each other by the top. This means that a hub could at most have one parent and five children. This wouldn't really be too bad (computers aren't too expensive after all), but it would make it much less of a hassle to just be able to connect more. If sides were passed, then it would be possible to make hubs ignore parent requests from their top port, enabling them to be connected at the top before reaching the parent hub.
OmegaVest #2
Posted 13 July 2012 - 05:39 PM
What you need is a table that stores sides and a string. If parents[top] == parent then it will override the parents along the line. If parent, send, if not parent, wait three seconds, attempt request. If line is busy, wait until not busy. If two machines are taken online at the same time, and there are no other machines on the line, then have one randomly assign itself a parent. Say, math.random(1,10), highest is new parent.

Basically, I'm saying the tools to make this work are already available. You just have to look at it a different way. On the other hand, I do think that sides should entered into the event arguments. But I think it would just as well be used in the standard redstone version as well.