org.tron.p2p.discover.message.kad.FindNodeMessage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of libp2p Show documentation
Show all versions of libp2p Show documentation
libp2p is a p2p network SDK implemented in java language.
The newest version!
package org.tron.p2p.discover.message.kad;
import com.google.protobuf.ByteString;
import org.tron.p2p.base.Constant;
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 FindNodeMessage extends KadMessage {
private Discover.FindNeighbours findNeighbours;
public FindNodeMessage(byte[] data) throws Exception {
super(MessageType.KAD_FIND_NODE, data);
this.findNeighbours = Discover.FindNeighbours.parseFrom(data);
}
public FindNodeMessage(Node from, byte[] targetId) {
super(MessageType.KAD_FIND_NODE, null);
Endpoint fromEndpoint = Endpoint.newBuilder()
.setAddress(ByteString.copyFrom(ByteArray.fromString(from.getHost())))
.setPort(from.getPort())
.setNodeId(ByteString.copyFrom(from.getId()))
.build();
this.findNeighbours = Discover.FindNeighbours.newBuilder()
.setFrom(fromEndpoint)
.setTargetId(ByteString.copyFrom(targetId))
.setTimestamp(System.currentTimeMillis())
.build();
this.data = this.findNeighbours.toByteArray();
}
public byte[] getTargetId() {
return this.findNeighbours.getTargetId().toByteArray();
}
@Override
public long getTimestamp() {
return this.findNeighbours.getTimestamp();
}
@Override
public Node getFrom() {
return NetUtil.getNode(findNeighbours.getFrom());
}
@Override
public String toString() {
return "[findNeighbours: " + findNeighbours;
}
@Override
public boolean valid() {
return NetUtil.validNode(getFrom())
&& getTargetId().length == Constant.NODE_ID_LEN;
}
}