diff options
3 files changed, 179 insertions, 0 deletions
diff --git a/src/com/isode/stroke/presence/DirectedPresenceSender.java b/src/com/isode/stroke/presence/DirectedPresenceSender.java new file mode 100644 index 0000000..a08a2b8 --- /dev/null +++ b/src/com/isode/stroke/presence/DirectedPresenceSender.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ +package com.isode.stroke.presence; + +import java.util.HashSet; +import java.util.Set; + +import com.isode.stroke.elements.Presence; +import com.isode.stroke.jid.JID; + +/** + * Class representing a directed presence sender. + * DirectedPresenceSender tracks who you want to send directed presence to and sends the + * presence to the directed targets. + * + */ +public class DirectedPresenceSender implements PresenceSender { + + public enum SendPresence { + AndSendPresence, + DontSendPresence + }; + + private Presence lastSentUndirectedPresence_; + private PresenceSender sender_; + private Set<JID> directedPresenceReceivers_ = new HashSet<JID>(); + + /** + * Constructor + * @param sender PresenceSender, not null + */ + public DirectedPresenceSender(PresenceSender sender) { + sender_ = sender; + } + + /** + * Send future broadcast presence also to this JID. + * @param jid Non-roster JID to receive global presence updates. + * @param sendPresence Also send the current global presence immediately. + */ + + public void addDirectedPresenceReceiver(JID jid, SendPresence sendPresence) { + directedPresenceReceivers_.add(jid); + if (sendPresence == SendPresence.AndSendPresence && sender_.isAvailable()) { + if (lastSentUndirectedPresence_ != null && + lastSentUndirectedPresence_.getType() == Presence.Type.Available) { + Presence presenceCopy = new Presence(lastSentUndirectedPresence_); + presenceCopy.setTo(jid); + sender_.sendPresence(presenceCopy); + } + } + } + + /** + * Remove the sender from the list of JIDs to whom broadcast presence will be sent. + * @param jid Non-roster JID to stop receiving global presence updates. + * @param sendPresence Also send presence type=unavailable immediately to jid. + */ + public void removeDirectedPresenceReceiver(JID jid, SendPresence sendPresence) { + directedPresenceReceivers_.remove(jid); + if (sendPresence == SendPresence.AndSendPresence && sender_.isAvailable()) { + Presence presence = new Presence(); + presence.setType(Presence.Type.Unavailable); + presence.setTo(jid); + sender_.sendPresence(presence); + } + } + + @Override + public void sendPresence(Presence presence) { + if (!sender_.isAvailable()) { + return; + } + sender_.sendPresence(presence); + if (presence.getTo() == null || !presence.getTo().isValid()) { + Presence presenceCopy = new Presence(presence); + for(JID jid : directedPresenceReceivers_) { + presenceCopy.setTo(jid); + sender_.sendPresence(presenceCopy); + } + + lastSentUndirectedPresence_ = presence; + } + } + + /** + * Gets either the last broadcast presence, or an empty stanza if none has been sent. + * @return presence, not null + * + */ + public Presence getLastSentUndirectedPresence(){ + if(lastSentUndirectedPresence_ == null) { + return new Presence(); + }else { + return new Presence(lastSentUndirectedPresence_); + } + } + + @Override + public boolean isAvailable() { + return sender_.isAvailable(); + } +} diff --git a/src/com/isode/stroke/presence/PresenceSender.java b/src/com/isode/stroke/presence/PresenceSender.java new file mode 100644 index 0000000..fc58bab --- /dev/null +++ b/src/com/isode/stroke/presence/PresenceSender.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ +package com.isode.stroke.presence; + +import com.isode.stroke.elements.Presence; + +/** + * Interface to be implemented by a Presence Sender + * + */ +public interface PresenceSender { + /** + * Send a Presence + * @param pres presence, not null + */ + public void sendPresence(Presence pres); + + /** + * Determine if the PresenceSender is available + * @return true if available and false otherwise + */ + public boolean isAvailable(); +}
\ No newline at end of file diff --git a/src/com/isode/stroke/presence/StanzaChannelPresenceSender.java b/src/com/isode/stroke/presence/StanzaChannelPresenceSender.java new file mode 100644 index 0000000..ef40ab4 --- /dev/null +++ b/src/com/isode/stroke/presence/StanzaChannelPresenceSender.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ +package com.isode.stroke.presence; + +import com.isode.stroke.client.StanzaChannel; +import com.isode.stroke.elements.Presence; + +/** + * Class representing a PresenceSender for StanzaChannel. + * StanzaChannelPresenceSender sends the presence straight through + * to the stanza channel. + * + */ +public class StanzaChannelPresenceSender implements PresenceSender { + + private StanzaChannel channel_; + + /** + * Constructor + * @param channel stanza channel, not null + */ + public StanzaChannelPresenceSender(StanzaChannel channel) { + this.channel_ = channel; + } + + @Override + public boolean isAvailable() { + return channel_.isAvailable(); + } + + @Override + public void sendPresence(Presence presence) { + channel_.sendPresence(presence); + } +} |