Search in sources :

Example 1 with Role

use of org.apache.qpid.protonj2.types.transport.Role in project qpid-protonj2 by apache.

the class AttachExpectation method handleAttach.

// ----- Handle the performative and configure response is told to respond
@Override
public void handleAttach(int frameSize, Attach attach, ByteBuf payload, int channel, AMQPTestDriver context) {
    super.handleAttach(frameSize, attach, payload, channel, context);
    final UnsignedShort remoteChannel = UnsignedShort.valueOf(channel);
    final SessionTracker session = driver.sessions().getSessionFromRemoteChannel(remoteChannel);
    if (session == null) {
        throw new AssertionError(String.format("Received Attach on channel [%s] that has no matching Session for that remote channel. ", remoteChannel));
    }
    final LinkTracker link = session.handleRemoteAttach(attach);
    if (response != null) {
        // to say otherwise by the test.
        if (response.onChannel() == BeginInjectAction.CHANNEL_UNSET) {
            response.onChannel(link.getSession().getLocalChannel());
        }
        // Populate the fields of the response with defaults if non set by the test script
        if (response.getPerformative().getHandle() == null) {
            response.withHandle(session.findFreeLocalHandle());
        }
        if (response.getPerformative().getName() == null) {
            response.withName(attach.getName());
        }
        if (response.getPerformative().getRole() == null) {
            response.withRole(Boolean.TRUE.equals(attach.getRole()) ? Role.SENDER : Role.RECEIVER);
        }
        if (response.getPerformative().getSenderSettleMode() == null) {
            response.withSndSettleMode(SenderSettleMode.valueOf(attach.getSenderSettleMode()));
        }
        if (response.getPerformative().getReceiverSettleMode() == null) {
            response.withRcvSettleMode(ReceiverSettleMode.valueOf(attach.getReceiverSettleMode()));
        }
        if (response.getPerformative().getSource() == null && !response.isNullSourceRequired()) {
            response.withSource(attach.getSource());
            if (attach.getSource() != null && Boolean.TRUE.equals(attach.getSource().getDynamic())) {
                attach.getSource().setAddress(UUID.randomUUID().toString());
            }
        }
        if (rejecting) {
            if (Boolean.FALSE.equals(attach.getRole())) {
                // Sender attach so response should have null target
                response.withNullTarget();
            } else {
                // Receiver attach so response should have null source
                response.withNullSource();
            }
        }
        if (response.getPerformative().getTarget() == null && !response.isNullTargetRequired()) {
            if (attach.getTarget() != null) {
                if (attach.getTarget() instanceof Target) {
                    Target target = (Target) attach.getTarget();
                    response.withTarget(target);
                    if (target != null && Boolean.TRUE.equals(target.getDynamic())) {
                        target.setAddress(UUID.randomUUID().toString());
                    }
                } else {
                    Coordinator coordinator = (Coordinator) attach.getTarget();
                    response.withTarget(coordinator);
                }
            }
        }
        if (response.getPerformative().getInitialDeliveryCount() == null) {
            Role role = Role.valueOf(response.getPerformative().getRole());
            if (role == Role.SENDER) {
                response.withInitialDeliveryCount(0);
            }
        }
    // Other fields are left not set for now unless test script configured
    }
}
Also used : Role(org.apache.qpid.protonj2.test.driver.codec.transport.Role) Target(org.apache.qpid.protonj2.test.driver.codec.messaging.Target) SessionTracker(org.apache.qpid.protonj2.test.driver.SessionTracker) Coordinator(org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator) LinkTracker(org.apache.qpid.protonj2.test.driver.LinkTracker) UnsignedShort(org.apache.qpid.protonj2.test.driver.codec.primitives.UnsignedShort)

Example 2 with Role

use of org.apache.qpid.protonj2.types.transport.Role in project qpid-protonj2 by apache.

the class AttachTypeDecoder method readAttach.

private Attach readAttach(ProtonBuffer buffer, DecoderState state, ListTypeDecoder listDecoder) throws DecodeException {
    final Attach attach = new Attach();
    @SuppressWarnings("unused") final int size = listDecoder.readSize(buffer);
    final int count = listDecoder.readCount(buffer);
    if (count < MIN_ATTACH_LIST_ENTRIES) {
        throw new DecodeException(errorForMissingRequiredFields(count));
    }
    if (count > MAX_ATTACH_LIST_ENTRIES) {
        throw new DecodeException("To many entries in Attach list encoding: " + count);
    }
    for (int index = 0; index < count; ++index) {
        // Peek ahead and see if there is a null in the next slot, if so we don't call
        // the setter for that entry to ensure the returned type reflects the encoded
        // state in the modification entry.
        final boolean nullValue = buffer.getByte(buffer.getReadIndex()) == EncodingCodes.NULL;
        if (nullValue) {
            // Ensure mandatory fields are set
            if (index < MIN_ATTACH_LIST_ENTRIES) {
                throw new DecodeException(errorForMissingRequiredFields(index));
            }
            buffer.readByte();
            continue;
        }
        switch(index) {
            case 0:
                attach.setName(state.getDecoder().readString(buffer, state));
                break;
            case 1:
                attach.setHandle(state.getDecoder().readUnsignedInteger(buffer, state, 0l));
                break;
            case 2:
                Boolean role = state.getDecoder().readBoolean(buffer, state);
                attach.setRole(Boolean.TRUE.equals(role) ? Role.RECEIVER : Role.SENDER);
                break;
            case 3:
                byte sndSettleMode = state.getDecoder().readUnsignedByte(buffer, state, (byte) 2);
                attach.setSenderSettleMode(SenderSettleMode.valueOf(sndSettleMode));
                break;
            case 4:
                byte rcvSettleMode = state.getDecoder().readUnsignedByte(buffer, state, (byte) 0);
                attach.setReceiverSettleMode(ReceiverSettleMode.valueOf(rcvSettleMode));
                break;
            case 5:
                attach.setSource(state.getDecoder().readObject(buffer, state, Source.class));
                break;
            case 6:
                attach.setTarget(state.getDecoder().readObject(buffer, state, Terminus.class));
                break;
            case 7:
                attach.setUnsettled(state.getDecoder().readMap(buffer, state));
                break;
            case 8:
                attach.setIncompleteUnsettled(state.getDecoder().readBoolean(buffer, state, true));
                break;
            case 9:
                attach.setInitialDeliveryCount(state.getDecoder().readUnsignedInteger(buffer, state, 0l));
                break;
            case 10:
                attach.setMaxMessageSize(state.getDecoder().readUnsignedLong(buffer, state));
                break;
            case 11:
                attach.setOfferedCapabilities(state.getDecoder().readMultiple(buffer, state, Symbol.class));
                break;
            case 12:
                attach.setDesiredCapabilities(state.getDecoder().readMultiple(buffer, state, Symbol.class));
                break;
            case 13:
                attach.setProperties(state.getDecoder().readMap(buffer, state));
                break;
        }
    }
    return attach;
}
Also used : Attach(org.apache.qpid.protonj2.types.transport.Attach) Symbol(org.apache.qpid.protonj2.types.Symbol) DecodeException(org.apache.qpid.protonj2.codec.DecodeException) Source(org.apache.qpid.protonj2.types.messaging.Source) Terminus(org.apache.qpid.protonj2.types.messaging.Terminus)

Example 3 with Role

use of org.apache.qpid.protonj2.types.transport.Role in project qpid-protonj2 by apache.

the class AttachTypeDecoder method readAttach.

private Attach readAttach(InputStream stream, StreamDecoderState state, ListTypeDecoder listDecoder) throws DecodeException {
    final Attach attach = new Attach();
    @SuppressWarnings("unused") final int size = listDecoder.readSize(stream);
    final int count = listDecoder.readCount(stream);
    if (count < MIN_ATTACH_LIST_ENTRIES) {
        throw new DecodeException(errorForMissingRequiredFields(count));
    }
    if (count > MAX_ATTACH_LIST_ENTRIES) {
        throw new DecodeException("To many entries in Attach list encoding: " + count);
    }
    for (int index = 0; index < count; ++index) {
        // the encoded state in the modification entry.
        if (stream.markSupported()) {
            stream.mark(1);
            final boolean nullValue = ProtonStreamUtils.readByte(stream) == EncodingCodes.NULL;
            if (nullValue) {
                // Ensure mandatory fields are set
                if (index < MIN_ATTACH_LIST_ENTRIES) {
                    throw new DecodeException(errorForMissingRequiredFields(index));
                }
                continue;
            } else {
                ProtonStreamUtils.reset(stream);
            }
        }
        switch(index) {
            case 0:
                attach.setName(state.getDecoder().readString(stream, state));
                break;
            case 1:
                attach.setHandle(state.getDecoder().readUnsignedInteger(stream, state, 0l));
                break;
            case 2:
                Boolean role = state.getDecoder().readBoolean(stream, state);
                attach.setRole(Boolean.TRUE.equals(role) ? Role.RECEIVER : Role.SENDER);
                break;
            case 3:
                byte sndSettleMode = state.getDecoder().readUnsignedByte(stream, state, (byte) 2);
                attach.setSenderSettleMode(SenderSettleMode.valueOf(sndSettleMode));
                break;
            case 4:
                byte rcvSettleMode = state.getDecoder().readUnsignedByte(stream, state, (byte) 0);
                attach.setReceiverSettleMode(ReceiverSettleMode.valueOf(rcvSettleMode));
                break;
            case 5:
                attach.setSource(state.getDecoder().readObject(stream, state, Source.class));
                break;
            case 6:
                attach.setTarget(state.getDecoder().readObject(stream, state, Terminus.class));
                break;
            case 7:
                attach.setUnsettled(state.getDecoder().readMap(stream, state));
                break;
            case 8:
                attach.setIncompleteUnsettled(state.getDecoder().readBoolean(stream, state, true));
                break;
            case 9:
                attach.setInitialDeliveryCount(state.getDecoder().readUnsignedInteger(stream, state, 0l));
                break;
            case 10:
                attach.setMaxMessageSize(state.getDecoder().readUnsignedLong(stream, state));
                break;
            case 11:
                attach.setOfferedCapabilities(state.getDecoder().readMultiple(stream, state, Symbol.class));
                break;
            case 12:
                attach.setDesiredCapabilities(state.getDecoder().readMultiple(stream, state, Symbol.class));
                break;
            case 13:
                attach.setProperties(state.getDecoder().readMap(stream, state));
                break;
        }
    }
    return attach;
}
Also used : Attach(org.apache.qpid.protonj2.types.transport.Attach) Symbol(org.apache.qpid.protonj2.types.Symbol) DecodeException(org.apache.qpid.protonj2.codec.DecodeException) Source(org.apache.qpid.protonj2.types.messaging.Source) Terminus(org.apache.qpid.protonj2.types.messaging.Terminus)

Example 4 with Role

use of org.apache.qpid.protonj2.types.transport.Role in project qpid-protonj2 by apache.

the class CodecTestSupport method assertTypesEqual.

/**
 * Compare a Attach to another Attach instance.
 *
 * @param attach1
 *      A {@link Attach} instances or null
 * @param attach2
 *      A {@link Attach} instances or null.
 *
 * @throws AssertionError
 *      If the two types are not equal to one another.
 */
public static void assertTypesEqual(Attach attach1, Attach attach2) throws AssertionError {
    if (attach1 == attach2) {
        return;
    } else if (attach1 == null || attach2 == null) {
        assertEquals(attach1, attach2);
    }
    assertSame(attach1.hasHandle(), attach2.hasHandle(), "Expected Attach with matching has handle values");
    assertEquals(attach1.getHandle(), attach2.getHandle(), "Handle values not equal");
    assertSame(attach1.hasInitialDeliveryCount(), attach2.hasInitialDeliveryCount(), "Expected Attach with matching has initial delivery count values");
    assertEquals(attach1.getInitialDeliveryCount(), attach2.getInitialDeliveryCount(), "Initial delivery count values not equal");
    assertSame(attach1.hasMaxMessageSize(), attach2.hasMaxMessageSize(), "Expected Attach with matching has max message size values");
    assertEquals(attach1.getMaxMessageSize(), attach2.getMaxMessageSize(), "Max MessageSize values not equal");
    assertSame(attach1.hasName(), attach2.hasName(), "Expected Attach with matching has name values");
    assertEquals(attach1.getName(), attach2.getName(), "Link Name values not equal");
    assertSame(attach1.hasSource(), attach2.hasSource(), "Expected Attach with matching has Source values");
    assertTypesEqual(attach1.getSource(), attach2.getSource());
    assertSame(attach1.hasTarget(), attach2.hasTarget(), "Expected Attach with matching has Target values");
    Target attach1Target = attach1.getTarget();
    Target attach2Target = attach2.getTarget();
    assertTypesEqual(attach1Target, attach2Target);
    assertSame(attach1.hasUnsettled(), attach2.hasUnsettled(), "Expected Attach with matching has handle values");
    assertTypesEqual(attach1.getUnsettled(), attach2.getUnsettled());
    assertSame(attach1.hasReceiverSettleMode(), attach2.hasReceiverSettleMode(), "Expected Attach with matching has receiver settle mode values");
    assertEquals(attach1.getReceiverSettleMode(), attach2.getReceiverSettleMode(), "Receiver settle mode values not equal");
    assertSame(attach1.hasSenderSettleMode(), attach2.hasSenderSettleMode(), "Expected Attach with matching has sender settle mode values");
    assertEquals(attach1.getSenderSettleMode(), attach2.getSenderSettleMode(), "Sender settle mode values not equal");
    assertSame(attach1.hasRole(), attach2.hasRole(), "Expected Attach with matching has Role values");
    assertEquals(attach1.getRole(), attach2.getRole(), "Role values not equal");
    assertSame(attach1.hasIncompleteUnsettled(), attach2.hasIncompleteUnsettled(), "Expected Attach with matching has incomplete unsettled values");
    assertEquals(attach1.getIncompleteUnsettled(), attach2.getIncompleteUnsettled(), "Handle values not equal");
    assertSame(attach1.hasProperties(), attach2.hasProperties(), "Expected Attach with matching has properties values");
    assertEquals(attach1.getProperties(), attach2.getProperties(), "Properties Map values not equal");
    assertSame(attach1.hasDesiredCapabilities(), attach2.hasDesiredCapabilities(), "Expected Attach with matching has desired capabilities values");
    assertArrayEquals(attach1.getDesiredCapabilities(), attach2.getDesiredCapabilities(), "Desired Capabilities are not equal");
    assertSame(attach1.hasOfferedCapabilities(), attach2.hasOfferedCapabilities(), "Expected Attach with matching has offered capabilities values");
    assertArrayEquals(attach1.getOfferedCapabilities(), attach2.getOfferedCapabilities(), "Offered Capabilities are not equal");
}
Also used : Target(org.apache.qpid.protonj2.types.messaging.Target)

Example 5 with Role

use of org.apache.qpid.protonj2.types.transport.Role in project qpid-protonj2 by apache.

the class ProtonSessionTest method doTestSessionTrackingOfLinks.

private void doTestSessionTrackingOfLinks(Role role, boolean localDetach, boolean remoteDetach, boolean remoteGoesFirst, boolean close) throws Exception {
    Engine engine = EngineFactory.PROTON.createNonSaslEngine();
    engine.errorHandler(result -> failure = result.failureCause());
    ProtonTestConnector peer = createTestPeer(engine);
    peer.expectAMQPHeader().respondWithAMQPHeader();
    peer.expectOpen().respond().withContainerId("driver");
    peer.expectBegin().respond();
    Connection connection = engine.start();
    connection.open();
    Session session = connection.session();
    session.open();
    assertTrue(session.senders().isEmpty());
    peer.expectAttach().withRole(role.getValue()).respond();
    final Link<?> link;
    if (role == Role.RECEIVER) {
        link = session.receiver("test");
    } else {
        link = session.sender("test");
    }
    link.open();
    if (role == Role.RECEIVER) {
        assertFalse(session.receivers().isEmpty());
        assertEquals(1, session.receivers().size());
    } else {
        assertFalse(session.senders().isEmpty());
        assertEquals(1, session.senders().size());
    }
    assertFalse(session.links().isEmpty());
    assertEquals(1, session.links().size());
    if (remoteDetach && remoteGoesFirst) {
        peer.remoteDetach().withClosed(close).now();
    }
    if (localDetach) {
        peer.expectDetach().withClosed(close);
        if (close) {
            link.close();
        } else {
            link.detach();
        }
    }
    if (remoteDetach && !remoteGoesFirst) {
        peer.remoteDetach().withClosed(close).now();
    }
    if (remoteDetach && localDetach) {
        assertTrue(session.receivers().isEmpty());
        assertTrue(session.senders().isEmpty());
        assertTrue(session.links().isEmpty());
    } else {
        if (role == Role.RECEIVER) {
            assertFalse(session.receivers().isEmpty());
            assertEquals(1, session.receivers().size());
        } else {
            assertFalse(session.senders().isEmpty());
            assertEquals(1, session.senders().size());
        }
        assertFalse(session.links().isEmpty());
        assertEquals(1, session.links().size());
    }
    peer.expectEnd().respond();
    session.close();
    peer.waitForScriptToComplete();
    assertNull(failure);
}
Also used : Connection(org.apache.qpid.protonj2.engine.Connection) ProtonTestConnector(org.apache.qpid.protonj2.test.driver.ProtonTestConnector) Engine(org.apache.qpid.protonj2.engine.Engine) Session(org.apache.qpid.protonj2.engine.Session)

Aggregations

DecodeException (org.apache.qpid.protonj2.codec.DecodeException)2 Connection (org.apache.qpid.protonj2.engine.Connection)2 Engine (org.apache.qpid.protonj2.engine.Engine)2 Session (org.apache.qpid.protonj2.engine.Session)2 ProtonTestConnector (org.apache.qpid.protonj2.test.driver.ProtonTestConnector)2 Symbol (org.apache.qpid.protonj2.types.Symbol)2 Source (org.apache.qpid.protonj2.types.messaging.Source)2 Terminus (org.apache.qpid.protonj2.types.messaging.Terminus)2 Attach (org.apache.qpid.protonj2.types.transport.Attach)2 LinkTracker (org.apache.qpid.protonj2.test.driver.LinkTracker)1 SessionTracker (org.apache.qpid.protonj2.test.driver.SessionTracker)1 Target (org.apache.qpid.protonj2.test.driver.codec.messaging.Target)1 UnsignedShort (org.apache.qpid.protonj2.test.driver.codec.primitives.UnsignedShort)1 Coordinator (org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator)1 Role (org.apache.qpid.protonj2.test.driver.codec.transport.Role)1 Target (org.apache.qpid.protonj2.types.messaging.Target)1