| 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 java.util.ArrayList; |
| 24 |
import java.util.EnumSet; |
| 25 |
import java.util.LinkedHashMap; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
|
| 29 |
import org.apache.qpid.proton.amqp.Symbol; |
| 30 |
import org.apache.qpid.proton.engine.EndpointState; |
| 31 |
import org.apache.qpid.proton.engine.Event; |
| 32 |
import org.apache.qpid.proton.engine.ProtonJSession; |
| 33 |
import org.apache.qpid.proton.engine.Session; |
| 34 |
|
| 35 |
public class SessionImpl extends EndpointImpl implements ProtonJSession |
| 36 |
{ |
| 37 |
private final ConnectionImpl _connection; |
| 38 |
|
| 39 |
private Map<String, SenderImpl> _senders = new LinkedHashMap<String, SenderImpl>(); |
| 40 |
private Map<String, ReceiverImpl> _receivers = new LinkedHashMap<String, ReceiverImpl>(); |
| 41 |
private List<LinkImpl> _oldLinksToFree = new ArrayList<LinkImpl>(); |
| 42 |
private TransportSession _transportSession; |
| 43 |
private int _incomingCapacity = 0; |
| 44 |
private int _incomingBytes = 0; |
| 45 |
private int _outgoingBytes = 0; |
| 46 |
private int _incomingDeliveries = 0; |
| 47 |
private int _outgoingDeliveries = 0; |
| 48 |
private long _outgoingWindow = Integer.MAX_VALUE; |
| 49 |
private Map<Symbol, Object> _properties; |
| 50 |
private Map<Symbol, Object> _remoteProperties; |
| 51 |
private Symbol[] _offeredCapabilities; |
| 52 |
private Symbol[] _remoteOfferedCapabilities; |
| 53 |
private Symbol[] _desiredCapabilities; |
| 54 |
private Symbol[] _remoteDesiredCapabilities; |
| 55 |
|
| 56 |
private LinkNode<SessionImpl> _node; |
| 57 |
|
| 58 |
|
| 59 |
SessionImpl(ConnectionImpl connection) |
| 60 |
{ |
| 61 |
_connection = connection; |
| 62 |
_connection.incref(); |
| 63 |
_node = _connection.addSessionEndpoint(this); |
| 64 |
_connection.put(Event.Type.SESSION_INIT, this); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public SenderImpl sender(String name) |
| 69 |
{ |
| 70 |
SenderImpl sender = _senders.get(name); |
| 71 |
if(sender == null) |
| 72 |
{ |
| 73 |
sender = new SenderImpl(this, name); |
| 74 |
_senders.put(name, sender); |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
if(sender.getLocalState() == EndpointState.CLOSED |
| 79 |
&& sender.getRemoteState() == EndpointState.CLOSED) |
| 80 |
{ |
| 81 |
_oldLinksToFree.add(sender); |
| 82 |
|
| 83 |
sender = new SenderImpl(this, name); |
| 84 |
_senders.put(name, sender); |
| 85 |
} |
| 86 |
} |
| 87 |
return sender; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public ReceiverImpl receiver(String name) |
| 92 |
{ |
| 93 |
ReceiverImpl receiver = _receivers.get(name); |
| 94 |
if(receiver == null) |
| 95 |
{ |
| 96 |
receiver = new ReceiverImpl(this, name); |
| 97 |
_receivers.put(name, receiver); |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
if(receiver.getLocalState() == EndpointState.CLOSED |
| 102 |
&& receiver.getRemoteState() == EndpointState.CLOSED) |
| 103 |
{ |
| 104 |
_oldLinksToFree.add(receiver); |
| 105 |
|
| 106 |
receiver = new ReceiverImpl(this, name); |
| 107 |
_receivers.put(name, receiver); |
| 108 |
} |
| 109 |
} |
| 110 |
return receiver; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public Session next(EnumSet<EndpointState> local, EnumSet<EndpointState> remote) |
| 115 |
{ |
| 116 |
LinkNode.Query<SessionImpl> query = new EndpointImplQuery<SessionImpl>(local, remote); |
| 117 |
|
| 118 |
LinkNode<SessionImpl> sessionNode = _node.next(query); |
| 119 |
|
| 120 |
return sessionNode == null ? null : sessionNode.getValue(); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
protected ConnectionImpl getConnectionImpl() |
| 125 |
{ |
| 126 |
return _connection; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public ConnectionImpl getConnection() |
| 131 |
{ |
| 132 |
return getConnectionImpl(); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
void postFinal() { |
| 137 |
_connection.put(Event.Type.SESSION_FINAL, this); |
| 138 |
_connection.decref(); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
void doFree() { |
| 143 |
_connection.freeSession(this); |
| 144 |
_connection.removeSessionEndpoint(_node); |
| 145 |
_node = null; |
| 146 |
|
| 147 |
List<SenderImpl> senders = new ArrayList<SenderImpl>(_senders.values()); |
| 148 |
for(SenderImpl sender : senders) { |
| 149 |
sender.free(); |
| 150 |
} |
| 151 |
_senders.clear(); |
| 152 |
|
| 153 |
List<ReceiverImpl> receivers = new ArrayList<ReceiverImpl>(_receivers.values()); |
| 154 |
for(ReceiverImpl receiver : receivers) { |
| 155 |
receiver.free(); |
| 156 |
} |
| 157 |
_receivers.clear(); |
| 158 |
|
| 159 |
List<LinkImpl> links = new ArrayList<LinkImpl>(_oldLinksToFree); |
| 160 |
for(LinkImpl link : links) { |
| 161 |
link.free(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
void modifyEndpoints() { |
| 166 |
for (SenderImpl snd : _senders.values()) { |
| 167 |
snd.modifyEndpoints(); |
| 168 |
} |
| 169 |
|
| 170 |
for (ReceiverImpl rcv : _receivers.values()) { |
| 171 |
rcv.modifyEndpoints(); |
| 172 |
} |
| 173 |
modified(); |
| 174 |
} |
| 175 |
|
| 176 |
TransportSession getTransportSession() |
| 177 |
{ |
| 178 |
return _transportSession; |
| 179 |
} |
| 180 |
|
| 181 |
void setTransportSession(TransportSession transportSession) |
| 182 |
{ |
| 183 |
_transportSession = transportSession; |
| 184 |
} |
| 185 |
|
| 186 |
void setNode(LinkNode<SessionImpl> node) |
| 187 |
{ |
| 188 |
_node = node; |
| 189 |
} |
| 190 |
|
| 191 |
void freeSender(SenderImpl sender) |
| 192 |
{ |
| 193 |
String name = sender.getName(); |
| 194 |
SenderImpl existing = _senders.get(name); |
| 195 |
if (sender.equals(existing)) |
| 196 |
{ |
| 197 |
_senders.remove(name); |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
_oldLinksToFree.remove(sender); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
void freeReceiver(ReceiverImpl receiver) |
| 206 |
{ |
| 207 |
String name = receiver.getName(); |
| 208 |
ReceiverImpl existing = _receivers.get(name); |
| 209 |
if (receiver.equals(existing)) |
| 210 |
{ |
| 211 |
_receivers.remove(name); |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
_oldLinksToFree.remove(receiver); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public int getIncomingCapacity() |
| 221 |
{ |
| 222 |
return _incomingCapacity; |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public void setIncomingCapacity(int capacity) |
| 227 |
{ |
| 228 |
_incomingCapacity = capacity; |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public int getIncomingBytes() |
| 233 |
{ |
| 234 |
return _incomingBytes; |
| 235 |
} |
| 236 |
|
| 237 |
void incrementIncomingBytes(int delta) |
| 238 |
{ |
| 239 |
_incomingBytes += delta; |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public int getOutgoingBytes() |
| 244 |
{ |
| 245 |
return _outgoingBytes; |
| 246 |
} |
| 247 |
|
| 248 |
void incrementOutgoingBytes(int delta) |
| 249 |
{ |
| 250 |
_outgoingBytes += delta; |
| 251 |
} |
| 252 |
|
| 253 |
void incrementIncomingDeliveries(int delta) |
| 254 |
{ |
| 255 |
_incomingDeliveries += delta; |
| 256 |
} |
| 257 |
|
| 258 |
int getOutgoingDeliveries() |
| 259 |
{ |
| 260 |
return _outgoingDeliveries; |
| 261 |
} |
| 262 |
|
| 263 |
void incrementOutgoingDeliveries(int delta) |
| 264 |
{ |
| 265 |
_outgoingDeliveries += delta; |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
void localOpen() |
| 270 |
{ |
| 271 |
getConnectionImpl().put(Event.Type.SESSION_LOCAL_OPEN, this); |
| 272 |
} |
| 273 |
|
| 274 |
@Override |
| 275 |
void localClose() |
| 276 |
{ |
| 277 |
getConnectionImpl().put(Event.Type.SESSION_LOCAL_CLOSE, this); |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public void setOutgoingWindow(long outgoingWindow) { |
| 282 |
if(outgoingWindow < 0 || outgoingWindow > 0xFFFFFFFFL) |
| 283 |
{ |
| 284 |
throw new IllegalArgumentException("Value '" + outgoingWindow + "' must be in the" |
| 285 |
+ " range [0 - 2^32-1]"); |
| 286 |
} |
| 287 |
|
| 288 |
_outgoingWindow = outgoingWindow; |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public long getOutgoingWindow() |
| 293 |
{ |
| 294 |
return _outgoingWindow; |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public Map<Symbol, Object> getProperties() |
| 299 |
{ |
| 300 |
return _properties; |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public void setProperties(Map<Symbol, Object> properties) |
| 305 |
{ |
| 306 |
_properties = properties; |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public Map<Symbol, Object> getRemoteProperties() |
| 311 |
{ |
| 312 |
return _remoteProperties; |
| 313 |
} |
| 314 |
|
| 315 |
void setRemoteProperties(Map<Symbol, Object> remoteProperties) |
| 316 |
{ |
| 317 |
_remoteProperties = remoteProperties; |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
public Symbol[] getDesiredCapabilities() |
| 322 |
{ |
| 323 |
return _desiredCapabilities; |
| 324 |
} |
| 325 |
|
| 326 |
@Override |
| 327 |
public void setDesiredCapabilities(Symbol[] desiredCapabilities) |
| 328 |
{ |
| 329 |
_desiredCapabilities = desiredCapabilities; |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public Symbol[] getRemoteDesiredCapabilities() |
| 334 |
{ |
| 335 |
return _remoteDesiredCapabilities; |
| 336 |
} |
| 337 |
|
| 338 |
void setRemoteDesiredCapabilities(Symbol[] remoteDesiredCapabilities) |
| 339 |
{ |
| 340 |
_remoteDesiredCapabilities = remoteDesiredCapabilities; |
| 341 |
} |
| 342 |
|
| 343 |
@Override |
| 344 |
public Symbol[] getOfferedCapabilities() |
| 345 |
{ |
| 346 |
return _offeredCapabilities; |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public void setOfferedCapabilities(Symbol[] offeredCapabilities) |
| 351 |
{ |
| 352 |
_offeredCapabilities = offeredCapabilities; |
| 353 |
} |
| 354 |
|
| 355 |
@Override |
| 356 |
public Symbol[] getRemoteOfferedCapabilities() |
| 357 |
{ |
| 358 |
return _remoteOfferedCapabilities; |
| 359 |
} |
| 360 |
|
| 361 |
void setRemoteOfferedCapabilities(Symbol[] remoteOfferedCapabilities) |
| 362 |
{ |
| 363 |
_remoteOfferedCapabilities = remoteOfferedCapabilities; |
| 364 |
} |
| 365 |
} |