-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_handler.go
277 lines (230 loc) · 8.35 KB
/
event_handler.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
package serfer
import (
"fmt"
"sync"
)
// EventHandler responds to a Serfer event
type EventHandler interface {
HandleEvent(e Event)
}
// EventHandlerFunc is an adapter to allow the use of ordinary functions as event
// handlers. If f is a function, EventHandlerFunc(f) is an EventHandler that calls f.
type EventHandlerFunc func(Event)
// HandleEvent calls f(e)
func (f EventHandlerFunc) HandleEvent(e Event) {
f(e)
}
// NotFound is the default function assigned to an event
func NotFound(e Event) {
// log.Printf("[WARN] serfer: Not found event handler for event %s", e.String())
}
// NotFoundEventHandler returns a default handler
func NotFoundEventHandler() EventHandler {
return EventHandlerFunc(NotFound)
}
// Handler is a event handler multiplexer.
// It match the event name with a list of registered patterns and calls the
// handler that matches with the name exactly.
// Partial matching of patters with names is not considered in Serfer but it
// could be implemented in the code that uses Serfer.
type Handler struct {
mu sync.RWMutex
entry map[string]map[string]handlerEntry
}
type handlerEntry struct {
h []EventHandler
name string
eventType string
}
func notFoundHandlerEntry() handlerEntry {
var hNotFound EventHandlerFunc = NotFound
ehs := make([]EventHandler, 1)
ehs[0] = hNotFound
return handlerEntry{
h: ehs,
name: "",
eventType: "",
}
}
func newEntries() map[string]map[string]handlerEntry {
return map[string]map[string]handlerEntry{
"": {
"": notFoundHandlerEntry(),
},
}
}
// NewHandler creates and returns a new Handler
func NewHandler() *Handler {
return &Handler{
entry: newEntries(),
}
}
// DefaultHandler is the default Handler used by Serfer
var DefaultHandler = &defaultHandler
var defaultHandler Handler
func (h *Handler) match(eventType, name string) *handlerEntry {
// log.Printf("[DEBUG] serfer: match() - Looking for a match for event %s(%s)", eventType, name)
// (1) Try with the exact requested type and name
if he, ok := h.entry[eventType][name]; ok {
return &he
}
// (2) Try for the generic event for the requested type
if he, ok := h.entry[eventType][""]; ok {
return &he
}
// (3) Return the generic event for any type and name
if he, ok := h.entry[""][""]; ok {
return &he
}
// (4) This shouldn't happen: return the generic event for any type and name
he := notFoundHandlerEntry()
return &he
}
// EventHandler returns the event handler to use for the given event as well as
// it's type and name. It uses the event type and name to find it. It always
// returns a non-nil handler, if no event matches, then it returns the NotFound
// event handler.
func (h *Handler) EventHandler(e Event) (eventHandler []EventHandler, eventType string, name string) {
h.mu.RLock()
defer h.mu.RUnlock()
// log.Printf("[DEBUG] serfer: EventHandler() - Received event of type %s(%d)", e.EventType(), e.EventType())
var he *handlerEntry
if ue, ok := Event2UserEvent(e); ok {
he = h.match("user", ue.Name)
}
if q, ok := Event2Query(e); ok {
he = h.match("query", q.Name)
}
if _, t, ok := Event2MemberEvent(e); ok {
he = h.match("member", t)
}
if he != nil {
// log.Printf("[DEBUG] serfer: EventHandler() - Found a match for event, returning %s(%s)", he.eventType, he.name)
return he.h, he.eventType, he.name
}
ehNotFound := make([]EventHandler, 1)
ehNotFound[0] = NotFoundEventHandler()
return ehNotFound, "", ""
}
// HandleEvent dispatches the events to the handler that matches with the event
// type and name ... or to the generic handler if it's not found.
func (h *Handler) HandleEvent(e Event) {
ehs, _, _ := h.EventHandler(e)
// log.Printf("[DEBUG] serfer: HandleEvent() - Found event handlers: %#v\n", ehs)
for _, eh := range ehs {
// log.Printf("[DEBUG] serfer: HandleEvent() - Handling event: %+v\n", eh)
eh.HandleEvent(e)
}
}
// General events Event Handlers:
// EventHandle register the event handler for the given event type and name
// In case of error it will panic
func (h *Handler) EventHandle(etype string, name string, eventHandler EventHandler) {
if eventHandler == nil {
panic("serfer: nil event handler")
}
if !validEventType(etype) {
panic(fmt.Sprintf("serfer: unknown event type %q", etype))
}
// The event type '*' is the same as an empty event type '' and the latest
// should be used
if etype == "*" {
etype = ""
}
// TODO: Do we really want this? or overwrite the event or add another event with same name?
// Only the default entry can be overwritten
// if _, ok := h.entry[etype][name]; ok && len(etype) != 0 && len(name) != 0 {
// panic(fmt.Sprintf("serfer: multiple registrations for %s named %q", etype, name))
// }
if h.entry == nil {
h.entry = newEntries()
}
var ehs []EventHandler
if _, ok := h.entry[etype][name]; ok {
ehs = append(h.entry[etype][name].h, eventHandler)
} else {
ehs = []EventHandler{eventHandler}
}
if _, ok := h.entry[etype]; !ok {
h.entry[etype] = make(map[string]handlerEntry)
}
h.entry[etype][name] = handlerEntry{
eventType: etype,
name: name,
h: ehs,
}
}
// EventHandleFunc register the event handler function for the given event type
// and name
func (h *Handler) EventHandleFunc(etype string, name string, eventHandler func(Event)) {
h.EventHandle(etype, name, EventHandlerFunc(eventHandler))
}
// EventHandle register the event handler for the given event type and name in
// the DefaultHandler
func EventHandle(etype string, name string, eventHandler EventHandler) {
DefaultHandler.EventHandle(etype, name, eventHandler)
}
// EventHandleFunc register the event handler function for the given event type
// and name in the DefaultHandler
func EventHandleFunc(etype string, name string, eventHandler func(Event)) {
DefaultHandler.EventHandleFunc(etype, name, eventHandler)
}
// User-events Event Handlers:
// UserEventHandle register the event handler for the given user event
func (h *Handler) UserEventHandle(name string, eventHandler EventHandler) {
h.EventHandle("user", name, eventHandler)
}
// UserEventHandleFunc register the event handler function for the given user
// event
func (h *Handler) UserEventHandleFunc(name string, eventHandler func(Event)) {
h.EventHandleFunc("user", name, eventHandler)
}
// UserEventHandle register the event handler for the given user event in
// the DefaultHandler
func UserEventHandle(name string, eventHandler EventHandler) {
DefaultHandler.UserEventHandle(name, eventHandler)
}
// UserEventHandleFunc register the event handler function for the given user
// event in the DefaultHandler
func UserEventHandleFunc(name string, eventHandler func(Event)) {
DefaultHandler.UserEventHandleFunc(name, eventHandler)
}
// Queries Event Handlers:
// QueryEventHandle register the event handler for the given query
func (h *Handler) QueryEventHandle(name string, eventHandler EventHandler) {
h.EventHandle("query", name, eventHandler)
}
// QueryEventHandleFunc register the event handler function for the given query
func (h *Handler) QueryEventHandleFunc(name string, eventHandler func(Event)) {
h.EventHandleFunc("query", name, eventHandler)
}
// QueryEventHandle register the event handler for the given query in
// the DefaultHandler
func QueryEventHandle(name string, eventHandler EventHandler) {
DefaultHandler.QueryEventHandle(name, eventHandler)
}
// QueryEventHandleFunc register the event handler function for the given query
// in the DefaultHandler
func QueryEventHandleFunc(name string, eventHandler func(Event)) {
DefaultHandler.QueryEventHandleFunc(name, eventHandler)
}
// Membership Event Handlers:
// MemberEventHandle register the event handler for the given member event
func (h *Handler) MemberEventHandle(name string, eventHandler EventHandler) {
h.EventHandle("member", name, eventHandler)
}
// MemberEventHandleFunc register the event handler function for the given
// member event
func (h *Handler) MemberEventHandleFunc(name string, eventHandler func(Event)) {
h.EventHandleFunc("member", name, eventHandler)
}
// MemberEventHandle register the event handler for the given member event in
// the DefaultHandler
func MemberEventHandle(name string, eventHandler EventHandler) {
DefaultHandler.MemberEventHandle(name, eventHandler)
}
// MemberEventHandleFunc register the event handler function for the given
// member event in the DefaultHandler
func MemberEventHandleFunc(name string, eventHandler func(Event)) {
DefaultHandler.MemberEventHandleFunc(name, eventHandler)
}