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

org.tron.p2p.discover.protocol.kad.table.NodeBucket Maven / Gradle / Ivy

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NodeBucket {
  private final int depth;
  private List nodes = new ArrayList<>();

  NodeBucket(int depth) {
    this.depth = depth;
  }

  public int getDepth() {
    return depth;
  }

  public synchronized NodeEntry addNode(NodeEntry e) {
    if (!nodes.contains(e)) {
      if (nodes.size() >= KademliaOptions.BUCKET_SIZE) {
        return getLastSeen();
      } else {
        nodes.add(e);
      }
    }

    return null;
  }

  private NodeEntry getLastSeen() {
    List sorted = nodes;
    Collections.sort(sorted, new TimeComparator());
    return sorted.get(0);
  }

  public synchronized void dropNode(NodeEntry entry) {
    for (NodeEntry e : nodes) {
      if (e.getId().equals(entry.getId())) {
        nodes.remove(e);
        break;
      }
    }
  }

  public int getNodesCount() {
    return nodes.size();
  }

  public List getNodes() {
    return nodes;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy