Rework writer deadline handling.
[stompngo.git] / unsubscribe.go
blobc85758b01461f054beafd3d0501db52f970a9989
1 //
2 // Copyright © 2011-2017 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 package stompngo
19 //import (
20 // "fmt"
21 //)
24 Unsubscribe from a STOMP subscription.
26 Headers MUST contain a "destination" header key, and for Stomp 1.1+,
27 a "id" header key per the specifications. The subscription MUST currently
28 exist for this session.
30 Example:
31 // Possible additional Header keys: "id".
32 h := stompngo.Headers{stompngo.HK_DESTINATION, "/queue/myqueue"}
33 e := c.Unsubscribe(h)
34 if e != nil {
35 // Do something sane ...
39 func (c *Connection) Unsubscribe(h Headers) error {
40 c.log(UNSUBSCRIBE, "start", h)
41 // fmt.Printf("Unsub Headers: %v\n", h)
42 if !c.connected {
43 return ECONBAD
45 e := checkHeaders(h, c.Protocol())
46 if e != nil {
47 return e
50 // Specification Requirements:
51 // 1.0) requires either a destination header or an id header
52 // 1.1) ... requires ... the id header ....
53 // 1.2) an id header MUST be included in the frame
55 _, okd := h.Contains(HK_DESTINATION)
56 shid, oki := h.Contains(HK_ID)
57 switch c.Protocol() {
58 case SPL_12:
59 if !oki {
60 return EUNOSID
62 case SPL_11:
63 if !oki {
64 return EUNOSID
66 case SPL_10:
67 if !oki && !okd {
68 return EUNODSID
70 default:
71 panic("unsubscribe version not supported: " + c.Protocol())
74 shaid := Sha1(h.Value(HK_DESTINATION)) // Special for 1.0
75 c.subsLock.RLock()
76 _, p := c.subs[shid]
77 _, ps := c.subs[shaid]
78 c.subsLock.RUnlock()
79 usekey := ""
81 switch c.Protocol() {
82 case SPL_12:
83 fallthrough
84 case SPL_11:
85 if !oki {
86 return EUNOSID // id required
88 if !p { // subscription does not exist
89 return EBADSID // invalid subscription-id
91 usekey = shid
92 case SPL_10:
93 if !p && !ps {
94 return EUNODSID
96 usekey = shaid
97 default:
98 panic("unsubscribe version not supported: " + c.Protocol())
101 e = c.transmitCommon(UNSUBSCRIBE, h) // transmitCommon Clones() the headers
102 if e != nil {
103 return e
106 c.subsLock.Lock()
107 delete(c.subs, usekey)
108 c.subsLock.Unlock()
109 c.log(UNSUBSCRIBE, "end", h)
110 return nil