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

org.nustaq.fastcast.impl.DataPacket Maven / Gradle / Ivy

/*
 * Copyright 2014 Ruediger Moeller.
 *
 * Licensed 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 org.nustaq.fastcast.impl;


import org.nustaq.offheap.structs.FSTStruct;
import org.nustaq.offheap.structs.FSTStructAllocator;

/**
 * Created with IntelliJ IDEA.
 * User: ruedi
 * Date: 8/10/13
 * Time: 11:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataPacket extends Packet {

    public static DataPacket getTemplate(int packetSize) {
        int emptyPackSize = new FSTStructAllocator(1).newStruct(new DataPacket()).getByteSize();
        DataPacket template = new DataPacket();
        int payMaxLen = packetSize - emptyPackSize - 2;
        template.data = new byte[payMaxLen];
        return template;
    }

    public static final short COMPLETE = 1; // payload fits inpacket
    public static final short CHAINED = 2;  // payload is chained. len then denotes len in current packet
    public static final short EOP = 3;      // end of packet
    public static final short MAX_CODE = EOP;  //

    public static final int HEADERLEN = 4;

    protected boolean isDecoded;
    protected boolean isRetrans = false;
    protected int bytesLeft;
    protected byte[] data = new byte[0];

    public void data(int index, byte val) {
        data[index] = val;
    }

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

    public int dataLen() {
        return data.length;
    }

    public int dataIndex() {
        // generated
        return -1;
    }

    /**
     * internal flag, anyway transmitted FIXME: not required anymore (relict of FC 2.x) !
     * @return
     */
    public boolean isDecoded() {
        return isDecoded;
    }

    public void setDecoded(boolean decoded) {
        isDecoded = decoded;
    }

    public void dataPointer(FSTStruct pointer) {
        // generated
    }

    public FSTStruct dataPointer() {
        // generated
        return null;
    }

    public boolean isRetrans() {
        return isRetrans;
    }

    public void setRetrans(boolean isRetrans) {
        this.isRetrans = isRetrans;
    }

    public int getBytesLeft() {
        return bytesLeft;
    }

    public int getDGramSize() {
        return getByteSize()-bytesLeft;
    }

    public void setBytesLeft(int bytesLeft) {
        this.bytesLeft = bytesLeft;
    }

    public void dumpBytes() {
        for ( int n = 0; n < dataLen()-getBytesLeft(); n++ ) {
            System.out.print(" [" + n + "], " + data(n));
        }
        System.out.println("-");
    }
    @Override
    public String toString() {
        return "DataPacket{" +
                "seqNo=" + seqNo +
                ", retr=" + isRetrans +
                ", topic=" + topic +
                ", sender=" + sender +
                ", receiver=" + receiver +
                ", left=" + bytesLeft +
                ", datalen=" + dataLen() +
                ", dgsize="+getDGramSize()+
                ", decoded="+isDecoded()+
                '}';
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy