|
1 | 1 | package com.pubnub.api; |
2 | 2 |
|
3 | | -import org.json.JSONArray; |
4 | | -import org.json.JSONException; |
5 | | -import org.json.JSONObject; |
6 | | - |
7 | | -import java.util.ArrayList; |
8 | | -import java.util.concurrent.CountDownLatch; |
9 | | - |
10 | 3 | public class ChannelGroup extends SubscriptionItem{ |
11 | | - ArrayList channels = new ArrayList(); |
12 | | - |
13 | | - public static class RetrieveChannelsCallback extends Callback { |
14 | | - private CountDownLatch latch; |
15 | | - private JSONArray channels; |
16 | | - |
17 | | - RetrieveChannelsCallback(CountDownLatch latch) { |
18 | | - this.latch = latch; |
19 | | - } |
20 | | - |
21 | | - public void successCallback(String channel, Object channels) { |
22 | | - try { |
23 | | - this.channels = ((JSONObject) channels).getJSONArray("channels"); |
24 | | - } catch (JSONException e) { |
25 | | - this.channels = new JSONArray(); |
26 | | - } |
27 | | - this.latch.countDown(); |
28 | | - } |
29 | | - |
30 | | - public void errorCallback(String channel, PubnubError error) { |
31 | | - this.channels = new JSONArray(); |
32 | | - this.latch.countDown(); |
33 | | - } |
34 | | - |
35 | | - public JSONArray getChannels() { |
36 | | - return channels; |
37 | | - } |
| 4 | + ChannelGroup(String name, Callback callback) { |
| 5 | + super(name, callback); |
38 | 6 | } |
39 | | - |
40 | | - ChannelGroup(String name, Callback callback, PubnubCore pubnub) { |
41 | | - CountDownLatch latch = new CountDownLatch(1); |
42 | | - |
43 | | - this.name = name; |
44 | | - this.callback = callback; |
45 | | - this.connected = false; |
46 | | - |
47 | | - RetrieveChannelsCallback cb = new RetrieveChannelsCallback(latch); |
48 | | - pubnub.channelGroup(name, cb); |
49 | | - |
50 | | - try { |
51 | | - latch.await(); |
52 | | - } catch (InterruptedException e) { |
53 | | - if (callback != null) { |
54 | | - callback.errorCallback( |
55 | | - name, |
56 | | - PubnubError.getErrorObject( |
57 | | - PubnubError.PNERROBJ_INTERNAL_ERROR, |
58 | | - "Failed to retrieve " + name + "'s channel list" |
59 | | - ) |
60 | | - ); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - JSONArray channelNames = cb.getChannels(); |
65 | | - |
66 | | - for (int i = 0; i < channelNames.length(); i++) { |
67 | | - try { |
68 | | - channels.add(channelNames.get(i).toString()); |
69 | | - } catch (JSONException e) { |
70 | | - if (callback != null) { |
71 | | - callback.errorCallback( |
72 | | - name, |
73 | | - PubnubError.getErrorObject( |
74 | | - PubnubError.PNERROBJ_INTERNAL_ERROR, |
75 | | - "Failed to retrieve " + name + "'s channel list" |
76 | | - ) |
77 | | - ); |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - public ArrayList getChannels() { |
84 | | - return channels; |
85 | | - } |
86 | | - |
87 | 7 | } |
0 commit comments