-
Notifications
You must be signed in to change notification settings - Fork 13
/
heartbeat.go
148 lines (115 loc) · 3.18 KB
/
heartbeat.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
package spine
import (
"sync"
"sync/atomic"
"time"
"github.com/enbility/eebus-go/logging"
"github.com/enbility/eebus-go/spine/model"
)
const hearbeatMsgCountersSize = 10
type HeartbeatSender struct {
heartBeatNum uint64 // see https://github.com/golang/go/issues/11891
stopHeartbeatC chan struct{}
stopMux sync.Mutex
senderAddr, destinationAddr *model.FeatureAddressType
sender Sender
heartBeatTimeout *model.DurationType
msgCounters []model.MsgCounterType
mux sync.Mutex
}
func NewHeartbeatSender(sender Sender) *HeartbeatSender {
h := &HeartbeatSender{
sender: sender,
}
// default to 4 seconds timeout
h.heartBeatTimeout = model.NewDurationType(time.Second * 4)
return h
}
func (c *HeartbeatSender) AddMsgCounter(msgCounter *model.MsgCounterType) {
if msgCounter == nil {
return
}
c.mux.Lock()
defer c.mux.Unlock()
c.msgCounters = append(c.msgCounters, *msgCounter)
if len(c.msgCounters) > hearbeatMsgCountersSize {
c.msgCounters = c.msgCounters[1:]
}
}
func (c *HeartbeatSender) IsHeartbeatMsgCounter(msgCounter model.MsgCounterType) bool {
c.mux.Lock()
defer c.mux.Unlock()
for _, item := range c.msgCounters {
if item == msgCounter {
return true
}
}
return false
}
func (c *HeartbeatSender) StartHeartbeatSend(senderAddr, destinationAddr *model.FeatureAddressType) {
// stop a already running heartbeat
c.StopHeartbeat()
c.senderAddr = senderAddr
c.destinationAddr = destinationAddr
c.stopHeartbeatC = make(chan struct{})
go func() {
c.sendHearbeat(c.stopHeartbeatC, 800*time.Millisecond)
}()
}
func (c *HeartbeatSender) StopHeartbeat() {
c.stopMux.Lock()
defer c.stopMux.Unlock()
if c.stopHeartbeatC != nil && !c.isHeartbeatClosed() {
close(c.stopHeartbeatC)
}
}
func (c *HeartbeatSender) heartbeatCmd(t time.Time) model.CmdType {
timestamp := t.UTC().Format(time.RFC3339)
cmd := model.CmdType{
DeviceDiagnosisHeartbeatData: &model.DeviceDiagnosisHeartbeatDataType{
Timestamp: ×tamp,
HeartbeatCounter: c.heartBeatCounter(),
HeartbeatTimeout: c.heartBeatTimeout,
},
}
return cmd
}
func (c *HeartbeatSender) SendHeartBeatData(requestHeader *model.HeaderType) error {
// TODO is this all we need here?
cmd := c.heartbeatCmd(time.Now())
return c.sender.Reply(requestHeader, c.senderAddr, cmd)
}
func (c *HeartbeatSender) sendHearbeat(stopC chan struct{}, d time.Duration) {
ticker := time.NewTicker(d)
for {
select {
case <-ticker.C:
if c.senderAddr == nil || c.destinationAddr == nil {
break
}
cmd := c.heartbeatCmd(time.Now())
msgCounter, err := c.sender.Notify(c.senderAddr, c.destinationAddr, cmd)
if err != nil {
logging.Log.Debug("ERROR sending heartbeat: ", err)
}
if msgCounter != nil {
c.msgCounters = append(c.msgCounters, *msgCounter)
}
case <-stopC:
return
}
}
}
func (c *HeartbeatSender) isHeartbeatClosed() bool {
select {
case <-c.stopHeartbeatC:
return true
default:
}
return false
}
// TODO heartBeatCounter should be global on CEM level, not on connection level
func (c *HeartbeatSender) heartBeatCounter() *uint64 {
i := atomic.AddUint64(&c.heartBeatNum, 1)
return &i
}