forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_interface.go
80 lines (72 loc) · 2.05 KB
/
base_interface.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
package base
import (
"errors"
"time"
"github.com/idoall/gocryptotrader/log"
)
// IComm is the main interface array across the communication packages
type IComm []ICommunicate
// ICommunicate enforces standard functions across communication packages
type ICommunicate interface {
Setup(config *CommunicationsConfig)
Connect() error
PushEvent(Event) error
IsEnabled() bool
IsConnected() bool
GetName() string
SetServiceStarted(time.Time)
}
// Setup sets up communication variables and initiates a connection to the
// communication mediums
func (c IComm) Setup() {
for i := range c {
if c[i].IsEnabled() && !c[i].IsConnected() {
err := c[i].Connect()
if err != nil {
log.Errorf(log.CommunicationMgr, "Communications: %s failed to connect. Err: %s", c[i].GetName(), err)
continue
}
log.Debugf(log.CommunicationMgr, "Communications: %v is enabled and online.", c[i].GetName())
c[i].SetServiceStarted(time.Now())
}
}
}
// PushEvent pushes triggered events to all enabled communication links
func (c IComm) PushEvent(event Event) {
for i := range c {
if c[i].IsEnabled() && c[i].IsConnected() {
err := c[i].PushEvent(event)
if err != nil {
log.Errorf(log.CommunicationMgr, "Communications error - PushEvent() in package %s with %v. Err %s",
c[i].GetName(), event, err)
}
}
}
}
// GetStatus returns the status of the comms relayers
func (c IComm) GetStatus() map[string]CommsStatus {
result := make(map[string]CommsStatus)
for x := range c {
result[c[x].GetName()] = CommsStatus{
Enabled: c[x].IsEnabled(),
Connected: c[x].IsConnected(),
}
}
return result
}
// GetEnabledCommunicationMediums prints out enabled and connected communication
// packages
// (#debug output only)
func (c IComm) GetEnabledCommunicationMediums() error {
var count int
for i := range c {
if c[i].IsEnabled() && c[i].IsConnected() {
log.Debugf(log.CommunicationMgr, "Communications: Medium %s is enabled.", c[i].GetName())
count++
}
}
if count == 0 {
return errors.New("no communication mediums are enabled")
}
return nil
}