-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
147 lines (129 loc) · 4.6 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
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"context"
"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"
"github.com/b2broker/simplefix-go/utils"
)
func mustConvToInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
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() {
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", 9991))
if err != nil {
panic(fmt.Errorf("could not dial: %s", err))
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
handler := simplefixgo.NewInitiatorHandler(ctx, fixgen.FieldMsgType, 10)
client := simplefixgo.NewInitiator(conn, handler, 10, time.Second*5)
handler.OnConnect(func() bool {
return true
})
exampleStorage := memory.NewStorage()
sess, err := session.NewInitiatorSession(
handler,
&pseudoGeneratedOpts,
&session.LogonSettings{
TargetCompID: "Server",
SenderCompID: "Client",
HeartBtInt: 5,
EncryptMethod: fixgen.EnumEncryptMethodNoneother,
Password: "password",
Username: "login",
},
exampleStorage,
exampleStorage,
)
if err != nil {
panic(err)
}
handler.HandleIncoming(fixgen.MsgTypeLogon, func(msg []byte) bool {
incomingLogon := fixgen.NewLogon()
err := encoding.Unmarshal(incomingLogon, msg)
_, _ = incomingLogon, err
return true
})
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
})
sess.OnChangeState(utils.EventLogon, func() bool {
err := sess.Send(fixgen.CreateMarketDataRequest(
"test",
fixgen.EnumSubscriptionRequestTypeSnapshot,
20,
fixgen.NewMDEntryTypesGrp(),
fixgen.NewRelatedSymGrp().
AddEntry(fixgen.NewRelatedSymEntry().SetInstrument(fixgen.NewInstrument().SetSymbol("BTC/USDT"))).
AddEntry(fixgen.NewRelatedSymEntry().SetInstrument(fixgen.NewInstrument().SetSymbol("ETH/USDT"))),
))
if err != nil {
panic(err)
}
return true
})
go func() {
time.Sleep(time.Second * 10)
fmt.Println("resend request after 10 seconds")
_ = sess.Send(fixgen.ResendRequest{}.New().SetFieldBeginSeqNo(2).SetFieldEndSeqNo(3))
}()
_ = sess.Run()
panic(client.Serve())
}