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

io.jsync.eventbus.impl.ReplyFailureMessage Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
/*
 * Copyright 2013 Red Hat, Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 *
 */

package io.jsync.eventbus.impl;

import io.jsync.buffer.Buffer;
import io.jsync.eventbus.Message;
import io.jsync.eventbus.ReplyException;
import io.jsync.eventbus.ReplyFailure;
import io.netty.util.CharsetUtil;


/**
 * Special message that transmits a send reply failure to a send handler
 *
 * @author Tim Fox
 */
public class ReplyFailureMessage extends BaseMessage {

    private byte[] encoded;

    ReplyFailureMessage(String address, ReplyException body) {
        super(true, address, body);
    }

    public ReplyFailureMessage(Buffer readBuff) {
        super(readBuff);
    }

    @Override
    protected void readBody(int pos, Buffer readBuff) {
        int i = (int) readBuff.getByte(pos);
        ReplyFailure rf = ReplyFailure.fromInt(i);
        pos++;
        int failureCode = readBuff.getInt(pos);
        pos += 4;
        boolean isNull = readBuff.getByte(pos) == (byte) 0;
        String message;
        if (!isNull) {
            pos++;
            int strLength = readBuff.getInt(pos);
            pos += 4;
            byte[] bytes = readBuff.getBytes(pos, pos + strLength);
            message = new String(bytes, CharsetUtil.UTF_8);
        } else {
            message = null;
        }
        body = new ReplyException(rf, failureCode, message);
    }

    @Override
    protected void writeBody(Buffer buff) {
        buff.appendByte((byte) body.failureType().toInt());
        buff.appendInt(body.failureCode());
        if (body.getMessage() == null) {
            buff.appendByte((byte) 0);
        } else {
            buff.appendByte((byte) 1);
            buff.appendInt(encoded.length);
            buff.appendBytes(encoded);
        }
    }

    @Override
    protected int getBodyLength() {
        if (body.getMessage() == null) {
            return 1 + 4 + 1;
        } else {
            encoded = body.getMessage().getBytes(CharsetUtil.UTF_8);
            return 1 + 4 + 1 + 4 + encoded.length;
        }
    }

    @Override
    protected Message copy() {
        // No need to copy since everything is immutable
        return this;
    }

    @Override
    protected byte type() {
        return MessageFactory.TYPE_REPLY_FAILURE;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy