[erlang-questions] Receiving identical messages from Erlang and from JInterface

Per Hedeland per@REDACTED
Thu Jan 4 23:40:45 CET 2007


Richard Carlsson <richardc@REDACTED> wrote:
>
>Hubert Matthews wrote:
>> Richard Carlsson wrote:
>>> Try using different variable names in the second receive, e.g. {P1,X1}.
>> 
>> Doing that works fine.  What I'm trying to understand is why seemingly
>> identical terms sent from an Erlang client match but when sent from Java
>> don't match.  If they were incorrectly sent by Java then why are they
>> displayed correctly in the process' message queue?
>
>There seems to be a difference in the bit representation, which is for
>some reason not visible in the displayed form <4999.1.0>.
>
>> P == P1 gives false, as does P =:= P1.
>> 
>> (foo@REDACTED)60> term_to_binary(P).
>> <<131,103,100,0,11,106,110,111,100,101,64,106,117,109,98,111,0,0,0,1,0,0,0,0,3>>
>> 
>> (foo@REDACTED)61> term_to_binary(P1).
>> <<131,103,100,0,11,106,110,111,100,101,64,106,117,109,98,111,0,0,0,1,0,0,0,0,1>>
>> 
>> The last byte is different: 3 then 1.

That would be the "creation" part of the pid (managed by epmd), intended
to be able to differentiate between processes from different invocations
of a node with a given name - e.g. if the node restarts, all the
processes spawned at startup are likely to get the same "number" as in
the previous startup, yet they are not the same processes:

(foo@REDACTED)1> self().
<0.36.0>
(foo@REDACTED)2> term_to_binary(v(1)).
<<131,103,100,0,9,102,111,111,64,112,108,117,116,111,0,0,0,36,0,0,0,0,2>>
                                                                      ^
(kill the node and start it again)

(foo@REDACTED)1> self().
<0.36.0>
(foo@REDACTED)2> term_to_binary(v(1)).
<<131,103,100,0,9,102,111,111,64,112,108,117,116,111,0,0,0,36,0,0,0,0,3>>
                                                                      ^

I don't know anything about JInterface, but if those messages were sent
from two different invocations of the Java program, it seems everything
is working as designed (otherwise not:-).

>> When sent from Erlang I get something completely different:
>> 
>> (foo@REDACTED)68> term_to_binary(PE).
>> <<131,103,100,0,24,119,105,98,98,108,101,64,106,117,109,98,111,46,111,120,121,119,97,114,101,46,110,101,116,...>>

It's essentially due to the node name that is part of the external
representation of a pid:

(foo@REDACTED)3> [106,110,111,100,101,64,106,117,109,98,111].
"jnode@REDACTED"
(foo@REDACTED)4> [119,105,98,98,108,101,64,106,117,109,98,111,46,111,120,121,119,97,114,101,46,110,101,116].
"wibble@REDACTED"

Btw, you should probably choose "long" or "short" host name the same way
for Erlang and JInterface - for Erlang nodes, those started with -name
vs -sname can't talk to each other - this is apparently not the case
with Erlang + Java, but there might be other problems waiting to
happen...

--Per Hedeland



More information about the erlang-questions mailing list