| 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 proton |
| 21 |
|
| 22 |
import "fmt" |
| 23 |
|
| 24 |
// EventHandler handles core proton events. |
| 25 |
type EventHandler interface { |
| 26 |
// HandleEvent is called with an event. |
| 27 |
// Typically HandleEvent() is implemented as a switch on e.Type() |
| 28 |
HandleEvent(e Event) |
| 29 |
} |
| 30 |
|
| 31 |
// MessagingHandler provides an alternative interface to EventHandler. |
| 32 |
// it is easier to use for most applications that send and receive messages. |
| 33 |
// |
| 34 |
// Implement this interface and then wrap your value with a MessagingHandlerDelegator. |
| 35 |
// MessagingHandlerDelegator implements EventHandler and can be registered with a Engine. |
| 36 |
// |
| 37 |
type MessagingHandler interface { |
| 38 |
// HandleMessagingEvent is called with MessagingEvent. |
| 39 |
// Typically HandleEvent() is implemented as a switch on e.Type() |
| 40 |
HandleMessagingEvent(MessagingEvent, Event) |
| 41 |
} |
| 42 |
|
| 43 |
// MessagingEvent provides a set of events that are easier to work with than the |
| 44 |
// core events defined by EventType |
| 45 |
// |
| 46 |
// There are 3 types of "endpoint": Connection, Session and Link. For each |
| 47 |
// endpoint there are 5 events: Opening, Opened, Closing, Closed and Error. |
| 48 |
// |
| 49 |
// The meaning of these events is as follows: |
| 50 |
// |
| 51 |
// Opening: The remote end opened, the local end will open automatically. |
| 52 |
// |
| 53 |
// Opened: Both ends are open, regardless of which end opened first. |
| 54 |
// |
| 55 |
// Closing: The remote end closed without error, the local end will close automatically. |
| 56 |
// |
| 57 |
// Error: The remote end closed with an error, the local end will close automatically. |
| 58 |
// |
| 59 |
// Closed: Both ends are closed, regardless of which end closed first or if there was an error. |
| 60 |
// No further events will be received for the endpoint. |
| 61 |
// |
| 62 |
type MessagingEvent int |
| 63 |
|
| 64 |
const ( |
| 65 |
// The event loop starts. |
| 66 |
MStart MessagingEvent = iota |
| 67 |
// The peer closes the connection with an error condition. |
| 68 |
MConnectionError |
| 69 |
// The peer closes the session with an error condition. |
| 70 |
MSessionError |
| 71 |
// The peer closes the link with an error condition. |
| 72 |
MLinkError |
| 73 |
// The peer Initiates the opening of the connection. |
| 74 |
MConnectionOpening |
| 75 |
// The peer initiates the opening of the session. |
| 76 |
MSessionOpening |
| 77 |
// The peer initiates the opening of the link. |
| 78 |
MLinkOpening |
| 79 |
// The connection is opened. |
| 80 |
MConnectionOpened |
| 81 |
// The session is opened. |
| 82 |
MSessionOpened |
| 83 |
// The link is opened. |
| 84 |
MLinkOpened |
| 85 |
// The peer initiates the closing of the connection. |
| 86 |
MConnectionClosing |
| 87 |
// The peer initiates the closing of the session. |
| 88 |
MSessionClosing |
| 89 |
// The peer initiates the closing of the link. |
| 90 |
MLinkClosing |
| 91 |
// Both ends of the connection are closed. |
| 92 |
MConnectionClosed |
| 93 |
// Both ends of the session are closed. |
| 94 |
MSessionClosed |
| 95 |
// Both ends of the link are closed. |
| 96 |
MLinkClosed |
| 97 |
// The sender link has credit and messages can |
| 98 |
// therefore be transferred. |
| 99 |
MSendable |
| 100 |
// The remote peer accepts an outgoing message. |
| 101 |
MAccepted |
| 102 |
// The remote peer rejects an outgoing message. |
| 103 |
MRejected |
| 104 |
// The peer releases an outgoing message. Note that this may be in response to |
| 105 |
// either the RELEASE or MODIFIED state as defined by the AMQP specification. |
| 106 |
MReleased |
| 107 |
// The peer has settled the outgoing message. This is the point at which it |
| 108 |
// should never be re-transmitted. |
| 109 |
MSettled |
| 110 |
// A message is received. Call Event.Delivery().Message() to decode as an amqp.Message. |
| 111 |
// To manage the outcome of this messages (e.g. to accept or reject the message) |
| 112 |
// use Event.Delivery(). |
| 113 |
MMessage |
| 114 |
// A network connection was disconnected. |
| 115 |
MDisconnected |
| 116 |
) |
| 117 |
|
| 118 |
func (t MessagingEvent) String() string { |
| 119 |
switch t { |
| 120 |
case MStart: |
| 121 |
return "Start" |
| 122 |
case MConnectionError: |
| 123 |
return "ConnectionError" |
| 124 |
case MSessionError: |
| 125 |
return "SessionError" |
| 126 |
case MLinkError: |
| 127 |
return "LinkError" |
| 128 |
case MConnectionOpening: |
| 129 |
return "ConnectionOpening" |
| 130 |
case MSessionOpening: |
| 131 |
return "SessionOpening" |
| 132 |
case MLinkOpening: |
| 133 |
return "LinkOpening" |
| 134 |
case MConnectionOpened: |
| 135 |
return "ConnectionOpened" |
| 136 |
case MSessionOpened: |
| 137 |
return "SessionOpened" |
| 138 |
case MLinkOpened: |
| 139 |
return "LinkOpened" |
| 140 |
case MConnectionClosing: |
| 141 |
return "ConnectionClosing" |
| 142 |
case MSessionClosing: |
| 143 |
return "SessionClosing" |
| 144 |
case MLinkClosing: |
| 145 |
return "LinkClosing" |
| 146 |
case MConnectionClosed: |
| 147 |
return "ConnectionClosed" |
| 148 |
case MSessionClosed: |
| 149 |
return "SessionClosed" |
| 150 |
case MLinkClosed: |
| 151 |
return "LinkClosed" |
| 152 |
case MDisconnected: |
| 153 |
return "Disconnected" |
| 154 |
case MSendable: |
| 155 |
return "Sendable" |
| 156 |
case MAccepted: |
| 157 |
return "Accepted" |
| 158 |
case MRejected: |
| 159 |
return "Rejected" |
| 160 |
case MReleased: |
| 161 |
return "Released" |
| 162 |
case MSettled: |
| 163 |
return "Settled" |
| 164 |
case MMessage: |
| 165 |
return "Message" |
| 166 |
default: |
| 167 |
return "Unknown" |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// ResourceHandler provides a simple way to track the creation and deletion of |
| 172 |
// various proton objects. |
| 173 |
// endpointDelegator captures common patterns for endpoints opening/closing |
| 174 |
type endpointDelegator struct { |
| 175 |
remoteOpen, remoteClose, localOpen, localClose EventType |
| 176 |
opening, opened, closing, closed, error MessagingEvent |
| 177 |
endpoint func(Event) Endpoint |
| 178 |
delegator *MessagingAdapter |
| 179 |
} |
| 180 |
|
| 181 |
// HandleEvent handles an open/close event for an endpoint in a generic way. |
| 182 |
func (d endpointDelegator) HandleEvent(e Event) { |
| 183 |
endpoint := d.endpoint(e) |
| 184 |
state := endpoint.State() |
| 185 |
|
| 186 |
switch e.Type() { |
| 187 |
|
| 188 |
case d.localOpen: |
| 189 |
if state.RemoteActive() { |
| 190 |
d.delegator.mhandler.HandleMessagingEvent(d.opened, e) |
| 191 |
} |
| 192 |
|
| 193 |
case d.remoteOpen: |
| 194 |
d.delegator.mhandler.HandleMessagingEvent(d.opening, e) |
| 195 |
switch { |
| 196 |
case state.LocalActive(): |
| 197 |
d.delegator.mhandler.HandleMessagingEvent(d.opened, e) |
| 198 |
case state.LocalUninit(): |
| 199 |
if d.delegator.AutoOpen { |
| 200 |
endpoint.Open() |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
case d.remoteClose: |
| 205 |
if endpoint.RemoteCondition().IsSet() { // Closed with error |
| 206 |
d.delegator.mhandler.HandleMessagingEvent(d.error, e) |
| 207 |
} else { |
| 208 |
d.delegator.mhandler.HandleMessagingEvent(d.closing, e) |
| 209 |
} |
| 210 |
if state.LocalClosed() { |
| 211 |
d.delegator.mhandler.HandleMessagingEvent(d.closed, e) |
| 212 |
} else if state.LocalActive() { |
| 213 |
endpoint.Close() |
| 214 |
} |
| 215 |
|
| 216 |
case d.localClose: |
| 217 |
if state.RemoteClosed() { |
| 218 |
d.delegator.mhandler.HandleMessagingEvent(d.closed, e) |
| 219 |
} |
| 220 |
|
| 221 |
default: |
| 222 |
// We shouldn't be called with any other event type. |
| 223 |
panic(fmt.Errorf("internal error, not an open/close event: %s", e)) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
type flowcontroller struct { |
| 228 |
window, drained int |
| 229 |
} |
| 230 |
|
| 231 |
func (d flowcontroller) HandleEvent(e Event) { |
| 232 |
link := e.Link() |
| 233 |
|
| 234 |
switch e.Type() { |
| 235 |
case ELinkLocalOpen, ELinkRemoteOpen, ELinkFlow, EDelivery: |
| 236 |
if link.IsReceiver() { |
| 237 |
d.drained += link.Drained() |
| 238 |
if d.drained != 0 { |
| 239 |
link.Flow(d.window - link.Credit()) |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// MessagingAdapter implements a EventHandler and delegates to a MessagingHandler. |
| 246 |
// You can modify the exported fields before you pass the MessagingAdapter to |
| 247 |
// a Engine. |
| 248 |
type MessagingAdapter struct { |
| 249 |
mhandler MessagingHandler |
| 250 |
connection, session, link endpointDelegator |
| 251 |
flowcontroller EventHandler |
| 252 |
|
| 253 |
// AutoSettle (default true) automatically pre-settle outgoing messages. |
| 254 |
AutoSettle bool |
| 255 |
// AutoAccept (default true) automatically accept and settle incoming messages |
| 256 |
// if they are not settled by the delegate. |
| 257 |
AutoAccept bool |
| 258 |
// AutoOpen (default true) automatically open remotely opened endpoints. |
| 259 |
AutoOpen bool |
| 260 |
// Prefetch (default 10) initial credit to issue for incoming links. |
| 261 |
Prefetch int |
| 262 |
// PeerCloseIsError (default false) if true a close by the peer will be treated as an error. |
| 263 |
PeerCloseError bool |
| 264 |
} |
| 265 |
|
| 266 |
func NewMessagingAdapter(h MessagingHandler) *MessagingAdapter { |
| 267 |
return &MessagingAdapter{ |
| 268 |
mhandler: h, |
| 269 |
flowcontroller: nil, |
| 270 |
AutoSettle: true, |
| 271 |
AutoAccept: true, |
| 272 |
AutoOpen: true, |
| 273 |
Prefetch: 10, |
| 274 |
PeerCloseError: false, |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
func handleIf(h EventHandler, e Event) { |
| 279 |
if h != nil { |
| 280 |
h.HandleEvent(e) |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// Handle a proton event by passing the corresponding MessagingEvent(s) to |
| 285 |
// the MessagingHandler. |
| 286 |
func (d *MessagingAdapter) HandleEvent(e Event) { |
| 287 |
handleIf(d.flowcontroller, e) |
| 288 |
|
| 289 |
switch e.Type() { |
| 290 |
|
| 291 |
case EConnectionInit: |
| 292 |
d.connection = endpointDelegator{ |
| 293 |
EConnectionRemoteOpen, EConnectionRemoteClose, EConnectionLocalOpen, EConnectionLocalClose, |
| 294 |
MConnectionOpening, MConnectionOpened, MConnectionClosing, MConnectionClosed, |
| 295 |
MConnectionError, |
| 296 |
func(e Event) Endpoint { return e.Connection() }, |
| 297 |
d, |
| 298 |
} |
| 299 |
d.session = endpointDelegator{ |
| 300 |
ESessionRemoteOpen, ESessionRemoteClose, ESessionLocalOpen, ESessionLocalClose, |
| 301 |
MSessionOpening, MSessionOpened, MSessionClosing, MSessionClosed, |
| 302 |
MSessionError, |
| 303 |
func(e Event) Endpoint { return e.Session() }, |
| 304 |
d, |
| 305 |
} |
| 306 |
d.link = endpointDelegator{ |
| 307 |
ELinkRemoteOpen, ELinkRemoteClose, ELinkLocalOpen, ELinkLocalClose, |
| 308 |
MLinkOpening, MLinkOpened, MLinkClosing, MLinkClosed, |
| 309 |
MLinkError, |
| 310 |
func(e Event) Endpoint { return e.Link() }, |
| 311 |
d, |
| 312 |
} |
| 313 |
if d.Prefetch > 0 { |
| 314 |
d.flowcontroller = flowcontroller{window: d.Prefetch, drained: 0} |
| 315 |
} |
| 316 |
d.mhandler.HandleMessagingEvent(MStart, e) |
| 317 |
|
| 318 |
case EConnectionRemoteOpen: |
| 319 |
|
| 320 |
d.connection.HandleEvent(e) |
| 321 |
|
| 322 |
case EConnectionRemoteClose: |
| 323 |
d.connection.HandleEvent(e) |
| 324 |
e.Connection().Transport().CloseTail() |
| 325 |
|
| 326 |
case EConnectionLocalOpen, EConnectionLocalClose: |
| 327 |
d.connection.HandleEvent(e) |
| 328 |
|
| 329 |
case ESessionRemoteOpen, ESessionRemoteClose, ESessionLocalOpen, ESessionLocalClose: |
| 330 |
d.session.HandleEvent(e) |
| 331 |
|
| 332 |
case ELinkRemoteOpen: |
| 333 |
e.Link().Source().Copy(e.Link().RemoteSource()) |
| 334 |
e.Link().Target().Copy(e.Link().RemoteTarget()) |
| 335 |
d.link.HandleEvent(e) |
| 336 |
|
| 337 |
case ELinkRemoteClose, ELinkLocalOpen, ELinkLocalClose: |
| 338 |
d.link.HandleEvent(e) |
| 339 |
|
| 340 |
case ELinkFlow: |
| 341 |
if e.Link().IsSender() && e.Link().Credit() > 0 { |
| 342 |
d.mhandler.HandleMessagingEvent(MSendable, e) |
| 343 |
} |
| 344 |
|
| 345 |
case EDelivery: |
| 346 |
if e.Delivery().Link().IsReceiver() { |
| 347 |
d.incoming(e) |
| 348 |
} else { |
| 349 |
d.outgoing(e) |
| 350 |
} |
| 351 |
|
| 352 |
case ETransportTailClosed: |
| 353 |
if !e.Connection().State().RemoteClosed() { // Unexpected transport closed |
| 354 |
e.Transport().CloseHead() // Complete transport close, no connection close expected |
| 355 |
} |
| 356 |
|
| 357 |
case ETransportClosed: |
| 358 |
d.mhandler.HandleMessagingEvent(MDisconnected, e) |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
func (d *MessagingAdapter) incoming(e Event) { |
| 363 |
delivery := e.Delivery() |
| 364 |
if delivery.HasMessage() { |
| 365 |
d.mhandler.HandleMessagingEvent(MMessage, e) |
| 366 |
if d.AutoAccept && !delivery.Settled() { |
| 367 |
delivery.Accept() |
| 368 |
} |
| 369 |
if delivery.Current() { |
| 370 |
e.Link().Advance() |
| 371 |
} |
| 372 |
} else if delivery.Updated() && delivery.Settled() { |
| 373 |
d.mhandler.HandleMessagingEvent(MSettled, e) |
| 374 |
} |
| 375 |
return |
| 376 |
} |
| 377 |
|
| 378 |
func (d *MessagingAdapter) outgoing(e Event) { |
| 379 |
delivery := e.Delivery() |
| 380 |
if delivery.Updated() { |
| 381 |
switch delivery.Remote().Type() { |
| 382 |
case Accepted: |
| 383 |
d.mhandler.HandleMessagingEvent(MAccepted, e) |
| 384 |
case Rejected: |
| 385 |
d.mhandler.HandleMessagingEvent(MRejected, e) |
| 386 |
case Released, Modified: |
| 387 |
d.mhandler.HandleMessagingEvent(MReleased, e) |
| 388 |
} |
| 389 |
if delivery.Settled() { |
| 390 |
// The delivery was settled remotely, inform the local end. |
| 391 |
d.mhandler.HandleMessagingEvent(MSettled, e) |
| 392 |
if d.AutoSettle { |
| 393 |
delivery.Settle() |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
return |
| 398 |
} |