-
Notifications
You must be signed in to change notification settings - Fork 122
/
historical_data_manager.go
59 lines (50 loc) · 1.34 KB
/
historical_data_manager.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
package ib
import "fmt"
// HistoricalDataManager .
type HistoricalDataManager struct {
AbstractManager
request RequestHistoricalData
histData []HistoricalDataItem
}
// NewHistoricalDataManager Create a new HistoricalDataManager for the given data request.
func NewHistoricalDataManager(e *Engine, request RequestHistoricalData) (*HistoricalDataManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}
request.id = e.NextRequestID()
m := &HistoricalDataManager{
AbstractManager: *am,
request: request,
}
go m.startMainLoop(m.preLoop, m.receive, m.preDestroy)
return m, nil
}
func (m *HistoricalDataManager) preLoop() error {
m.eng.Subscribe(m.rc, m.request.id)
return m.eng.Send(&m.request)
}
func (m *HistoricalDataManager) receive(r Reply) (UpdateStatus, error) {
switch r.(type) {
case *ErrorMessage:
r := r.(*ErrorMessage)
if r.SeverityWarning() {
return UpdateFalse, nil
}
return UpdateFalse, r.Error()
case *HistoricalData:
hd := r.(*HistoricalData)
m.histData = hd.Data
return UpdateFinish, nil
}
return UpdateFalse, fmt.Errorf("Unexpected type %v", r)
}
func (m *HistoricalDataManager) preDestroy() {
m.eng.Unsubscribe(m.rc, m.request.id)
}
// Items .
func (m *HistoricalDataManager) Items() []HistoricalDataItem {
m.rwm.RLock()
defer m.rwm.RUnlock()
return m.histData
}