-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemstore.go
127 lines (104 loc) · 2.95 KB
/
memstore.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
package mqtt
import (
"fmt"
"git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git/packets"
"sync"
)
type MemoryStore struct {
sync.RWMutex
messages map[string]map[string]packets.ControlPacket
}
func newMemoryStore() *MemoryStore {
store := &MemoryStore{
messages: make(map[string]map[string]packets.ControlPacket),
}
return store
}
func (this *MemoryStore) StoreSubscriptions(client *client) {
if client.clean {
return
}
}
func (this *MemoryStore) LookupSubscriptions(client *client) {
}
func (this *MemoryStore) StoreRetained(message *packets.PublishPacket) {
}
func (this *MemoryStore) LookupRetained(topic string, callback func(*packets.PublishPacket)) {
}
func (this *MemoryStore) FindInboundPacket(cid string, mid uint16) packets.ControlPacket {
this.Lock()
defer this.Unlock()
if _, ok := this.messages[cid]; ok {
return this.messages[cid][keyOfControlPackets(true, mid)]
}
return nil
}
func (this *MemoryStore) StoreInboundPacket(cid string, p packets.ControlPacket) error {
return this.storePacket(cid, keyOfControlPackets(true, p.Details().MessageID), p)
}
func (this *MemoryStore) StoreOutboundPacket(cid string, p packets.ControlPacket) error {
return this.storePacket(cid, keyOfControlPackets(false, p.Details().MessageID), p)
}
func keyOfControlPackets(inbound bool, mid uint16) string {
if inbound {
return fmt.Sprintf("i.%v", mid)
}
return fmt.Sprintf("o.%v", mid)
}
func (this *MemoryStore) storePacket(cid string, key string, message packets.ControlPacket) error {
switch message.(type) {
case *packets.PublishPacket, *packets.PubrelPacket:
default:
return ErrInvalidPacket
}
this.Lock()
defer this.Unlock()
if _, ok := this.messages[cid]; !ok {
this.messages[cid] = make(map[string]packets.ControlPacket)
}
this.messages[cid][key] = message
return nil
}
func (this *MemoryStore) StreamOfflinePackets(cid string, callback func(packets.ControlPacket)) {
this.RLock()
defer this.RUnlock()
if v, ok := this.messages[cid]; ok {
for key, message := range v {
if key[0] == 'o' {
callback(message)
}
}
}
}
func (this *MemoryStore) DeleteInboundPacket(cid string, mid uint16) {
this.deletePacket(cid, keyOfControlPackets(true, mid))
}
func (this *MemoryStore) DeleteOutboundPacket(cid string, mid uint16) {
this.deletePacket(cid, keyOfControlPackets(false, mid))
}
func (this *MemoryStore) deletePacket(cid string, key string) {
this.Lock()
defer this.Unlock()
if _, ok := this.messages[cid]; ok {
delete(this.messages[cid], key)
if len(this.messages[cid]) == 0 {
delete(this.messages, cid)
}
}
}
func (this *MemoryStore) UpdatePacket(cid string, key string, p packets.ControlPacket) {
}
func (this *MemoryStore) CleanPackets(cid string) {
this.Lock()
defer this.Unlock()
delete(this.messages, cid)
}
func (this *MemoryStore) OfflineMessageLen() uint64 {
this.RLock()
defer this.RUnlock()
var count uint64 = 0
for _, value := range this.messages {
count += uint64(len(value))
}
return count
}