@@ -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
0 commit comments