forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_router.go
77 lines (63 loc) · 2.27 KB
/
message_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
package quickfix
import (
"github.com/quickfixgo/quickfix/enum"
)
type routeKey struct {
FIXVersion string
MsgType string
}
//A MessageRoute is a function that can process a fromApp/fromAdmin callback
type MessageRoute func(msg Message, sessionID SessionID) MessageRejectError
//A MessageRouter is a mutex for MessageRoutes
type MessageRouter struct {
routes map[routeKey]MessageRoute
}
//NewMessageRouter returns an initialized MessageRouter instance
func NewMessageRouter() *MessageRouter {
return &MessageRouter{routes: make(map[routeKey]MessageRoute)}
}
//AddRoute adds a route to the MessageRouter instance keyed to begin string and msgType.
func (c MessageRouter) AddRoute(beginString string, msgType string, router MessageRoute) {
c.routes[routeKey{beginString, msgType}] = router
}
//Route may be called from the fromApp/fromAdmin callbacks. Messages that cannot be routed will be rejected with UnsupportedMessageType.
func (c MessageRouter) Route(msg Message, sessionID SessionID) MessageRejectError {
var beginString FIXString
if err := msg.Header.GetField(tagBeginString, &beginString); err != nil {
return nil
}
var msgType FIXString
if err := msg.Header.GetField(tagMsgType, &msgType); err != nil {
return err
}
return c.tryRoute(string(beginString), string(msgType), msg, sessionID)
}
func (c MessageRouter) tryRoute(beginString string, msgType string, msg Message, sessionID SessionID) MessageRejectError {
fixVersion := beginString
if beginString == enum.BeginStringFIXT11 && !isAdminMessageType(msgType) {
var applVerID FIXString
if err := msg.Header.GetField(tagApplVerID, &applVerID); err != nil {
session, _ := lookupSession(sessionID)
applVerID = FIXString(session.TargetDefaultApplicationVersionID())
}
switch string(applVerID) {
case enum.ApplVerID_FIX40:
fixVersion = enum.BeginStringFIX40
case enum.ApplVerID_FIX41:
fixVersion = enum.BeginStringFIX41
case enum.ApplVerID_FIX42:
fixVersion = enum.BeginStringFIX42
case enum.ApplVerID_FIX43:
fixVersion = enum.BeginStringFIX43
case enum.ApplVerID_FIX44:
fixVersion = enum.BeginStringFIX44
default:
fixVersion = string(applVerID)
}
}
route, ok := c.routes[routeKey{fixVersion, msgType}]
if !ok {
return UnsupportedMessageType()
}
return route(msg, sessionID)
}