|
| 1 | +package com.pusher.client.channel.impl; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.LinkedHashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +import com.google.gson.Gson; |
| 10 | + |
| 11 | +import com.google.gson.GsonBuilder; |
| 12 | +import com.pusher.client.channel.*; |
| 13 | +import com.pusher.client.channel.impl.message.SubscribeMessage; |
| 14 | +import com.pusher.client.channel.impl.message.UnsubscribeMessage; |
| 15 | +import com.pusher.client.util.Factory; |
| 16 | + |
| 17 | +public abstract class BaseChannel implements InternalChannel { |
| 18 | + protected final Gson GSON; |
| 19 | + private static final String INTERNAL_EVENT_PREFIX = "pusher_internal:"; |
| 20 | + protected static final String SUBSCRIPTION_SUCCESS_EVENT = "pusher_internal:subscription_succeeded"; |
| 21 | + private Set<SubscriptionEventListener> globalListeners = new HashSet<SubscriptionEventListener>(); |
| 22 | + private final Map<String, Set<SubscriptionEventListener>> eventNameToListenerMap = new HashMap<String, Set<SubscriptionEventListener>>(); |
| 23 | + protected volatile ChannelState state = ChannelState.INITIAL; |
| 24 | + private ChannelEventListener eventListener; |
| 25 | + private final Factory factory; |
| 26 | + private final Object lock = new Object(); |
| 27 | + |
| 28 | + public BaseChannel(final Factory factory) { |
| 29 | + GsonBuilder gsonBuilder = new GsonBuilder(); |
| 30 | + gsonBuilder.registerTypeAdapter(PusherEvent.class, new PusherEventDeserializer()); |
| 31 | + GSON = gsonBuilder.create(); |
| 32 | + this.factory = factory; |
| 33 | + } |
| 34 | + |
| 35 | + /* Channel implementation */ |
| 36 | + |
| 37 | + @Override |
| 38 | + abstract public String getName(); |
| 39 | + |
| 40 | + @Override |
| 41 | + public void bind(final String eventName, final SubscriptionEventListener listener) { |
| 42 | + validateArguments(eventName, listener); |
| 43 | + |
| 44 | + synchronized (lock) { |
| 45 | + Set<SubscriptionEventListener> listeners = eventNameToListenerMap.get(eventName); |
| 46 | + if (listeners == null) { |
| 47 | + listeners = new HashSet<SubscriptionEventListener>(); |
| 48 | + eventNameToListenerMap.put(eventName, listeners); |
| 49 | + } |
| 50 | + listeners.add(listener); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void bindGlobal(SubscriptionEventListener listener) { |
| 56 | + validateArguments("", listener); |
| 57 | + |
| 58 | + synchronized(lock) { |
| 59 | + globalListeners.add(listener); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void unbind(String eventName, SubscriptionEventListener listener) { |
| 65 | + validateArguments(eventName, listener); |
| 66 | + |
| 67 | + synchronized (lock) { |
| 68 | + final Set<SubscriptionEventListener> listeners = eventNameToListenerMap.get(eventName); |
| 69 | + if (listeners != null) { |
| 70 | + listeners.remove(listener); |
| 71 | + if (listeners.isEmpty()) { |
| 72 | + eventNameToListenerMap.remove(eventName); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void unbindGlobal(SubscriptionEventListener listener) { |
| 80 | + validateArguments("", listener); |
| 81 | + |
| 82 | + synchronized(lock) { |
| 83 | + if (globalListeners != null) { |
| 84 | + globalListeners.remove(listener); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean isSubscribed() { |
| 91 | + return state == ChannelState.SUBSCRIBED; |
| 92 | + } |
| 93 | + |
| 94 | + /* InternalChannel implementation */ |
| 95 | + |
| 96 | + @Override |
| 97 | + public String toSubscribeMessage() { |
| 98 | + return GSON.toJson(new SubscribeMessage(getName())); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String toUnsubscribeMessage() { |
| 103 | + return GSON.toJson(new UnsubscribeMessage(getName())); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public PusherEvent prepareEvent(String event, String message) { |
| 108 | + return GSON.fromJson(message, PusherEvent.class); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onMessage(String event, String message) { |
| 113 | + if (event.equals(SUBSCRIPTION_SUCCESS_EVENT)) { |
| 114 | + updateState(ChannelState.SUBSCRIBED); |
| 115 | + } else { |
| 116 | + final Set<SubscriptionEventListener> listeners = getInterestedListeners(event); |
| 117 | + if (listeners != null) { |
| 118 | + final PusherEvent pusherEvent = prepareEvent(event, message); |
| 119 | + if (pusherEvent != null) { |
| 120 | + for (final SubscriptionEventListener listener : listeners) { |
| 121 | + factory.queueOnEventThread(new Runnable() { |
| 122 | + @Override |
| 123 | + public void run() { |
| 124 | + listener.onEvent(pusherEvent); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void updateState(ChannelState state) { |
| 135 | + this.state = state; |
| 136 | + |
| 137 | + if (state == ChannelState.SUBSCRIBED && eventListener != null) { |
| 138 | + factory.queueOnEventThread(new Runnable() { |
| 139 | + @Override |
| 140 | + public void run() { |
| 141 | + eventListener.onSubscriptionSucceeded(getName()); |
| 142 | + } |
| 143 | + }); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void setEventListener(final ChannelEventListener listener) { |
| 149 | + eventListener = listener; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public ChannelEventListener getEventListener() { |
| 154 | + return eventListener; |
| 155 | + } |
| 156 | + |
| 157 | + /* Comparable implementation */ |
| 158 | + |
| 159 | + @Override |
| 160 | + public int compareTo(final InternalChannel other) { |
| 161 | + return getName().compareTo(other.getName()); |
| 162 | + } |
| 163 | + |
| 164 | + /* Implementation detail */ |
| 165 | + |
| 166 | + @Override |
| 167 | + public String toString() { |
| 168 | + return String.format("[Channel: name=%s]", getName()); |
| 169 | + } |
| 170 | + |
| 171 | + private void validateArguments(final String eventName, final SubscriptionEventListener listener) { |
| 172 | + |
| 173 | + if (eventName == null) { |
| 174 | + throw new IllegalArgumentException("Cannot bind or unbind to channel " + getName() + " with a null event name"); |
| 175 | + } |
| 176 | + |
| 177 | + if (listener == null) { |
| 178 | + throw new IllegalArgumentException("Cannot bind or unbind to channel " + getName() + " with a null listener"); |
| 179 | + } |
| 180 | + |
| 181 | + if (eventName.startsWith(INTERNAL_EVENT_PREFIX)) { |
| 182 | + throw new IllegalArgumentException("Cannot bind or unbind channel " + getName() |
| 183 | + + " with an internal event name such as " + eventName); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + protected Set<SubscriptionEventListener> getInterestedListeners(String event) { |
| 188 | + synchronized (lock) { |
| 189 | + Set<SubscriptionEventListener> listeners = new HashSet<SubscriptionEventListener>(); |
| 190 | + |
| 191 | + final Set<SubscriptionEventListener> sharedListeners = |
| 192 | + eventNameToListenerMap.get(event); |
| 193 | + |
| 194 | + if (sharedListeners != null ) { |
| 195 | + listeners.addAll(sharedListeners); |
| 196 | + } |
| 197 | + if (!globalListeners.isEmpty()) { |
| 198 | + listeners.addAll(globalListeners); |
| 199 | + } |
| 200 | + |
| 201 | + if (listeners.isEmpty()){ |
| 202 | + return null; |
| 203 | + } |
| 204 | + |
| 205 | + return listeners; |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments