forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus.go
53 lines (45 loc) · 1.24 KB
/
bus.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
package event
import (
"fmt"
"reflect"
log "github.com/coreos/fleet/third_party/github.com/golang/glog"
)
type EventBus struct {
listeners map[string]interface{}
Channel chan *Event
}
func NewEventBus() *EventBus {
listeners := make(map[string]interface{}, 0)
return &EventBus{listeners, make(chan *Event)}
}
func (eb *EventBus) Listen(stop chan bool) {
go func() {
for {
select {
case <-stop:
return
case ev := <-eb.Channel:
eb.dispatch(ev)
}
}
}()
}
func (eb *EventBus) AddListener(name string, l interface{}) {
eb.listeners[name] = l
}
func (eb *EventBus) RemoveListener(name string) {
delete(eb.listeners, name)
}
// Distribute an Event to all listeners registered to Event.Type
func (eb *EventBus) dispatch(ev *Event) {
log.V(1).Infof("Dispatching %s to listeners", ev.Type)
handlerFuncName := fmt.Sprintf("Handle%s", ev.Type)
for name, listener := range eb.listeners {
log.V(1).Infof("Looking for event handler func %s on listener %s", handlerFuncName, name)
handlerFunc := reflect.ValueOf(listener).MethodByName(handlerFuncName)
if handlerFunc.IsValid() {
log.V(1).Infof("Calling event handler for %s on listener %s", ev.Type, name)
go handlerFunc.Call([]reflect.Value{reflect.ValueOf(*ev)})
}
}
}