Reformat: comments.
[stompngo.git] / unsubscribe.go
blobb07df81eaf9e68710bc5d9f787eae77f1358fef1
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 if !c.connected {
42 return ECONBAD
44 e := checkHeaders(h, c.Protocol())
45 if e != nil {
46 return e
50 d, okd := h.Contains(HK_DESTINATION)
51 //fmt.Printf("Unsubscribe Headers 01: <%q> <%t>\n", h, okd)
52 _ = d
53 if !okd {
54 return EREQDSTUNS
57 shid, oki := h.Contains(HK_ID)
58 shaid := Sha1(h.Value(HK_DESTINATION)) // Special for 1.0
59 //fmt.Printf("Unsubscribe Headers 02: <%q> <%t>\n", h, oki)
60 // This is a read lock
61 //fmt.Printf("UNSUB DBG00 %q\n", c.subs)
62 c.subsLock.RLock()
63 _, p := c.subs[shid]
64 _, ps := c.subs[shaid]
65 c.subsLock.RUnlock()
66 //fmt.Printf("UNSUB DBG01 %t %t\n", p, ps)
67 usekey := ""
69 switch c.Protocol() {
70 case SPL_12:
71 fallthrough
72 case SPL_11:
73 if !oki {
74 return EUNOSID // id required
76 if !p { // subscription does not exist
77 return EBADSID // invalid subscription-id
79 usekey = shid
80 case SPL_10:
82 //fmt.Printf("SPL_10 D01 %v %v %vn", oki, p, ps)
83 if !p && !ps {
84 return EUNOSID
86 usekey = shaid
87 default:
88 panic("unsubscribe version not supported: " + c.Protocol())
91 e = c.transmitCommon(UNSUBSCRIBE, h) // transmitCommon Clones() the headers
92 if e != nil {
93 return e
96 c.subsLock.Lock()
97 delete(c.subs, usekey)
98 c.subsLock.Unlock()
99 c.log(UNSUBSCRIBE, "end", h)
100 return nil