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

org.tron.p2p.discover.message.kad.PongMessage Maven / Gradle / Ivy

The newest version!
package org.tron.p2p.discover.message.kad;

import com.google.protobuf.ByteString;
import org.tron.p2p.base.Parameter;
import org.tron.p2p.discover.Node;
import org.tron.p2p.discover.message.MessageType;
import org.tron.p2p.protos.Discover;
import org.tron.p2p.protos.Discover.Endpoint;
import org.tron.p2p.utils.ByteArray;
import org.tron.p2p.utils.NetUtil;

public class PongMessage extends KadMessage {
  private Discover.PongMessage pongMessage;

  public PongMessage(byte[] data) throws Exception {
    super(MessageType.KAD_PONG, data);
    this.pongMessage = Discover.PongMessage.parseFrom(data);
  }

  public PongMessage(Node from) {
    super(MessageType.KAD_PONG, null);
    Endpoint toEndpoint = Endpoint.newBuilder()
        .setAddress(ByteString.copyFrom(ByteArray.fromString(from.getHost())))
        .setPort(from.getPort())
        .setNodeId(ByteString.copyFrom(from.getId()))
        .build();
    this.pongMessage = Discover.PongMessage.newBuilder()
        .setFrom(toEndpoint)
        .setEcho(Parameter.p2pConfig.getVersion())
        .setTimestamp(System.currentTimeMillis())
        .build();
    this.data = this.pongMessage.toByteArray();
  }

  public int getVersion() {
    return this.pongMessage.getEcho();
  }

  @Override
  public long getTimestamp() {
    return this.pongMessage.getTimestamp();
  }

  @Override
  public Node getFrom() {
    return NetUtil.getNode(pongMessage.getFrom());
  }

  @Override
  public String toString() {
    return "[pongMessage: " + pongMessage;
  }

  @Override
  public boolean valid() {
    return NetUtil.validNode(getFrom());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy