-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub.go
More file actions
134 lines (121 loc) · 2.91 KB
/
pubsub.go
File metadata and controls
134 lines (121 loc) · 2.91 KB
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
package main
import (
"log"
"net"
"sync"
)
const (
PubSubTopicBasic = "basic"
)
// UdpClient describes a client by its address and a channel for its mesages
type UdpClient struct {
Addr *net.UDPAddr
Chan chan []byte
}
// Pubsub describes a publish/subscribe broker with different topics to subscribe on.
type Pubsub struct {
nm *NetworkMgr
mu sync.Mutex
subs map[string]*sync.Map
closed bool
}
// NewPubsub creates a new Pubsub.
func NewPubsub(nm *NetworkMgr) *Pubsub {
ps := &Pubsub{}
ps.nm = nm
ps.subs = make(map[string]*sync.Map)
return ps
}
// Subscribe a client to a topic.
func (ps *Pubsub) Subscribe(topic string, addr *net.UDPAddr) {
ps.mu.Lock()
defer ps.mu.Unlock()
if ps.subs[topic] == nil {
ps.subs[topic] = &sync.Map{}
}
s := addr.String()
if _, ok := ps.subs[topic].Load(s); !ok {
ps.subs[topic].Store(s, &UdpClient{
Addr: addr,
Chan: make(chan []byte),
})
}
}
// Unsubscribe a client from a topic.
func (ps *Pubsub) Unsubscribe(topic string, addr *net.UDPAddr) {
ps.mu.Lock()
defer ps.mu.Unlock()
if ps.subs[topic] == nil {
return
}
s := addr.String()
_, _ = ps.subs[topic].LoadAndDelete(s)
}
// Publish a message to a topic.
func (ps *Pubsub) Publish(topic string, msg []byte) {
ps.PublishWithOptions(topic, msg, PlainMode)
}
// PublishWithOptions publishes s message to a topic with a config if encryption should be used.
func (ps *Pubsub) PublishWithOptions(topic string, msg []byte, plain bool) {
ps.mu.Lock()
defer ps.mu.Unlock()
if ps.closed {
return
}
if !plain {
data, err := ps.nm.gz.Pack(msg)
if err != nil {
log.Printf("ERROR compressing data: %v", err)
}
msg = data
}
ps.subs[topic].Range(func(k interface{}, client interface{}) bool {
client.(*UdpClient).Chan <- msg
return true
})
}
// Unicast sends a message to a client
func (ps *Pubsub) Unicast(addr *net.UDPAddr, msg []byte) {
ps.UnicastWithOptions(addr, msg, PlainMode)
}
// UnicastWithOptions sends a message to a client with a config if encryption should be used.
func (ps *Pubsub) UnicastWithOptions(addr *net.UDPAddr, msg []byte, plain bool) {
ps.mu.Lock()
defer ps.mu.Unlock()
if ps.closed {
return
}
if !plain {
data, err := ps.nm.gz.Pack(msg)
if err != nil {
log.Printf("ERROR compressing data: %v", err)
}
msg = data
}
client, _ := ps.subs[PubSubTopicBasic].Load(addr.String())
client.(*UdpClient).Chan <- msg
}
func (ps *Pubsub) GetClients() []string {
ps.mu.Lock()
defer ps.mu.Unlock()
var clients []string
ps.subs[PubSubTopicBasic].Range(func(k interface{}, c interface{}) bool {
clients = append(clients, k.(string))
return true
})
return clients
}
// Close unsubscribes all clients from all topics.
func (ps *Pubsub) Close() {
ps.mu.Lock()
defer ps.mu.Unlock()
if !ps.closed {
ps.closed = true
for _, clients := range ps.subs {
clients.Range(func(k interface{}, c interface{}) bool {
close(c.(*UdpClient).Chan)
return true
})
}
}
}