-
Notifications
You must be signed in to change notification settings - Fork 57
/
sub.go
145 lines (122 loc) · 3.19 KB
/
sub.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
// Copyright 2018 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zmq4
import (
"context"
"net"
"sort"
"sync"
)
// NewSub returns a new SUB ZeroMQ socket.
// The returned socket value is initially unbound.
func NewSub(ctx context.Context, opts ...Option) Socket {
sub := &subSocket{sck: newSocket(ctx, Sub, opts...)}
sub.sck.r = newQReader(sub.sck.ctx)
sub.sck.subTopics = sub.Topics
sub.topics = make(map[string]struct{})
return sub
}
// subSocket is a SUB ZeroMQ socket.
type subSocket struct {
sck *socket
mu sync.RWMutex
topics map[string]struct{}
}
// Close closes the open Socket
func (sub *subSocket) Close() error {
return sub.sck.Close()
}
// Send puts the message on the outbound send queue.
// Send blocks until the message can be queued or the send deadline expires.
func (sub *subSocket) Send(msg Msg) error {
return sub.sck.Send(msg)
}
// SendMulti puts the message on the outbound send queue.
// SendMulti blocks until the message can be queued or the send deadline expires.
// The message will be sent as a multipart message.
func (sub *subSocket) SendMulti(msg Msg) error {
return sub.sck.SendMulti(msg)
}
// Recv receives a complete message.
func (sub *subSocket) Recv() (Msg, error) {
return sub.sck.Recv()
}
// Listen connects a local endpoint to the Socket.
func (sub *subSocket) Listen(ep string) error {
return sub.sck.Listen(ep)
}
// Dial connects a remote endpoint to the Socket.
func (sub *subSocket) Dial(ep string) error {
err := sub.sck.Dial(ep)
if err != nil {
return err
}
return nil
}
// Type returns the type of this Socket (PUB, SUB, ...)
func (sub *subSocket) Type() SocketType {
return sub.sck.Type()
}
// Addr returns the listener's address.
// Addr returns nil if the socket isn't a listener.
func (sub *subSocket) Addr() net.Addr {
return sub.sck.Addr()
}
// GetOption is used to retrieve an option for a socket.
func (sub *subSocket) GetOption(name string) (interface{}, error) {
return sub.sck.GetOption(name)
}
// SetOption is used to set an option for a socket.
func (sub *subSocket) SetOption(name string, value interface{}) error {
err := sub.sck.SetOption(name, value)
if err != nil {
return err
}
var (
topic []byte
)
switch name {
case OptionSubscribe:
k := value.(string)
sub.subscribe(k, 1)
topic = append([]byte{1}, k...)
case OptionUnsubscribe:
k := value.(string)
topic = append([]byte{0}, k...)
sub.subscribe(k, 0)
default:
return ErrBadProperty
}
sub.sck.mu.RLock()
if len(sub.sck.conns) > 0 {
err = sub.Send(NewMsg(topic))
}
sub.sck.mu.RUnlock()
return err
}
// Topics returns the sorted list of topics a socket is subscribed to.
func (sub *subSocket) Topics() []string {
sub.mu.RLock()
var topics = make([]string, 0, len(sub.topics))
for topic := range sub.topics {
topics = append(topics, topic)
}
sub.mu.RUnlock()
sort.Strings(topics)
return topics
}
func (sub *subSocket) subscribe(topic string, v int) {
sub.mu.Lock()
switch v {
case 0:
delete(sub.topics, topic)
case 1:
sub.topics[topic] = struct{}{}
}
sub.mu.Unlock()
}
var (
_ Socket = (*subSocket)(nil)
_ Topics = (*subSocket)(nil)
)