Commit 6c2d4930 authored by Armando Jagucki's avatar Armando Jagucki Committed by ajagucki

Added remote presence event dispatch and listener.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/branches/pep@9108 b35dd754-fafc-0310-a699-88a17e54d16e
parent 742f6f9b
...@@ -18,6 +18,7 @@ import org.jivesoftware.openfire.interceptor.InterceptorManager; ...@@ -18,6 +18,7 @@ import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketRejectedException; import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.ClientSession; import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.session.Session; import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.openfire.user.RemotePresenceEventDispatcher;
import org.jivesoftware.util.LocaleUtils; import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.xmpp.packet.*; import org.xmpp.packet.*;
...@@ -98,6 +99,7 @@ public class PresenceRouter extends BasicModule { ...@@ -98,6 +99,7 @@ public class PresenceRouter extends BasicModule {
private void handle(Presence packet) { private void handle(Presence packet) {
JID recipientJID = packet.getTo(); JID recipientJID = packet.getTo();
// Check if the packet was sent to the server hostname // Check if the packet was sent to the server hostname
if (recipientJID != null && recipientJID.getNode() == null && if (recipientJID != null && recipientJID.getNode() == null &&
recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) { recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) {
...@@ -108,8 +110,11 @@ public class PresenceRouter extends BasicModule { ...@@ -108,8 +110,11 @@ public class PresenceRouter extends BasicModule {
return; return;
} }
} }
try { try {
JID senderJID = packet.getFrom();
Presence.Type type = packet.getType(); Presence.Type type = packet.getType();
// Presence updates (null is 'available') // Presence updates (null is 'available')
if (type == null || Presence.Type.unavailable == type) { if (type == null || Presence.Type.unavailable == type) {
// check for local server target // check for local server target
...@@ -120,6 +125,18 @@ public class PresenceRouter extends BasicModule { ...@@ -120,6 +125,18 @@ public class PresenceRouter extends BasicModule {
updateHandler.process(packet); updateHandler.process(packet);
} }
else { else {
// Manage remote presence event dispatching
if (senderJID != null && !serverName.equals(senderJID.getDomain())) {
if (type == null) {
// Remote user has become available
RemotePresenceEventDispatcher.availableRemoteUser(packet);
}
else if (type == Presence.Type.unavailable) {
// Remote user is now unavailable
RemotePresenceEventDispatcher.unavailableRemoteUser(packet);
}
}
// Check that sender session is still active // Check that sender session is still active
Session session = sessionManager.getSession(packet.getFrom()); Session session = sessionManager.getSession(packet.getFrom());
if (session != null && session.getStatus() == Session.STATUS_CLOSED) { if (session != null && session.getStatus() == Session.STATUS_CLOSED) {
......
...@@ -22,7 +22,7 @@ import java.util.concurrent.CopyOnWriteArrayList; ...@@ -22,7 +22,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
* Dispatches presence events. The following events are supported: * Dispatches presence events. The following events are supported:
* <ul> * <ul>
* <li><b>availableSession</b> --> A session is now available to receive communication.</li> * <li><b>availableSession</b> --> A session is now available to receive communication.</li>
* <li><b>unavailableSession</b> --> A session is now longer available.</li> * <li><b>unavailableSession</b> --> A session is no longer available.</li>
* <li><b>presencePriorityChanged</b> --> The priority of a resource has changed.</li> * <li><b>presencePriorityChanged</b> --> The priority of a resource has changed.</li>
* <li><b>presenceChanged</b> --> The show or status value of an available session has changed.</li> * <li><b>presenceChanged</b> --> The show or status value of an available session has changed.</li>
* </ul> * </ul>
......
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.user;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.xmpp.packet.Presence;
/**
* Dispatches remote presence events. The following events are supported:
* <ul>
* <li><b>availableRemoteUser</b> --> A remote user is now available.</li>
* <li><b>unavailableRemoteUser</b> --> A remote user is no longer available.</li>
* </ul>
* Use {@link #addListener(RemotePresenceEventListener)} and
* {@link #removeListener(RemotePresenceEventListener)} to add or remove {@link RemotePresenceEventListener}.
*
* @author Armando Jagucki
*/
public class RemotePresenceEventDispatcher {
private static List<RemotePresenceEventListener> listeners =
new CopyOnWriteArrayList<RemotePresenceEventListener>();
/**
* Registers a listener to receive events.
*
* @param listener the listener.
*/
public static void addListener(RemotePresenceEventListener listener) {
if (listener == null) {
throw new NullPointerException();
}
listeners.add(listener);
}
/**
* Unregisters a listener to receive events.
*
* @param listener the listener.
*/
public static void removeListener(PresenceEventListener listener) {
listeners.remove(listener);
}
/**
* Notification message indicating that a remote user that was not available is now
* available. A remote user becomes available when an available presence is received
* by <tt>PresenceRouter</tt>.
*
* @param presence the received available presence.
*/
public static void availableRemoteUser(Presence presence) {
if (!listeners.isEmpty()) {
for (RemotePresenceEventListener listener : listeners) {
listener.availableRemoteUser(presence);
}
}
}
/**
* Notification message indicating that a remote user that was available is no longer
* available. A remote user becomes unavailable when an unavailable presence is received.
* by <tt>PresenceRouter</tt>.
*
* @param presence the received unavailable presence.
*/
public static void unavailableRemoteUser(Presence presence) {
if (!listeners.isEmpty()) {
for (RemotePresenceEventListener listener : listeners) {
listener.unavailableRemoteUser(presence);
}
}
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.user;
import org.xmpp.packet.Presence;
/**
* Interface to listen for remote presence events. Use the
* {@link RemotePresenceEventDispatcher#addListener(RemotePresenceEventListener)}
* method to register for events.
*
* @author Armando Jagucki
*/
public interface RemotePresenceEventListener {
/**
* Notification message indicating that a remote user that was not available is now
* available. A remote user becomes available when an available presence is received
* by <tt>PresenceRouter</tt>.
*
* @param presence the received available presence.
*/
public void availableRemoteUser(Presence presence);
/**
* Notification message indicating that a remote user that was available is no longer
* available. A remote user becomes unavailable when an unavailable presence is received.
* by <tt>PresenceRouter</tt>.
*
* @param presence the received unavailable presence.
*/
public void unavailableRemoteUser(Presence presence);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment