io.flood.rpc.network.NetPackage.java Source code

Java tutorial

Introduction

Here is the source code for io.flood.rpc.network.NetPackage.java

Source

/*
 * Copyright 2015 The RPC Project
 *
 * The RPC Project 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.flood.rpc.network;

import com.google.common.primitives.Bytes;
import io.flood.common.utils.BytesUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class NetPackage {

    public static final int CURRENT_VERSION = 0x1;
    public static final int HEADER_LENTH = 16;
    /**
     *  ?00
     */
    public static final int SYS_EVENT_POINT = 0; //
    private int length = HEADER_LENTH;
    private int sequeue;
    private int version;
    private int command;
    private int state;
    private byte[] data;

    private NetPackage() {

    }

    public static NetPackage create(int length, ByteBuf buffer) {
        NetPackage netPackage = new NetPackage();
        netPackage.length = length;
        if (length < HEADER_LENTH) {
            throw new IllegalArgumentException(
                    "buffer's length less than header length(" + length + "<" + HEADER_LENTH + ")");
        }
        netPackage.sequeue = buffer.readInt();
        netPackage.version = buffer.readInt();
        netPackage.command = buffer.readInt();
        netPackage.state = buffer.readInt();
        netPackage.data = new byte[length - HEADER_LENTH];
        buffer.readBytes(netPackage.data);
        return netPackage;
    }

    public static NetPackage create(int seq, int cmd, int state, byte[] data) {
        NetPackage netPackage = new NetPackage();
        if (null == data) {
            data = new byte[0];
        }
        netPackage.length = HEADER_LENTH + data.length;
        netPackage.sequeue = seq;
        netPackage.version = CURRENT_VERSION;
        netPackage.command = cmd;
        netPackage.state = state;
        netPackage.data = data;
        return netPackage;
    }

    public static NetPackage create(int seq, int cmd, int state) {
        NetPackage netPackage = new NetPackage();
        netPackage.length = HEADER_LENTH;
        netPackage.sequeue = seq;
        netPackage.version = CURRENT_VERSION;
        netPackage.command = cmd;
        netPackage.state = state;
        return netPackage;
    }

    public static NetPackage create(int seq, int cmd, byte[] data) {
        return create(seq, cmd, 0, data);
    }

    public int length() {
        return length;
    }

    public int sequeue() {
        return sequeue;
    }

    public int version() {
        return version;
    }

    public int command() {
        return command;
    }

    public int state() {
        return state;
    }

    public byte[] data() {
        return data;
    }

    public byte[] toByteArray() {
        // Unpooled
        return Bytes.concat(BytesUtils.toBytes(length), BytesUtils.toBytes(sequeue), BytesUtils.toBytes(version),
                BytesUtils.toBytes(command), BytesUtils.toBytes(state), data);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(length).append("|").append(sequeue).append("|").append(version).append("|").append(command)
                .append("|").append(state);
        return sb.toString();
    }

    public ByteBuf toByteBuf() {
        return Unpooled.wrappedBuffer(BytesUtils.toBytes(length), BytesUtils.toBytes(sequeue),
                BytesUtils.toBytes(version), BytesUtils.toBytes(command), BytesUtils.toBytes(state), data);
    }
}