Search in sources :

Example 1 with AMQPFrame

use of com.swiftmq.amqp.v100.transport.AMQPFrame in project swiftmq-client by iitsoftware.

the class ConnectionDispatcher method dataAvailable.

public void dataAvailable(LengthCaptureDataInput in) {
    try {
        if (connectionDisabled) {
            if (fTracer.isEnabled())
                fTracer.trace(toString(), "Connection is disabled, ignore inbound traffic");
            return;
        }
        lastActivity = System.currentTimeMillis();
        if (awaitProtocolHeader) {
            ProtocolHeader header = new ProtocolHeader();
            header.readContent(in);
            awaitProtocolHeader = false;
            protocolHandler.setProtHeaderExpected(false);
            if (fTracer.isEnabled())
                fTracer.trace("amqp", "RCV: " + header);
            dispatch(new POProtocolResponse(header));
        } else {
            if (saslActive) {
                SaslFrameIF frame = FrameReader.createSaslFrame(in);
                int size = frame.getPredictedSize();
                if (size > maxLocalFrameSize) {
                    if (fTracer.isEnabled())
                        fTracer.trace(toString(), ", dataAvailable, Frame size (" + size + ") > max frame size (" + maxLocalFrameSize + ")");
                    connectionDisabled = true;
                    new Disconnecter(ConnectionError.FRAMING_ERROR.getValue(), "Frame size (" + size + ") > max frame size (" + maxLocalFrameSize + ")").start();
                } else {
                    if (fTracer.isEnabled())
                        fTracer.trace("amqp", "RCV[" + ((AMQPFrame) frame).getChannel() + "] (size=" + size + "): " + frame);
                    frame.accept(dispatchVisitor);
                }
            } else {
                FrameIF frame = FrameReader.createFrame(in);
                int size = frame.getPredictedSize();
                if (size > maxLocalFrameSize) {
                    if (fTracer.isEnabled())
                        fTracer.trace(toString(), ", dataAvailable, Frame size (" + size + ") > max frame size (" + maxLocalFrameSize + ")");
                    connectionDisabled = true;
                    new Disconnecter(ConnectionError.FRAMING_ERROR.getValue(), "Frame size (" + size + ") > max frame size (" + maxLocalFrameSize + ")").start();
                } else {
                    if (fTracer.isEnabled())
                        fTracer.trace("amqp", "RCV[" + ((AMQPFrame) frame).getChannel() + "] (size=" + size + "): " + frame + (((AMQPFrame) frame).getPayload() != null ? " | payload size=" + ((AMQPFrame) frame).getPayload().length : ""));
                    frame.accept(dispatchVisitor);
                }
            }
        }
    } catch (Exception e) {
        new Disconnecter(ConnectionError.FRAMING_ERROR.getValue(), e.toString()).start();
    }
}
Also used : AMQPFrame(com.swiftmq.amqp.v100.transport.AMQPFrame) ProtocolHeader(com.swiftmq.amqp.ProtocolHeader) SaslException(javax.security.sasl.SaslException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 2 with AMQPFrame

use of com.swiftmq.amqp.v100.transport.AMQPFrame in project swiftmq-client by iitsoftware.

the class FrameReader method createSaslFrame.

/**
 * Creates a SaslFrameIF object.
 *
 * @param in input stream
 * @return frame
 */
public static SaslFrameIF createSaslFrame(LengthCaptureDataInput in) throws Exception {
    long frameSize = 0;
    byte dataOffset = 0;
    byte typeCode = 0;
    int channel = 0;
    byte[] extendedHeader = null;
    in.startCaptureLength();
    // frame header
    frameSize = in.readInt();
    dataOffset = in.readByte();
    typeCode = in.readByte();
    if (!((typeCode == AMQPFrame.TYPE_CODE_AMQP_FRAME) || (typeCode == AMQPFrame.TYPE_CODE_SASL_FRAME)))
        throw new IOException("Invalid frame type (" + typeCode + "), not an AMQP or SASL frame!");
    channel = in.readUnsignedShort();
    // extended header
    int doff = dataOffset;
    if (doff < 2)
        throw new Exception("Malformed frame, data offset is " + doff);
    if (doff > 2) {
        extendedHeader = new byte[doff * 4 - 8];
        in.readFully(extendedHeader);
    }
    // body
    long bodySize = frameSize - doff * 4;
    if (bodySize > 0) {
        if (bodySize > Integer.MAX_VALUE)
            throw new Exception("Frame body size (" + bodySize + ") is greater than Integer.MAX_VALUE (" + Integer.MAX_VALUE + ")");
    } else
        return new HeartbeatFrame(channel);
    AMQPFrame frame = (AMQPFrame) SaslFrameFactory.create(channel, AMQPTypeDecoder.decode(in));
    int plLength = (int) (frameSize - in.stopCaptureLength());
    if (plLength > 0) {
        byte[] b = new byte[plLength];
        in.readFully(b);
        frame.setPayload(b);
    }
    return frame;
}
Also used : AMQPFrame(com.swiftmq.amqp.v100.transport.AMQPFrame) HeartbeatFrame(com.swiftmq.amqp.v100.transport.HeartbeatFrame) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with AMQPFrame

use of com.swiftmq.amqp.v100.transport.AMQPFrame in project swiftmq-client by iitsoftware.

the class FrameReader method createFrame.

/**
 * Creates a FrameIF object.
 *
 * @param in input stream
 * @return frame
 */
public static FrameIF createFrame(LengthCaptureDataInput in) throws Exception {
    long frameSize = 0;
    byte dataOffset = 0;
    byte typeCode = 0;
    int channel = 0;
    byte[] extendedHeader = null;
    in.startCaptureLength();
    // frame header
    frameSize = in.readInt();
    dataOffset = in.readByte();
    typeCode = in.readByte();
    if (!((typeCode == AMQPFrame.TYPE_CODE_AMQP_FRAME) || (typeCode == AMQPFrame.TYPE_CODE_SASL_FRAME)))
        throw new IOException("Invalid frame type (" + typeCode + "), not an AMQP or SASL frame!");
    channel = in.readUnsignedShort();
    // extended header
    int doff = dataOffset;
    if (doff < 2)
        throw new Exception("Malformed frame, data offset is " + doff);
    if (doff > 2) {
        extendedHeader = new byte[doff * 4 - 8];
        in.readFully(extendedHeader);
    }
    // body
    long bodySize = frameSize - doff * 4;
    if (bodySize > 0) {
        if (bodySize > Integer.MAX_VALUE)
            throw new Exception("Frame body size (" + bodySize + ") is greater than Integer.MAX_VALUE (" + Integer.MAX_VALUE + ")");
    } else
        return new HeartbeatFrame(channel);
    AMQPFrame frame = (AMQPFrame) FrameFactory.create(channel, AMQPTypeDecoder.decode(in));
    int plLength = (int) (frameSize - in.stopCaptureLength());
    if (plLength > 0) {
        byte[] b = new byte[plLength];
        in.readFully(b);
        frame.setPayload(b);
    }
    return frame;
}
Also used : AMQPFrame(com.swiftmq.amqp.v100.transport.AMQPFrame) HeartbeatFrame(com.swiftmq.amqp.v100.transport.HeartbeatFrame) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

AMQPFrame (com.swiftmq.amqp.v100.transport.AMQPFrame)3 IOException (java.io.IOException)3 HeartbeatFrame (com.swiftmq.amqp.v100.transport.HeartbeatFrame)2 ProtocolHeader (com.swiftmq.amqp.ProtocolHeader)1 UnknownHostException (java.net.UnknownHostException)1 SaslException (javax.security.sasl.SaslException)1