use of io.nuls.network.model.Node in project nuls by nuls-io.
the class ServerChannelHandler method channelUnregistered.
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
super.channelUnregistered(ctx);
SocketChannel channel = (SocketChannel) ctx.channel();
String nodeId = IpUtil.getNodeId(channel.remoteAddress());
Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId));
Node node = nodeAttribute.get();
if (node != null && node.getDisconnectListener() != null) {
node.getDisconnectListener().action();
}
// channelUnregistered之前,channel就已经close了,可以调用channel.isOpen()查看状态
// channel.close();
}
use of io.nuls.network.model.Node in project nuls by nuls-io.
the class NodesContainer method addNeedCheckNode.
public boolean addNeedCheckNode(Node newNode) {
String nodeId = newNode.getId();
Node node = uncheckNodes.get(nodeId);
if (node != null) {
return false;
}
node = canConnectNodes.get(nodeId);
if (node != null) {
return false;
}
node = connectedNodes.get(nodeId);
if (node != null) {
return false;
}
node = disconnectNodes.get(nodeId);
if (node != null) {
return false;
}
node = failNodes.get(nodeId);
if (node != null) {
return false;
}
newNode.setLastProbeTime(0L);
uncheckNodes.put(nodeId, newNode);
newNode.setStatus(NodeStatusEnum.UNCHECK);
return true;
}
use of io.nuls.network.model.Node in project nuls by nuls-io.
the class NodeManager method nodeConnectIn.
public boolean nodeConnectIn(String ip, int port, SocketChannel channel) {
if (!canConnectIn(ip, port)) {
return false;
}
Node node = new Node(ip, port, Node.IN);
node.setConnectStatus(NodeConnectStatusEnum.CONNECTED);
node.setChannel(channel);
cacheNode(node, channel);
nodesContainer.getConnectedNodes().put(node.getId(), node);
nodesContainer.markCanuseNodeByIp(ip, NodeStatusEnum.AVAILABLE);
// 监听被动连接的断开
node.setDisconnectListener(() -> {
// Log.info("------------in node disconnect:" + node.getId());
nodesContainer.getConnectedNodes().remove(node.getId());
nodesContainer.markCanuseNodeByIp(ip, NodeStatusEnum.CONNECTABLE);
});
sendHandshakeMessage(node, NetworkConstant.HANDSHAKE_SEVER_TYPE);
return true;
}
use of io.nuls.network.model.Node in project nuls by nuls-io.
the class MessageBusServiceImplTest method receiveMessage.
@Test
public void receiveMessage() {
BlockMessage blockMessage = new BlockMessage();
Node node = new Node("192.168.1.90", 8003, 1);
messageBusService.receiveMessage(blockMessage, node);
}
use of io.nuls.network.model.Node in project nuls by nuls-io.
the class BroadcastHandler method broadcastToList.
private BroadcastResult broadcastToList(Collection<Node> nodeList, BaseMessage message, Node excludeNode, boolean asyn, int percent) {
BroadcastResult result = new BroadcastResult();
try {
int successCount = 0;
int minCount = 5;
// 根据百分比决定直接广播给多少个节点
if (nodeList.size() > minCount && percent < 100) {
int needCount = nodeList.size() * percent / 100;
if (needCount < minCount) {
needCount = minCount;
}
Set<Integer> set = new HashSet<>();
while (true) {
Random rand = new Random();
int ran = rand.nextInt(nodeList.size());
set.add(ran);
if (set.size() == needCount + 1) {
break;
}
}
int nodeListIndex = 0;
Collection<Node> nodeBroadcastList = new ArrayList<>();
for (Node node : nodeList) {
if (set.contains(nodeListIndex)) {
if (excludeNode != null && node.getId().equals(excludeNode.getId())) {
nodeListIndex++;
continue;
}
nodeBroadcastList.add(node);
if (nodeBroadcastList.size() == needCount) {
break;
}
}
nodeListIndex++;
}
nodeList = nodeBroadcastList;
}
for (Node node : nodeList) {
if (excludeNode != null && node.getId().equals(excludeNode.getId())) {
continue;
}
BroadcastResult br = broadcastToNode(message, node, asyn);
if (br.isSuccess()) {
successCount++;
result.getBroadcastNodes().add(node);
} else if (br.getErrorCode().equals(NetworkErrorCode.NET_MESSAGE_ERROR)) {
return br;
}
}
if (successCount == 0) {
return new BroadcastResult(false, NetworkErrorCode.NET_BROADCAST_FAIL);
}
} catch (Exception e) {
return new BroadcastResult(false, NetworkErrorCode.NET_MESSAGE_ERROR);
}
result.setSuccess(true);
result.setErrorCode(KernelErrorCode.SUCCESS);
return result;
}
Aggregations