Skip to content

Commit 150ff80

Browse files
committed
add setGroupState() method and tests
1 parent 96683b9 commit 150ff80

2 files changed

Lines changed: 147 additions & 28 deletions

File tree

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

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -967,38 +967,71 @@ public void whereNow(Callback callback) {
967967
}
968968

969969
public void setState(String channel, String uuid, JSONObject state, Callback callback) {
970+
_setState(channelSubscriptions, PubnubUtil.urlEncode(channel), null, uuid, state, callback);
971+
}
970972

971-
final Callback cb = getWrappedCallback(callback);
973+
public void setStateGroup(String group, String uuid, JSONObject state, Callback callback) {
974+
_setState(channelSubscriptions, ".", group, uuid, state, callback);
975+
}
972976

977+
protected void _setState(Subscriptions sub, String channel, String group, String uuid, JSONObject state, Callback callback) {
978+
SubscriptionItem item = sub.getItem(channel);
979+
final Callback cb = getWrappedCallback(callback);
973980
Hashtable parameters = PubnubUtil.hashtableClone(params);
974-
String[] urlargs = { getPubnubUrl(), "v2", "presence", "sub-key",
975-
this.SUBSCRIBE_KEY, "channel", PubnubUtil.urlEncode(channel), "uuid", PubnubUtil.urlEncode(uuid),
981+
982+
String[] urlArgs = { getPubnubUrl(), "v2", "presence", "sub-key",
983+
this.SUBSCRIBE_KEY, "channel", channel, "uuid", PubnubUtil.urlEncode(uuid),
976984
"data"
977985
};
986+
978987
if (state != null) parameters.put("state", state.toString());
979-
SubscriptionItem ch = channelSubscriptions.getItem(channel);
980-
if (ch != null) {
988+
if (group != null) parameters.put("channel-group", group);
989+
990+
if (item != null) {
981991
try {
982-
channelSubscriptions.state.put(channel, state);
992+
sub.state.put(channel, state);
983993
} catch (JSONException e) {
984994

985995
}
986996
}
987-
HttpRequest hreq = new HttpRequest(urlargs, parameters,
997+
998+
HttpRequest hreq = new HttpRequest(urlArgs, parameters,
988999
new ResponseHandler() {
9891000
public void handleResponse(HttpRequest hreq, String response) {
9901001
invokeCallback("", response, "payload", cb, 2 );
9911002
}
9921003

9931004
public void handleError(HttpRequest hreq, PubnubError error) {
9941005
cb.errorCallback("", error);
995-
return;
9961006
}
9971007
});
9981008

9991009
_request(hreq, nonSubscribeManager);
10001010
}
10011011

1012+
protected void getState(String channel, String uuid, Callback callback) {
1013+
final Callback cb = getWrappedCallback(callback);
1014+
Hashtable parameters = PubnubUtil.hashtableClone(params);
1015+
1016+
String[] urlArgs = { getPubnubUrl(), "v2", "presence", "sub-key",
1017+
this.SUBSCRIBE_KEY, "channel", PubnubUtil.urlEncode(channel),
1018+
"uuid", PubnubUtil.urlEncode(uuid)
1019+
};
1020+
1021+
HttpRequest hreq = new HttpRequest(urlArgs, parameters,
1022+
new ResponseHandler() {
1023+
public void handleResponse(HttpRequest hreq, String response) {
1024+
invokeCallback("", response, "payload", cb, 1 );
1025+
}
1026+
1027+
public void handleError(HttpRequest hreq, PubnubError error) {
1028+
cb.errorCallback("", error);
1029+
}
1030+
});
1031+
1032+
_request(hreq, nonSubscribeManager);
1033+
}
1034+
10021035
protected void invokeCallback(String channel, String response, String key,
10031036
Callback callback, int extendedErrorCode) {
10041037
invokeCallback(channel, response, key, callback, extendedErrorCode, false);
@@ -1268,26 +1301,6 @@ public void handleError(HttpRequest hreq, PubnubError error) {
12681301
_request(hreq, nonSubscribeManager);
12691302
}
12701303

1271-
public void getState(String channel, String uuid, Callback callback) {
1272-
final Callback cb = getWrappedCallback(callback);
1273-
String[] urlargs = { getPubnubUrl(), "v2", "presence", "sub-key",
1274-
this.SUBSCRIBE_KEY, "channel", PubnubUtil.urlEncode(channel), "uuid", PubnubUtil.urlEncode(uuid)
1275-
};
1276-
1277-
HttpRequest hreq = new HttpRequest(urlargs, params,
1278-
new ResponseHandler() {
1279-
public void handleResponse(HttpRequest hreq, String response) {
1280-
invokeCallback("", response, "payload", cb, 1 );
1281-
}
1282-
1283-
public void handleError(HttpRequest hreq, PubnubError error) {
1284-
cb.errorCallback("", error);
1285-
return;
1286-
}
1287-
});
1288-
1289-
_request(hreq, nonSubscribeManager);
1290-
}
12911304
/**
12921305
* Read presence information from a channel
12931306
*
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.pubnub.api;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.util.concurrent.CountDownLatch;
9+
import java.util.concurrent.TimeUnit;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class StateTest {
14+
Pubnub pubnub;
15+
String channel;
16+
String group;
17+
String auth_key;
18+
19+
double random;
20+
21+
@Before
22+
public void setUp() throws InterruptedException {
23+
random = Math.random();
24+
25+
pubnub = new Pubnub("demo", "demo");
26+
pubnub.setOrigin("dara24.devbuild");
27+
pubnub.setCacheBusting(false);
28+
29+
group = "jtest-" + random;
30+
channel = "ch1" + random;
31+
}
32+
33+
@Test
34+
public void testStateForChannel()
35+
throws InterruptedException, PubnubException, JSONException {
36+
37+
JSONObject state = new JSONObject();
38+
state.put("nickname", "jtester");
39+
state.put("status", "online");
40+
state.put("age", 32);
41+
42+
final CountDownLatch latch1 = new CountDownLatch(1);
43+
final CountDownLatch latch2 = new CountDownLatch(1);
44+
final CountDownLatch latch3 = new CountDownLatch(1);
45+
46+
final TestHelper.SimpleCallback cb1 = new TestHelper.SubscribeCallback(latch1);
47+
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
48+
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
49+
50+
pubnub.setHeartbeat(6);
51+
pubnub.subscribe(channel, cb1);
52+
latch1.await(10, TimeUnit.SECONDS);
53+
54+
pubnub.setState(channel, pubnub.getUUID(), state, cb2);
55+
latch2.await(10, TimeUnit.SECONDS);
56+
57+
pubnub.getState(channel, pubnub.getUUID(), cb3);
58+
latch3.await(10, TimeUnit.SECONDS);
59+
60+
JSONObject result = (JSONObject) cb3.getResponse();
61+
62+
assertEquals(state.getString("nickname"), result.getString("nickname"));
63+
assertEquals(state.getString("status"), result.getString("status"));
64+
assertEquals(state.getInt("age"), result.getInt("age"));
65+
}
66+
67+
@Test
68+
public void testStateForChannelGroup()
69+
throws InterruptedException, PubnubException, JSONException {
70+
71+
JSONObject state = new JSONObject();
72+
state.put("nickname", "jtester");
73+
state.put("status", "online");
74+
state.put("age", 32);
75+
76+
final CountDownLatch latch1 = new CountDownLatch(1);
77+
final CountDownLatch latch2 = new CountDownLatch(1);
78+
final CountDownLatch latch3 = new CountDownLatch(1);
79+
final CountDownLatch latch4 = new CountDownLatch(1);
80+
81+
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
82+
final TestHelper.SubscribeCallback cb2 = new TestHelper.SubscribeCallback(latch2);
83+
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
84+
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4);
85+
86+
pubnub.setHeartbeat(6);
87+
88+
pubnub.addChannelToGroup(group, channel, cb1);
89+
latch1.await(10, TimeUnit.SECONDS);
90+
91+
pubnub.subscribeGroup(group, cb2);
92+
latch2.await(10, TimeUnit.SECONDS);
93+
94+
pubnub.setStateGroup(group, pubnub.getUUID(), state, cb3);
95+
latch3.await(10, TimeUnit.SECONDS);
96+
97+
pubnub.getState(channel, pubnub.getUUID(), cb4);
98+
latch4.await(10, TimeUnit.SECONDS);
99+
100+
JSONObject result = (JSONObject) cb4.getResponse();
101+
102+
assertEquals(state.getString("nickname"), result.getString("nickname"));
103+
assertEquals(state.getString("status"), result.getString("status"));
104+
assertEquals(state.getInt("age"), result.getInt("age"));
105+
}
106+
}

0 commit comments

Comments
 (0)