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

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

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

import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import org.tron.p2p.discover.Node;
import org.tron.p2p.discover.message.MessageType;
import org.tron.p2p.discover.protocol.kad.table.KademliaOptions;
import org.tron.p2p.protos.Discover;
import org.tron.p2p.protos.Discover.Endpoint;
import org.tron.p2p.protos.Discover.Neighbours;
import org.tron.p2p.protos.Discover.Neighbours.Builder;
import org.tron.p2p.utils.ByteArray;
import org.tron.p2p.utils.NetUtil;

public class NeighborsMessage extends KadMessage {

  private Discover.Neighbours neighbours;

  public NeighborsMessage(byte[] data) throws Exception {
    super(MessageType.KAD_NEIGHBORS, data);
    this.neighbours = Discover.Neighbours.parseFrom(data);
  }

  public NeighborsMessage(Node from, List neighbours, long sequence) {
    super(MessageType.KAD_NEIGHBORS, null);
    Builder builder = Neighbours.newBuilder()
        .setTimestamp(sequence);

    neighbours.forEach(neighbour -> {
      Endpoint endpoint = Endpoint.newBuilder()
          .setAddress(ByteString.copyFrom(ByteArray.fromString(neighbour.getHost())))
          .setPort(neighbour.getPort())
          .setNodeId(ByteString.copyFrom(neighbour.getId()))
          .build();

      builder.addNeighbours(endpoint);
    });

    Endpoint fromEndpoint = Endpoint.newBuilder()
        .setAddress(ByteString.copyFrom(ByteArray.fromString(from.getHost())))
        .setPort(from.getPort())
        .setNodeId(ByteString.copyFrom(from.getId()))
        .build();

    builder.setFrom(fromEndpoint);

    this.neighbours = builder.build();

    this.data = this.neighbours.toByteArray();
  }

  public List getNodes() {
    List nodes = new ArrayList<>();
    neighbours.getNeighboursList().forEach(n -> nodes.add(NetUtil.getNode(n)));
    return nodes;
  }

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

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

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

  @Override
  public boolean valid() {
    if (!NetUtil.validNode(getFrom())) {
      return false;
    }
    if (getNodes().size() > 0) {
      if (getNodes().size() > KademliaOptions.BUCKET_SIZE) {
        return false;
      }
      for (Node node : getNodes()) {
        if (!NetUtil.validNode(node)) {
          return false;
        }
      }
    }
    return true;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy