-
Notifications
You must be signed in to change notification settings - Fork 123
/
pubsubclient.go
107 lines (95 loc) · 2.95 KB
/
pubsubclient.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
// Copyright 2009-2012 Joubin Houshyar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis
// -----------------------------------------------------------------------------
// pubsubClient - supports PubSubClient interface
// -----------------------------------------------------------------------------
type pubsubClient struct {
// messages chan []byte
// subscriptions map[string]*Subscription
conn PubSubConnection
}
func NewPubSubClient() (PubSubClient, Error) {
spec := DefaultSpec().Protocol(REDIS_PUBSUB)
return NewPubSubClientWithSpec(spec)
}
func NewPubSubClientWithSpec(spec *ConnectionSpec) (PubSubClient, Error) {
c := new(pubsubClient)
var err Error
c.conn, err = NewPubSubConnection(spec)
if err != nil {
return nil, err
}
// c.messages = make(chan []byte, spec.rspChanCap)
// c.subscriptions = make(map[string]*Subscription)
return c, nil
}
func (c *pubsubClient) Messages(topic string) PubSubChannel {
// REVU - only after impl blocking subscribe in connection#ServiceRequest
if s := c.conn.Subscriptions()[topic]; s != nil {
ok, err := s.activated.Get()
if err != nil {
panic("BUG")
}
if ok {
return s.Channel
} else {
panic("BUG - isActivated.Get() returned nil err and false future results")
}
}
// if s := c.conn.Subscriptions()[topic]; s != nil {
// return s.Channel
// }
return nil
}
func (c *pubsubClient) Subscriptions() []string {
topics := make([]string, 0)
for topic, s := range c.conn.Subscriptions() {
if s.IsActive {
topics = append(topics, topic)
}
}
return topics
}
// REVU - why not async semantics?
func (c *pubsubClient) Subscribe(topic string, otherTopics ...string) (err Error) {
args := appendAndConvert(topic, otherTopics...)
// var ok bool
_, err = c.conn.ServiceRequest(&SUBSCRIBE, args)
// if err == nil {
// err = NewError(REDIS_ERR, "Subscribe() NOT IMPLEMENTED")
// }
return
}
// REVU - why not async semantics?
func (c *pubsubClient) Unsubscribe(topics ...string) (err Error) {
if topics == nil {
topics = c.Subscriptions()
}
var otherTopics []string = nil
if len(topics) > 1 {
otherTopics = topics[1:]
}
args := appendAndConvert(topics[0], otherTopics...)
// var ok bool
_, err = c.conn.ServiceRequest(&UNSUBSCRIBE, args)
// if err == nil {
// err = NewError(REDIS_ERR, "Subscribe() NOT IMPLEMENTED")
// }
return
}
// REVU - why not async semantics?
func (c *pubsubClient) Quit() Error {
return newSystemError("Quit() NOT IMPLEMENTED")
}