| 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 |
package org.apache.qpid.proton.engine.impl; |
| 22 |
|
| 23 |
import org.apache.qpid.proton.amqp.UnsignedInteger; |
| 24 |
import org.apache.qpid.proton.codec.ReadableBuffer; |
| 25 |
import org.apache.qpid.proton.codec.WritableBuffer; |
| 26 |
import org.apache.qpid.proton.engine.Receiver; |
| 27 |
|
| 28 |
public class ReceiverImpl extends LinkImpl implements Receiver |
| 29 |
{ |
| 30 |
private boolean _drainFlagMode = true; |
| 31 |
|
| 32 |
@Override |
| 33 |
public boolean advance() |
| 34 |
{ |
| 35 |
DeliveryImpl current = current(); |
| 36 |
if(current != null) |
| 37 |
{ |
| 38 |
current.setDone(); |
| 39 |
} |
| 40 |
final boolean advance = super.advance(); |
| 41 |
if(advance) |
| 42 |
{ |
| 43 |
decrementQueued(); |
| 44 |
decrementCredit(); |
| 45 |
getSession().incrementIncomingBytes(-current.pending()); |
| 46 |
getSession().incrementIncomingDeliveries(-1); |
| 47 |
if (getSession().getTransportSession().getIncomingWindowSize().equals(UnsignedInteger.ZERO)) { |
| 48 |
modified(); |
| 49 |
} |
| 50 |
} |
| 51 |
return advance; |
| 52 |
} |
| 53 |
|
| 54 |
private TransportReceiver _transportReceiver; |
| 55 |
private int _unsentCredits; |
| 56 |
|
| 57 |
|
| 58 |
ReceiverImpl(SessionImpl session, String name) |
| 59 |
{ |
| 60 |
super(session, name); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void flow(final int credits) |
| 65 |
{ |
| 66 |
addCredit(credits); |
| 67 |
_unsentCredits += credits; |
| 68 |
modified(); |
| 69 |
if (!_drainFlagMode) |
| 70 |
{ |
| 71 |
setDrain(false); |
| 72 |
_drainFlagMode = false; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
int clearUnsentCredits() |
| 77 |
{ |
| 78 |
int credits = _unsentCredits; |
| 79 |
_unsentCredits = 0; |
| 80 |
return credits; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public int recv(final byte[] bytes, int offset, int size) |
| 85 |
{ |
| 86 |
if (_current == null) { |
| 87 |
throw new IllegalStateException("no current delivery"); |
| 88 |
} |
| 89 |
|
| 90 |
int consumed = _current.recv(bytes, offset, size); |
| 91 |
if (consumed > 0) { |
| 92 |
getSession().incrementIncomingBytes(-consumed); |
| 93 |
if (getSession().getTransportSession().getIncomingWindowSize().equals(UnsignedInteger.ZERO)) { |
| 94 |
modified(); |
| 95 |
} |
| 96 |
} |
| 97 |
return consumed; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public int recv(final WritableBuffer buffer) |
| 102 |
{ |
| 103 |
if (_current == null) { |
| 104 |
throw new IllegalStateException("no current delivery"); |
| 105 |
} |
| 106 |
|
| 107 |
int consumed = _current.recv(buffer); |
| 108 |
if (consumed > 0) { |
| 109 |
getSession().incrementIncomingBytes(-consumed); |
| 110 |
if (getSession().getTransportSession().getIncomingWindowSize().equals(UnsignedInteger.ZERO)) { |
| 111 |
modified(); |
| 112 |
} |
| 113 |
} |
| 114 |
return consumed; |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public ReadableBuffer recv() |
| 119 |
{ |
| 120 |
if (_current == null) { |
| 121 |
throw new IllegalStateException("no current delivery"); |
| 122 |
} |
| 123 |
|
| 124 |
ReadableBuffer consumed = _current.recv(); |
| 125 |
if (consumed.remaining() > 0) { |
| 126 |
getSession().incrementIncomingBytes(-consumed.remaining()); |
| 127 |
if (getSession().getTransportSession().getIncomingWindowSize().equals(UnsignedInteger.ZERO)) { |
| 128 |
modified(); |
| 129 |
} |
| 130 |
} |
| 131 |
return consumed; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
void doFree() |
| 136 |
{ |
| 137 |
getSession().freeReceiver(this); |
| 138 |
super.doFree(); |
| 139 |
} |
| 140 |
|
| 141 |
boolean hasIncoming() |
| 142 |
{ |
| 143 |
return false; //TODO - Implement |
| 144 |
} |
| 145 |
|
| 146 |
void setTransportLink(TransportReceiver transportReceiver) |
| 147 |
{ |
| 148 |
_transportReceiver = transportReceiver; |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
TransportReceiver getTransportLink() |
| 153 |
{ |
| 154 |
return _transportReceiver; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void drain(int credit) |
| 159 |
{ |
| 160 |
setDrain(true); |
| 161 |
flow(credit); |
| 162 |
_drainFlagMode = false; |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public boolean draining() |
| 167 |
{ |
| 168 |
return getDrain() && (getCredit() > getQueued()); |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void setDrain(boolean drain) |
| 173 |
{ |
| 174 |
super.setDrain(drain); |
| 175 |
modified(); |
| 176 |
_drainFlagMode = true; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public int getRemoteCredit() |
| 181 |
{ |
| 182 |
// Credit is only decremented once advance is called on a received message, |
| 183 |
// so we also need to consider the queued count. |
| 184 |
return getCredit() - getQueued(); |
| 185 |
} |
| 186 |
} |