-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
134 lines (118 loc) · 4.22 KB
/
main.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
package main
import (
"bytes"
"fmt"
"github.com/b2broker/simplefix-go/storages/memory"
"net"
"strconv"
"time"
simplefixgo "github.com/b2broker/simplefix-go"
"github.com/b2broker/simplefix-go/fix"
"github.com/b2broker/simplefix-go/fix/encoding"
"github.com/b2broker/simplefix-go/session"
"github.com/b2broker/simplefix-go/session/messages"
fixgen "github.com/b2broker/simplefix-go/tests/fix44"
)
func mustConvToInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
// TODO: move boilerplate to generator.
var pseudoGeneratedOpts = session.Opts{
MessageBuilders: session.MessageBuilders{
HeaderBuilder: fixgen.Header{}.New(),
TrailerBuilder: fixgen.Trailer{}.New(),
LogonBuilder: fixgen.Logon{}.New(),
LogoutBuilder: fixgen.Logout{}.New(),
RejectBuilder: fixgen.Reject{}.New(),
HeartbeatBuilder: fixgen.Heartbeat{}.New(),
TestRequestBuilder: fixgen.TestRequest{}.New(),
ResendRequestBuilder: fixgen.ResendRequest{}.New(),
},
Tags: &messages.Tags{
MsgType: mustConvToInt(fixgen.FieldMsgType),
MsgSeqNum: mustConvToInt(fixgen.FieldMsgSeqNum),
HeartBtInt: mustConvToInt(fixgen.FieldHeartBtInt),
EncryptedMethod: mustConvToInt(fixgen.FieldEncryptMethod),
},
AllowedEncryptedMethods: map[string]struct{}{
fixgen.EnumEncryptMethodNoneother: {},
},
SessionErrorCodes: &messages.SessionErrorCodes{
InvalidTagNumber: mustConvToInt(fixgen.EnumSessionRejectReasonInvalidtagnumber),
RequiredTagMissing: mustConvToInt(fixgen.EnumSessionRejectReasonRequiredtagmissing),
TagNotDefinedForMessageType: mustConvToInt(fixgen.EnumSessionRejectReasonTagNotDefinedForThisMessageType),
UndefinedTag: mustConvToInt(fixgen.EnumSessionRejectReasonUndefinedtag),
TagSpecialWithoutValue: mustConvToInt(fixgen.EnumSessionRejectReasonTagspecifiedwithoutavalue),
IncorrectValue: mustConvToInt(fixgen.EnumSessionRejectReasonValueisincorrectoutofrangeforthistag),
IncorrectDataFormatValue: mustConvToInt(fixgen.EnumSessionRejectReasonIncorrectdataformatforvalue),
DecryptionProblem: mustConvToInt(fixgen.EnumSessionRejectReasonDecryptionproblem),
SignatureProblem: mustConvToInt(fixgen.EnumSessionRejectReasonSignatureproblem),
CompIDProblem: mustConvToInt(fixgen.EnumSessionRejectReasonCompidproblem),
Other: mustConvToInt(fixgen.EnumSessionRejectReasonOther),
},
}
func main() {
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", 9991))
if err != nil {
panic(err)
}
fmt.Println("Connected")
handlerFactory := simplefixgo.NewAcceptorHandlerFactory(fixgen.FieldMsgType, 10)
exampleStorage := memory.NewStorage()
server := simplefixgo.NewAcceptor(listener, handlerFactory, time.Second*5, func(handler simplefixgo.AcceptorHandler) {
sess, err := session.NewAcceptorSession(
&pseudoGeneratedOpts,
handler,
&session.LogonSettings{
HeartBtInt: 30,
LogonTimeout: time.Second * 30,
HeartBtLimits: &session.IntLimits{
Min: 5,
Max: 60,
},
},
func(request *session.LogonSettings) (err error) {
fmt.Printf(
"Logon passed for '%s' (%s)\n",
request.Username,
request.Password,
)
return nil
},
exampleStorage,
exampleStorage,
)
if err != nil {
panic(err)
}
_ = sess.Run()
handler.HandleIncoming(simplefixgo.AllMsgTypes, func(msg []byte) bool {
fmt.Println("incoming", string(bytes.ReplaceAll(msg, fix.Delimiter, []byte("|"))))
return true
})
handler.HandleOutgoing(simplefixgo.AllMsgTypes, func(msg simplefixgo.SendingMessage) bool {
data, err := msg.ToBytes()
if err != nil {
panic(err)
}
fmt.Println("outgoing", string(bytes.ReplaceAll(data, fix.Delimiter, []byte("|"))))
return true
})
handler.HandleIncoming(fixgen.MsgTypeMarketDataRequest, func(msg []byte) bool {
request := fixgen.NewMarketDataRequest()
err := encoding.Unmarshal(request, msg)
if err != nil {
panic(err)
}
for _, relatedSymbolEntry := range request.RelatedSymGrp().Entries() {
fmt.Println(relatedSymbolEntry.Instrument().Symbol())
}
return true
})
})
panic(fmt.Errorf("the server was stopped: %s", server.ListenAndServe()))
}