Skip to content

Commit 49fe8be

Browse files
committed
add hereNowGroup() method and tests for HereNow
1 parent 37af9c7 commit 49fe8be

2 files changed

Lines changed: 268 additions & 30 deletions

File tree

java/srcPubnubApi/com/pubnub/api/PubnubCore.java

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.IOException;
1111
import java.io.UnsupportedEncodingException;
12+
import java.util.ArrayList;
1213
import java.util.Hashtable;
1314
import java.util.Random;
1415

@@ -1296,54 +1297,79 @@ public void handleError(HttpRequest hreq, PubnubError error) {
12961297
* object of sub class of Callback class
12971298
*/
12981299
public void hereNow(final String channel, Callback callback) {
1299-
hereNow(channel, false, true, callback);
1300+
hereNow(new String[]{channel}, null, false, true, callback);
13001301
}
13011302

13021303
public void hereNow(boolean state, boolean uuids, Callback callback) {
1303-
hereNow(null, state, uuids, callback);
1304+
hereNow(null, null, state, uuids, callback);
1305+
}
1306+
1307+
public void hereNow(final String channel, boolean state, boolean uuids, Callback callback) {
1308+
hereNow(new String[]{channel}, null, state, uuids, callback);
1309+
}
1310+
1311+
public void hereNowGroup(String group, Callback callback) {
1312+
hereNowGroup(group, false, true, callback);
1313+
}
1314+
1315+
public void hereNowGroup(String group, boolean state, boolean uuids, Callback callback) {
1316+
hereNowGroup(new String[]{group}, state, uuids, callback);
1317+
}
1318+
1319+
public void hereNowGroup(String[] groups, boolean state, boolean uuids, Callback callback) {
1320+
hereNow(null, groups, state, uuids, callback);
13041321
}
13051322

13061323
/**
1307-
* Read presence information from a channel
1324+
* Read presence information from a channel or a channel group
13081325
*
1309-
* @param channel
1310-
* Channel name
1311-
* @param state
1312-
* state enabled ?
1313-
* @param uuids
1314-
* enable / disable returning uuids in response ?
1315-
* @param callback
1316-
* object of sub class of Callback class
1326+
* @param channels array
1327+
* @param channelGroups array
1328+
* @param state state enabled ?
1329+
* @param uuids enable / disable returning uuids in response ?
1330+
* @param callback object of sub class of Callback class
13171331
*/
1318-
public void hereNow(final String channel, boolean state, boolean uuids, Callback callback) {
1332+
public void hereNow(String[] channels, String[] channelGroups, boolean state,
1333+
boolean uuids, Callback callback) {
13191334

13201335
final Callback cb = getWrappedCallback(callback);
1321-
13221336
Hashtable parameters = PubnubUtil.hashtableClone(params);
1323-
String[] urlargs = null;
1324-
if (channel != null && channel.length() > 0) {
1325-
urlargs = new String[]{ getPubnubUrl(), "v2", "presence", "sub_key",
1326-
this.SUBSCRIBE_KEY, "channel", PubnubUtil.urlEncode(channel)
1327-
};
1328-
} else {
1329-
urlargs = new String[]{ getPubnubUrl(), "v2", "presence", "sub_key",
1330-
this.SUBSCRIBE_KEY
1331-
};
1337+
ArrayList<String> urlArgs = new ArrayList<String>();
1338+
1339+
urlArgs.add(getPubnubUrl());
1340+
urlArgs.add("v2");
1341+
urlArgs.add("presence");
1342+
urlArgs.add("sub_key");
1343+
urlArgs.add(this.SUBSCRIBE_KEY);
1344+
1345+
if (channels != null || channelGroups != null) {
1346+
String channelsString = PubnubUtil.joinString(channels, ",");
1347+
if ("".equals(channelsString)) {
1348+
channelsString = ",";
1349+
} else {
1350+
channelsString = PubnubUtil.urlEncode(channelsString);
1351+
}
1352+
1353+
urlArgs.add("channel");
1354+
urlArgs.add(channelsString);
13321355
}
13331356

13341357
if (state) parameters.put("state", "1");
13351358
if (!uuids) parameters.put("disable_uuids", "1");
1359+
if (channelGroups != null && channelGroups.length > 0) {
1360+
parameters.put("channel-group", PubnubUtil.joinString(channelGroups, ","));
1361+
}
13361362

1337-
HttpRequest hreq = new HttpRequest(urlargs, parameters,
1363+
HttpRequest hreq = new HttpRequest(urlArgs.toArray(new String[urlArgs.size()]), parameters,
13381364
new ResponseHandler() {
1339-
public void handleResponse(HttpRequest hreq, String response) {
1340-
invokeCallback(channel, response, "payload",cb, 1);
1341-
}
1365+
public void handleResponse(HttpRequest hreq, String response) {
1366+
invokeCallback(null, response, "payload", cb, 1);
1367+
}
13421368

1343-
public void handleError(HttpRequest hreq, PubnubError error) {
1344-
cb.errorCallback(channel, error);
1345-
}
1346-
});
1369+
public void handleError(HttpRequest hreq, PubnubError error) {
1370+
cb.errorCallback(null, error);
1371+
}
1372+
});
13471373

13481374
_request(hreq, nonSubscribeManager);
13491375
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package com.pubnub.api;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.concurrent.CountDownLatch;
10+
import java.util.concurrent.TimeUnit;
11+
12+
import static com.pubnub.api.matchers.JSONAssert.assertJSONArrayHas;
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class HereNowTest {
16+
Pubnub pubnub;
17+
String group;
18+
String auth_key;
19+
20+
double random;
21+
22+
@Before
23+
public void setUp() throws InterruptedException {
24+
random = Math.random();
25+
26+
pubnub = new Pubnub("demo", "demo", "demo");
27+
pubnub.setOrigin("dara24.devbuild");
28+
pubnub.setCacheBusting(false);
29+
30+
group = "jtest-" + random;
31+
}
32+
33+
@Test
34+
public void testHereNowForOneChannel()
35+
throws InterruptedException, PubnubException, JSONException {
36+
37+
final String channel = "ch1" + random;
38+
39+
final CountDownLatch latch1 = new CountDownLatch(1);
40+
final CountDownLatch latch2 = new CountDownLatch(1);
41+
42+
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1) {
43+
@Override
44+
public void connectCallback(String item, Object message) {
45+
if (item.equals(channel)) latch.countDown();
46+
}
47+
};
48+
49+
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
50+
51+
pubnub.subscribe(channel, cb1);
52+
latch1.await(10, TimeUnit.SECONDS);
53+
Thread.sleep(1000);
54+
55+
pubnub.hereNow(channel, cb2);
56+
latch2.await(10, TimeUnit.SECONDS);
57+
58+
JSONObject response = (JSONObject) cb2.getResponse();
59+
JSONArray uuids = response.getJSONArray("uuids");
60+
assertJSONArrayHas(pubnub.getUUID(), uuids);
61+
}
62+
63+
@Test
64+
public void testHereNowForOneChannelGroup()
65+
throws InterruptedException, PubnubException, JSONException {
66+
67+
final String[] channels = new String[]{"ch1" + random, "ch2" + random};
68+
69+
final CountDownLatch latch1 = new CountDownLatch(1);
70+
final CountDownLatch latch2 = new CountDownLatch(1);
71+
final CountDownLatch latch3 = new CountDownLatch(1);
72+
73+
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
74+
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2) {
75+
@Override
76+
public void connectCallback(String item, Object message) {
77+
latch.countDown();
78+
}
79+
};
80+
81+
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
82+
83+
pubnub.addChannelToGroup(group, channels, cb1);
84+
latch1.await(10, TimeUnit.SECONDS);
85+
86+
pubnub.subscribe(channels[0], cb2);
87+
latch2.await(10, TimeUnit.SECONDS);
88+
Thread.sleep(1000);
89+
90+
pubnub.hereNowGroup(group, cb3);
91+
latch3.await(10, TimeUnit.SECONDS);
92+
93+
JSONObject response = (JSONObject) cb3.getResponse();
94+
JSONArray uuids = response
95+
.getJSONObject("channels")
96+
.getJSONObject(channels[0])
97+
.getJSONArray("uuids");
98+
99+
assertEquals(1, response.getInt("total_occupancy"));
100+
assertEquals(1, response.getInt("total_channels"));
101+
102+
assertJSONArrayHas(pubnub.getUUID(), uuids);
103+
}
104+
105+
@Test
106+
public void testHereNowForMultipleChannels() throws InterruptedException, PubnubException, JSONException {
107+
final String[] channels = new String[]{"ch1" + random, "ch2" + random};
108+
final String[] groups = new String[]{group, "jtest2-" + random};
109+
110+
final CountDownLatch latch1 = new CountDownLatch(1);
111+
final CountDownLatch latch2 = new CountDownLatch(1);
112+
final CountDownLatch latch3 = new CountDownLatch(1);
113+
final CountDownLatch latch4 = new CountDownLatch(1);
114+
115+
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
116+
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
117+
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3) {
118+
@Override
119+
public void connectCallback(String item, Object message) {
120+
latch.countDown();
121+
}
122+
};
123+
124+
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4);
125+
126+
pubnub.addChannelToGroup(groups[0], channels, cb1);
127+
pubnub.addChannelToGroup(groups[1], channels, cb2);
128+
latch1.await(10, TimeUnit.SECONDS);
129+
latch2.await(10, TimeUnit.SECONDS);
130+
131+
pubnub.subscribe(channels, cb3);
132+
latch3.await(10, TimeUnit.SECONDS);
133+
Thread.sleep(1000);
134+
135+
pubnub.hereNowGroup(group, cb4);
136+
latch4.await(10, TimeUnit.SECONDS);
137+
138+
JSONObject response = (JSONObject) cb4.getResponse();
139+
JSONArray uuids1 = response
140+
.getJSONObject("channels")
141+
.getJSONObject(channels[0])
142+
.getJSONArray("uuids");
143+
144+
JSONArray uuids2 = response
145+
.getJSONObject("channels")
146+
.getJSONObject(channels[1])
147+
.getJSONArray("uuids");
148+
149+
assertEquals(2, response.getInt("total_occupancy"));
150+
assertEquals(2, response.getInt("total_channels"));
151+
152+
assertJSONArrayHas(pubnub.getUUID(), uuids1);
153+
assertJSONArrayHas(pubnub.getUUID(), uuids2);
154+
}
155+
156+
@Test
157+
public void testHereNowGlobal() throws JSONException, InterruptedException, PubnubException {
158+
final String[] channels = new String[]{"ch1-" + random, "ch2-" + random};
159+
160+
Pubnub pubnub2 = new Pubnub("demo", "demo");
161+
pubnub2.setOrigin("dara24.devbuild");
162+
pubnub2.setCacheBusting(false);
163+
164+
final CountDownLatch latch3 = new CountDownLatch(1);
165+
final CountDownLatch latch4 = new CountDownLatch(1);
166+
final CountDownLatch latch5 = new CountDownLatch(1);
167+
168+
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3) {
169+
@Override
170+
public void connectCallback(String item, Object message) {
171+
latch.countDown();
172+
}
173+
};
174+
175+
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4) {
176+
@Override
177+
public void connectCallback(String item, Object message) {
178+
latch.countDown();
179+
}
180+
};
181+
182+
final TestHelper.SimpleCallback cb5 = new TestHelper.SimpleCallback(latch5);
183+
184+
pubnub.subscribe(channels[0], cb3);
185+
latch3.await(10, TimeUnit.SECONDS);
186+
pubnub2.subscribe(channels[1], cb4);
187+
latch4.await(10, TimeUnit.SECONDS);
188+
Thread.sleep(1000);
189+
190+
pubnub.hereNow(true, true, cb5);
191+
latch5.await(10, TimeUnit.SECONDS);
192+
193+
JSONObject response = (JSONObject) cb5.getResponse();
194+
195+
String uuid1 = response
196+
.getJSONObject("channels")
197+
.getJSONObject(channels[0])
198+
.getJSONArray("uuids")
199+
.getJSONObject(0)
200+
.getString("uuid");
201+
202+
String uuid2 = response
203+
.getJSONObject("channels")
204+
.getJSONObject(channels[1])
205+
.getJSONArray("uuids")
206+
.getJSONObject(0)
207+
.getString("uuid");
208+
209+
assertEquals(pubnub.getUUID(), uuid1);
210+
assertEquals(pubnub2.getUUID(), uuid2);
211+
}
212+
}

0 commit comments

Comments
 (0)