Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/reactor/Handshaker.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 package org.apache.qpid.proton.reactor;
23
24 import org.apache.qpid.proton.engine.BaseHandler;
25 import org.apache.qpid.proton.engine.Endpoint;
26 import org.apache.qpid.proton.engine.EndpointState;
27 import org.apache.qpid.proton.engine.Event;
28 import org.apache.qpid.proton.engine.Link;
29
30 /**
31 * A handler that mirrors the actions of the remote end of a connection. This
32 * handler responds in kind when the remote end of the connection is opened and
33 * closed. Likewise if the remote end of the connection opens or closes
34 * sessions and links, this handler responds by opening or closing the local end
35 * of the session or link.
36 */
37 public class Handshaker extends BaseHandler {
38
39 private void open(Endpoint endpoint) {
40 if (endpoint.getLocalState() == EndpointState.UNINITIALIZED) {
41 endpoint.open();
42 }
43 }
44
45 private void close(Endpoint endpoint) {
46 if (endpoint.getLocalState() != EndpointState.CLOSED) {
47 endpoint.close();
48 }
49 }
50
51 @Override
52 public void onConnectionRemoteOpen(Event event) {
53 open(event.getConnection());
54 }
55
56 @Override
57 public void onSessionRemoteOpen(Event event) {
58 open(event.getSession());
59 }
60
61 @Override
62 public void onLinkRemoteOpen(Event event) {
63 Link link = event.getLink();
64 if (link.getLocalState() == EndpointState.UNINITIALIZED) {
65 if (link.getRemoteSource() != null) {
66 link.setSource(link.getRemoteSource().copy());
67 }
68 if (link.getRemoteTarget() != null) {
69 link.setTarget(link.getRemoteTarget().copy());
70 }
71 }
72 open(link);
73 }
74
75 @Override
76 public void onConnectionRemoteClose(Event event) {
77 close(event.getConnection());
78 }
79
80 @Override
81 public void onSessionRemoteClose(Event event) {
82 close(event.getSession());
83 }
84
85 @Override
86 public void onLinkRemoteClose(Event event) {
87 close(event.getLink());
88 }
89 }