use of org.aion.p2p.INode in project aion by aionnetwork.
the class NodeMgrTest method testDumpNodeInfo.
@Test(timeout = 30_000)
public void testDumpNodeInfo() {
String dump = nMgr.dumpNodeInfo("testId", false);
assertNotNull(dump);
INode node = nMgr.allocNode(ip1, 1);
addNodetoOutbound(node, UUID.randomUUID());
nMgr.movePeerToActive(node.getIdHash(), "outbound");
String dump2 = nMgr.dumpNodeInfo("testId", false);
assertEquals(dump.length(), dump2.length());
String dump3 = nMgr.dumpNodeInfo("testId", true);
assertTrue(dump3.length() > dump2.length());
}
use of org.aion.p2p.INode in project aion by aionnetwork.
the class NodeMgrTest method testMoveInboundToActive.
@Test(timeout = 30_000)
public void testMoveInboundToActive() {
INode node = nMgr.allocNode(ip2, 1);
addNodetoInbound(node, UUID.fromString(nodeId1));
nMgr.movePeerToActive(node.getChannel().hashCode(), "inbound");
assertNull(nMgr.getInboundNode(node.getChannel().hashCode()));
INode activeNode = nMgr.getActiveNode(node.getIdHash());
assertNotNull(activeNode);
assertEquals(node, activeNode);
}
use of org.aion.p2p.INode in project aion by aionnetwork.
the class NodeMgr method movePeerToActive.
public void movePeerToActive(int _hash, String _type) {
Map<Integer, INode> nodes = (_type.contentEquals("inbound")) ? inboundNodes : outboundNodes;
INode node = nodes.remove(_hash);
if (node != null) {
if (p2pLOG.isTraceEnabled()) {
p2pLOG.trace("<movePeerToActive: {} {}>", _type, node.toString());
}
// noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (node) {
if (activeNodes.size() >= maxActiveNodes) {
p2pMgr.closeSocket(node.getChannel(), _type + " -> active, active full");
return;
}
if (p2pMgr.isSelf(node)) {
p2pMgr.closeSocket(node.getChannel(), _type + " -> active, self-connected");
return;
}
node.setConnection(_type);
node.setFromBootList(seedIps.contains(node.getIpStr()));
INode previous = activeNodes.putIfAbsent(node.getIdHash(), node);
if (previous != null) {
p2pMgr.closeSocket(node.getChannel(), _type + " -> active, node " + previous.getIdShort() + " exits");
} else if (!activeIpAllow(node.getIpStr())) {
p2pMgr.closeSocket(node.getChannel(), _type + " -> active, ip " + node.getIpStr() + " exits");
}
if (p2pLOG.isDebugEnabled()) {
p2pLOG.debug("<{} -> active node-id={} ip={}>", _type, node.getIdShort(), node.getIpStr());
}
}
} else {
if (p2pLOG.isTraceEnabled()) {
p2pLOG.trace("<movePeerToActive empty {} {}>", _type, _hash);
}
}
}
use of org.aion.p2p.INode in project aion by aionnetwork.
the class NodeMgr method timeoutActive.
private void timeoutActive(long now) {
OptionalDouble average = activeNodes.values().stream().mapToLong(n -> now - n.getTimestamp()).average();
this.avgLatency = (int) average.orElse(0);
long timeout = ((long) average.orElse(4000)) * 5;
timeout = Math.max(MIN_TIMEOUT_ACTIVE_NODES, Math.min(timeout, MAX_TIMEOUT_ACTIVE_NODES));
if (p2pLOG.isDebugEnabled()) {
p2pLOG.debug("<average-delay={}ms>", this.avgLatency);
}
try {
Iterator<Map.Entry<Integer, INode>> it = activeNodes.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, INode> entry = it.next();
INode node = entry.getValue();
if (now - node.getTimestamp() > timeout) {
p2pMgr.closeSocket(node.getChannel(), "active-timeout ip=" + node.getIpStr());
it.remove();
} else if (!node.getChannel().isConnected()) {
p2pMgr.closeSocket(node.getChannel(), "channel-already-closed node=" + node.getIdShort() + " ip=" + node.getIpStr());
it.remove();
}
}
} catch (IllegalStateException e) {
p2pLOG.info("<timeoutActive IllegalStateException>", e);
}
}
use of org.aion.p2p.INode in project aion by aionnetwork.
the class ResActiveNodesTest method testEncodeDecode.
@Test
public void testEncodeDecode() {
int m = ThreadLocalRandom.current().nextInt(0, 20);
List<INode> srcNodes = new ArrayList<>();
for (int i = 0; i < m; i++) {
srcNodes.add(randomNode());
}
ResActiveNodes res = ResActiveNodes.decode(new ResActiveNodes(p2pLOG, srcNodes).encode(), p2pLOG);
assertEquals(res.getNodes().size(), m);
List<INode> tarNodes = res.getNodes();
for (int i = 0; i < m; i++) {
INode srcNode = srcNodes.get(i);
INode tarNode = tarNodes.get(i);
assertArrayEquals(srcNode.getId(), tarNode.getId());
assertEquals(srcNode.getIdHash(), tarNode.getIdHash());
assertArrayEquals(srcNode.getIp(), tarNode.getIp());
assertEquals(srcNode.getIpStr(), tarNode.getIpStr());
assertEquals(srcNode.getPort(), tarNode.getPort());
}
}
Aggregations