forked from go-stomp/stomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.go
123 lines (111 loc) · 2.81 KB
/
subscription.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
package stomp
import (
"fmt"
"log"
"github.com/go-stomp/stomp/frame"
)
// The Subscription type represents a client subscription to
// a destination. The subscription is created by calling Conn.Subscribe.
//
// Once a client has subscribed, it can receive messages from the C channel.
type Subscription struct {
C chan *Message
id string
destination string
conn *Conn
ackMode AckMode
completed bool
}
// BUG(jpj): If the client does not read messages from the Subscription.C
// channel quickly enough, the client will stop reading messages from the
// server.
// Identification for this subscription. Unique among
// all subscriptions for the same Client.
func (s *Subscription) Id() string {
return s.id
}
// Destination for which the subscription applies.
func (s *Subscription) Destination() string {
return s.destination
}
// AckMode returns the Acknowledgement mode specified when the
// subscription was created.
func (s *Subscription) AckMode() AckMode {
return s.ackMode
}
// Active returns whether the subscription is still active.
// Returns false if the subscription has been unsubscribed.
func (s *Subscription) Active() bool {
return !s.completed
}
// Unsubscribes and closes the channel C.
func (s *Subscription) Unsubscribe() error {
if s.completed {
return ErrCompletedSubscription
}
f := frame.New(frame.UNSUBSCRIBE, frame.Id, s.id)
s.conn.sendFrame(f)
s.completed = true
close(s.C)
return nil
}
// Read a message from the subscription. This is a convenience
// method: many callers will prefer to read from the channel C
// directly.
func (s *Subscription) Read() (*Message, error) {
if s.completed {
return nil, ErrCompletedSubscription
}
msg, ok := <-s.C
if !ok {
return nil, ErrCompletedSubscription
}
if msg.Err != nil {
return nil, msg.Err
}
return msg, nil
}
func (s *Subscription) readLoop(ch chan *frame.Frame) {
for {
f, ok := <-ch
if !ok {
return
}
if f.Command == frame.MESSAGE {
destination := f.Header.Get(frame.Destination)
contentType := f.Header.Get(frame.ContentType)
msg := &Message{
Destination: destination,
ContentType: contentType,
Conn: s.conn,
Subscription: s,
Header: f.Header,
Body: f.Body,
}
s.C <- msg
} else if f.Command == frame.ERROR {
message, _ := f.Header.Contains(frame.Message)
text := fmt.Sprintf("Subscription %s: %s: ERROR message:%s",
s.id,
s.destination,
message)
log.Println(text)
contentType := f.Header.Get(frame.ContentType)
msg := &Message{
Err: &Error{
Message: f.Header.Get(frame.Message),
Frame: f,
},
ContentType: contentType,
Conn: s.conn,
Subscription: s,
Header: f.Header,
Body: f.Body,
}
s.C <- msg
s.completed = true
close(s.C)
return
}
}
}