Project

General

Profile

Groovy script and presence handling

Steffen Larsen
Added almost 5 years ago

Hi,

I am writing some groovy scripts that would allow externals ad hoc commands (though the http REST api) to get a list of full JIDs when having a bare JID as a input field.

E.g: So in the form I enter "hello@world.com"

And I will get a list of this back:

"hello@word.com/tigase-1",

"hello@word.com/tigase-2"

etc.

This works fine, I am doing it by getting the session and from there getting the XMPPResourceConnection list:

..
XMPPSession session = sessions.get(BareJID.bareJIDInstanceNS(userJid))
    if (session == null) {
        return ""
    } else {
        List<XMPPResourceConnection> conns = session.getActiveResources()
        String conns_str = ""
        for (XMPPResourceConnection con: conns)
            conns_str += "" + con.getJID() + ","                    
        return conns_str
    }

My question is, how do I get the current presence of each resource?

So for example I would be able to see that "hello@word.com/tigase-1" is "available" and have a status "Drinking beer" etc.

Another question, do I have to do something special in my script to make it work in a clustered environment?


Replies (12)

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

  1. Wouldn't session.getPresence() work for you?

  2. Currently scripts are only executed locally. In a few of our scripts we have implemented a rather hack-ish workaround, please see BroadcastToOnline.groovy for possible solution.

Added by Steffen Larsen almost 5 years ago

Hi Wojciech!,

1) I've been looking at the tigase.xmpp.XMPPSession.java class but can't see the method you are mentioning. My version is 5.2.0-RC2. Is it added later or is groovy hooked up somewhere else?

2) ok, that means every script (besides the broadcast) wouldn't work prob. in a clustered environment?.. Of course some of them are doing the clustering deeper down in the API, but still.

Another question. I am doing another script that allows you to send message stanzas to a JID. Should I use the offer mechanism, like its done on the ordinary java/component side?

def msg = Message.getMessage("admin@world.com", jidTo, StanzaType.chat, msg_body, null, null, "admin")
def result = p.commandResult(Command.DataType.result)
Command.addTextField(result, "Note", "Operation successful");

Queue results = new LinkedList()
results += result
results.offer(msg)
return results

or should it be done in another way? And if I instead of using "admin@world.com" just using null will it still be able to route it? What I mean is does the groovy "component" have some kind of special routing JID like ordinary components? and can I somehow get it, if it exists.

-Cheers and thanks!

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

  1. mea culpa, it's for XMPPResourceConnection so con.getPresence()

  2. Yes, if the script is not modified it won't work in clustered environment unfortunately.

The way you are doing it now is preferred.

Added by Steffen Larsen almost 5 years ago

1) ahhh sure thing! it works now. It would also be strange to have the presence on the session. :-)

2) cool, thanks!

Added by Steffen Larsen almost 5 years ago

Ohhh one more question regarding Groovy script (ad-hoc) and a clustered environment.

  • When I am using the offer method will it work in a clustered environment?

  • When using mucRepository will it work in a clustered environment?

Regarding the mucRepository, how is it injected in the Groovy scripts? I can't see it imported anywhere or event initialised in the code. When I for example uses def room = mucRepository.getRoom(jid); it just works, how come?

PS: Sorry for all the questions, but normally I write components and java code, not groovy scripts. :-)

-Cheers!

/Steffen

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

Steffen Larsen wrote:

Ohhh one more question regarding Groovy script (ad-hoc) and a clustered environment.

  • When I am using the offer method will it work in a clustered environment?

  • When using mucRepository will it work in a clustered environment?

I'm not following. What do you mean by "work in cluster environment"? The offer method will simply put the packed in the queue for processing. If you explicitly address packets for other nodes then they will get processed on other nodes. MucComponent doesn't support (in it's basic form) clustering altogether so I'm not sure what you are asking here.

Regarding the mucRepository, how is it injected in the Groovy scripts? I can't see it imported anywhere or event initialised in the code. When I for example uses def room = mucRepository.getRoom(jid); it just works, how come?

I think reading Component implementation - Lesson 6 - Scripting Support and then having a quick peek at line 365 of MUCComponent.java should clear this a bit :-)

Added by Steffen Larsen almost 5 years ago

Hi W,

Ohh yes, didn't see the initBindings section, sure thing, I understand now. Sorry about that.

About clustering and MUC, so If I want a number of participants within a cluster, I just have to assemble the IQ stanza my self instead of using the MUCComponent?

Because I tried using room.getOccupantsCount() in my cluster and it didn't work. I've tried it both using:

@

--comp-name-1 = muc

--comp-class-1 = tigase.muc.cluster.MUCComponentClustered

@

And:

@

--comp-name-1 = muc

--comp-class-1 = tigase.muc.cluster.MUCComponentClustered

muc/muc-strategy-class[S]=tigase.muc.cluster.DefaultStrategy

@

In both cases the form just hangs. It works if I run only one instance.

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

Currently this method doesn't work in cluster environment and is neither supported in tigase.muc.cluster.MUCComponentClustered class.

Added by Steffen Larsen almost 5 years ago

Ok, thanks for the update Wojciech!

If I do the recipient implementation based on pure IQ stanza (see http://xmpp.org/extensions/xep-0045.html#getmemberlist) and send:

<iq from='hag66@shakespeare.lit/pda'
    id='getnick1'
    to='coven@chat.shakespeare.lit'
    type='get'>
  <query xmlns='http://jabber.org/protocol/disco#info'
         node='x-roomuser-item'/>
</iq>

will that work in a clustered environment (using the MUCComponentClustered class that is)?

Cheers and thanks once again! :-)

/Steffen

Added by Steffen Larsen almost 5 years ago

Ohh BTW: I am running my MUC internally inside the cluster (running tigase.muc.cluster.MUCComponentClustered class). I could prob. be running them as virtual components, but I thought that the new MUC clustered component should do the correct routing, when I am sending the IQ stanzas, right?

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

Currently VirtualComponent is more thoroughly tested solution and while new MUCComponentClustered is already functional in cluster environment and should work well (including routing) there still may be some issues - feel free to report them in the related project.

As a sidenote - we should release improved muc clustering strategy, including a few fixes, shortly.

Added by Steffen Larsen almost 5 years ago

Ok, Thanks Wojciech.

I'll try it with creating a IQ stanza and testing it on 5.2.1 server branch. :-)

Cheers!

/Steffen

    (1-12/12)