Broadcast

I am creating a plugin for openfire.My plugin has to send an IQ stanza in broadcast to all clients that are connected to the server, after some processing.

It should be simple to do.

Ideas?

thanks.

There is already one brodcast plugin source available with the openfire source. You can go through it. I think this fulfill your requirement.

ok, I have taken a look at broadcast plugin, I need something similar to this:

SessionManager.broadcast(message).

This method only broadcasts message packets and I need to send IQ stanzas.

You can modify this code according to your needs by some modification in current plugin code.

sry for highjacking your post but i also want to modify the broadcast plugin.

I want to broadcast to all users registered on the server, including the offline ones. So i thought a function similar to the one which broadcasts to all users of an group would be fine.

So here comes the question: How can i retrieve a list of all JID registered to to the server?

//Edit: Already found the getUsers method from the user manager, is there a way to cast this to a JID?

//Edit the 2.:

I substituded the sessionManager.broadcast(message); with the following, but it doesnt seem to work:

Collection<User> allusers =userManager.getUsers();

for(User u : allusers){

Message newMessage = message.createCopy();

newMessage.setTo(u.getName()*"@"*componentManager.getServerName());

try {

componentManager.sendPacket(this, newMessage);

}

catch (Exception e) {

componentManager.getLog().error(e);

}

}

The following code does send a packet (in this chase a message) to everyone.

I mean really everyone: If a user is online with more than one client, all clients of this user will get the message, not only the one with highest priority. Offline users will get an offline-message.

The code works ok for normal server sizes like 1000-2000 users. For larger sizes you should possibly not create a user list first, because the online-status of a user could change during the process. I need this precomputed list, because I include the user count into the message.

XMPPServer xmppServer = XMPPServer.getInstance();
UserManager userManager = xmppServer.getUserManager();
PresenceManager presenceManager = xmppServer.getPresenceManager();
RoutingTable routingTable = xmppServer.getRoutingTable();
PacketRouter packetRouter = xmppServer.getPacketRouter(); // use this switch to send only to all online resources
boolean onlineUsersOnly = false; // collect JIDs of active resources and offline users
String domain = xmppServer.getServerInfo().getXMPPDomain();
Collection<User> users = userManager.getUsers();
Collection<JID> jids = new ArrayList<JID>(users.size());
Iterator<User> itrUsers = users.iterator();
while (itrUsers.hasNext()) {
     User user = itrUsers.next();
     JID jid = new JID(user.getUsername(), domain, null, true);
     if (presenceManager.isAvailable(user)) {
          // add all active resources
          jids.addAll(routingTable.getRoutes(jid, null));
     }
     else if (!onlineUsersOnly) {
          // send offline message to bareJID
          jids.add(jid);
     }
} // send message to all users
Iterator<JID> itrJIDs = jids.iterator();
while (itrJIDs.hasNext()) {
     JID jid = itrJIDs.next();
     Message msgPacket = new Message();
     msgPacket.setType(Message.Type.chat);
     msgPacket.setFrom(context.getFromBareJID());
     msgPacket.setTo(jid);
     msgPacket.setBody(message);      packetRouter.route(msgPacket);
}

//sessionManager.broadcast(message);

Collection<User> allusers =userManager.getUsers();

String domain = xmppServer.getServerInfo().getXMPPDomain();

JID jid = null;

for(User u : allusers){

jid = new JID(u.getUsername(), domain, null, true);

Message newMessage = message.createCopy();

newMessage.setTo(jid);

packetRouter.route(newMessage);

}

Works like a charm, Thank you very much!