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

org.bdware.dogp.codec.DOGPMessage Maven / Gradle / Ivy

The newest version!
/*
 *    Copyright (c) [2021] [Peking University]
 *    [BDWare DOIP SDK] is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */
package org.bdware.dogp.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

import java.net.InetSocketAddress;

public class DOGPMessage {

    public byte opcode;
    byte[] dataSpaceAddress;//16字节
    byte[] dataAddress;//16字节
    byte[] reserved;//5byte
    byte[] srcIpAndPort;//6byte BIG Endian
    byte[] dstIpAndPort;//6byte BIG Endian
    byte[] sourceGatewayId;//32字节
    byte[] targetGatewayId;//32字节
    int contentLength;
    public static final int MESSAG_LENGTH = 54;
    public transient ByteBuf content;
    transient InetSocketAddress sender;

    public DOGPMessage() {
    }

    public DOGPMessage(InetSocketAddress sender) {
        this.sender = sender;
    }

    public void setContent(byte[] content) {
        this.content = Unpooled.wrappedBuffer(content);
    }

    public InetSocketAddress getSender() {
        return sender;
    }

    public void setSender(InetSocketAddress sender) {
        this.sender = sender;
    }

    public DOGPMessage setDst(byte[] ipAndPort) {
        this.dstIpAndPort = ipAndPort;
        return this;
    }

    public DOGPMessage setSrc(byte[] ipAndPort) {
        this.srcIpAndPort = ipAndPort;
        return this;
    }

    public String prettyPrint() {
        ByteBuf duplicated = content.duplicate();
        int count = duplicated.readableBytes();
        byte[] contentArray = new byte[count];
        duplicated.readBytes(contentArray);

        return String.format("op:%d dsAddr:%s dataAddr:%s reserved:%s srcIp:%s dstIp:%s len:%d actLen:%d %s",
                opcode, byteToHex(dataSpaceAddress),
                byteToHex(dataAddress),
                byteToHex(reserved),
                byteToHex(srcIpAndPort),
                byteToHex(dstIpAndPort),
                contentLength, content.readableBytes(), ByteUtils.toHexString(contentArray)
        );

    }

    public String byteToHex(byte[] data) {
        if (data == null) return "-empty-";
        return ByteUtils.toHexString(data);
    }

    public byte[] getSrc() {
        return srcIpAndPort;
    }

    public byte[] getDst() {
        return dstIpAndPort;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy