Merge branch 'api_ws'

This commit is contained in:
evilsocket 2018-03-06 13:54:45 +01:00
commit 973b864132
5 changed files with 232 additions and 61 deletions

View file

@ -39,21 +39,29 @@ func (e Event) Label() string {
type EventPool struct {
sync.Mutex
NewEvents chan Event
debug bool
silent bool
events []Event
listeners []chan Event
}
func NewEventPool(debug bool, silent bool) *EventPool {
return &EventPool{
NewEvents: make(chan Event, 0xff),
debug: debug,
silent: silent,
events: make([]Event, 0),
listeners: make([]chan Event, 0),
}
}
func (p *EventPool) Listen() <-chan Event {
p.Lock()
defer p.Unlock()
l := make(chan Event, 1)
p.listeners = append(p.listeners, l)
return l
}
func (p *EventPool) SetSilent(s bool) {
p.Lock()
defer p.Unlock()
@ -69,9 +77,17 @@ func (p *EventPool) SetDebug(d bool) {
func (p *EventPool) Add(tag string, data interface{}) {
p.Lock()
defer p.Unlock()
e := NewEvent(tag, data)
p.events = append([]Event{e}, p.events...)
p.NewEvents <- e
// broadcast the event to every listener
for _, l := range p.listeners {
select {
case l <- e:
default:
}
}
}
func (p *EventPool) Log(level int, format string, args ...interface{}) {