-
Notifications
You must be signed in to change notification settings - Fork 294
/
settings.go
191 lines (152 loc) · 5.22 KB
/
settings.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
186
187
188
189
190
191
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact [email protected] if any conditions of this licensing
// are not clear to you.
package quickfix
import (
"bufio"
"errors"
"fmt"
"io"
"regexp"
"github.com/quickfixgo/quickfix/config"
)
// The Settings type represents a collection of global and session settings.
type Settings struct {
globalSettings *SessionSettings
sessionSettings map[SessionID]*SessionSettings
}
// Init initializes or resets a Settings instance.
func (s *Settings) Init() {
s.globalSettings = NewSessionSettings()
s.sessionSettings = make(map[SessionID]*SessionSettings)
}
func (s *Settings) lazyInit() {
if s.globalSettings == nil {
s.Init()
}
}
// NewSettings creates a Settings instance.
func NewSettings() *Settings {
s := &Settings{}
s.Init()
return s
}
func sessionIDFromSessionSettings(globalSettings *SessionSettings, sessionSettings *SessionSettings) SessionID {
sessionID := SessionID{}
for _, settings := range []*SessionSettings{globalSettings, sessionSettings} {
if settings.HasSetting(config.BeginString) {
sessionID.BeginString, _ = settings.Setting(config.BeginString)
}
if settings.HasSetting(config.TargetCompID) {
sessionID.TargetCompID, _ = settings.Setting(config.TargetCompID)
}
if settings.HasSetting(config.TargetSubID) {
sessionID.TargetSubID, _ = settings.Setting(config.TargetSubID)
}
if settings.HasSetting(config.TargetLocationID) {
sessionID.TargetLocationID, _ = settings.Setting(config.TargetLocationID)
}
if settings.HasSetting(config.SenderCompID) {
sessionID.SenderCompID, _ = settings.Setting(config.SenderCompID)
}
if settings.HasSetting(config.SenderSubID) {
sessionID.SenderSubID, _ = settings.Setting(config.SenderSubID)
}
if settings.HasSetting(config.SenderLocationID) {
sessionID.SenderLocationID, _ = settings.Setting(config.SenderLocationID)
}
if settings.HasSetting(config.SessionQualifier) {
sessionID.Qualifier, _ = settings.Setting(config.SessionQualifier)
}
}
return sessionID
}
// ParseSettings creates and initializes a Settings instance with config parsed from a Reader.
// Returns error if the config is has parse errors.
func ParseSettings(reader io.Reader) (*Settings, error) {
s := NewSettings()
scanner := bufio.NewScanner(reader)
blankRegEx := regexp.MustCompile(`^\s*$`)
commentRegEx := regexp.MustCompile(`^#.*`)
defaultRegEx := regexp.MustCompile(`^\[(?i)DEFAULT\]\s*$`)
sessionRegEx := regexp.MustCompile(`^\[(?i)SESSION\]\s*$`)
settingRegEx := regexp.MustCompile(`^([^=]*)=(.*)$`)
var settings *SessionSettings
lineNumber := 0
for scanner.Scan() {
lineNumber++
line := scanner.Text()
switch {
case commentRegEx.MatchString(line) || blankRegEx.MatchString(line):
continue
case defaultRegEx.MatchString(line):
settings = s.GlobalSettings()
case sessionRegEx.MatchString(line):
if settings != nil && settings != s.GlobalSettings() {
if _, err := s.AddSession(settings); err != nil {
return nil, err
}
}
settings = NewSessionSettings()
case settingRegEx.MatchString(line):
parts := settingRegEx.FindStringSubmatch(line)
settings.Set(parts[1], parts[2])
default:
return s, fmt.Errorf("error parsing line %v", lineNumber)
}
}
if err := scanner.Err(); err != nil {
return s, err
}
if settings == nil || settings == s.GlobalSettings() {
return s, fmt.Errorf("no sessions declared")
}
_, err := s.AddSession(settings)
return s, err
}
// GlobalSettings are default setting inherited by all session settings.
func (s *Settings) GlobalSettings() *SessionSettings {
s.lazyInit()
return s.globalSettings
}
// SessionSettings return all session settings overlaying globalsettings.
func (s *Settings) SessionSettings() map[SessionID]*SessionSettings {
allSessionSettings := make(map[SessionID]*SessionSettings)
for sessionID, settings := range s.sessionSettings {
cloneSettings := s.globalSettings.clone()
cloneSettings.overlay(settings)
allSessionSettings[sessionID] = cloneSettings
}
return allSessionSettings
}
// AddSession adds Session Settings to Settings instance. Returns an error if session settings with duplicate sessionID has already been added.
func (s *Settings) AddSession(sessionSettings *SessionSettings) (SessionID, error) {
s.lazyInit()
sessionID := sessionIDFromSessionSettings(s.GlobalSettings(), sessionSettings)
switch sessionID.BeginString {
case BeginStringFIX40:
case BeginStringFIX41:
case BeginStringFIX42:
case BeginStringFIX43:
case BeginStringFIX44:
case BeginStringFIXT11:
default:
return sessionID, errors.New("BeginString must be FIX.4.0 to FIX.4.4 or FIXT.1.1")
}
if _, dup := s.sessionSettings[sessionID]; dup {
return sessionID, fmt.Errorf("duplicate session configured for %v", sessionID)
}
s.sessionSettings[sessionID] = sessionSettings
return sessionID, nil
}