forked from gofinance/ib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
current_time_manager.go
57 lines (47 loc) · 1.18 KB
/
current_time_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
package ib
import "time"
// CurrentTimeManager provides a Manager to access the IB current time on the server side
type CurrentTimeManager struct {
AbstractManager
id int64
t time.Time
}
// NewCurrentTimeManager .
func NewCurrentTimeManager(e *Engine) (*CurrentTimeManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}
m := &CurrentTimeManager{AbstractManager: *am, id: UnmatchedReplyID}
go m.startMainLoop(m.preLoop, m.receive, m.preDestroy)
return m, nil
}
func (m *CurrentTimeManager) preLoop() error {
req := &RequestCurrentTime{}
m.eng.Subscribe(m.rc, m.id)
return m.eng.Send(req)
}
func (m *CurrentTimeManager) receive(r Reply) (UpdateStatus, error) {
switch r.(type) {
case *ErrorMessage:
r := r.(*ErrorMessage)
if r.SeverityWarning() {
return UpdateFalse, nil
}
return UpdateFalse, r.Error()
case *CurrentTime:
ct := r.(*CurrentTime)
m.t = ct.Time
return UpdateFinish, nil
}
return UpdateFalse, nil
}
func (m *CurrentTimeManager) preDestroy() {
m.eng.Unsubscribe(m.rc, m.id)
}
// Time returns the current server time.
func (m *CurrentTimeManager) Time() time.Time {
m.rwm.RLock()
defer m.rwm.RUnlock()
return m.t
}