-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponsehandler.go
110 lines (89 loc) · 2.94 KB
/
responsehandler.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
package messagix
import (
"fmt"
"time"
)
type ChannelType int
const (
RequestChannel ChannelType = iota
PacketChannel
)
type ResponseHandler struct {
client *Client
requestChannels map[uint16]chan interface{}
packetChannels map[uint16]chan interface{}
packetTimeout time.Duration
}
func (p *ResponseHandler) SetPacketTimeout(d time.Duration) {
p.packetTimeout = d
}
func (p *ResponseHandler) hasPacket(packetId uint16) bool {
_, ok := p.packetChannels[packetId]
return ok
}
func (p *ResponseHandler) addPacketChannel(packetId uint16) {
p.packetChannels[packetId] = make(chan interface{}, 1) // buffered channel with capacity of 1
}
func (p *ResponseHandler) addRequestChannel(packetId uint16) {
p.requestChannels[packetId] = make(chan interface{}, 1)
}
func (p *ResponseHandler) updatePacketChannel(packetId uint16, packetData interface{}) error {
if ch, ok := p.packetChannels[packetId]; ok {
ch <- packetData
//p.client.Logger.Info().Any("data", packetData).Any("packetId", packetId).Msg("Updated packet channel!")
return nil
}
return fmt.Errorf("failed to update packet channel for packetId %d", packetId)
}
func (p *ResponseHandler) updateRequestChannel(packetId uint16, packetData interface{}) error {
if ch, ok := p.requestChannels[packetId]; ok {
ch <- packetData
//p.client.Logger.Info().Any("data", packetData).Any("packetId", packetId).Msg("Updated request channel!")
return nil
}
return fmt.Errorf("failed to update request channel for packetId %d", packetId)
}
func (p *ResponseHandler) waitForPubACKDetails(packetId uint16) *Event_PublishACK {
return p.waitForDetails(packetId, PacketChannel).(*Event_PublishACK)
}
func (p *ResponseHandler) waitForSubACKDetails(packetId uint16) *Event_SubscribeACK {
return p.waitForDetails(packetId, PacketChannel).(*Event_SubscribeACK)
}
func (p *ResponseHandler) waitForPubResponseDetails(packetId uint16) *Event_PublishResponse {
return p.waitForDetails(packetId, RequestChannel).(*Event_PublishResponse)
}
func (p *ResponseHandler) waitForDetails(packetId uint16, channelType ChannelType) interface{} {
ch, ok := p.getChannel(packetId, channelType)
if !ok {
return nil
}
select {
case response := <- ch:
p.deleteDetails(packetId, channelType)
return response
case <-time.After(p.packetTimeout):
p.deleteDetails(packetId, channelType)
return &Event_PublishResponse{}
}
}
func (p *ResponseHandler) deleteDetails(packetId uint16, channelType ChannelType) {
if ch, ok := p.getChannel(packetId, channelType); ok {
close(ch)
if channelType == RequestChannel {
delete(p.requestChannels, packetId)
} else {
delete(p.packetChannels, packetId)
}
}
}
func (p *ResponseHandler) getChannel(packetId uint16, channelType ChannelType) (chan interface{}, bool) {
var ch chan interface{}
var ok bool
switch channelType {
case RequestChannel:
ch, ok = p.requestChannels[packetId]
case PacketChannel:
ch, ok = p.packetChannels[packetId]
}
return ch, ok
}