private boolean checkIsSurvive(Node node) { if (node.getChannel() == null || node.getStatus() != NodeStatusEnum.CONNECTABLE || node.getConnectStatus() != NodeConnectStatusEnum.AVAILABLE) { if(node.getChannel() != null) { node.getChannel().close(); } return false; } return true; } }
public void removeNode(String nodeId) { Log.info("-------------remove node:" + nodeId); Node node = getNode(nodeId); if (node.getChannel() != null) { node.getChannel().close(); } else { nodesContainer.getConnectedNodes().remove(nodeId); } }
public boolean start() { try { ChannelFuture future = boot.connect(node.getIp(), node.getPort()); future.await(); return future.isSuccess(); } catch (Exception e) { Log.error("{}", e); if (node.getChannel() != null) { node.getChannel().close(); } return false; } }
return new BroadcastResult(false, NetworkErrorCode.NET_NODE_DEAD); if (node.getChannel() == null || !node.getChannel().isActive()) { return new BroadcastResult(false, NetworkErrorCode.NET_NODE_MISS_CHANNEL); node.getChannel().eventLoop().execute(() -> { try { Channel channel = node.getChannel(); if (channel != null) { channel.writeAndFlush(Unpooled.wrappedBuffer(message.serialize())); ChannelFuture future = node.getChannel().writeAndFlush(Unpooled.wrappedBuffer(message.serialize())); future.await(); boolean success = future.isSuccess();
private int doProbe(Node node) { if(node == null) { return PROBE_STATUS_FAIL; } CompletableFuture<Integer> future = new CompletableFuture<>(); node.setConnectedListener(() -> { node.setConnectStatus(NodeConnectStatusEnum.CONNECTED); node.getChannel().close(); }); node.setDisconnectListener(() -> { node.setChannel(null); if (node.getConnectStatus() == NodeConnectStatusEnum.CONNECTED) { future.complete(PROBE_STATUS_SUCCESS); } else if (nodeManager.getAvailableNodesCount() == 0) { future.complete(PROBE_STATUS_IGNORE); } else { future.complete(PROBE_STATUS_FAIL); } }); boolean result = connectionManager.connection(node); if (!result) { return PROBE_STATUS_FAIL; } try { return future.get(); } catch (Exception e) { Log.error(e); return PROBE_STATUS_IGNORE; } }
private void shareMyServer() { String externalIp = getMyExtranetIp(); if (externalIp == null) { return; } Log.info("my external ip is {}" , externalIp); networkParam.getLocalIps().add(externalIp); Node myNode = new Node(externalIp, networkParam.getPort(), Node.OUT); myNode.setConnectedListener(() -> { myNode.getChannel().close(); doShare(externalIp); }); myNode.setDisconnectListener(() -> myNode.setChannel(null)); connectionManager.connection(myNode); }
public void nodeConnectDisconnect(Node node) { if (node.getChannel() != null) { node.setChannel(null); } //连接断开后,判断是否是为连接成功,还是连接成功后断开 if (node.getConnectStatus() == NodeConnectStatusEnum.CONNECTED || node.getConnectStatus() == NodeConnectStatusEnum.AVAILABLE) { node.setFailCount(0); node.setConnectStatus(NodeConnectStatusEnum.DISCONNECT); nodesContainer.getDisconnectNodes().put(node.getId(), node); nodesContainer.getConnectedNodes().remove(node.getId()); // Log.info("node {} disconnect !", node.getId()); } else { // 如果是未连接成功,标记为连接失败,失败次数+1,记录当前失败时间,供下次尝试连接使用 nodeConnectFail(node); nodesContainer.getCanConnectNodes().remove(node.getId()); nodesContainer.getFailNodes().put(node.getId(), node); } }
private void handlerMessage(List<BaseMessage> messageList, Node node) { for (BaseMessage message : messageList) { if (MessageFilterChain.getInstance().doFilter(message)) { MessageHeader header = message.getHeader(); if (node.getMagicNumber() == 0L) { node.setMagicNumber(header.getMagicNumber()); } handler(message, node); } else { node.getChannel().close(); if(node.getDisconnectListener() != null) { node.getDisconnectListener().action(); } } } }