-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
237 lines (211 loc) · 5.89 KB
/
transport.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package stomp
import (
"bytes"
"io"
"io/ioutil"
"net"
"strconv"
"strings"
"time"
)
// Transport represents a STOMP 1.2 compatible connection.
// A transport object provides STOMP functionality atop an underlying
// stream.
type Transport struct {
enc *Encoder
dec *Decoder
conn net.Conn
}
// NewTransport returns a new transport object that wraps conn.
func NewTransport(conn net.Conn) *Transport {
return &Transport{
enc: NewEncoder(conn),
dec: NewDecoder(conn),
conn: conn,
}
}
// Close closes the underlying stream.
func (t *Transport) Close() (err error) {
return t.conn.Close()
}
// Disconnect prepares a DISCONNECT frame to gracefully shutdown the transport.
// Disconnect does not close the underlying stream.
func (t *Transport) Disconnect(receipt string) error {
f := NewFrame("DISCONNECT", nil)
f.Headers["receipt"] = receipt
return t.enc.Encode(f)
}
// Heartbeat sends a heart-beat frame.
func (t *Transport) Heartbeat() error {
f := NewFrame("HEARTBEAT", nil)
return t.enc.Encode(f)
}
// Send sends a message to requested destination dest.
// The parameters hdrs, body, and receipt may be nil, indicating that they
// will not be used for the sent message.
// Send automatically generates a content-length for the provided body.
func (t *Transport) Send(dest string, hdrs *map[string]string, bodyType string, body io.Reader, receipt *string) error {
f, err := makeSendFrame(dest, hdrs, bodyType, body)
if err != nil {
return err
}
if receipt != nil {
f.Headers["receipt"] = *receipt
}
return t.enc.Encode(f)
}
// Ack sends an ACK frame.
// A non-nil receipt value will be attached to the frame.
func (t *Transport) Ack(id string, receipt *string) error {
f := NewFrame("ACK", nil)
f.Headers["id"] = id
if receipt != nil {
f.Headers["receipt"] = *receipt
}
return t.enc.Encode(f)
}
// Nack sends a NACK frame.
// A non-nil receipt value will be attached to the frame.
func (t *Transport) Nack(id string, receipt *string) error {
f := NewFrame("NACK", nil)
f.Headers["id"] = id
if receipt != nil {
f.Headers["receipt"] = *receipt
}
return t.enc.Encode(f)
}
// Subscribe initiates a subscription to the requested destination dest.
// A non-nil receipt value will be attached to the frame.
func (t *Transport) Subscribe(id string, dest string, mode AckMode, receipt *string) error {
f := NewFrame("SUBSCRIBE", nil)
f.Headers["destination"] = dest
f.Headers["id"] = id
f.Headers["ack"] = string(mode)
if receipt != nil {
f.Headers["receipt"] = *receipt
}
return t.enc.Encode(f)
}
// Unsubscribe unsubscribes from the subscription with id.
// A non-nil receipt value will be attached to the frame.
func (t *Transport) Unsubscribe(id string, receipt *string) error {
f := NewFrame("UNSUBSCRIBE", nil)
f.Headers["id"] = id
if receipt != nil {
f.Headers["receipt"] = *receipt
}
return t.enc.Encode(f)
}
// TxBegin sends a BEGIN frame.
// A non-nil receipt value will be attached to the frame.
func (t *Transport) TxBegin(tid string, receipt *string) error {
f := NewFrame("BEGIN", nil)
f.Headers["transaction"] = tid
if receipt != nil {
f.Headers["receipt"] = *receipt
}
return t.enc.Encode(f)
}
// TxCommit sends a COMMIT frame.
// A non-nil receipt value will be attached to the frame.
func (t *Transport) TxCommit(tid string, receipt *string) error {
f := NewFrame("COMMIT", nil)
f.Headers["transaction"] = tid
if receipt != nil {
f.Headers["receipt"] = *receipt
}
return t.enc.Encode(f)
}
// TxAbort sends a ABORT frame.
// A non-nil receipt value will be attached to the frame.
func (t *Transport) TxAbort(tid string, receipt *string) error {
f := NewFrame("ABORT", nil)
f.Headers["transaction"] = tid
if receipt != nil {
f.Headers["receipt"] = *receipt
}
return t.enc.Encode(f)
}
// TxSend behaves just as Send does, with the exception of being
// within a transaction.
func (t *Transport) TxSend(tid string, dest string, hdrs *map[string]string, bodyType string, body io.Reader) error {
f, err := makeSendFrame(dest, hdrs, bodyType, body)
if err != nil {
return err
}
f.Headers["transaction"] = tid
return t.enc.Encode(f)
}
// TxAck behaves just as Ack does, with the exception of being
// within a transaction.
func (t *Transport) TxAck(tid string, id string) error {
f := NewFrame("ACK", nil)
f.Headers["id"] = id
f.Headers["transaction"] = tid
return t.enc.Encode(f)
}
// TxNack behaves just as Nack does, with the exception of being
// within a transaction.
func (t *Transport) TxNack(tid string, id string) error {
f := NewFrame("NACK", nil)
f.Headers["id"] = id
f.Headers["transaction"] = tid
return t.enc.Encode(f)
}
// Recv returns a frame from the underlying stream.
// Any errors encountered while reading will be returned.
func (t *Transport) Recv(timeout time.Duration) (*Frame, error) {
if timeout > 0 {
t.conn.SetReadDeadline(time.Now().Add(timeout * 2))
}
f := &Frame{}
err := t.dec.Decode(f)
if err != nil {
return nil, err
}
return f, nil
}
type sizedReader interface {
Len() int
}
var forbidden = map[string]struct{}{
"destination": struct{}{},
"id": struct{}{},
"content-type": struct{}{},
"content-length": struct{}{},
"receipt": struct{}{},
"transaction": struct{}{},
}
func makeSendFrame(dest string, hdrs *map[string]string, bodyType string, body io.Reader) (*Frame, error) {
f := NewFrame("SEND", body)
f.Headers["destination"] = dest
if f.Body != nil {
var n int64
if sr, ok := f.Body.(sizedReader); ok {
n = int64(sr.Len())
} else {
tmp := &bytes.Buffer{}
var err error
n, err = io.Copy(tmp, f.Body)
if err != nil {
return nil, err
}
err = f.Body.Close()
if err != nil {
return nil, err
}
f.Body = ioutil.NopCloser(tmp)
}
f.Headers["content-type"] = bodyType
f.Headers["content-length"] = strconv.Itoa(int(n))
}
if hdrs != nil {
for k, v := range *hdrs {
k = strings.ToLower(k)
if _, ok := forbidden[k]; !ok {
f.Headers[k] = v
}
}
}
return f, nil
}