diff options
| author | Alex Clayton <alex.clayton@isode.com> | 2016-03-11 13:18:15 (GMT) |
|---|---|---|
| committer | Alex Clayton <alex.clayton@isode.com> | 2016-03-14 15:17:54 (GMT) |
| commit | a0a0b9fd74f6d99da1c756b36aad6cc0122f011b (patch) | |
| tree | 0c79a9093b73db6eaf459ef4fc311006822a1201 | |
| parent | c77facbc3036590cfa347f38859f9f536f082d6b (diff) | |
| download | stroke-a0a0b9fd74f6d99da1c756b36aad6cc0122f011b.zip stroke-a0a0b9fd74f6d99da1c756b36aad6cc0122f011b.tar.bz2 | |
Add methods to PresenceOracle.
As per swiften patch 'Change bare JID presence lookup code to ignore
priorities' (0f5ef716a50c8d9761cafda12aacf818cdfd6353) add a couple of
methods and a test for PresenceOracle.
Test-information: Unit tests still pass.
Change-Id: If3961f29be821a065ffa854faeab7f20da666d25
3 files changed, 195 insertions, 33 deletions
diff --git a/src/com/isode/stroke/presence/PresenceOracle.java b/src/com/isode/stroke/presence/PresenceOracle.java index 7ac5b9d..e983d51 100644 --- a/src/com/isode/stroke/presence/PresenceOracle.java +++ b/src/com/isode/stroke/presence/PresenceOracle.java | |||
| @@ -6,8 +6,11 @@ package com.isode.stroke.presence; | |||
| 6 | 6 | ||
| 7 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
| 8 | import java.util.Collection; | 8 | import java.util.Collection; |
| 9 | import java.util.Comparator; | ||
| 9 | import java.util.HashMap; | 10 | import java.util.HashMap; |
| 11 | import java.util.List; | ||
| 10 | import java.util.Map; | 12 | import java.util.Map; |
| 13 | import java.util.PriorityQueue; | ||
| 11 | 14 | ||
| 12 | import com.isode.stroke.client.StanzaChannel; | 15 | import com.isode.stroke.client.StanzaChannel; |
| 13 | import com.isode.stroke.elements.Presence; | 16 | import com.isode.stroke.elements.Presence; |
| @@ -135,6 +138,120 @@ public class PresenceOracle { | |||
| 135 | results.addAll(presenceMap.values()); | 138 | results.addAll(presenceMap.values()); |
| 136 | return results; | 139 | return results; |
| 137 | } | 140 | } |
| 141 | |||
| 142 | private static class PresenceAccountCmp implements Comparator<Presence> { | ||
| 143 | |||
| 144 | private static int preferenceFromStatusShow(StatusShow.Type showType) { | ||
| 145 | switch (showType) { | ||
| 146 | case FFC: | ||
| 147 | return 5; | ||
| 148 | case Online: | ||
| 149 | return 4; | ||
| 150 | case DND: | ||
| 151 | return 3; | ||
| 152 | case Away: | ||
| 153 | return 2; | ||
| 154 | case XA: | ||
| 155 | return 1; | ||
| 156 | case None: | ||
| 157 | return 0; | ||
| 158 | } | ||
| 159 | assert(false); | ||
| 160 | return -1; | ||
| 161 | } | ||
| 162 | |||
| 163 | @Override | ||
| 164 | public int compare(Presence a, Presence b) { | ||
| 165 | int aPreference = preferenceFromStatusShow(a.getShow()); | ||
| 166 | int bPreference = preferenceFromStatusShow(b.getShow()); | ||
| 167 | |||
| 168 | if (aPreference != bPreference) { | ||
| 169 | return (aPreference > bPreference) ? -1 : 1; | ||
| 170 | } | ||
| 171 | if (a.getPriority() != b.getPriority()) { | ||
| 172 | return (a.getPriority() > b.getPriority()) ? -1 : 1; | ||
| 173 | } | ||
| 174 | return -a.getFrom().getResource().compareTo(b.getFrom().getResource()); | ||
| 175 | } | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | /** | ||
| 180 | * Returns the relevant presence for a list of resource presences. | ||
| 181 | * | ||
| 182 | * It only takes the presence show type into account. Priorities are | ||
| 183 | * ignored as various clients set them to arbitrary values unrelated | ||
| 184 | * to actual end point availability. | ||
| 185 | * | ||
| 186 | * The presences of the resources are group by availablilty and sorted | ||
| 187 | * by show type in the following order: | ||
| 188 | * | ||
| 189 | * -# Online | ||
| 190 | * -# Free for Chat | ||
| 191 | * -# Available | ||
| 192 | * -# Away | ||
| 193 | * -# DND | ||
| 194 | * -# Extended Away | ||
| 195 | * -# Away | ||
| 196 | * -# Offline | ||
| 197 | * -# Unavailable | ||
| 198 | * @param presences List of resource presences. Should not be null. | ||
| 199 | * @return The relevant presence. | ||
| 200 | */ | ||
| 201 | public static Presence getActivePresence(Collection<? extends Presence> presences) { | ||
| 202 | |||
| 203 | PriorityQueue<Presence> online = new PriorityQueue<Presence>(presences.size(),new PresenceAccountCmp()); | ||
| 204 | PriorityQueue<Presence> away = new PriorityQueue<Presence>(presences.size(),new PresenceAccountCmp()); | ||
| 205 | PriorityQueue<Presence> offline = new PriorityQueue<Presence>(presences.size(),new PresenceAccountCmp()); | ||
| 206 | |||
| 207 | for (Presence presence : presences) { | ||
| 208 | switch (presence.getShow()) { | ||
| 209 | case Online: | ||
| 210 | online.add(presence); | ||
| 211 | break; | ||
| 212 | case Away: | ||
| 213 | away.add(presence); | ||
| 214 | break; | ||
| 215 | case FFC: | ||
| 216 | online.add(presence); | ||
| 217 | break; | ||
| 218 | case XA: | ||
| 219 | away.add(presence); | ||
| 220 | break; | ||
| 221 | case DND: | ||
| 222 | away.add(presence); | ||
| 223 | break; | ||
| 224 | case None: | ||
| 225 | offline.add(presence); | ||
| 226 | break; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | Presence accountPresence = null; | ||
| 231 | if (!online.isEmpty()) { | ||
| 232 | accountPresence = online.peek(); | ||
| 233 | } | ||
| 234 | else if (!away.isEmpty()) { | ||
| 235 | accountPresence = away.peek(); | ||
| 236 | } | ||
| 237 | else if (!offline.isEmpty()) { | ||
| 238 | accountPresence = offline.peek(); | ||
| 239 | } | ||
| 240 | return accountPresence; | ||
| 241 | } | ||
| 242 | |||
| 243 | /** | ||
| 244 | * This considers all online resources of a bare JID and returns | ||
| 245 | * the value returned by {@link #getActivePresence(List)} | ||
| 246 | * when passing this list. | ||
| 247 | * @param jid A bare JID | ||
| 248 | * @return The value returned by {@link #getActivePresence(List)} | ||
| 249 | */ | ||
| 250 | public Presence getAccountPresence(JID jid) { | ||
| 251 | Collection<Presence> allPresences = getAllPresence(jid.toBare()); | ||
| 252 | Presence accountPresence = getActivePresence(allPresences); | ||
| 253 | return accountPresence; | ||
| 254 | } | ||
| 138 | 255 | ||
| 139 | public Presence getHighestPriorityPresence(final JID bareJID) { | 256 | public Presence getHighestPriorityPresence(final JID bareJID) { |
| 140 | Map<JID,Presence> presenceMap = entries_.get(bareJID); | 257 | Map<JID,Presence> presenceMap = entries_.get(bareJID); |
diff --git a/test/com/isode/stroke/parser/payloadparsers/JingleParserTest.java b/test/com/isode/stroke/parser/payloadparsers/JingleParserTest.java index ec7f044..d561222 100644 --- a/test/com/isode/stroke/parser/payloadparsers/JingleParserTest.java +++ b/test/com/isode/stroke/parser/payloadparsers/JingleParserTest.java | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | * See Documentation/Licenses/BSD-simplified.txt for more information. | 4 | * See Documentation/Licenses/BSD-simplified.txt for more information. |
| 5 | */ | 5 | */ |
| 6 | /* | 6 | /* |
| 7 | * Copyright (c) 2015 Isode Limited. | 7 | * Copyright (c) 2015-2016 Isode Limited. |
| 8 | * All rights reserved. | 8 | * All rights reserved. |
| 9 | * See the COPYING file for more information. | 9 | * See the COPYING file for more information. |
| 10 | */ | 10 | */ |
| @@ -18,28 +18,27 @@ package com.isode.stroke.parser.payloadparsers; | |||
| 18 | 18 | ||
| 19 | import static org.junit.Assert.assertEquals; | 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertNotNull; | 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertNull; | 21 | |
| 22 | import java.net.InetAddress; | ||
| 23 | import java.net.UnknownHostException; | ||
| 24 | import java.util.Vector; | ||
| 25 | import java.util.logging.Logger; | ||
| 26 | |||
| 22 | import org.junit.Test; | 27 | import org.junit.Test; |
| 23 | import com.isode.stroke.serializer.payloadserializers.JingleContentPayloadSerializer; | 28 | |
| 24 | import com.isode.stroke.elements.JinglePayload; | 29 | import com.isode.stroke.base.DateTime; |
| 25 | import com.isode.stroke.elements.JingleContentPayload; | 30 | import com.isode.stroke.elements.JingleContentPayload; |
| 26 | import com.isode.stroke.elements.JingleFileTransferDescription; | 31 | import com.isode.stroke.elements.JingleFileTransferDescription; |
| 27 | import com.isode.stroke.elements.JingleFileTransferFileInfo; | 32 | import com.isode.stroke.elements.JingleFileTransferFileInfo; |
| 33 | import com.isode.stroke.elements.JingleFileTransferHash; | ||
| 28 | import com.isode.stroke.elements.JingleIBBTransportPayload; | 34 | import com.isode.stroke.elements.JingleIBBTransportPayload; |
| 35 | import com.isode.stroke.elements.JinglePayload; | ||
| 29 | import com.isode.stroke.elements.JingleS5BTransportPayload; | 36 | import com.isode.stroke.elements.JingleS5BTransportPayload; |
| 30 | import com.isode.stroke.elements.JingleFileTransferHash; | ||
| 31 | import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; | ||
| 32 | import com.isode.stroke.eventloop.DummyEventLoop; | 37 | import com.isode.stroke.eventloop.DummyEventLoop; |
| 33 | import com.isode.stroke.jid.JID; | 38 | import com.isode.stroke.jid.JID; |
| 34 | import com.isode.stroke.base.DateTime; | ||
| 35 | import com.isode.stroke.stringcodecs.Base64; | ||
| 36 | import java.util.Vector; | ||
| 37 | import java.util.logging.Logger; | ||
| 38 | import java.util.logging.Level; | ||
| 39 | import com.isode.stroke.network.HostAddress; | 39 | import com.isode.stroke.network.HostAddress; |
| 40 | import com.isode.stroke.network.HostAddressPort; | 40 | import com.isode.stroke.network.HostAddressPort; |
| 41 | import java.net.InetAddress; | 41 | import com.isode.stroke.stringcodecs.Base64; |
| 42 | import java.net.UnknownHostException; | ||
| 43 | 42 | ||
| 44 | public class JingleParserTest { | 43 | public class JingleParserTest { |
| 45 | 44 | ||
| @@ -431,7 +430,6 @@ public class JingleParserTest { | |||
| 431 | // http://xmpp.org/extensions/xep-0234.html#example-10 | 430 | // http://xmpp.org/extensions/xep-0234.html#example-10 |
| 432 | @Test | 431 | @Test |
| 433 | public void testParse_Xep0234_Example10() { | 432 | public void testParse_Xep0234_Example10() { |
| 434 | logger_.setLevel(Level.FINE); | ||
| 435 | DummyEventLoop eventLoop = new DummyEventLoop(); | 433 | DummyEventLoop eventLoop = new DummyEventLoop(); |
| 436 | PayloadsParserTester parser = new PayloadsParserTester(eventLoop); | 434 | PayloadsParserTester parser = new PayloadsParserTester(eventLoop); |
| 437 | assertNotNull(parser.parse( | 435 | assertNotNull(parser.parse( |
diff --git a/test/com/isode/stroke/presence/PresenceOracleTest.java b/test/com/isode/stroke/presence/PresenceOracleTest.java index ac82045..9a440e6 100644 --- a/test/com/isode/stroke/presence/PresenceOracleTest.java +++ b/test/com/isode/stroke/presence/PresenceOracleTest.java | |||
| @@ -12,32 +12,24 @@ | |||
| 12 | package com.isode.stroke.presence; | 12 | package com.isode.stroke.presence; |
| 13 | 13 | ||
| 14 | import static org.junit.Assert.assertEquals; | 14 | import static org.junit.Assert.assertEquals; |
| 15 | import static org.junit.Assert.assertNotNull; | ||
| 16 | import static org.junit.Assert.assertNull; | 15 | import static org.junit.Assert.assertNull; |
| 17 | import static org.junit.Assert.assertFalse; | ||
| 18 | import static org.junit.Assert.assertTrue; | ||
| 19 | 16 | ||
| 20 | import org.junit.Test; | 17 | import java.util.ArrayList; |
| 18 | import java.util.Collection; | ||
| 19 | import java.util.List; | ||
| 20 | import java.util.Vector; | ||
| 21 | |||
| 21 | import org.junit.Before; | 22 | import org.junit.Before; |
| 23 | import org.junit.Test; | ||
| 22 | 24 | ||
| 23 | import com.isode.stroke.elements.Presence; | ||
| 24 | import com.isode.stroke.elements.Payload; | ||
| 25 | import com.isode.stroke.client.DummyStanzaChannel; | 25 | import com.isode.stroke.client.DummyStanzaChannel; |
| 26 | import com.isode.stroke.presence.DirectedPresenceSender; | 26 | import com.isode.stroke.elements.Presence; |
| 27 | import com.isode.stroke.presence.StanzaChannelPresenceSender; | 27 | import com.isode.stroke.elements.StatusShow; |
| 28 | import com.isode.stroke.presence.PayloadAddingPresenceSender; | ||
| 29 | import com.isode.stroke.presence.PresenceOracle; | ||
| 30 | import com.isode.stroke.presence.SubscriptionManager; | ||
| 31 | import com.isode.stroke.jid.JID; | 28 | import com.isode.stroke.jid.JID; |
| 32 | import com.isode.stroke.roster.XMPPRoster; | 29 | import com.isode.stroke.roster.XMPPRoster; |
| 33 | import com.isode.stroke.roster.XMPPRosterImpl; | 30 | import com.isode.stroke.roster.XMPPRosterImpl; |
| 34 | import com.isode.stroke.signals.SignalConnection; | ||
| 35 | import com.isode.stroke.signals.Slot2; | ||
| 36 | import com.isode.stroke.signals.Slot3; | ||
| 37 | import com.isode.stroke.signals.Slot1; | 31 | import com.isode.stroke.signals.Slot1; |
| 38 | 32 | import com.isode.stroke.signals.Slot3; | |
| 39 | import java.util.Collection; | ||
| 40 | import java.util.Vector; | ||
| 41 | 33 | ||
| 42 | public class PresenceOracleTest { | 34 | public class PresenceOracleTest { |
| 43 | 35 | ||
| @@ -91,6 +83,16 @@ public class PresenceOracleTest { | |||
| 91 | sentPresence.setFrom(jid); | 83 | sentPresence.setFrom(jid); |
| 92 | return sentPresence; | 84 | return sentPresence; |
| 93 | } | 85 | } |
| 86 | |||
| 87 | private Presence createPresence(JID jid, int priority, Presence.Type type, StatusShow.Type statusShow) { | ||
| 88 | Presence presence = new Presence(); | ||
| 89 | presence.setFrom(jid); | ||
| 90 | presence.setPriority(priority); | ||
| 91 | presence.setType(type); | ||
| 92 | presence.setShow(statusShow); | ||
| 93 | assertEquals(statusShow,presence.getShow()); | ||
| 94 | return presence; | ||
| 95 | } | ||
| 94 | 96 | ||
| 95 | @Before | 97 | @Before |
| 96 | public void setUp() { | 98 | public void setUp() { |
| @@ -222,5 +224,50 @@ public class PresenceOracleTest { | |||
| 222 | stanzaChannel_.setAvailable(true); | 224 | stanzaChannel_.setAvailable(true); |
| 223 | 225 | ||
| 224 | assertNull(oracle_.getLastPresence(user1)); | 226 | assertNull(oracle_.getLastPresence(user1)); |
| 225 | } | 227 | } |
| 226 | } \ No newline at end of file | 228 | |
| 229 | @Test | ||
| 230 | public void testGetActivePresence() { | ||
| 231 | { | ||
| 232 | List<Presence> presenceList = new ArrayList<Presence>(); | ||
| 233 | presenceList.add(createPresence(new JID("alice@wonderland.lit/resourceA"), 10, | ||
| 234 | Presence.Type.Available, StatusShow.Type.Away)); | ||
| 235 | presenceList.add(createPresence(new JID("alice@wonderland.lit/resourceB"), 5, | ||
| 236 | Presence.Type.Available, StatusShow.Type.Online)); | ||
| 237 | |||
| 238 | assertEquals(StatusShow.Type.Online,PresenceOracle.getActivePresence(presenceList).getShow()); | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | { | ||
| 243 | List<Presence> presenceList = new ArrayList<Presence>(); | ||
| 244 | presenceList.add(createPresence(new JID("alice@wonderland.lit/resourceA"), 10, | ||
| 245 | Presence.Type.Available, StatusShow.Type.Away)); | ||
| 246 | presenceList.add(createPresence(new JID("alice@wonderland.lit/resourceB"), 5, | ||
| 247 | Presence.Type.Available, StatusShow.Type.DND)); | ||
| 248 | |||
| 249 | assertEquals(StatusShow.Type.DND,PresenceOracle.getActivePresence(presenceList).getShow()); | ||
| 250 | } | ||
| 251 | |||
| 252 | { | ||
| 253 | List<Presence> presenceList = new ArrayList<Presence>(); | ||
| 254 | presenceList.add(createPresence(new JID("alice@wonderland.lit/resourceA"), 0, | ||
| 255 | Presence.Type.Available, StatusShow.Type.Online)); | ||
| 256 | presenceList.add(createPresence(new JID("alice@wonderland.lit/resourceB"), 0, | ||
| 257 | Presence.Type.Available, StatusShow.Type.DND)); | ||
| 258 | |||
| 259 | assertEquals(StatusShow.Type.Online,PresenceOracle.getActivePresence(presenceList).getShow()); | ||
| 260 | } | ||
| 261 | |||
| 262 | { | ||
| 263 | List<Presence> presenceList = new ArrayList<Presence>(); | ||
| 264 | presenceList.add(createPresence(new JID("alice@wonderland.lit/resourceA"), 1, | ||
| 265 | Presence.Type.Available, StatusShow.Type.Online)); | ||
| 266 | presenceList.add(createPresence(new JID("alice@wonderland.lit/resourceB"), 0, | ||
| 267 | Presence.Type.Available, StatusShow.Type.Online)); | ||
| 268 | |||
| 269 | assertEquals(new JID("alice@wonderland.lit/resourceA"), PresenceOracle.getActivePresence(presenceList).getFrom()); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | } | ||
Swift