Project

General

Profile

Message.java plugin catches all stanzas in XMPPPreprocessorIfc

Igor Khomenko
Added almost 5 years ago

Hi there,

I'm trying to customise a bit Message.java plugin. I added XMPPPreprocessorIfc interface and it's implementation:

@Override
public boolean preProcess(Packet packet, XMPPResourceConnection xmppResourceConnection, NonAuthUserRepository nonAuthUserRepository, Queue<Packet> packets, Map<String, Object> stringObjectMap) {
    log.log(Level.INFO, "preProcess: {0}", new Object[] { packet.getElemName()});
}

I was seeing next log when logged in to Tigase:

2014-05-28 15:48:45.852 [in_31-sess-man]   Message.preProcess()   INFO:     preProcess: auth
2014-05-28 15:48:46.003 [in_31-sess-man]   Message.preProcess()   INFO:     preProcess: iq
2014-05-28 15:48:46.012 [in_31-sess-man]   Message.preProcess()   INFO:     preProcess: iq
2014-05-28 15:48:46.021 [in_31-sess-man]   Message.preProcess()   INFO:     preProcess: iq
2014-05-28 15:48:46.027 [in_31-sess-man]   Message.preProcess()   INFO:     preProcess: presence

This is a bit strange because this plugin only handles 'message' stanza:

@Override
public String[][] supElementNamePaths() {
    return ELEMENTS;
}

...

private static final String     ELEM_NAME = tigase.server.Message.ELEM_NAME;
private static final String[][] ELEMENTS  = {
    { ELEM_NAME }
};

...

public static final java.lang.String ELEM_NAME = "message";

Any ideas?


Replies (4)

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

Igor Khomenko wrote:

I added XMPPPreprocessorIfc interface and it's implementation:

Any ideas?

Hi, as per How packets are processed by the SM and plugins

Pre-processing - all loaded pre-processors receive the packet for processing. They work within session manager thread and they have no internal queue for processing. As they work within Session Manager thread it is important that they limit processing time to absolute minimum as they may affect the Session Manager performance.
The intention for the pre-processors is to allow them for packet blocking. If the pre-processing result is ‘true’ then the packet is blocked and no further processing is performed.

And as the log confirms, entries comes from the .preProcess() method, hence everything works according to plugins API.

Added by Igor Khomenko almost 5 years ago

Thanks for your response

I'm not sure that this info contains an explanation of why 'preProcess' method receives all types of stanzas, but not only 'messsage' which I set in 'supElementNamePaths' method

Pre-processing - all loaded pre-processors receive the packet for processing. They work within session manager thread and they have no internal queue for processing. As they work within Session Manager thread it is important that they limit processing time to absolute minimum as they may affect the Session Manager performance.
The intention for the pre-processors is to allow them for packet blocking. If the pre-processing result is ‘true’ then the packet is blocked and no further processing is performed.

Actually I expect to receive only 'message' stanza in 'preProcess' method, but not any others like iq, auth

That's why I'm a bit confused

Could you please provide a bit more info what I should do to limit 'preProcess' method only for 'message' stanza.

Because do every time something like

@Override
public boolean preProcess(Packet packet, XMPPResourceConnection xmppResourceConnection, NonAuthUserRepository nonAuthUserRepository, Queue<Packet> packets, Map<String, Object> stringObjectMap) {
    if(packet.getElemName().equal("message")){
        // do something
    }
}

doesn't mean 'limit processing time to absolute minimum'

Thanks

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam almost 5 years ago

Igor Khomenko wrote:

Thanks for your response

I'm not sure that this info contains an explanation of why 'preProcess' method receives all types of stanzas, but not only 'messsage' which I set in 'supElementNamePaths' method

[...]

Actually I expect to receive only 'message' stanza in 'preProcess' method, but not any others like iq, auth

That's why I'm a bit confused

Could you please provide a bit more info what I should do to limit 'preProcess' method only for 'message' stanza.

By design preprocessors receive all stanzas. This is just a nature of preprocessing. The function of preprocessing is to block or alter traffic before it is processed by plugins. And as it handles all stanza types it has to be implemented carefully to reduce performance impact.

Your code is correct but more efficient would be:

@Override
public boolean preProcess(Packet packet, XMPPResourceConnection xmppResourceConnection, NonAuthUserRepository nonAuthUserRepository, Queue<Packet> packets, Map<String, Object> stringObjectMap) {
    if(packet.getElemName() == "message"){
        // do something
    }
}

doesn't mean 'limit processing time to absolute minimum'

Please note, this kind of "processing" that is detecting what kind of packet this is before putting it to a preprocessor has to be done somewhere. If it is not inside preprocessor then it would have to be in some other place. And because most preprocessors are interested in all packets anyway, we decided that preprocessors should take care of packet type detection if necessary.

Added by Igor Khomenko almost 5 years ago

Thanks Artur for great clarification!

It's clear now

    (1-4/4)