-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobservable.go
325 lines (268 loc) · 6.06 KB
/
observable.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package y3
import (
"io"
"sync"
"github.com/yomorun/y3-codec-golang/pkg/common"
)
// Iterable iterate through and get the data of observe
type Iterable interface {
Observe() <-chan interface{}
}
// Observable provide subscription and notification processing
type Observable interface {
Iterable
Subscribe(key byte) Observable
OnObserve(function func(v []byte) (interface{}, error)) chan interface{}
}
type observableImpl struct {
iterable Iterable
}
type iterableImpl struct {
next chan interface{}
subscribers []chan interface{}
mutex sync.RWMutex
producerAlreadyCreated bool
}
func (i *iterableImpl) Observe() <-chan interface{} {
ch := make(chan interface{})
i.mutex.Lock()
i.subscribers = append(i.subscribers, ch)
i.mutex.Unlock()
i.connect()
return ch
}
func (i *iterableImpl) connect() {
i.mutex.Lock()
if !i.producerAlreadyCreated {
go i.produce()
i.producerAlreadyCreated = true
}
i.mutex.Unlock()
}
func (i *iterableImpl) produce() {
defer func() {
i.mutex.RLock()
for _, subscriber := range i.subscribers {
close(subscriber)
}
i.mutex.RUnlock()
}()
for {
select {
case item, ok := <-i.next:
if !ok {
return
}
i.mutex.RLock()
for _, subscriber := range i.subscribers {
subscriber <- item
}
i.mutex.RUnlock()
}
}
}
func (o *observableImpl) Observe() <-chan interface{} {
return o.iterable.Observe()
}
//FromStream reads data from reader
func FromStream(reader io.Reader) Observable {
f := func(next chan interface{}) {
defer close(next)
for {
buf := make([]byte, 3*1024)
n, err := reader.Read(buf)
if err != nil {
break
} else {
value := buf[:n]
//fmt.Printf("%v:\t $1 on y3 value=%#v\n", time.Now().Format("2006-01-02 15:04:05"), value)
next <- value
}
}
}
return createObservable(f)
}
//Processing callback function when there is data
func (o *observableImpl) OnObserve(function func(v []byte) (interface{}, error)) chan interface{} {
_next := make(chan interface{})
f := func(next chan interface{}) {
defer close(next)
observe := o.Observe()
for {
select {
case item, ok := <-observe:
if !ok {
return
}
buf := item.([]byte)
value, err := function(buf)
if err != nil {
return
}
next <- value
}
}
}
go f(_next)
return _next
}
//Get the value of the subscribe key from the stream
func (o *observableImpl) Subscribe(key byte) Observable {
f := func(next chan interface{}) {
defer close(next)
resultBuffer := make([]byte, 0)
var (
flow int32 = 0 //0 start,1 get length,2 get value
length int32 = 1
value int32 = 0
reject bool = false
)
_observe := o.Observe()
observe := filterRoot(_observe)
for {
select {
case nextitem, ok := <-observe:
if !ok {
return
}
reject = false
for item := range nextitem.(chan interface{}) {
buf := item.([]byte)
for _, b := range buf {
if reject {
break
}
if flow == 0 && (b<<2)>>2 == key { //T
flow = 1
resultBuffer = append(resultBuffer, b)
continue
}
if flow == 1 { // L
resultBuffer = append(resultBuffer, b)
l, e := common.DecodeLength(resultBuffer[1 : length+1]) //l 是value占字节,s是l占字节
if l == 0 {
next <- resultBuffer
reject = true
break
}
if e != nil {
length++
} else {
value = l
flow = 2
}
continue
}
if flow == 2 {
l := len(resultBuffer)
if int32(l) == (length + value) {
resultBuffer = append(resultBuffer, b)
next <- resultBuffer
flow = 0
length = 1
value = 0
resultBuffer = make([]byte, 0)
reject = true
break
} else if int32(l) < (length + value) {
resultBuffer = append(resultBuffer, b)
continue
}
}
}
}
}
}
}
return createObservable(f)
}
func createObservable(f func(next chan interface{})) Observable {
next := make(chan interface{})
subscribers := make([]chan interface{}, 0)
go f(next)
return &observableImpl{iterable: &iterableImpl{next: next, subscribers: subscribers}}
}
//filter root data from the stream
func filterRoot(observe <-chan interface{}) <-chan interface{} {
next := make(chan interface{})
f := func(observe <-chan interface{}) {
defer close(next)
rootBuffer := make([]byte, 0)
var send chan interface{}
var (
rootflow int32 = 0 //0 start,1 get length,2 get value
rootlength int32 = 1
rootvalue int32 = 0
rootkey byte = 0x01
index int32 = 0
)
for {
select {
case item, ok := <-observe:
if !ok {
return
}
buf := item.([]byte)
i := 0
for {
if i == len(buf) {
break
}
b := buf[i]
if rootflow == 0 {
if (b<<2)>>2 == rootkey {
rootflow = 1
rootBuffer = append(rootBuffer, b)
}
i++
continue
}
if rootflow == 1 { // L
rootBuffer = append(rootBuffer, b)
l, e := common.DecodeLength(rootBuffer[1 : rootlength+1])
if e != nil {
rootlength++
} else {
rootvalue = l
rootflow = 2
send = make(chan interface{})
next <- send
}
i++
continue
}
if rootflow == 2 {
if (rootvalue - index) > int32(len(buf[i:])) {
send <- buf[i:]
index = index + int32(len(buf[i:]))
i = len(buf)
continue
} else if (rootvalue - index) == int32(len(buf[i:])) {
send <- buf[i:]
rootflow = 0
rootlength = 1
rootvalue = 0
index = 0
rootBuffer = make([]byte, 0)
close(send)
i = len(buf)
continue
} else if (rootvalue - index) < int32(len(buf[i:])) {
send <- buf[i:(int32(i) + (rootvalue - index))]
i = i + int(rootvalue-index)
rootflow = 0
rootlength = 1
rootvalue = 0
index = 0
rootBuffer = make([]byte, 0)
close(send)
continue
}
}
}
}
}
}
go f(observe)
return next
}