-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregister.go
77 lines (61 loc) · 1.68 KB
/
register.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
package exposed
import (
"errors"
"github.com/cespare/xxhash"
)
var (
errNilOperationTypes = errors.New("nil operation types")
errNilOperationArgType = errors.New("nil operation argument type")
errNilOperationReplyType = errors.New("nil operation reply type")
)
//TypeMaker creates a new instance of the provided type
//is used to avoid reflection
type TypeMaker func() Message
var registeredOpTypes = map[uint64]*OperationTypes{}
type OperationTypes struct {
ArgsType TypeMaker
ReplyType TypeMaker
}
func registerOperationInfo(name string, info *OperationTypes) error {
if info == nil {
return errNilOperationTypes
}
if info.ArgsType == nil {
return errNilOperationArgType
}
if info.ReplyType == nil {
return errNilOperationReplyType
}
registeredOpTypes[xxhash.Sum64String(name)] = info
return nil
}
func getOperationInfo(name uint64) *OperationTypes {
return registeredOpTypes[name]
}
type OperationInfo struct {
Operation string
Handler HandlerFunc
OperationTypes *OperationTypes
}
var operationHandlers = map[uint64]HandlerFunc{}
func registerService(e Exposable) {
ops := e.ExposedOperations()
for i := range ops {
registerHandleFunc(ops[i].Operation, ops[i].Handler, ops[i].OperationTypes)
}
}
func registerHandleFunc(path string, handlerFunc HandlerFunc, info *OperationTypes) {
op := xxhash.Sum64String(path)
if _, ok := operationHandlers[op]; ok {
panic(errOperationAlreadyRegistered.Error() + " " + path)
}
registerOperationInfo(path, info)
operationHandlers[op] = handlerFunc
}
func match(operation uint64) (HandlerFunc, error) {
h, ok := operationHandlers[(operation)]
if !ok {
return nil, errHandlerNotFound
}
return h, nil
}