forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
100 lines (76 loc) · 2.4 KB
/
store.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 quickfix
import "time"
//The MessageStore interface provides methods to record and retrieve messages for resend purposes
type MessageStore interface {
NextSenderMsgSeqNum() int
NextTargetMsgSeqNum() int
IncrNextSenderMsgSeqNum()
IncrNextTargetMsgSeqNum()
SetNextSenderMsgSeqNum(next int)
SetNextTargetMsgSeqNum(next int)
CreationTime() time.Time
SaveMessage(seqNum int, msg []byte)
GetMessages(beginSeqNum, endSeqNum int) chan []byte
Refresh()
Reset()
}
//The MessageStoreFactory interface is used by session to create a session specific message store
type MessageStoreFactory interface {
Create(sessionID SessionID) (MessageStore, error)
}
type memoryStore struct {
senderMsgSeqNum, targetMsgSeqNum int
creationTime time.Time
messageMap map[int][]byte
}
func (store memoryStore) NextSenderMsgSeqNum() int {
return store.senderMsgSeqNum + 1
}
func (store memoryStore) NextTargetMsgSeqNum() int {
return store.targetMsgSeqNum + 1
}
func (store *memoryStore) IncrNextSenderMsgSeqNum() {
store.senderMsgSeqNum++
}
func (store *memoryStore) IncrNextTargetMsgSeqNum() {
store.targetMsgSeqNum++
}
func (store *memoryStore) SetNextSenderMsgSeqNum(nextSeqNum int) {
store.senderMsgSeqNum = nextSeqNum - 1
}
func (store *memoryStore) SetNextTargetMsgSeqNum(nextSeqNum int) {
store.targetMsgSeqNum = nextSeqNum - 1
}
func (store memoryStore) CreationTime() time.Time {
return store.creationTime
}
func (store *memoryStore) Reset() {
store.senderMsgSeqNum = 0
store.targetMsgSeqNum = 0
store.creationTime = time.Now()
store.messageMap = make(map[int][]byte)
}
func (store *memoryStore) Refresh() {
//nop, nothing to refresh
}
func (store memoryStore) SaveMessage(seqNum int, msg []byte) {
store.messageMap[seqNum] = msg
}
func (store memoryStore) GetMessages(beginSeqNum, endSeqNum int) chan []byte {
msgs := make(chan []byte)
go func() {
for seqNum := beginSeqNum; seqNum <= endSeqNum; seqNum++ {
if msg, ok := store.messageMap[seqNum]; ok {
msgs <- msg
}
}
close(msgs)
}()
return msgs
}
type memoryStoreFactory struct{}
func (f memoryStoreFactory) Create(sessionID SessionID) (MessageStore, error) {
return &memoryStore{messageMap: make(map[int][]byte)}, nil
}
//NewMemoryStoreFactory returns a MessageStoreFactory instance that created in-memory MessageStores
func NewMemoryStoreFactory() MessageStoreFactory { return memoryStoreFactory{} }