|
| 1 | +package com.pubnub.api.managers; |
| 2 | + |
| 3 | + |
| 4 | +import com.pubnub.api.builder.dto.StateOperation; |
| 5 | +import com.pubnub.api.builder.dto.SubscribeOperation; |
| 6 | +import com.pubnub.api.builder.dto.UnsubscribeOperation; |
| 7 | +import com.pubnub.api.models.SubscriptionItem; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +public class StateManager { |
| 15 | + |
| 16 | + /** |
| 17 | + * Contains a list of subscribed channels |
| 18 | + */ |
| 19 | + private Map<String, SubscriptionItem> channels; |
| 20 | + /** |
| 21 | + * Contains a list of subscribed presence channels. |
| 22 | + */ |
| 23 | + private Map<String, SubscriptionItem> presenceChannels; |
| 24 | + |
| 25 | + /** |
| 26 | + * Contains a list of subscribed channel groups. |
| 27 | + */ |
| 28 | + private Map<String, SubscriptionItem> groups; |
| 29 | + |
| 30 | + /** |
| 31 | + * Contains a list of subscribed presence channel groups. |
| 32 | + */ |
| 33 | + private Map<String, SubscriptionItem> presenceGroups; |
| 34 | + |
| 35 | + public StateManager() { |
| 36 | + this.channels = new HashMap<>(); |
| 37 | + this.presenceChannels = new HashMap<>(); |
| 38 | + |
| 39 | + this.groups = new HashMap<>(); |
| 40 | + this.presenceGroups = new HashMap<>(); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + public final synchronized void adaptSubscribeBuilder(final SubscribeOperation subscribeOperation) { |
| 45 | + for (String channel : subscribeOperation.getChannels()) { |
| 46 | + SubscriptionItem subscriptionItem = new SubscriptionItem().setName(channel); |
| 47 | + channels.put(channel, subscriptionItem); |
| 48 | + |
| 49 | + if (subscribeOperation.isPresenceEnabled()) { |
| 50 | + SubscriptionItem presenceSubscriptionItem = new SubscriptionItem().setName(channel); |
| 51 | + presenceChannels.put(channel, presenceSubscriptionItem); |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + for (String channelGroup : subscribeOperation.getChannelGroups()) { |
| 57 | + SubscriptionItem subscriptionItem = new SubscriptionItem().setName(channelGroup); |
| 58 | + groups.put(channelGroup, subscriptionItem); |
| 59 | + |
| 60 | + if (subscribeOperation.isPresenceEnabled()) { |
| 61 | + SubscriptionItem presenceSubscriptionItem = new SubscriptionItem().setName(channelGroup); |
| 62 | + presenceGroups.put(channelGroup, presenceSubscriptionItem); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public final synchronized void adaptStateBuilder(final StateOperation stateOperation) { |
| 69 | + for (String channel: stateOperation.getChannels()) { |
| 70 | + SubscriptionItem subscribedChannel = channels.get(channel); |
| 71 | + |
| 72 | + if (subscribedChannel != null) { |
| 73 | + subscribedChannel.setState(stateOperation.getState()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + for (String channelGroup: stateOperation.getChannelGroups()) { |
| 78 | + SubscriptionItem subscribedChannelGroup = groups.get(channelGroup); |
| 79 | + |
| 80 | + if (subscribedChannelGroup != null) { |
| 81 | + subscribedChannelGroup.setState(stateOperation.getState()); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public final synchronized void adaptUnsubscribeBuilder(final UnsubscribeOperation unsubscribeOperation) { |
| 88 | + |
| 89 | + for (String channel: unsubscribeOperation.getChannels()) { |
| 90 | + this.channels.remove(channel); |
| 91 | + this.presenceChannels.remove(channel); |
| 92 | + } |
| 93 | + |
| 94 | + for (String channelGroup: unsubscribeOperation.getChannelGroups()) { |
| 95 | + this.groups.remove(channelGroup); |
| 96 | + this.presenceGroups.remove(channelGroup); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public Map<String, Object> createStatePayload() { |
| 101 | + Map<String, Object> stateResponse = new HashMap<>(); |
| 102 | + |
| 103 | + for (SubscriptionItem channel: channels.values()) { |
| 104 | + if (channel.getState() != null) { |
| 105 | + stateResponse.put(channel.getName(), channel.getState()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + for (SubscriptionItem channelGroup: groups.values()) { |
| 110 | + if (channelGroup.getState() != null) { |
| 111 | + stateResponse.put(channelGroup.getName(), channelGroup.getState()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return stateResponse; |
| 116 | + } |
| 117 | + |
| 118 | + public List<String> prepareChannelList(final boolean includePresence) { |
| 119 | + return prepareMembershipList(channels, presenceChannels, includePresence); |
| 120 | + } |
| 121 | + |
| 122 | + public List<String> prepareChannelGroupList(final boolean includePresence) { |
| 123 | + return prepareMembershipList(groups, presenceGroups, includePresence); |
| 124 | + } |
| 125 | + |
| 126 | + private List<String> prepareMembershipList(final Map<String, SubscriptionItem> dataStorage, |
| 127 | + final Map<String, SubscriptionItem> presenceStorage, |
| 128 | + final boolean includePresence) { |
| 129 | + List<String> response = new ArrayList<>(); |
| 130 | + |
| 131 | + for (SubscriptionItem channelGroupItem: dataStorage.values()) { |
| 132 | + response.add(channelGroupItem.getName()); |
| 133 | + } |
| 134 | + |
| 135 | + if (includePresence) { |
| 136 | + for (SubscriptionItem presenceChannelGroupItem: presenceStorage.values()) { |
| 137 | + response.add(presenceChannelGroupItem.getName().concat("-pnpres")); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + return response; |
| 143 | + } |
| 144 | +} |
0 commit comments