Skip to content

Commit 38d096c

Browse files
committed
update logic of wildcard channel response parsing
1 parent 5650070 commit 38d096c

10 files changed

Lines changed: 639 additions & 53 deletions

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

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ abstract class PubnubCore {
4747
private volatile String _timetoken = "0";
4848
private volatile String _saved_timetoken = "0";
4949

50-
private String PRESENCE_SUFFIX = "-pnpres";
50+
protected static String PRESENCE_SUFFIX = "-pnpres";
51+
protected static String WILDCARD_SUFFIX = "*";
5152
protected static String VERSION = "";
5253
private Random generator = new Random();
5354

@@ -195,7 +196,7 @@ public void handleError(HttpRequest hreq, PubnubError error) {
195196
* Presence Expiry timeout in seconds
196197
*/
197198
public void setPnExpires(int pnexpires, Callback callback) {
198-
setHeartbeat(pnexpires,callback);
199+
setHeartbeat(pnexpires, callback);
199200
}
200201

201202
/**
@@ -2315,12 +2316,33 @@ private void _subscribe(Hashtable args) {
23152316

23162317
for (int i = 0; i < channelList.length; i++) {
23172318
String channel = channelList[i];
2318-
SubscriptionItem channelObj = (SubscriptionItem) channelSubscriptions.getItem(channel);
23192319

2320-
if (channelObj == null) {
2321-
SubscriptionItem ch = new SubscriptionItem(channel, callback);
2320+
// HACK: wrap '*-pnpres' channels with cutted-off suffix
2321+
if (channel.endsWith(WILDCARD_SUFFIX + PRESENCE_SUFFIX)) {
2322+
String messagesChannel = channel.substring(0, channel.indexOf(PRESENCE_SUFFIX));
23222323

2323-
channelSubscriptions.addItem(ch);
2324+
SubscriptionItem wildcardMessagesObj = (SubscriptionItem) channelSubscriptions.getItem(messagesChannel);
2325+
SubscriptionItem wildcardPresenceObj = (SubscriptionItem) channelSubscriptions.getItem(channel);
2326+
2327+
if (wildcardMessagesObj == null) {
2328+
SubscriptionItem ch = new SubscriptionItem(messagesChannel, callback);
2329+
2330+
channelSubscriptions.addItem(ch);
2331+
}
2332+
2333+
if (wildcardPresenceObj == null) {
2334+
SubscriptionItem pr = new SubscriptionItem(channel, callback);
2335+
2336+
channelSubscriptions.addItem(pr);
2337+
}
2338+
} else {
2339+
SubscriptionItem channelObj = (SubscriptionItem) channelSubscriptions.getItem(channel);
2340+
2341+
if (channelObj == null) {
2342+
SubscriptionItem ch = new SubscriptionItem(channel, callback);
2343+
2344+
channelSubscriptions.addItem(ch);
2345+
}
23242346
}
23252347
}
23262348

@@ -2357,7 +2379,7 @@ private boolean isWorkerDead(HttpRequest hreq) {
23572379
return (hreq == null || hreq.getWorker() == null)?false:hreq.getWorker()._die;
23582380
}
23592381
private void _subscribe_base(boolean fresh, boolean dar, Worker worker) {
2360-
String channelString = channelSubscriptions.getItemString();
2382+
String channelString = channelSubscriptions.getItemStringSorted();
23612383
String groupString = channelGroupSubscriptions.getItemString();
23622384
String[] channelsArray = channelSubscriptions.getItemNames();
23632385
String[] groupsArray = channelGroupSubscriptions.getItemNames();
@@ -2444,26 +2466,8 @@ public void handleResponse(HttpRequest hreq, String response) {
24442466
String[] _channels = PubnubUtil.splitString(jsa.getString(3), ",");
24452467

24462468
for (int i = 0; i < _channels.length; i++) {
2447-
String _groupName = _groups[i];
2448-
String _channelName = _channels[i];
2449-
Object message = messages.get(i);
2450-
2451-
SubscriptionItem _group = channelGroupSubscriptions.getItem(_groups[i]);
2452-
SubscriptionItem _channel = channelSubscriptions.getItem(_groups[i]);
2453-
2454-
if ((_groupName.equals(_channelName) || _groupName.endsWith("*"))
2455-
&& _channel != null
2456-
&& !isWorkerDead(hreq)
2457-
) {
2458-
invokeSubscribeCallback(_channelName, _channel.callback,
2459-
message, _timetoken, hreq);
2460-
} else if (!_groupName.equals(_channelName)
2461-
&& _group != null
2462-
&& !isWorkerDead(hreq)
2463-
) {
2464-
invokeSubscribeCallback(_channelName, _group.callback,
2465-
message, _timetoken, hreq);
2466-
}
2469+
handleFourElementsSubscribeResponse(_groups[i], _channels[i], messages.get(i),
2470+
_timetoken, hreq);
24672471
}
24682472
} else if(jsa.length() == 3) {
24692473
/*
@@ -2553,6 +2557,45 @@ public String getTimetoken() {
25532557
_request(hreq, subscribeManager, fresh);
25542558
}
25552559

2560+
/**
2561+
* Logic of 4-elements success response is to complex, so it's excluded into separate method
2562+
*
2563+
* @param thirdString element of JSON response
2564+
* @param fourthString element of JSON response
2565+
* @param message from JSON response
2566+
* @param timetoken from response
2567+
* @param hreq request
2568+
* @throws JSONException
2569+
*/
2570+
private void handleFourElementsSubscribeResponse(String thirdString, String fourthString, Object message,
2571+
String timetoken, HttpRequest hreq) throws JSONException {
2572+
2573+
SubscriptionItem thirdChannelGroup = channelGroupSubscriptions.getItem(thirdString);
2574+
SubscriptionItem thirdChannel = channelSubscriptions.getItem(thirdString);
2575+
SubscriptionItem fourthChannel = channelSubscriptions.getItem(fourthString);
2576+
2577+
if (isWorkerDead(hreq)) return;
2578+
2579+
if (thirdString.equals(fourthString) && fourthChannel != null) {
2580+
invokeSubscribeCallback(fourthString, fourthChannel.callback, message, timetoken, hreq);
2581+
} else if (thirdString.endsWith("*")) {
2582+
if (fourthChannel != null && fourthString.endsWith(PRESENCE_SUFFIX)) {
2583+
invokeSubscribeCallback(fourthString, fourthChannel.callback, message, timetoken, hreq);
2584+
} else if (thirdChannelGroup != null && !fourthString.endsWith(PRESENCE_SUFFIX)) {
2585+
invokeSubscribeCallback(fourthString, thirdChannelGroup.callback, message, timetoken, hreq);
2586+
} else if (thirdChannel != null && thirdString.endsWith(WILDCARD_SUFFIX)) {
2587+
invokeSubscribeCallback(fourthString, thirdChannel.callback, message, timetoken, hreq);
2588+
} else {
2589+
System.out.println("ERROR: Unable to handle wildcard response: " + message);
2590+
}
2591+
} else if (!thirdString.equals(fourthString) && thirdChannelGroup != null) {
2592+
invokeSubscribeCallback(fourthString, thirdChannelGroup.callback,
2593+
message, timetoken, hreq);
2594+
} else {
2595+
System.out.println("ERROR: Unable to handle response: " + message);
2596+
}
2597+
}
2598+
25562599
private void invokeSubscribeCallback(String channel, Callback callback, Object message, String timetoken,
25572600
HttpRequest hreq) throws JSONException {
25582601
if (CIPHER_KEY.length() > 0

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,47 @@ public static synchronized String hashTableKeysToDelimitedString(
146146
}
147147
}
148148
return sb.toString();
149+
}
150+
151+
public static synchronized String hashTableKeysToSortedSuffixString(
152+
Hashtable ht, String delimiter, String lastSuffix) {
153+
154+
StringBuffer sb = new StringBuffer();
155+
StringBuffer sbPresence = new StringBuffer();
156+
boolean first = true;
157+
boolean firstPresence = true;
158+
Enumeration e = ht.keys();
159+
160+
while (e.hasMoreElements()) {
161+
162+
String s = (String) e.nextElement();
149163

164+
if (s.endsWith(lastSuffix)) {
165+
if (firstPresence) {
166+
sbPresence.append(s);
167+
firstPresence = false;
168+
} else {
169+
sbPresence.append(delimiter).append(s);
170+
}
171+
} else {
172+
if (first) {
173+
sb.append(s);
174+
first = false;
175+
} else {
176+
sb.append(delimiter).append(s);
177+
}
178+
}
179+
}
180+
181+
if (sb.length() > 0 && sbPresence.length() > 0) {
182+
return sb.toString() + delimiter + sbPresence.toString();
183+
} else if (sb.length() > 0 && sbPresence.length() == 0) {
184+
return sb.toString();
185+
} else if (sb.length() == 0 && sbPresence.length() > 0) {
186+
return sbPresence.toString();
187+
} else {
188+
return "";
189+
}
150190
}
151191

152192
/**
@@ -158,11 +198,8 @@ public static synchronized String hashTableKeysToDelimitedString(
158198
* , String
159199
* @return , string array with hash keys string
160200
*/
161-
public static String hashTableKeysToDelimitedString(
162-
Hashtable ht, String delimiter) {
163-
201+
public static String hashTableKeysToDelimitedString(Hashtable ht, String delimiter) {
164202
return hashTableKeysToDelimitedString(ht, delimiter, null);
165-
166203
}
167204

168205
static Hashtable hashtableClone(Hashtable ht) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ class SubscriptionItem {
1818
this.callback = callback;
1919
this.connected = false;
2020
}
21+
22+
public String toString() {
23+
return this.name;
24+
}
2125
}
2226

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public String getItemStringNoPresence() {
5555
return PubnubUtil.hashTableKeysToDelimitedString(items, ",", "-pnpres");
5656
}
5757

58+
public String getItemStringSorted() {
59+
return PubnubUtil.hashTableKeysToSortedSuffixString(items, ",", "-pnpres");
60+
}
61+
5862
public String getItemString() {
5963
return PubnubUtil.hashTableKeysToDelimitedString(items, ",");
6064
}

0 commit comments

Comments
 (0)