-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
100 lines (79 loc) · 2.59 KB
/
dispatcher.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
package jsonrpc
import (
"sync"
)
// NewDispatcher creates default dispatcher
func NewDispatcher() *DefaultDispatcher {
return &DefaultDispatcher{
callbacks: make(map[string]Callback),
handlers: make(map[string]Handler),
methods: make(map[string]Method),
}
}
// DefaultDispatcher provides default dispatcher implementation
type DefaultDispatcher struct {
guard sync.RWMutex
callbacks map[string]Callback
handlers map[string]Handler
methods map[string]Method
}
// RegisterMethod register new RPC method
func (dispatcher *DefaultDispatcher) RegisterMethod(name string, method Method) {
dispatcher.guard.Lock()
defer dispatcher.guard.Unlock()
dispatcher.methods[name] = method
}
// RemoveMethod removes RPC method
func (dispatcher *DefaultDispatcher) RemoveMethod(name string) {
dispatcher.guard.Lock()
defer dispatcher.guard.Unlock()
delete(dispatcher.methods, name)
}
// RegisterCallback register new RPC method
func (dispatcher *DefaultDispatcher) RegisterCallback(id string, callback Callback) {
dispatcher.guard.Lock()
defer dispatcher.guard.Unlock()
dispatcher.callbacks[id] = callback
}
// RegisterHandler register new RPC method
func (dispatcher *DefaultDispatcher) RegisterHandler(name string, handler Handler) {
dispatcher.guard.Lock()
defer dispatcher.guard.Unlock()
dispatcher.handlers[name] = handler
}
// RemoveHandler removes RPC method
func (dispatcher *DefaultDispatcher) RemoveHandler(name string) {
dispatcher.guard.Lock()
defer dispatcher.guard.Unlock()
delete(dispatcher.handlers, name)
}
// DispatchNotification provided request params to RPC method
func (dispatcher *DefaultDispatcher) DispatchNotification(request Request) {
dispatcher.guard.RLock()
defer dispatcher.guard.RUnlock()
handler, exists := dispatcher.handlers[request.Method]
if exists {
handler.Process(request.Params.Data)
}
}
// DispatchResponse provided request params to RPC method
func (dispatcher *DefaultDispatcher) DispatchResponse(response Response) {
dispatcher.guard.RLock()
defer dispatcher.guard.RUnlock()
id := string(response.ID)
callback, exists := dispatcher.callbacks[id]
if exists {
callback.Process(response)
delete(dispatcher.callbacks, id)
}
}
// DispatchRequest provided request params to RPC method
func (dispatcher *DefaultDispatcher) DispatchRequest(request Request, responder Responder) {
dispatcher.guard.RLock()
defer dispatcher.guard.RUnlock()
method, exists := dispatcher.methods[request.Method]
if exists {
responder.Respond(method.Process(request.Params.Data))
}
responder.Respond(nil, NewError(MethodNotFound, "Method ", request.Method, " Not Found"))
}