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

Revision For Lan Device Indexing

Started by soccermiles, 21 October 2013 - 12:18 PM
soccermiles #1
Posted 21 October 2013 - 02:18 PM
Hello All,

Currently, once a device has been connected to a network, the ID being used for that device is never reused, even after the device has been disconnected. I examined the save files under "computer", and it looks like the only thing that's tracked to determine the next index for a device is the last assigned index for each type of device. Even if you just break some connections and remake them, the ID of the device will be set to the next in line. This means that someone who plays around with LAN networks too much will end up with huge LAN device IDs that are impossible to remember.

I'd like to recommend a new indexing system that allows reuse without severely complicating the ID selection process. There will still be a file for each device type, but in addition to storing the next ID up from the highest so far selected, it will include IDs that have been released by modems or devices being broken, in ascending order.

When a device or modem is broken, it will release its ID into the file for its type, inserting it in-order with the other IDs present. When we need a new ID, it will read the file, and take the first number it encounters, and erase it from the file. If that number was the last number in the file, we will increment the ID we received and write it back to the file. I understand that adding the functionality of releasing IDs when devices are disconnected will require a source modification, and I hope I'm not requesting the impossible, but I think it will pay off if it can be done.

This will result in intuitive reuse of IDs, resulting in smaller IDs and eliminating the tendency for IDs to explode to huge numbers that are impossible for coders to remember, just because they messed around with their LAN networks too much.
Also, this will be a must-have for any server with ComputerCraft, since multiple inhabitants are certain to be making LAN networks.

Thanks,
-Miles
Edited on 30 December 2013 - 02:33 PM
Cranium #2
Posted 21 October 2013 - 05:02 PM
The way it's currently handled sounds a lot like how computer ID's are handled as well. This should be something that's looked into, because generating 500 unique ID's for several computers that are only replaced quickly should be changed. With LAN ID's it sounds much more feasible, just to use the lowest unused ID available.
Lyqyd #3
Posted 21 October 2013 - 05:09 PM
Computers are that way for a reason, though. The files and folders created on an unlabeled computer will never be overwritten if it's broken. The files can be retrieved (or a computer matching the ID created by a server OP) simply by going in to the save folder. It makes sense for computer IDs to be one-time use in that way. Every computer should be labelled, ideally. With good labelling practices, it's easy to keep computer ID numbers quite low.

Network peripherals are another story. The current model does minimize disk I/O, though this change may not significantly impact that. Obviously, another optimization that would need to be done is to remove all but one of the consecutive IDs from the end of the file (so change 0, 2, 4, 5, 6 to 0, 2, 4).
Engineer #4
Posted 21 October 2013 - 06:23 PM
Computers are that way for a reason, though. The files and folders created on an unlabeled computer will never be overwritten if it's broken. The files can be retrieved (or a computer matching the ID created by a server OP) simply by going in to the save folder. It makes sense for computer IDs to be one-time use in that way. Every computer should be labelled, ideally. With good labelling practices, it's easy to keep computer ID numbers quite low.

That is actually very easy to do.
With this little code:

local chars = {}
for ixChar = 65, 90 do chars[ixChar - 65] = string.char(ixChar) end

local id = os.getComputerID()

local label = ""
for ixValue = #chars, 1, -1 do
	repeat
		if id - ixValue >= 0 then
			id = id - ixValue
			label = label .. chars[ ixValue ]
		else
			break
		end
	until id <= 0
	
	if id == 0 then
		break
	end
end

os.setComputerLabel( label )	
The code is untested though, should work honestly.

You should put that into a resource pack with a path to the file:
assets/computercraft/lua/rom/autorun/THEFILE

But back to the OP, I think it would be awesome to see this implemented. Maybe to enhance the idea, why dont we have different ID's per network?
To me, it would make a lot more sense, but I dont know if it's even possible without a major restructure. However it looks like there needs to be a major restructure in the Java code to accomplish this anyway.

Just my two cents.
electrodude512 #5
Posted 24 October 2013 - 05:29 PM
That is actually very easy to do.
With this little code:

local chars = {}
for ixChar = 65, 90 do chars[ixChar - 65] = string.char(ixChar) end

local id = os.getComputerID()

local label = ""
for ixValue = #chars, 1, -1 do
	repeat
		if id - ixValue >= 0 then
			id = id - ixValue
			label = label .. chars[ ixValue ]
		else
			break
		end
	until id <= 0
	
	if id == 0 then
		break
	end
end

os.setComputerLabel( label )	

Or just:

if not os.getComputerLabel() then
  os.setComputerLabel("auto"..tostring(os.getComputerID()))
end
A lot shorter and makes a more understandable label.

I've been wondering, is there any reason not to have two computers with the same label (besides that it's confusing)? In that case, then just:

if not os.getComputerLabel() then
  os.setComputerLabel("autolabel")
end
Cranium #6
Posted 24 October 2013 - 05:59 PM
Computers can have the same label, it's up to you, but good luck finding the program file you need in 100 "autolabel" computers.
soccermiles #7
Posted 30 December 2013 - 03:30 PM
The problem of network device IDs still stands. I recently got back to Minecraft, and realized I needed to check in on this topic.
Everyone so far seems to agree that network device IDs should be reused if possible.
Is there a way we can get this implemented?

EDIT: Additionally, I'm wondering if it's possible to have a different set of device IDs for each LAN network.
Edited on 30 December 2013 - 02:33 PM
Lyqyd #8
Posted 30 December 2013 - 03:43 PM
They can't be per-network, as networks can be merged and split at any time.
Bomb Bloke #9
Posted 30 December 2013 - 05:23 PM
Which is true in the real world too, yet here I am using a private IP common to who knows how many other systems out there.

I'm a little confused - why does it matter to a CC user what ID a modem gets? Or are we talking about the way the rednet API just goes with whatever the computer ID is? soccermiles, can you give some sort of example as to how this is a problem? It sounds to me like this is a fairly simple "custom rednet API" fix, on the surface.
Lyqyd #10
Posted 30 December 2013 - 05:33 PM
I believe the suggestion relates to peripherals on wired networks, so names hard coded into programs is relatively common, from what I've seen. Merging two networks and having two monitor_0 instead of just one might be undesirable.

You do not have the same MAC as other computers out there, regardless of network. I think it's a slightly more apt comparison in this case.
soccermiles #11
Posted 01 January 2014 - 01:38 PM
EDIT: Removed all previous stuff because I just realized something…
The problem of computers having constantly incremented network IDs just irks me. It's not actually a problem.

The real problem is that non-computer devices have no inherent IDs. They can't be identified by a network ID because that changes any time you reconnect it to the network. The only way to give them IDs is to put them on one side of a computer and use that computer as an interface to the device.

It would be really convenient not to have to identify networked devices by a computer attached to them, but I guess it's not that important.
Edited on 01 January 2014 - 01:36 PM
Rybec #12
Posted 04 January 2014 - 01:13 AM
What if we could label the wired modems themselves? Even if you have more than one of the same peripheral behind like-labeled modems you could still wrap them successfully, you just can't tell them apart (which could have it's own uses). Then we just need a function to return a list of devices connected via modems of a particular label, perhaps an extra argument to modem.getNamesRemote().

Going that route means only the modems need to be modified, instead of every peripheral.

To clarify, labeling a modem would not change it's basic function in any way; you would still wrap a peripheral directly via monitor_## etc. You simply have an extra option when using getNamesRemote() to filter by modem label and only return the relevant device IDs.
Edited on 04 January 2014 - 12:18 AM