forked from smhanov/zwibserve
/
hub.go
191 lines (164 loc) · 3.53 KB
/
hub.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package zwibserve
import "log"
type hub struct {
ch chan func()
sessions map[string]*session
}
type session struct {
clients []*client
keys []clientKey
}
type clientKey struct {
Key
owner *client
}
func newHub() *hub {
h := &hub{
ch: make(chan func()),
sessions: make(map[string]*session),
}
go func() {
for fn := range h.ch {
fn()
}
}()
return h
}
func (h *hub) addClient(docID string, c *client) {
h.ch <- func() {
log.Printf("Client %v registers for document %v", c.id, docID)
s := h.sessions[docID]
if s == nil {
h.sessions[docID] = &session{
clients: []*client{c},
}
} else {
s.clients = append(s.clients, c)
}
}
}
func (h *hub) removeClient(docID string, c *client) {
h.ch <- func() {
log.Printf("Client %v is removed from document %v", c.id, docID)
sess := h.sessions[docID]
list := sess.clients
for i, value := range list {
if value == c {
list[i] = list[len(list)-1]
list = list[:len(list)-1]
sess.clients = list
if len(list) == 0 {
delete(h.sessions, docID)
}
break
}
}
// now remove it's client keys
n := 0
var removed []Key
for _, key := range sess.keys {
if key.owner != c {
sess.keys[n] = key
n++
} else {
key.Value = ""
key.Version++
removed = append(removed, key.Key)
}
}
sess.keys = sess.keys[:n]
if len(removed) > 0 {
for _, client := range sess.clients {
client.notifyKeysUpdated(removed)
}
}
}
}
func (h *hub) append(docID string, source *client, offset uint64, data []uint8) {
h.ch <- func() {
log.Printf("client %v appends %v bytes offset %v: %s", source.id,
len(data), offset, string(data))
for _, other := range h.sessions[docID].clients {
if other != source {
log.Printf("... send to client %d", other.id)
other.enqueueAppend(data, offset)
}
}
}
}
func (h *hub) broadcast(docID string, source *client, data []uint8) {
h.ch <- func() {
log.Printf("client %v broadcasts %v bytes: %s", source.id,
len(data), string(data))
for _, other := range h.sessions[docID].clients {
if other != source {
log.Printf("... send to client %d", other.id)
other.enqueueBroadcast(data)
}
}
}
}
func (h *hub) setSessionKey(docID string, source *client, key Key) {
h.ch <- func() {
for _, other := range h.sessions[docID].clients {
if other != source {
log.Printf("... send to client %d", other.id)
other.notifyKeysUpdated([]Key{key})
}
}
}
}
func (h *hub) setClientKey(docID string, source *client, oldVersion, newVersion int, name, value string) bool {
reply := make(chan bool)
newKey := clientKey{
owner: source,
Key: Key{
Version: newVersion,
Name: name,
Value: value,
},
}
h.ch <- func() {
sess := h.sessions[docID]
found := false
for i, k := range sess.keys {
if k.Name == name {
found = true
if k.Version == oldVersion {
sess.keys[i] = newKey
} else {
reply <- false
return
}
break
}
}
if !found && oldVersion == 0 {
sess.keys = append(sess.keys, newKey)
found = true
}
if found {
for _, other := range h.sessions[docID].clients {
if other != source {
log.Printf("... send to client %d", other.id)
other.notifyKeysUpdated([]Key{newKey.Key})
}
}
}
reply <- found
}
return <-reply
}
func (h *hub) getClientKeys(docID string) []Key {
reply := make(chan bool)
var keys []Key
h.ch <- func() {
sess := h.sessions[docID]
for _, k := range sess.keys {
keys = append(keys, k.Key)
}
reply <- true
}
<-reply
return keys
}