Code:
@name Router
@inputs Group:string
@outputs
@persists Count OpenPort
#It's important to recognise that any variable name passed to the router will result in the string *and* numerical data being passed, with the exception of the string being "" or the number being 0
#It's also important to remember that the data on the network cannot be cleared because we must assume that more than 2 computers are on the network and thus 2 or more can access the same data. That means we don't clear data, so do everyone a favor, don't clutter.
#It's important to recognise that you cannot send data out to a specific computer - if a module needs to send data only to a set of computers, let it create it's own network, and save the one we were last on, let it do what it has to on it's own network, and restore itself to the old one when done.
#Not all data will always pass through, if you have enough programs trying to use the same network and enough computers reading from it, it can clog the router by writing to ports that should be open but arn't, that means that it's not unwise to send data *back* to indicate that you *did* receive the values you wanted. Just like a real router, not every packet will make it through, most will, some may not. If you know that you're using lots of computers on the network, set the NetSpeed lower (faster) to help fix this issue.
#Data that does pass through will always be registered. That means that even data you send *to* the network will come back to you. For this reason, I advise against sending global variables you intend to keep because the data will bounce back into the port, even if you updated it.
#NetOut variables - There are 25 NetOut variables, they reset themselves automatically, but they allow you to tell the network what data you're sending out by the (global) variable's name. When you set the data to send, you also set the NetOut to send it on. You shouldn't need to send 25 pieces of data at once, but if you do, adjust the clock with NetSpeed
if(Group != "") #Has the computer assigned itself a group
{
gSetGroup(Group)
Network = gGetStr("Network") #The network name is set by other computer modules
NetSpeed = gGetNum("NetSpeed") #Allows modules to set the update rate for internet data, some need more than others
if(NetSpeed == 0) { NetSpeed = 100, gSetNum("NetSpeed", 100) } #Default value is high to account for the 25 ports, it can be sped up for faster data transfer
interval((Network != "" ? NetSpeed : 200)) #Run at the normal pace of 200 miliseconds to reduce lag until a speed has been set from within the computer or the network group has been named
if(Network != "")
{
Count++
if(Count > 25) { Count = 0 } #Never reads in as 0 because of the above line
#For setting data to the network, we need to tell the network what NetOut to put it on for computers reading from the network. We cannot assign it at random, and it doesn't really matter which port it goes on, but we do need to ensure that it is free.
#NetOut allocation managed here
gSetGroup(Network) #Set ourselves to modify the network
NetOutStr = "NetOut", FindOpen = NetOutStr + toChar(Count) #Build a port number
if(gGetStr(NetOutStr + toChar(OpenPort)) != "" | OpenPort == 0) #If the port we think is open isn't or we don't have one that we think is open, try and find out that is...open
{
if(gGetStr(NetOutStr + toChar(Count)) != "") { OpenPort = Count } #If the port of our count variable is open, take it, else, wait for the next time around.
else { OpenPort = 0 } #Ensure that the value *is* actually 0 so we don't write to it later
}
#There is no built in clearing of the NetOut ports that are *on the network* because multiple computers can receive the data. Instead there is a NetClrPort global variable to clear ports on the network. If you're sending from one computer to only one other, this can clear it easily, if you're sending to more than one computer, add a variable to keep track of how many computers receive and *use* the data, when the count is high enough, clear it.
#NetClrPort - port clears handled here
gSetGroup(Group) #Set to the computer to get the next value
ClrPort = gGetNum("NetClrPort")
if(ClrPort != 0)
{
gSetNum("NetClrPort", 0) #Ensure that the port is reset for the next value
gSetGroup(Network) #Set ourselves to modify the network
ClrPortName = "NetOut", ClrPortName += toChar(ClrPort)
gSetStr(ClrPortName, "")
}
#Passing data handled below
gSetGroup(Network) #Set ourselves to access the network
Test = "NetOut", Test += toChar(Count) #The index to check, NetOut is a global array numbered 1-25 which holds the name of the variable to send, if you need to send more than 25 pieces of data, you're doing something wrong.
DataName = gGetStr(Test)
if(DataName != "") #Found incoming data in this index
{
StrData = gGetStr(DataName) #Get the string data at the name given to us
NumData = gGetNum(DataName) #Get the numerical data associated with the name given
gSetGroup(Group) #Set ourselves to modify the computer's memory
if(StrData != "") #We assume that whatever module gets the data sets it to "" when done with it, thus if it's trying to pass "", it's not necessary to actually pass it
{
gSetStr(DataName, StrData) #Sets the variable with the same name and same data as the on the Network
}
if(NumData != 0) #We assume that whatever module gets the data sets it to 0 when done with it, thus if it's trying to pass 0, it's not necessary to actually pass it
{
gSetNum(DataName, NumData) #Sets the variable with the same name and same data as the on the Network
}
}
#Clear all data that we just modified on the router
StrData = ""
NumData = ""
Test = ""
DataName = ""
#Now we perform the same operations, but this time setting data to the network instead
gSetGroup(Group) #Set ourselves to access the computer
Test = "NetOut", Test += toChar(Count) #The index to check, NetOut is a global array numbered 1-25 which holds the name of the variable to send, if you need to send more than 25 pieces of data, you're doing something wrong.
DataName = gGetStr(Test)
if(DataName != "" & !OpenPort != 0) #Found incoming data in this index and the Network's ports arn't clogged
{
gSetStr(Test, "") #So that it doesn't read in next loop unless set again - remember this is on the sending computer, so it doesn't make a difference if we clear it, it's not like receiving where all routers hooked up need to get the data.
StrData = gGetStr(DataName) #Get the string data at the name given to us
NumData = gGetNum(DataName) #Get the numerical data associated with the name given
gSetGroup(Network) #Set ourselves to modify the network's memory
gSetNum("NetOut" + toChar(OpenPort), 1) #Say that the port is written to
if(StrData != "") #We assume that whatever module gets the data sets it to "" when done with it, thus if it's trying to pass "", it's not necessary to actually pass it
{
gSetStr(DataName, StrData) #Sets the variable with the same name and same data as the on the Network
}
if(NumData != 0) #We assume that whatever module gets the data sets it to 0 when done with it, thus if it's trying to pass 0, it's not necessary to actually pass it
{
gSetNum(Test, NumData) #Sets the variable with the same name and same data as the on the Network
}
}
}
} It's completely hectic! In short, you need to:
Bookmarks