Debian Codegraph @

go/pkg/electron/handler.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 package electron
21
22 import (
23 "github.com/apache/qpid-proton/go/pkg/amqp"
24 "github.com/apache/qpid-proton/go/pkg/proton"
25 )
26
27 // NOTE: methods in this file are called only in the proton goroutine unless otherwise indicated.
28
29 type handler struct {
30 delegator *proton.MessagingAdapter
31 connection *connection
32 links map[proton.Link]Endpoint
33 sent map[proton.Delivery]*sendable // Waiting for outcome
34 sessions map[proton.Session]*session
35 }
36
37 func newHandler(c *connection) *handler {
38 h := &handler{
39 connection: c,
40 links: make(map[proton.Link]Endpoint),
41 sent: make(map[proton.Delivery]*sendable),
42 sessions: make(map[proton.Session]*session),
43 }
44 h.delegator = proton.NewMessagingAdapter(h)
45 // Disable auto features of MessagingAdapter, we do these ourselves.
46 h.delegator.Prefetch = 0
47 h.delegator.AutoAccept = false
48 h.delegator.AutoSettle = false
49 h.delegator.AutoOpen = false
50 return h
51 }
52
53 func (h *handler) linkError(l proton.Link, msg string) {
54 proton.CloseError(l, amqp.Errorf(amqp.InternalError, "%s for %s %s", msg, l.Type(), l))
55 }
56
57 func (h *handler) HandleMessagingEvent(t proton.MessagingEvent, e proton.Event) {
58 switch t {
59
60 case proton.MMessage:
61 if r, ok := h.links[e.Link()].(*receiver); ok {
62 r.message(e.Delivery())
63 } else {
64 h.linkError(e.Link(), "no receiver")
65 }
66
67 case proton.MSettled:
68 if sm, ok := h.sent[e.Delivery()]; ok {
69 d := e.Delivery().Remote()
70 sm.ack <- Outcome{sentStatus(d.Type()), d.Condition().Error(), sm.v}
71 delete(h.sent, e.Delivery())
72 }
73
74 case proton.MSendable:
75 if s, ok := h.links[e.Link()].(*sender); ok {
76 s.trySend()
77 } else {
78 h.linkError(e.Link(), "no sender")
79 }
80
81 case proton.MConnectionOpening:
82 h.connection.heartbeat = e.Transport().RemoteIdleTimeout()
83 if e.Connection().State().LocalUninit() { // Remotely opened
84 h.incoming(newIncomingConnection(h.connection))
85 }
86 h.connection.wakeSync()
87
88 case proton.MSessionOpening:
89 if e.Session().State().LocalUninit() { // Remotely opened
90 h.incoming(newIncomingSession(h, e.Session()))
91 }
92 h.sessions[e.Session()].wakeSync()
93
94 case proton.MSessionClosed:
95 h.sessionClosed(e.Session(), proton.EndpointError(e.Session()))
96
97 case proton.MLinkOpening:
98 l := e.Link()
99 if ss := h.sessions[l.Session()]; ss != nil {
100 if l.State().LocalUninit() { // Remotely opened.
101 if l.IsReceiver() {
102 h.incoming(newIncomingReceiver(ss, l))
103 } else {
104 h.incoming(newIncomingSender(ss, l))
105 }
106 }
107 if ep, ok := h.links[l]; ok {
108 ep.(endpointInternal).wakeSync()
109 } else {
110 h.linkError(l, "no link")
111 }
112 } else {
113 h.linkError(l, "no session")
114 }
115
116 case proton.MLinkClosing:
117 e.Link().Close()
118
119 case proton.MLinkClosed:
120 h.linkClosed(e.Link(), proton.EndpointError(e.Link()))
121
122 case proton.MConnectionClosing:
123 h.connection.err.Set(e.Connection().RemoteCondition().Error())
124
125 case proton.MConnectionClosed:
126 h.shutdown(proton.EndpointError(e.Connection()))
127
128 case proton.MDisconnected:
129 var err error
130 if err = e.Connection().RemoteCondition().Error(); err == nil {
131 if err = e.Connection().Condition().Error(); err == nil {
132 if err = e.Transport().Condition().Error(); err == nil {
133 err = amqp.Errorf(amqp.IllegalState, "unexpected disconnect on %s", h.connection)
134 }
135 }
136 }
137 h.shutdown(err)
138 }
139 }
140
141 func (h *handler) incoming(in Incoming) {
142 var err error
143 if h.connection.incoming != nil {
144 h.connection.incoming <- in
145 // Must block until accept/reject, subsequent events may use the incoming endpoint.
146 err = in.wait()
147 } else {
148 err = amqp.Errorf(amqp.NotAllowed, "rejected incoming %s %s",
149 in.pEndpoint().Type(), in.pEndpoint().String())
150 }
151 if err == nil {
152 in.pEndpoint().Open()
153 } else {
154 proton.CloseError(in.pEndpoint(), err)
155 }
156 }
157
158 func (h *handler) addLink(pl proton.Link, el Endpoint) {
159 h.links[pl] = el
160 }
161
162 func (h *handler) linkClosed(l proton.Link, err error) {
163 if link, ok := h.links[l]; ok {
164 _ = link.(endpointInternal).closed(err)
165 delete(h.links, l)
166 l.Free()
167 }
168 }
169
170 func (h *handler) sessionClosed(ps proton.Session, err error) {
171 if s, ok := h.sessions[ps]; ok {
172 delete(h.sessions, ps)
173 err = s.closed(err)
174 for l, _ := range h.links {
175 if l.Session() == ps {
176 h.linkClosed(l, err)
177 }
178 }
179 ps.Free()
180 }
181 }
182
183 func (h *handler) shutdown(err error) {
184 err = h.connection.closed(err)
185 for _, sm := range h.sent {
186 // Don't block but ensure outcome is sent eventually.
187 if sm.ack != nil {
188 o := Outcome{Unacknowledged, err, sm.v}
189 select {
190 case sm.ack <- o:
191 default:
192 go func(ack chan<- Outcome) { ack <- o }(sm.ack) // Deliver it eventually
193 }
194 }
195 }
196 h.sent = nil
197 for _, l := range h.links {
198 _ = l.(endpointInternal).closed(err)
199 }
200 h.links = nil
201 for _, s := range h.sessions {
202 _ = s.closed(err)
203 }
204 h.sessions = nil
205 }