forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_manager.go
159 lines (124 loc) · 3.48 KB
/
state_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package pubnub
import (
"fmt"
"sync"
)
type StateManager struct {
sync.RWMutex
channels map[string]*SubscriptionItem
groups map[string]*SubscriptionItem
presenceChannels map[string]*SubscriptionItem
presenceGroups map[string]*SubscriptionItem
}
type SubscriptionItem struct {
name string
state map[string]interface{}
}
func newStateManager() *StateManager {
return &StateManager{
channels: make(map[string]*SubscriptionItem),
presenceChannels: make(map[string]*SubscriptionItem),
groups: make(map[string]*SubscriptionItem),
presenceGroups: make(map[string]*SubscriptionItem),
}
}
func newSubscriptionItem(name string) *SubscriptionItem {
return &SubscriptionItem{
name: name,
state: make(map[string]interface{}),
}
}
func (m *StateManager) prepareChannelList(includePresence bool) []string {
m.RLock()
defer m.RUnlock()
return prepareMembershipList(m.channels, m.presenceChannels, includePresence)
}
func (m *StateManager) prepareGroupList(includePresence bool) []string {
m.RLock()
defer m.RUnlock()
return prepareMembershipList(m.groups, m.presenceGroups, includePresence)
}
func (m *StateManager) adaptSubscribeBuilder(
subscribeOperation *SubscribeOperation) {
m.Lock()
defer m.Unlock()
for _, ch := range subscribeOperation.Channels {
m.channels[ch] = newSubscriptionItem(ch)
if subscribeOperation.PresenceEnabled {
m.presenceChannels[ch] = newSubscriptionItem(ch)
}
}
for _, cg := range subscribeOperation.ChannelGroups {
m.groups[cg] = newSubscriptionItem(cg)
if subscribeOperation.PresenceEnabled {
m.presenceGroups[cg] = newSubscriptionItem(cg)
}
}
}
func (m *StateManager) adaptStateBuilder(stateOperation StateOperation) {
m.Lock()
defer m.Unlock()
for _, ch := range stateOperation.channels {
if _, ok := m.channels[ch]; ok {
subscribedChannel := m.channels[ch]
if subscribedChannel.name != "" {
subscribedChannel.state = stateOperation.state
}
}
}
for _, cg := range stateOperation.channelGroups {
if _, ok := m.groups[cg]; ok {
subscribedChannelGroup := m.groups[cg]
if subscribedChannelGroup.name != "" {
subscribedChannelGroup.state = stateOperation.state
}
}
}
}
func (m *StateManager) adaptUnsubscribeBuilder(unsubscribeOperation *UnsubscribeOperation) {
m.Lock()
defer m.Unlock()
for _, ch := range unsubscribeOperation.Channels {
delete(m.channels, ch)
delete(m.presenceChannels, ch)
}
for _, cg := range unsubscribeOperation.ChannelGroups {
delete(m.groups, cg)
delete(m.presenceGroups, cg)
}
}
func (m *StateManager) createStatePayload() map[string]interface{} {
m.RLock()
defer m.RUnlock()
stateResponse := make(map[string]interface{})
for _, ch := range m.channels {
if len(ch.state) != 0 {
stateResponse[ch.name] = ch.state
}
}
for _, gr := range m.groups {
if len(gr.state) != 0 {
stateResponse[gr.name] = gr.state
}
}
return stateResponse
}
func (m *StateManager) isEmpty() bool {
m.RLock()
defer m.RUnlock()
return len(m.channels) != 0 && len(m.presenceChannels) != 0 &&
len(m.groups) != 0 && len(m.presenceGroups) != 0
}
func prepareMembershipList(dataStorage map[string]*SubscriptionItem,
presenceStorage map[string]*SubscriptionItem, includePresence bool) []string {
response := []string{}
for _, v := range dataStorage {
response = append(response, v.name)
}
if includePresence {
for _, v := range presenceStorage {
response = append(response, fmt.Sprintf("%s-pnpres", v.name))
}
}
return response
}