-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevwatcher.go
220 lines (184 loc) · 5.29 KB
/
evwatcher.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
package main
import (
"fmt"
"labix.org/v2/mgo/bson"
"log"
"math/big"
)
const (
EV_NULL = 0
APPSTA = 1
APPSTO = 2
MYSQKO = 3
MYSQOK = 4
MONGKO = 5
MONGOK = 6
TCALOK = 7
TCALKO = 8
CCALOK = 9 // check call action success
CCALKO = 10
)
type BitSet struct {
bits big.Int
}
func (b *BitSet) Set(bit int) *BitSet {
b.bits.SetBit(&b.bits, int(bit), 1)
return b
}
func (b *BitSet) Clear(bit int) *BitSet {
b.bits.SetBit(&b.bits, bit, 0)
return b
}
func (b *BitSet) HasBit(bit int) bool {
return b.bits.Bit(bit) == 1
}
func (b *BitSet) Flip(bit int) *BitSet {
if !b.HasBit(bit) {
return b.Set(bit)
}
return b.Clear(bit)
}
func (b BitSet) String() string {
return fmt.Sprintf("%b", &b.bits)
}
func EmptyBitSet() *BitSet {
return new(BitSet)
}
type Event struct {
Mask *BitSet // Mask of event
Datas string // Data of event
Code string // Code of event
}
type EventWatcher struct {
event chan *Event // Events are returned on this channel
done chan bool // Channel for sending a "quit message"
//storage chan bson.M
isClosed bool // Set to true when Close() is first called
eventsMask *BitSet
storageWorker *EventStorageWorker
config *Config
}
//Create a event watcher and set the mask of events to all
func NewEventWatcher(config *Config) (ew *EventWatcher) {
ew = new(EventWatcher)
ew.config = config
ew.event = make(chan *Event)
ew.done = make(chan bool, 1)
ew.eventsMask = EmptyBitSet()
ew.eventsMask.Set(APPSTA)
ew.eventsMask.Set(APPSTO)
ew.eventsMask.Set(MYSQKO)
ew.eventsMask.Set(MYSQOK)
ew.eventsMask.Set(MONGKO)
ew.eventsMask.Set(MONGOK)
ew.eventsMask.Set(TCALKO)
ew.eventsMask.Set(TCALOK)
ew.eventsMask.Set(CCALOK)
ew.eventsMask.Set(CCALKO)
ew.storageWorker = NewEventStorageWorker()
//comment cause Save methode used from EventStorageWorker
//ew.storage = make(chan bson.M)
//go ew.storageWorker.Work(ew.storage)
return ew
}
func (eventWatcher *EventWatcher) publishEvent(event bson.M) {
for _, notification := range eventWatcher.config.Notifications {
event["appid"] = "vorimport"
event["asteriskid"] = eventWatcher.config.AsteriskID
event["transport"] = notification
log.Println("publishEvent : ", event)
eventWatcher.storageWorker.Save(config.EventsMongoHost, event)
}
}
//Handler dispatch events and flip the event type
//Flip is used to send one time the same type of notification
func (eventWatcher *EventWatcher) processEvent(event *Event) {
//log.Println("processEvent : ", event)
if event.Mask.HasBit(APPSTA) {
var pushEvent = bson.M{"type": 1, "code": "APPSTA", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
}
if event.Mask.HasBit(APPSTO) {
var pushEvent = bson.M{"type": 1, "code": "APPSTO", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.done <- true
}
//Follow code can/has be refactored
//mysql parts
if event.Mask.HasBit(MYSQKO) {
if eventWatcher.eventsMask.HasBit(MYSQKO) {
var pushEvent = bson.M{"type": 1, "code": "MYSQKO", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.eventsMask.Clear(MYSQKO)
eventWatcher.eventsMask.Set(MYSQOK)
}
}
if event.Mask.HasBit(MYSQOK) {
if eventWatcher.eventsMask.HasBit(MYSQOK) {
var pushEvent = bson.M{"type": 1, "code": "MYSQOK", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.eventsMask.Clear(MYSQOK)
eventWatcher.eventsMask.Set(MYSQKO)
}
}
//mongo parts
if event.Mask.HasBit(MONGKO) {
if eventWatcher.eventsMask.HasBit(MONGKO) {
var pushEvent = bson.M{"type": 1, "code": "MONGKO", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.eventsMask.Clear(MONGKO)
eventWatcher.eventsMask.Set(MONGOK)
}
}
if event.Mask.HasBit(MONGOK) {
if eventWatcher.eventsMask.HasBit(MONGOK) {
var pushEvent = bson.M{"type": 1, "code": "MONGOK", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.eventsMask.Clear(MONGOK)
eventWatcher.eventsMask.Set(MONGKO)
}
}
//test call part
if event.Mask.HasBit(TCALKO) {
if eventWatcher.eventsMask.HasBit(TCALKO) {
var pushEvent = bson.M{"type": 1, "code": "TCALKO", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.eventsMask.Clear(TCALKO)
eventWatcher.eventsMask.Set(TCALOK)
}
}
if event.Mask.HasBit(TCALOK) {
if eventWatcher.eventsMask.HasBit(TCALOK) {
var pushEvent = bson.M{"type": 1, "code": "TCALOK", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.eventsMask.Clear(TCALOK)
eventWatcher.eventsMask.Set(TCALKO)
}
}
if event.Mask.HasBit(CCALKO) {
if eventWatcher.eventsMask.HasBit(CCALKO) {
var pushEvent = bson.M{"type": CCALKO, "code": "CCALKO", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.eventsMask.Clear(CCALKO)
eventWatcher.eventsMask.Set(CCALOK)
}
}
if event.Mask.HasBit(CCALOK) {
fmt.Println("Enter CCALOK")
if eventWatcher.eventsMask.HasBit(CCALOK) {
fmt.Println("Enter CCALOK send and change flag")
var pushEvent = bson.M{"type": 1, "code": "CCALOK", "data": event.Datas}
eventWatcher.publishEvent(pushEvent)
eventWatcher.eventsMask.Clear(CCALOK)
eventWatcher.eventsMask.Set(CCALKO)
}
}
}
func (eventWatcher *EventWatcher) run() {
for {
select {
case c := <-eventWatcher.event:
eventWatcher.processEvent(c)
}
}
}