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

Weird problem with string.find [devving on autmatically routing packet switched network]

Started by Monochrome, 08 March 2012 - 06:34 PM
Monochrome #1
Posted 08 March 2012 - 07:34 PM
Hi guys,

I'm working on a packet switched network. Some computers run a program, called mnet_gateway, that handles incoming packets, and is able to forward them along other gateways until they finally reach their destinations. Normal terminals can discover these gateways and register in order to be able to use them, etcetera.

Anyway, a problem is BREAKING my brains for a couple of hours now.. In my mnet api, I have the following code section:

function parse(packet)
local sep1, sep2, sep3, to, source, misc, data

sep1 = string.find(packet, "|")
if (sep1 == nil) then return nil, nil, packet, nil end

sep2 = string.find(packet, "|", sep1 + 1)
if (sep1 == nil) then return nil, nil, packet, nil end

sep3 = string.find(packet, "|", sep2 + 1)
if (sep1 == nil) then return nil, nil, packet, nil end

to = tonumber(string.sub(packet, 0, sep1 - 1))
source = tonumber(string.sub(packet, sep1 + 1, sep2 - 1))
data = string.sub(packet, sep2 + 1, sep3 - 1)
misc = string.sub(packet, sep3 + 1)

[b]print(misc)
a, b = string.find(misc, " ", 0, true)
print(a .. " till " .. :mellow:/>/>[/b]

return to, source, data, misc
end
function receive(timeout)
local from, packet, to, source, data, misc
from, packet = rednet.receive(timeout)
print("[ ] " .. from .. ": " .. packet)
to, source, data, misc = parse(packet)


exit()

return from, to, source, data, misc
end

The function receive() waits for a packet to arrive. It is then parsed by the function parse(), which returns the different pieces of info it contains. This all is used in my mnet_gateway program. The output:



> mnet_gateway
[i] Opening network device on [top]...
[i] Broadcasting gateway...
Listening for connections.
[ ] 0: 6|0|gateway_announce|0>0 5>0 7>0
[b]0>0 5>0 7>0 [/b]
[b]25 till 25[/b]
mnet: 56: attempt to call nil

WHAT?? As you can see in the source of the parser, I'm looking for a " " (space) char in the string "0>0 5>0 7>0". It is found at position 25 (!) which is, ironically, the exact position in the original network packet, 6|0|gateway_announce|0>0 5>0 7>0
I quadruple-checked everything, I just don't get it… Any idea anyone??

ps1: the exit() is just in order for the program to stop execution, even though it crashes.

ps2: for those who want to understand what I actually do: 6|0|gateway_announce|0>0 5>0 7>0 means packet from 6 to 0, then the command, then the routing table (3>6 means "for 3, send your packet to 6")
Espen #2
Posted 08 March 2012 - 08:10 PM
There is a known problem with the string.find() function.
It has been covered here already, if you want to take a look: http://www.computerc...d-twice-bugged/
It was also reported in another thread a while ago before that, I believe.
A workaround that was proposed there was to assign an empty string to the result, like this:

sep1 = string.find(packet, "|")
sep1 = sep1 + ""
Not 100% sure if that will fix your problem, but give it a try. :mellow:/>/>
Monochrome #3
Posted 08 March 2012 - 09:03 PM
There is a known problem with the string.find() function.
It has been covered here already, if you want to take a look: http://www.computerc...d-twice-bugged/
It was also reported in another thread a while ago before that, I believe.
A workaround that was proposed there was to assign an empty string to the result, like this:

sep1 = string.find(packet, "|")
sep1 = sep1 + ""
Not 100% sure if that will fix your problem, but give it a try. ;)/>/>

YOU ARE MY HERO!

I actually did search for trouble with string.find, but maybe not thouroughly enough… Thank you for bringing this to my attention. All I needed was adding this:

misc = misc .. ""

and string.find worked as expected :mellow:/>/>
Espen #4
Posted 08 March 2012 - 09:34 PM
No problem, glad it helped. :mellow:/>/>