-
Notifications
You must be signed in to change notification settings - Fork 51
/
registerer.go
61 lines (46 loc) · 1.71 KB
/
registerer.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
package registerer
import (
"fmt"
"github.com/aporeto-inc/trireme-lib/common"
"github.com/aporeto-inc/trireme-lib/monitor/processor"
)
// registerer provides a way for others to register a registerer
type registerer struct {
handlers map[common.PUType]map[common.Event]common.EventHandler
}
// New returns a new registerer
func New() Registerer {
return ®isterer{
handlers: map[common.PUType]map[common.Event]common.EventHandler{},
}
}
// RegisterProcessor registers an event processor for a given PUTYpe. Only one
// processor is allowed for a given PU Type.
func (r *registerer) RegisterProcessor(puType common.PUType, ep processor.Processor) error {
if _, ok := r.handlers[puType]; ok {
return fmt.Errorf("Processor already registered for this PU type %d ", puType)
}
r.handlers[puType] = map[common.Event]common.EventHandler{}
r.addHandler(puType, common.EventStart, ep.Start)
r.addHandler(puType, common.EventStop, ep.Stop)
r.addHandler(puType, common.EventCreate, ep.Create)
r.addHandler(puType, common.EventDestroy, ep.Destroy)
r.addHandler(puType, common.EventPause, ep.Pause)
r.addHandler(puType, common.EventResync, ep.ReSync)
return nil
}
func (r *registerer) GetHandler(puType common.PUType, eventType common.Event) (common.EventHandler, error) {
handlers, ok := r.handlers[puType]
if !ok {
return nil, fmt.Errorf("PUType %d not registered", puType)
}
e, ok := handlers[eventType]
if !ok {
return nil, fmt.Errorf("PUType %d event type %s not registered", puType, eventType)
}
return e, nil
}
// addHandler adds a handler for a puType/event.
func (r *registerer) addHandler(puType common.PUType, event common.Event, handler common.EventHandler) {
r.handlers[puType][event] = handler
}