-
Notifications
You must be signed in to change notification settings - Fork 88
/
router.go
185 lines (160 loc) · 4.5 KB
/
router.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package turnpike
import (
"fmt"
"sync"
"time"
)
var defaultWelcomeDetails = map[string]interface{}{
"roles": map[string]struct{}{
"broker": {},
"dealer": {},
},
}
type RealmExistsError string
func (e RealmExistsError) Error() string {
return "realm exists: " + string(e)
}
type NoSuchRealmError string
func (e NoSuchRealmError) Error() string {
return "no such realm: " + string(e)
}
// Peer was unable to authenticate
type AuthenticationError string
func (e AuthenticationError) Error() string {
return "authentication error: " + string(e)
}
// A Router handles new Peers and routes requests to the requested Realm.
type Router interface {
Accept(Peer) error
Close() error
RegisterRealm(URI, Realm) error
GetLocalPeer(URI, map[string]interface{}) (Peer, error)
AddSessionOpenCallback(func(*Session, string))
AddSessionCloseCallback(func(*Session, string))
}
// DefaultRouter is the default WAMP router implementation.
type defaultRouter struct {
realms map[URI]Realm
closing bool
closeLock sync.Mutex
sessionOpenCallbacks []func(*Session, string)
sessionCloseCallbacks []func(*Session, string)
}
// NewDefaultRouter creates a very basic WAMP router.
func NewDefaultRouter() Router {
return &defaultRouter{
realms: make(map[URI]Realm),
sessionOpenCallbacks: []func(*Session, string){},
sessionCloseCallbacks: []func(*Session, string){},
}
}
func (r *defaultRouter) AddSessionOpenCallback(fn func(*Session, string)) {
r.sessionOpenCallbacks = append(r.sessionOpenCallbacks, fn)
}
func (r *defaultRouter) AddSessionCloseCallback(fn func(*Session, string)) {
r.sessionCloseCallbacks = append(r.sessionCloseCallbacks, fn)
}
func (r *defaultRouter) Close() error {
r.closeLock.Lock()
if r.closing {
r.closeLock.Unlock()
return fmt.Errorf("already closed")
}
r.closing = true
r.closeLock.Unlock()
for _, realm := range r.realms {
realm.Close()
}
return nil
}
func (r *defaultRouter) RegisterRealm(uri URI, realm Realm) error {
if _, ok := r.realms[uri]; ok {
return RealmExistsError(uri)
}
realm.init()
r.realms[uri] = realm
log.Println("registered realm:", uri)
return nil
}
func (r *defaultRouter) Accept(client Peer) error {
if r.closing {
logErr(client.Send(&Abort{Reason: ErrSystemShutdown}))
logErr(client.Close())
return fmt.Errorf("Router is closing, no new connections are allowed")
}
msg, err := GetMessageTimeout(client, 5*time.Second)
if err != nil {
return err
}
log.Printf("%s: %+v", msg.MessageType(), msg)
hello, ok := msg.(*Hello)
if !ok {
logErr(client.Send(&Abort{Reason: URI("wamp.error.protocol_violation")}))
logErr(client.Close())
return fmt.Errorf("protocol violation: expected HELLO, received %s", msg.MessageType())
}
realm, ok := r.realms[hello.Realm]
if !ok {
logErr(client.Send(&Abort{Reason: ErrNoSuchRealm}))
logErr(client.Close())
return NoSuchRealmError(hello.Realm)
}
welcome, err := realm.handleAuth(client, hello.Details)
if err != nil {
abort := &Abort{
Reason: ErrAuthorizationFailed, // TODO: should this be AuthenticationFailed?
Details: map[string]interface{}{"error": err.Error()},
}
logErr(client.Send(abort))
logErr(client.Close())
return AuthenticationError(err.Error())
}
welcome.Id = NewID()
if welcome.Details == nil {
welcome.Details = make(map[string]interface{})
}
// add default details to welcome message
for k, v := range defaultWelcomeDetails {
if _, ok := welcome.Details[k]; !ok {
welcome.Details[k] = v
}
}
if err := client.Send(welcome); err != nil {
return err
}
log.Println("Established session:", welcome.Id)
// session details
welcome.Details["session"] = welcome.Id
welcome.Details["realm"] = hello.Realm
sess := &Session{
Peer: client,
Id: welcome.Id,
Details: welcome.Details,
kill: make(chan URI, 1),
}
for _, callback := range r.sessionOpenCallbacks {
go callback(sess, string(hello.Realm))
}
go func() {
realm.handleSession(sess)
sess.Close()
for _, callback := range r.sessionCloseCallbacks {
go callback(sess, string(hello.Realm))
}
}()
return nil
}
// GetLocalPeer returns an internal peer connected to the specified realm.
func (r *defaultRouter) GetLocalPeer(realmURI URI, details map[string]interface{}) (Peer, error) {
realm, ok := r.realms[realmURI]
if !ok {
return nil, NoSuchRealmError(realmURI)
}
// TODO: session open/close callbacks?
return realm.getPeer(details)
}
func (r *defaultRouter) getTestPeer() Peer {
peerA, peerB := localPipe()
go r.Accept(peerA)
return peerB
}