user_receive_packet hook and JID parameter

I am trying to use user_receive_packet hook but am confused about the JID parameter which gets passed along when the hook is invoked. It seems like JID parameter and "to" attribute of the stanza are always the same. or at least in case of MUC presence stanza. is it by design ? we can always get the receiving user's JID from the "to" attribute , then why do we need it as an additional parameter. sorry if there is a use case I am missing here. thanks.

JJ

Design decision: save CPU consumption

I didn't write those lines of code, but I can imagine the reason.

Let's imagine I will receive this stanza:

<iq from='localhost' type='result' to='badlop@localhost/Tka'>
  <query xmlns='jabber:iq:last' seconds='104'/>
</iq>

In that case, the hook user_receive_packet is run with those parameters:

JID: {jid,"badlop","localhost","Tka","badlop","localhost","Tka"}
From: {jid,[],"localhost",[],[],"localhost",[]}
To: {jid,"badlop","localhost","Tka","badlop","localhost","Tka"}
Packet: {xmlelement,"iq",
                    [{"from","localhost"},
                     {"to","badlop@localhost/Tka"},
                     {"type","result"}],
                    [{xmlelement,"query",
                                 [{"xmlns","jabber:iq:last"},
                                  {"seconds","104"}],
                                 []}]}

As you noticed, the To parameter is equivalent to the 'to' attribute in the stanza. Please note both values are not equal: the stanza contains a string, and the parameter contains the string already parsed and splitted in the building parts of the JID (username, server, resource...).

If you only have the stanza and you want to perform some operation with the destination address, you would need to parse the stanza searching the 'to' attribute. And quite probably you would also need to parse the string and split in parts.

If the string was already searched and processed before calling the hook, it can be passed inside the hook with minimal cost. And if you need that To, you can use it with minimal cost. Imagine your hook is called for EVERY single stanza that ANY user in your server receives. A small cost reduction here means a large cost reduction in overall, right?

The only drawback is that the To parameter is passed to all hook handlers, even those that are not interested in it. So maybe memory consumption increases a little. As you can see, in this design decision the factor to reduce was CPU consumption instead of memory consumption.

i guess my question was

i guess my question was regarding the first parameter to the hook, which is documented as JID. don't know what it should denote but in some MUC related testing I did, it always has the same value as To parameter and "to" attribute. so it seems like JID==To==to.
so was wondering why the JID parameter? is there any scenario when JID /= To.
thanks for the quick reply as always :-)

JJ

Syndicate content