use of org.apache.qpid.server.protocol.v1_0.type.DeliveryState in project qpid-broker-j by apache.
the class StandardReceivingLinkEndpoint method receiveDelivery.
@Override
protected Error receiveDelivery(Delivery delivery) {
ReceiverSettleMode transferReceiverSettleMode = delivery.getReceiverSettleMode();
if (delivery.getResume()) {
DeliveryState deliveryState = _unsettled.get(delivery.getDeliveryTag());
if (deliveryState instanceof Outcome) {
boolean settled = shouldReceiverSettleFirst(transferReceiverSettleMode);
updateDisposition(delivery.getDeliveryTag(), deliveryState, settled);
return null;
} else {
// TODO: QPID-7845: create message ?
}
} else {
ServerMessage<?> serverMessage;
UnsignedInteger messageFormat = delivery.getMessageFormat();
DeliveryState xfrState = delivery.getState();
MessageFormat format = MessageFormatRegistry.getFormat(messageFormat.intValue());
if (format != null) {
if (delivery.getTotalPayloadSize() == 0) {
return new Error(AmqpError.NOT_IMPLEMENTED, "Delivery without payload is not supported");
}
try (QpidByteBuffer payload = delivery.getPayload()) {
serverMessage = format.createMessage(payload, getAddressSpace().getMessageStore(), getSession().getConnection().getReference());
} catch (AmqpErrorRuntimeException e) {
return e.getCause().getError();
}
} else {
final Error err = new Error();
err.setCondition(AmqpError.NOT_IMPLEMENTED);
err.setDescription("Unknown message format: " + messageFormat);
return err;
}
MessageReference<?> reference = serverMessage.newReference();
try {
Binary transactionId = null;
if (xfrState != null) {
if (xfrState instanceof TransactionalState) {
transactionId = ((TransactionalState) xfrState).getTxnId();
}
}
final ServerTransaction transaction;
boolean setRollbackOnly = true;
if (transactionId != null) {
try {
transaction = getSession().getTransaction(transactionId);
} catch (UnknownTransactionException e) {
return new Error(TransactionError.UNKNOWN_ID, String.format("transaction-id '%s' is unknown.", transactionId));
}
if (!(transaction instanceof AutoCommitTransaction)) {
transaction.addPostTransactionAction(new ServerTransaction.Action() {
@Override
public void postCommit() {
updateDisposition(delivery.getDeliveryTag(), null, true);
}
@Override
public void onRollback() {
updateDisposition(delivery.getDeliveryTag(), null, true);
}
});
}
} else {
transaction = new AsyncAutoCommitTransaction(getAddressSpace().getMessageStore(), this);
}
try {
Session_1_0 session = getSession();
session.getAMQPConnection().checkAuthorizedMessagePrincipal(serverMessage.getMessageHeader().getUserId());
Outcome outcome;
if (serverMessage.isPersistent() && !getAddressSpace().getMessageStore().isPersistent()) {
final Error preconditionFailedError = new Error(AmqpError.PRECONDITION_FAILED, "Non-durable message store cannot accept durable message.");
if (_rejectedOutcomeSupportedBySource) {
final Rejected rejected = new Rejected();
rejected.setError(preconditionFailedError);
outcome = rejected;
} else {
// TODO - disposition not updated for the non-transaction case
return preconditionFailedError;
}
} else {
try {
getReceivingDestination().send(serverMessage, transaction, session.getSecurityToken());
outcome = ACCEPTED;
} catch (UnroutableMessageException e) {
final Error error = new Error();
error.setCondition(e.getErrorCondition());
error.setDescription(e.getMessage());
String targetAddress = getTarget().getAddress();
if (targetAddress == null || "".equals(targetAddress.trim())) {
error.setInfo(Collections.singletonMap(DELIVERY_TAG, delivery.getDeliveryTag()));
}
if (!_rejectedOutcomeSupportedBySource || (delivery.isSettled() && !(transaction instanceof LocalTransaction))) {
return error;
} else {
if (delivery.isSettled() && transaction instanceof LocalTransaction) {
((LocalTransaction) transaction).setRollbackOnly();
}
Rejected rejected = new Rejected();
rejected.setError(error);
outcome = rejected;
}
}
}
Outcome sourceDefaultOutcome = getSource().getDefaultOutcome();
boolean defaultOutcome = sourceDefaultOutcome != null && sourceDefaultOutcome.getSymbol().equals(outcome.getSymbol());
DeliveryState resultantState;
if (transactionId == null) {
resultantState = defaultOutcome ? null : outcome;
} else {
TransactionalState transactionalState = new TransactionalState();
transactionalState.setOutcome(defaultOutcome ? null : outcome);
transactionalState.setTxnId(transactionId);
resultantState = transactionalState;
}
boolean settled = shouldReceiverSettleFirst(transferReceiverSettleMode);
if (transaction instanceof AsyncAutoCommitTransaction) {
_pendingDispositions.add(new PendingDispositionHolder(delivery.getDeliveryTag(), resultantState, settled));
} else {
getSession().receivedComplete();
updateDisposition(delivery.getDeliveryTag(), resultantState, settled);
}
getSession().registerMessageReceived(serverMessage.getSize());
if (transactionId != null) {
getSession().registerTransactedMessageReceived();
}
setRollbackOnly = false;
} catch (AccessControlException e) {
final Error err = new Error();
err.setCondition(AmqpError.NOT_ALLOWED);
err.setDescription(e.getMessage());
return err;
} finally {
if (setRollbackOnly && transaction instanceof LocalTransaction) {
((LocalTransaction) transaction).setRollbackOnly();
}
}
} finally {
reference.release();
}
}
return null;
}
use of org.apache.qpid.server.protocol.v1_0.type.DeliveryState in project qpid-broker-j by apache.
the class TxnCoordinatorReceivingLinkEndpoint method receiveDelivery.
@Override
protected Error receiveDelivery(Delivery delivery) {
// Only interested in the amqp-value section that holds the message to the coordinator
try (QpidByteBuffer payload = delivery.getPayload()) {
List<EncodingRetainingSection<?>> sections = getSectionDecoder().parseAll(payload);
boolean amqpValueSectionFound = false;
for (EncodingRetainingSection section : sections) {
try {
if (section instanceof AmqpValueSection) {
if (amqpValueSectionFound) {
throw new ConnectionScopedRuntimeException("Received more than one AmqpValue sections");
}
amqpValueSectionFound = true;
Object command = section.getValue();
Session_1_0 session = getSession();
AMQPConnection_1_0<?> connection = session.getConnection();
connection.receivedComplete();
if (command instanceof Declare) {
final IdentifiedTransaction txn = connection.createIdentifiedTransaction();
_createdTransactions.put(txn.getId(), txn.getServerTransaction());
long notificationRepeatPeriod = getSession().getContextValue(Long.class, Session.TRANSACTION_TIMEOUT_NOTIFICATION_REPEAT_PERIOD);
connection.registerTransactionTickers(txn.getServerTransaction(), this::doTimeoutAction, notificationRepeatPeriod);
Declared state = new Declared();
state.setTxnId(Session_1_0.integerToTransactionId(txn.getId()));
updateDisposition(delivery.getDeliveryTag(), state, true);
} else if (command instanceof Discharge) {
Discharge discharge = (Discharge) command;
Error error = discharge(discharge.getTxnId(), Boolean.TRUE.equals(discharge.getFail()));
final DeliveryState outcome;
if (error == null) {
outcome = new Accepted();
} else if (Arrays.asList(getSource().getOutcomes()).contains(Rejected.REJECTED_SYMBOL)) {
final Rejected rejected = new Rejected();
rejected.setError(error);
outcome = rejected;
error = null;
} else {
outcome = null;
}
if (error == null) {
updateDisposition(delivery.getDeliveryTag(), outcome, true);
}
return error;
} else {
throw new ConnectionScopedRuntimeException(String.format("Received unknown command '%s'", command.getClass().getSimpleName()));
}
}
} finally {
section.dispose();
}
}
if (!amqpValueSectionFound) {
throw new ConnectionScopedRuntimeException("Received no AmqpValue section");
}
} catch (AmqpErrorException e) {
return e.getError();
}
return null;
}
use of org.apache.qpid.server.protocol.v1_0.type.DeliveryState in project qpid-broker-j by apache.
the class AbstractReceivingLinkEndpoint method updateDispositions.
void updateDispositions(final Set<Binary> deliveryTags, final DeliveryState state, final boolean settled) {
final Set<Binary> unsettledKeys = new HashSet<>(_unsettled.keySet());
unsettledKeys.retainAll(deliveryTags);
final int settledDeliveryCount = deliveryTags.size() - unsettledKeys.size();
if (!unsettledKeys.isEmpty()) {
boolean outcomeUpdate = false;
Outcome outcome = null;
if (state instanceof Outcome) {
outcome = (Outcome) state;
} else if (state instanceof TransactionalState) {
outcome = ((TransactionalState) state).getOutcome();
}
if (outcome != null) {
for (final Binary deliveryTag : unsettledKeys) {
if (!(_unsettled.get(deliveryTag) instanceof Outcome)) {
Object oldOutcome = _unsettled.put(deliveryTag, outcome);
outcomeUpdate = outcomeUpdate || !outcome.equals(oldOutcome);
}
}
}
if (outcomeUpdate || settled) {
getSession().updateDisposition(this, deliveryTags, state, settled);
}
if (settled) {
int credit = 0;
for (final Binary deliveryTag : unsettledKeys) {
if (settled(deliveryTag)) {
if (!isDetached() && _creditWindow) {
credit++;
}
}
}
if (credit > 0) {
setLinkCredit(getLinkCredit().add(UnsignedInteger.valueOf(credit)));
sendFlowConditional();
} else {
getSession().sendFlowConditional();
}
}
}
if (settledDeliveryCount > 0 && _creditWindow) {
setLinkCredit(getLinkCredit().add(UnsignedInteger.ONE));
sendFlowConditional();
}
}
use of org.apache.qpid.server.protocol.v1_0.type.DeliveryState in project qpid-broker-j by apache.
the class Session_1_0 method updateDisposition.
private void updateDisposition(final Role role, final UnsignedInteger first, final UnsignedInteger last, final DeliveryState state, final boolean settled) {
Disposition disposition = new Disposition();
disposition.setRole(role);
disposition.setFirst(first);
disposition.setLast(last);
disposition.setSettled(settled);
disposition.setState(state);
if (settled) {
final DeliveryRegistry deliveryRegistry = role == Role.RECEIVER ? _incomingDeliveryRegistry : _outgoingDeliveryRegistry;
SequenceNumber pos = new SequenceNumber(first.intValue());
SequenceNumber end = new SequenceNumber(last.intValue());
while (pos.compareTo(end) <= 0) {
deliveryRegistry.removeDelivery(UnsignedInteger.valueOf(pos.intValue()));
pos.incr();
}
}
send(disposition);
}
use of org.apache.qpid.server.protocol.v1_0.type.DeliveryState in project qpid-broker-j by apache.
the class Session_1_0 method updateDisposition.
void updateDisposition(final LinkEndpoint<?, ?> linkEndpoint, final Set<Binary> deliveryTags, final DeliveryState state, final boolean settled) {
final Role role = linkEndpoint.getRole();
final Iterator<UnsignedInteger> iterator = getDeliveryIds(deliveryTags, linkEndpoint).iterator();
if (iterator.hasNext()) {
UnsignedInteger begin = iterator.next();
UnsignedInteger end = begin;
while (iterator.hasNext()) {
final UnsignedInteger deliveryId = iterator.next();
if (!end.add(UnsignedInteger.ONE).equals(deliveryId)) {
updateDisposition(role, begin, end, state, settled);
begin = deliveryId;
end = begin;
} else {
end = deliveryId;
}
}
updateDisposition(role, begin, end, state, settled);
}
}
Aggregations