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

io.permazen.kv.raft.msg.AbstractPingMessage Maven / Gradle / Ivy


/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package io.permazen.kv.raft.msg;

import com.google.common.base.Preconditions;

import io.permazen.kv.raft.Timestamp;

import java.nio.ByteBuffer;

abstract class AbstractPingMessage extends Message {

    private final Timestamp timestamp;

// Constructors

    /**
     * Constructor.
     *
     * @param timestamp request timestamp
     */
    AbstractPingMessage(byte type, int clusterId, String senderId, String recipientId, long term, Timestamp timestamp) {
        super(type, clusterId, senderId, recipientId, term);
        this.timestamp = timestamp;
    }

    AbstractPingMessage(byte type, ByteBuffer buf, int version) {
        super(type, buf, version);
        this.timestamp = Message.getTimestamp(buf, version);
    }

    @Override
    void checkArguments() {
        super.checkArguments();
        Preconditions.checkArgument(this.timestamp != null);
    }

// Properties

    /**
     * Get the ping request timestamp.
     *
     * @return ping request timestamp
     */
    public Timestamp getTimestamp() {
        return this.timestamp;
    }

// Message

    @Override
    public void writeTo(ByteBuffer dest, int version) {
        super.writeTo(dest, version);
        Message.putTimestamp(dest, this.timestamp, version);
    }

    @Override
    protected int calculateSize(int version) {
        return super.calculateSize(version)
          + Message.calculateSize(this.timestamp, version);
    }

// Object

    @Override
    public String toString() {
        return this.getClass().getSimpleName()
          + "[\"" + this.getSenderId() + "\"->\"" + this.getRecipientId() + "\""
          + ",clusterId=" + String.format("%08x", this.getClusterId())
          + ",term=" + this.getTerm()
          + ",timestamp=" + this.timestamp
          + "]";
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy