Commit bf21e9ae authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Refactoring work. JM-311


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1556 b35dd754-fafc-0310-a699-88a17e54d16e
parent eec804e5
......@@ -72,27 +72,6 @@ public interface PresenceManager {
*/
public void probePresence(JID prober, JID probee);
/**
* Saves the last unavailable presence of the user so that future presence probes may answer
* this presence which may contain valuable information. If a last unavailable presence does
* not exist for a contact then no unavailable presence will be sent to the owner of the
* contact.
*
* @param username the name of the user whose last presence is going to be saved to the
* database.
* @param presence the last unavailable presence to save to the database for this user.
*/
public void saveLastUnavailablePresence(String username, Presence presence);
/**
* Deletes the last unavailable presence of a user. This may be necessary if the user has
* become available.
*
* @param username the name of the user whose last presence is going to be delete from the
* database.
*/
public void deleteLastUnavailablePresence(String username);
/**
* Handle a presence probe sent by a remote server. The logic to apply is the following: If
* the remote user is not in the local user's roster with a subscription state of "From", or
......@@ -113,4 +92,38 @@ public interface PresenceManager {
* @param userJID JID of the local user.
*/
public void sendUnavailableFromSessions(JID recipientJID, JID userJID);
/**
* Notification message saying that the sender of the given presence just became available.
*
* @param presence the presence sent by the available user.
*/
public void userAvailable(Presence presence);
/**
* Notification message saying that the sender of the given presence just became unavailable.
*
* @param presence the presence sent by the unavailable user.
*/
public void userUnavailable(Presence presence);
/**
* Returns the status sent by the user in his last unavailable presence or <tt>null</tt> if the
* user is online or never set such information.
*
* @param user the user to return his last status information
* @return the status sent by the user in his last unavailable presence or <tt>null</tt> if the
* user is online or never set such information.
*/
public String getLastPresenceStatus(User user);
/**
* Returns the number of milliseconds since the user went offline or -1 if such information
* is not available or if the user is online.
*
* @param user the user to return his information.
* @return the number of milliseconds since the user went offline or -1 if such information
* is not available or if the user is online.
*/
public long getLastActivity(User user);
}
\ No newline at end of file
......@@ -253,6 +253,7 @@ public class XMPPServer {
loadModule(IQTimeHandler.class.getName());
loadModule(IQvCardHandler.class.getName());
loadModule(IQVersionHandler.class.getName());
loadModule(IQLastActivityHandler.class.getName());
loadModule(PresenceSubscribeHandler.class.getName());
loadModule(PresenceUpdateHandler.class.getName());
loadModule(IQDiscoInfoHandler.class.getName());
......
......@@ -31,7 +31,7 @@ import org.xmpp.packet.PacketError;
public abstract class IQHandler extends BasicModule implements ChannelHandler {
protected PacketDeliverer deliverer;
private SessionManager sessionManager;
protected SessionManager sessionManager;
/**
* Create a basic module with the given name.
......
......@@ -97,12 +97,9 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
session.setInitialized(true);
}
}
// Delete the last unavailable presence of this user since the user is now
// available. Only perform this operation if this is an available presence sent to
// THE SERVER and the presence belongs to a local user.
if (presence.getTo() == null && localServer.isLocal(presence.getFrom())) {
presenceManager.deleteLastUnavailablePresence(presence.getFrom().getNode());
}
// Notify the presence manager that the user is now available. The manager may
// remove the last presence status sent by the user when he went offline.
presenceManager.userAvailable(presence);
}
else if (Presence.Type.unavailable == type) {
broadcastUpdate(presence.createCopy());
......@@ -110,15 +107,10 @@ public class PresenceUpdateHandler extends BasicModule implements ChannelHandler
if (session != null) {
session.setPresence(presence);
}
// Save the last unavailable presence of this user if the presence contains any
// child element such as <status>. Only perform this operation if this is an
// unavailable presence sent to THE SERVER and the presence belongs to a local user.
if (presence.getTo() == null && localServer.isLocal(presence.getFrom())) {
if (!presence.getElement().elements().isEmpty()) {
presenceManager.saveLastUnavailablePresence(presence.getFrom().getNode(),
presence);
}
}
// Notify the presence manager that the user is now unavailable. The manager may
// save the last presence status sent by the user and keep track when the user
// went offline.
presenceManager.userUnavailable(presence);
}
else {
presence = presence.createCopy();
......
......@@ -13,6 +13,7 @@ package org.jivesoftware.messenger.spi;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentException;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.component.InternalComponentManager;
import org.jivesoftware.messenger.auth.UnauthorizedException;
......@@ -42,7 +43,8 @@ import java.util.List;
*/
public class PresenceManagerImpl extends BasicModule implements PresenceManager {
private static final String lastPresenceProp = "lastUnavailablePresence";
private static final String LAST_PRESENCE_PROP = "lastUnavailablePresence";
private static final String LAST_ACTIVITY_PROP = "lastActivity";
private SessionManager sessionManager;
private XMPPServer server;
......@@ -98,6 +100,81 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
return Collections.unmodifiableCollection(presences);
}
public String getLastPresenceStatus(User user) {
String answer = null;
String presenceXML = user.getProperties().get(LAST_PRESENCE_PROP);
if (presenceXML != null) {
try {
// Parse the element
Document element = DocumentHelper.parseText(presenceXML);
answer = element.getRootElement().elementTextTrim("status");
}
catch (DocumentException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
return answer;
}
public long getLastActivity(User user) {
long answer = -1;
String offline = user.getProperties().get(LAST_ACTIVITY_PROP);
if (offline != null) {
try {
answer = (System.currentTimeMillis() - Long.parseLong(offline)) / 1000;
}
catch (NumberFormatException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
return answer;
}
public void userAvailable(Presence presence) {
// Delete the last unavailable presence of this user since the user is now
// available. Only perform this operation if this is an available presence sent to
// THE SERVER and the presence belongs to a local user.
if (presence.getTo() == null && server.isLocal(presence.getFrom())) {
String username = presence.getFrom().getNode();
if (username == null) {
// Ignore anonymous users
return;
}
try {
User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.getProperties().remove(LAST_PRESENCE_PROP);
}
catch (UserNotFoundException e) {
}
}
}
public void userUnavailable(Presence presence) {
// Only save the last presence status and keep track of the time when the user went
// offline if this is an unavailable presence sent to THE SERVER and the presence belongs
// to a local user.
if (presence.getTo() == null && server.isLocal(presence.getFrom())) {
String username = presence.getFrom().getNode();
if (username == null) {
// Ignore anonymous users
return;
}
try {
User probeeUser = UserManager.getInstance().getUser(username);
if (!presence.getElement().elements().isEmpty()) {
// Save the last unavailable presence of this user if the presence contains any
// child element such as <status>
probeeUser.getProperties().put(LAST_PRESENCE_PROP, presence.toXML());
}
// Keep track of the time when the user went offline
probeeUser.getProperties().put(LAST_ACTIVITY_PROP,
String.valueOf(System.currentTimeMillis()));
}
catch (UserNotFoundException e) {
}
}
}
public void handleProbe(Presence packet) throws UnauthorizedException {
String username = packet.getTo().getNode();
// Check for a cached roster:
......@@ -157,7 +234,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
// prober
try {
User probeeUser = UserManager.getInstance().getUser(probee.getNode());
String presenceXML = probeeUser.getProperties().get(lastPresenceProp);
String presenceXML = probeeUser.getProperties().get(LAST_PRESENCE_PROP);
if (presenceXML != null) {
try {
// Parse the element
......@@ -246,30 +323,6 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
}
}
public void deleteLastUnavailablePresence(String username) {
if (username == null) {
return;
}
try {
User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.getProperties().remove(lastPresenceProp);
}
catch (UserNotFoundException e) {
}
}
public void saveLastUnavailablePresence(String username, Presence presence) {
if (username == null) {
return;
}
try {
User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.getProperties().put(lastPresenceProp, presence.toXML());
}
catch (UserNotFoundException e) {
}
}
// #####################################################################
// Module management
// #####################################################################
......
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