Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/reactor/FlowController.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.Event;
26 import org.apache.qpid.proton.engine.Link;
27 import org.apache.qpid.proton.engine.Receiver;
28
29 /**
30 * A handler that applies flow control to a connection. This handler tops-up
31 * link credit each time credit is expended by the receipt of messages.
32 */
33 public class FlowController extends BaseHandler {
34
35 private int drained;
36 private int window;
37
38 public FlowController(int window) {
39 // XXX: a window of 1 doesn't work because we won't necessarily get
40 // notified when the one allowed delivery is settled
41 if (window <= 1) throw new IllegalArgumentException();
42 this.window = window;
43 this.drained = 0;
44 }
45
46 public FlowController() {
47 this(1024);
48 }
49
50 private void topup(Receiver link, int window) {
51 int delta = window - link.getCredit();
52 link.flow(delta);
53 }
54
55 @Override
56 public void onUnhandled(Event event) {
57 int window = this.window;
58 Link link = event.getLink();
59
60 switch(event.getType()) {
61 case LINK_LOCAL_OPEN:
62 case LINK_REMOTE_OPEN:
63 case LINK_FLOW:
64 case DELIVERY:
65 if (link instanceof Receiver) {
66 this.drained += link.drained();
67 if (this.drained == 0) {
68 topup((Receiver)link, window);
69 }
70 }
71 break;
72 default:
73 break;
74 }
75 }
76 }