All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.mq.protocol.ProtocolDecoder Maven / Gradle / Ivy

There is a newer version: 2.2.23.1
Show newest version
/*
 *
 *  * Copyright 2005-2015 Red Hat, Inc.
 *  * Red Hat licenses this file to you under the Apache License, version
 *  * 2.0 (the "License"); you may not use this file except in compliance
 *  * with the License.  You may obtain a copy of the License at
 *  *    http://www.apache.org/licenses/LICENSE-2.0
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *  * implied.  See the License for the specific language governing
 *  * permissions and limitations under the License.
 *
 */
package io.fabric8.mq.protocol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;

import java.io.IOException;
import java.net.ProtocolException;

/**
 * An abstract base class used to implement a Vertx handler which
 * decode a buffer stream to a protocol specific frame objects.
 */
public abstract class ProtocolDecoder implements Handler {

    private static final transient Logger LOG = LoggerFactory.getLogger(ProtocolDecoder.class);
    protected Buffer buff = null;
    protected long bytesDecoded;
    protected int readStart;
    protected Action nextDecodeAction;
    protected int readEnd;
    private String error;
    private Handler codecHander;
    private Handler errorHandler;

    abstract protected Action initialDecodeAction();

    public ProtocolDecoder codecHandler(Handler codecHander) {
        this.codecHander = codecHander;
        return this;
    }

    public ProtocolDecoder errorHandler(Handler errorHandler) {
        this.errorHandler = errorHandler;
        return this;
    }

    @Override
    public void handle(Buffer event) {
        if (error == null) {
            if (buff == null) {
                buff = event;
            } else {
                buff.appendBuffer(event);
            }
            try {
                T rc = read();
                while (rc != null) {
                    codecHander.handle(rc);
                    rc = read();
                }
            } catch (Exception e) {
                LOG.debug("Protocol decoding failure: " + e, e);
                error = e.getMessage();
                errorHandler.handle(error);
            }
        }
    }

    public T read() throws IOException {
        T command = null;
        if (buff != null && readEnd < buff.length()) {
            if (nextDecodeAction == null) {
                nextDecodeAction = initialDecodeAction();
            }
            command = nextDecodeAction.apply();

            // did we fully read the buffer?
            if (readStart == buff.length()) {
                buff = null;
                readEnd -= readStart;
                readStart = 0;
            }
            // Are we wasting too much space?
            else if (readStart > 1024 * 4) {
                // lets compact the buffer...
                buff = buff.getBuffer(readStart, buff.length());
                readEnd -= readStart;
                readStart = 0;
            }

            assert readStart <= readEnd;
        }
        return command;
    }

    protected Buffer readUntil(Byte octet) throws ProtocolException {
        return readUntil(octet, -1);
    }

    protected Buffer readUntil(Byte octet, int max) throws ProtocolException {
        return readUntil(octet, max, "Maximum protocol buffer length exceeded");
    }

    protected Buffer readUntil(byte octet, int max, String msg) throws ProtocolException {
        int pos = io.fabric8.mq.util.BufferSupport.indexOf(buff, readEnd, buff.length(), octet);
        if (pos >= 0) {
            int offset = readStart;
            readEnd = pos + 1;
            bytesDecoded += readEnd - readStart;
            readStart = readEnd;
            int length = readEnd - offset;
            if (max >= 0 && length > max) {
                throw new ProtocolException(msg);
            }
            return buff.getBuffer(offset, readEnd);
        } else {
            readEnd += buff.length();
            if (max >= 0 && (readEnd - readStart) > max) {
                throw new ProtocolException(msg);
            }
            return null;
        }
    }

    protected Buffer readBytes(int length) {
        readEnd = readStart + length;
        if (buff.length() < readEnd) {
            return null;
        } else {
            bytesDecoded += readEnd - readStart;
            int offset = readStart;
            readStart = readEnd;
            return buff.getBuffer(offset, readEnd);
        }
    }

    protected Buffer peekBytes(int length) {
        readEnd = readStart + length;
        if (buff.length() < readEnd) {
            return null;
        } else {
            return buff.getBuffer(readStart, readEnd);
        }
    }

    public long getBytesDecoded() {
        return bytesDecoded;
    }

    public interface Action {
        T apply() throws IOException;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy