| 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.codec.ReadableBuffer; |
| 24 |
import org.apache.qpid.proton.engine.EndpointState; |
| 25 |
import org.apache.qpid.proton.engine.Sender; |
| 26 |
|
| 27 |
public class SenderImpl extends LinkImpl implements Sender |
| 28 |
{ |
| 29 |
private int _offered; |
| 30 |
private TransportSender _transportLink; |
| 31 |
|
| 32 |
SenderImpl(SessionImpl session, String name) |
| 33 |
{ |
| 34 |
super(session, name); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public void offer(final int credits) |
| 39 |
{ |
| 40 |
_offered = credits; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public int send(final byte[] bytes, int offset, int length) |
| 45 |
{ |
| 46 |
if (getLocalState() == EndpointState.CLOSED) |
| 47 |
{ |
| 48 |
throw new IllegalStateException("send not allowed after the sender is closed."); |
| 49 |
} |
| 50 |
DeliveryImpl current = current(); |
| 51 |
if (current == null || current.getLink() != this) |
| 52 |
{ |
| 53 |
throw new IllegalArgumentException();//TODO. |
| 54 |
} |
| 55 |
int sent = current.send(bytes, offset, length); |
| 56 |
if (sent > 0) { |
| 57 |
getSession().incrementOutgoingBytes(sent); |
| 58 |
} |
| 59 |
return sent; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public int send(final ReadableBuffer buffer) |
| 64 |
{ |
| 65 |
if (getLocalState() == EndpointState.CLOSED) |
| 66 |
{ |
| 67 |
throw new IllegalStateException("send not allowed after the sender is closed."); |
| 68 |
} |
| 69 |
DeliveryImpl current = current(); |
| 70 |
if (current == null || current.getLink() != this) |
| 71 |
{ |
| 72 |
throw new IllegalArgumentException(); |
| 73 |
} |
| 74 |
int sent = current.send(buffer); |
| 75 |
if (sent > 0) { |
| 76 |
getSession().incrementOutgoingBytes(sent); |
| 77 |
} |
| 78 |
return sent; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public int sendNoCopy(final ReadableBuffer buffer) |
| 83 |
{ |
| 84 |
if (getLocalState() == EndpointState.CLOSED) |
| 85 |
{ |
| 86 |
throw new IllegalStateException("send not allowed after the sender is closed."); |
| 87 |
} |
| 88 |
DeliveryImpl current = current(); |
| 89 |
if (current == null || current.getLink() != this) |
| 90 |
{ |
| 91 |
throw new IllegalArgumentException(); |
| 92 |
} |
| 93 |
int sent = current.sendNoCopy(buffer); |
| 94 |
if (sent > 0) { |
| 95 |
getSession().incrementOutgoingBytes(sent); |
| 96 |
} |
| 97 |
return sent; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void abort() |
| 102 |
{ |
| 103 |
//TODO. |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
void doFree() |
| 108 |
{ |
| 109 |
getSession().freeSender(this); |
| 110 |
super.doFree(); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public boolean advance() |
| 115 |
{ |
| 116 |
DeliveryImpl delivery = current(); |
| 117 |
if (delivery != null) { |
| 118 |
delivery.setComplete(); |
| 119 |
} |
| 120 |
|
| 121 |
boolean advance = super.advance(); |
| 122 |
if(advance && _offered > 0) |
| 123 |
{ |
| 124 |
_offered--; |
| 125 |
} |
| 126 |
if(advance) |
| 127 |
{ |
| 128 |
decrementCredit(); |
| 129 |
delivery.addToTransportWorkList(); |
| 130 |
getSession().incrementOutgoingDeliveries(1); |
| 131 |
} |
| 132 |
|
| 133 |
return advance; |
| 134 |
} |
| 135 |
|
| 136 |
boolean hasOfferedCredits() |
| 137 |
{ |
| 138 |
return _offered > 0; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
TransportSender getTransportLink() |
| 143 |
{ |
| 144 |
return _transportLink; |
| 145 |
} |
| 146 |
|
| 147 |
void setTransportLink(TransportSender transportLink) |
| 148 |
{ |
| 149 |
_transportLink = transportLink; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
@Override |
| 154 |
public void setCredit(int credit) |
| 155 |
{ |
| 156 |
super.setCredit(credit); |
| 157 |
/* while(getQueued()>0 && getCredit()>0) |
| 158 |
{ |
| 159 |
advance(); |
| 160 |
}*/ |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public int getRemoteCredit() |
| 165 |
{ |
| 166 |
// Credit is decremented as soon as advance is called on a send, |
| 167 |
// so we need only consider the credit count, not the queued count. |
| 168 |
return getCredit(); |
| 169 |
} |
| 170 |
} |