Search in sources :

Example 11 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class Connection method decode.

/**
 * Decode received messages
 * @param buf received data from socket
 * @return    decoded message
 */
public Msg decode(ByteBuffer buf) {
    if (raw == null) {
        header.put(buf);
        if (header.remaining() != 0) {
            return null;
        }
        header.flip();
        raw = new Buffer(header.getVarInt() + header.position());
        header.rewind();
        raw.put(header);
        header.clear();
    }
    raw.put(buf);
    if (raw.remaining() == 0) {
        raw.flip();
        Buffer msg = raw;
        raw = null;
        return Msg.create(msg);
    }
    return null;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(tezc.base.common.Buffer)

Example 12 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class ClientResp method encode.

/**
 * Encode message
 */
@Override
public void encode() {
    if (!rawReady) {
        int headerLen = ProtoUtil.byteLen(TYPE) + ProtoUtil.booleanLen(result) + ProtoUtil.longLen(sequence);
        length = headerLen + data.remaining();
        headerLen += ProtoUtil.varIntLen(length);
        if (rawMsg == null) {
            rawMsg = new Buffer(headerLen);
        }
        rawMsg.clear();
        rawMsg.putVarInt(length);
        rawMsg.put(ClientResp.TYPE);
        rawMsg.putBoolean(result);
        rawMsg.putLong(sequence);
        rawMsg.flip();
        rawReady = true;
        assert (rawMsg.remaining() >= Msg.MIN_MSG_SIZE);
    }
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 13 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class ConnectReq method encode.

/**
 * Encode message
 */
@Override
public void encode() {
    if (!rawReady) {
        length = ProtoUtil.byteLen(ConnectReq.TYPE) + ProtoUtil.stringLen(clusterName) + ProtoUtil.stringLen(name) + ProtoUtil.booleanLen(client);
        if (rawMsg == null) {
            rawMsg = new Buffer(length + ProtoUtil.varIntLen(length));
        }
        rawMsg.clear();
        rawMsg.putVarInt(length);
        rawMsg.put(ConnectReq.TYPE);
        rawMsg.putString(clusterName);
        rawMsg.putString(name);
        rawMsg.putBoolean(client);
        rawMsg.flip();
        rawReady = true;
        assert (rawMsg.remaining() >= Msg.MIN_MSG_SIZE);
    }
}
Also used : Buffer(tezc.base.common.Buffer)

Example 14 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class JoinReq method encode.

/**
 * Encode JoinReq
 */
@Override
public void encode() {
    if (!rawReady) {
        length = 1 + record.encodedLen();
        if (rawMsg == null) {
            rawMsg = new Buffer(length + ProtoUtil.varIntLen(length));
        }
        rawMsg.clear();
        rawMsg.putVarInt(length);
        rawMsg.put(JoinReq.TYPE);
        record.encode(rawMsg);
        rawMsg.flip();
        rawReady = true;
        assert (rawMsg.remaining() >= Msg.MIN_MSG_SIZE);
    }
}
Also used : Buffer(tezc.base.common.Buffer)

Example 15 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class JoinResp method encode.

/**
 * Encode JoinResp
 */
@Override
public void encode() {
    if (!rawReady) {
        length = 1 + +ProtoUtil.booleanLen(result) + record.encodedLen();
        if (rawMsg == null) {
            rawMsg = new Buffer(length + ProtoUtil.varIntLen(length));
        }
        rawMsg.clear();
        rawMsg.putVarInt(length);
        rawMsg.put(JoinReq.TYPE);
        record.encode(rawMsg);
        rawMsg.putBoolean(result);
        rawMsg.flip();
        rawReady = true;
        assert (rawMsg.remaining() >= Msg.MIN_MSG_SIZE);
    }
}
Also used : Buffer(tezc.base.common.Buffer)

Aggregations

Buffer (tezc.base.common.Buffer)28 ByteBuffer (java.nio.ByteBuffer)16 FileChannel (java.nio.channels.FileChannel)2 Path (java.nio.file.Path)2 StandardOpenOption (java.nio.file.StandardOpenOption)2 Session (tezc.core.cluster.Session)2 ClusterRecord (tezc.core.record.ClusterRecord)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 State (tezc.State)1