Search in sources :

Example 11 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class ClientChannelHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    String channelId = ctx.channel().id().asLongText();
    SocketChannel channel = (SocketChannel) ctx.channel();
    Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
    // check node exist
    if (node == null || (node != null && node.getStatus() != Node.WAIT)) {
        ctx.channel().close();
        return;
    }
    NioChannelMap.add(channelId, channel);
    node.setChannelId(channelId);
    node.setStatus(Node.CONNECT);
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node)

Example 12 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class ServerChannelHandler method channelRegistered.

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    SocketChannel socketChannel = (SocketChannel) ctx.channel();
    String remoteIP = socketChannel.remoteAddress().getHostString();
    Node node = getNetworkService().getNode(remoteIP);
    if (node != null) {
        if (node.getStatus() == Node.CONNECT) {
            ctx.channel().close();
            return;
        }
        // When nodes try to connect to each other but not connected, select one of the smaller IP addresses as the server
        if (node.getType() == Node.OUT) {
            String localIP = InetAddress.getLocalHost().getHostAddress();
            boolean isLocalServer = IpUtil.judgeIsLocalServer(localIP, remoteIP);
            if (!isLocalServer) {
                ctx.channel().close();
                return;
            } else {
                getNetworkService().removeNode(remoteIP);
            }
        }
    }
    NodeGroup group = getNetworkService().getNodeGroup(NetworkConstant.NETWORK_NODE_IN_GROUP);
    if (group.size() > getNetworkService().getNetworkParam().maxInCount()) {
        ctx.channel().close();
        return;
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node) NodeGroup(io.nuls.network.entity.NodeGroup)

Example 13 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class ServerChannelHandler method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    Log.debug("----------------------server channelInactive ------------------------- ");
    SocketChannel channel = (SocketChannel) ctx.channel();
    String channelId = ctx.channel().id().asLongText();
    NioChannelMap.remove(channelId);
    Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
    if (node != null && channelId.equals(node.getChannelId())) {
        getNetworkService().removeNode(channel.remoteAddress().getHostString());
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node)

Example 14 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class BroadcastHandler method broadcastToList.

// 
// /**
// * At least maxOutCount() Nodes should be broadcast success
// *
// * @param message
// * @param excludeNodeId
// * @return
// */
// private BroadcastResult broadcast(NulsMessage message, String excludeNodeId) {
// //        List<Node> broadNodes = nodesManager.getAvailableNodes(excludeNodeId);
// //        //todo only one node connected can't send message
// //        if (broadNodes.size() < NetworkConstant.NETWORK_BROAD_SUCCESS_MIN_COUNT) {
// //            return new BroadcastResult(false, "no node can be broadcast");
// //        }
// //
// //        int successCount = 0;
// //        int count = 0;
// //        BroadcastResult result = new BroadcastResult(true, "OK");
// //        List<Node> successNodes = new ArrayList<>();
// //
// //        while (successCount < network.maxOutCount() && count < NetworkConstant.NETWORK_BROAD_MAX_TRY_COUNT) {
// //            for (Node node : broadNodes) {
// //                if (successNodes.contains(node)) {
// //                    continue;
// //                }
// //                try {
// //                    node.sendMessage(message);
// //                    successNodes.add(node);
// //                    successCount++;
// //                } catch (NotYetConnectedException | IOException e) {
// //                    Log.warn("broadcast message error , maybe the node closed ! node ip :{}, {}", node.getIp(), e.getMessage());
// //                }
// //            }
// //            if (successCount < network.maxOutCount()) {
// //                try {
// //                    Thread.sleep(3000);
// //                } catch (InterruptedException e) {
// //                    Log.error(e);
// //                }
// //                broadNodes = nodesManager.getAvailableNodes(excludeNodeId);
// //            }
// //            count++;
// //        }
// //
// //        Log.debug("成功广播给{}个节点,消息{}", successCount, message);
// //        result.setBroadcastNodes(successNodes);
// //        return result;
// return null;
// }
// 
// /**
// * Broadcast half of the nodes, waiting for the other half to reply
// *
// * @param message
// * @return
// */
// private BroadcastResult broadcastSync(NulsMessage message, String excludeNodeId) {
// //        List<Node> broadNodes = nodesManager.getAvailableNodes(excludeNodeId);
// //
// //        if (broadNodes.size() <= 0) {
// //            return new BroadcastResult(false, "no node can be broadcast");
// //        }
// //
// //        int numConnected = broadNodes.size();
// //        int numToBroadcastTo = (int) Math.max(1, Math.round(Math.ceil(broadNodes.size() / 2.0)));
// //        Collections.shuffle(broadNodes);
// //        broadNodes = broadNodes.subList(0, numToBroadcastTo);
// //
// //        int successCount = 0;
// //        for (Node node : broadNodes) {
// //            try {
// //                node.sendMessage(message);
// //                successCount++;
// //            } catch (IOException e) {
// //                broadNodes.remove(node);
// //                Log.warn("broadcast message error , maybe the node closed ! node ip :{}, {}", node.getIp(), e.getMessage());
// //            }
// //        }
// //        if (successCount == 0) {
// //            return new BroadcastResult(false, "broadcast fail");
// //        }
// //
// //        BroadcastResult result = new BroadcastResult(true, "OK");
// //        result.setHash(Sha256Hash.twiceOf(message.getData()).toString());
// //        result.setBroadcastNodes(broadNodes);
// //        result.setWaitReplyCount(numConnected - numToBroadcastTo);
// //        NetworkCacheService.getInstance().addBroadCastResult(result);
// 
// return null;
// }
// 
// 
// private BroadcastResult broadcastToNode(NulsMessage message, String nodeId) {
// //        Node node = nodesManager.getNode(nodeId);
// //        if (node == null || node.getStatus() != Node.HANDSHAKE) {
// //            return new BroadcastResult(false, "no node can be broadcast");
// //        }
// //        try {
// //            node.sendMessage(message);
// //        } catch (NotYetConnectedException | IOException e) {
// //            Log.warn("broadcast message error , maybe the node closed ! node ip :{}, {}", node.getIp(), e.getMessage());
// //            return new BroadcastResult(false, "broadcast fail");
// //        }
// //        List<Node> broadNodes = new ArrayList<>();
// //        broadNodes.add(node);
// //        return new BroadcastResult(true, "OK", broadNodes);
// return null;
// }
// 
// private BroadcastResult broadcastToGroup(NulsMessage message,String areaName, String groupName, String excludeNodeId) {
// //        List<Node> broadNodes = nodesManager.getGroupAvailableNodes(areaName, groupName, excludeNodeId);
// //        if (broadNodes.size() <= 1) {
// //            return new BroadcastResult(false, "no node can be broadcast");
// //        }
// //
// //        int successCount = 0;
// //        for (Node node : broadNodes) {
// //            try {
// //                node.sendMessage(message);
// //                successCount++;
// //            } catch (NotYetConnectedException | IOException e) {
// //                Log.warn("broadcast message error , maybe the node closed ! node ip :{}, {}", node.getIp(), e.getMessage());
// //            }
// //        }
// //
// //        if (successCount == 0) {
// //            new BroadcastResult(false, "broadcast fail", broadNodes);
// //        }
// //        Log.debug("成功广播给{}个节点,消息{}", successCount, message);
// return new BroadcastResult(true, "OK");
// }
// 
// private BroadcastResult broadcastToGroup(NulsMessage message, String groupName, String excludeNodeId) {
// return  broadcastToGroup(message,null,groupName,excludeNodeId);
// }
// 
// 
// private BroadcastResult broadcastToGroupSync(NulsMessage message, String groupName, String excludeNodeId) {
// 
// return null;
// }
// 
// 
// @Override
// public BroadcastResult broadcast(BaseEvent event) {
// return broadcast(event, null);
// }
// 
// @Override
// public BroadcastResult broadcast(BaseEvent event, String excludeNodeId) {
// NulsMessage message = null;
// try {
// message = new NulsMessage(network.packetMagic(), event.serialize());
// } catch (IOException e) {
// return new BroadcastResult(false, "event.serialize() error");
// }
// 
// return broadcast(message, excludeNodeId);
// }
// 
// 
// 
// 
// 
// public BroadcastResult broadcastSync(BaseEvent event) {
// return broadcastSync(event, null);
// }
// 
// public BroadcastResult broadcastSync(BaseEvent event, String excludeNodeId) {
// NulsMessage message = null;
// try {
// message = new NulsMessage(network.packetMagic(), event.serialize());
// } catch (IOException e) {
// return new BroadcastResult(false, "event.serialize() error");
// }
// 
// return broadcastSync(message, excludeNodeId);
// }
// 
// public BroadcastResult broadcastSync(byte[] data) {
// return broadcastSync(data, null);
// }
// 
// public BroadcastResult broadcastSync(byte[] data, String excludeNodeId) {
// NulsMessage message = new NulsMessage(network.packetMagic(), data);
// return broadcastSync(message, excludeNodeId);
// }
// 
// @Override
// public BroadcastResult broadcastToNode(BaseEvent event, String nodeId) {
// NulsMessage message = null;
// try {
// message = new NulsMessage(network.packetMagic(), event.serialize());
// } catch (IOException e) {
// return new BroadcastResult(false, "event.serialize() error");
// }
// return broadcastToNode(message, nodeId);
// }
// 
// @Override
// public BroadcastResult broadcastToNode(BaseEvent event, Node node) {
// return null;
// }
// 
// @Override
// public BroadcastResult broadcastToGroup(BaseEvent event, String groupName) {
// return broadcastToGroup(event, groupName, null);
// }
// 
// @Override
// public BroadcastResult broadcastToGroup(BaseEvent event, String groupName, String excludeNodeId) {
// return broadcastToGroup(event,null ,groupName, excludeNodeId);
// }
private BroadcastResult broadcastToList(List<Node> nodeList, BaseEvent event, String excludeNodeId, boolean asyn) {
    NulsMessage message;
    BroadcastResult result = new BroadcastResult();
    try {
        message = new NulsMessage(network.packetMagic(), event.serialize());
        int successCount = 0;
        for (Node node : nodeList) {
            if (excludeNodeId != null && node.getId().equals(excludeNodeId)) {
                continue;
            }
            BroadcastResult br = broadcast(message, node, asyn);
            if (br.isSuccess()) {
                successCount++;
                result.getBroadcastNodes().add(node);
            }
        }
        if (successCount == 0) {
            return new BroadcastResult(false, "send message failed");
        }
    } catch (IOException e) {
        return new BroadcastResult(false, "event.serialize() error");
    }
    result.setSuccess(true);
    result.setMessage("OK");
    return result;
}
Also used : NulsMessage(io.nuls.core.mesasge.NulsMessage) BroadcastResult(io.nuls.network.entity.BroadcastResult) Node(io.nuls.network.entity.Node) IOException(java.io.IOException)

Example 15 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class GetNodeEventHandler method process.

@Override
public NetworkEventResult process(BaseEvent event, Node node) {
    GetNodeEvent getNodeEvent = (GetNodeEvent) event;
    // String key = event.getHeader().getEventType() + "-" + node.getIp();
    // if (cacheService.existEvent(key)) {
    // getNetworkService().removeNode(node.getId());
    // return null;
    // }
    // cacheService.putEvent(key, event, false);
    List<Node> list = getAvailableNodes(getNodeEvent.getLength(), node.getId());
    NodeEvent replyEvent = new NodeEvent(list);
    return new NetworkEventResult(true, replyEvent);
}
Also used : GetNodeEvent(io.nuls.network.message.entity.GetNodeEvent) NodeEvent(io.nuls.network.message.entity.NodeEvent) Node(io.nuls.network.entity.Node) GetNodeEvent(io.nuls.network.message.entity.GetNodeEvent) NetworkEventResult(io.nuls.network.message.NetworkEventResult)

Aggregations

Node (io.nuls.network.entity.Node)25 SocketChannel (io.netty.channel.socket.SocketChannel)8 NodeGroup (io.nuls.network.entity.NodeGroup)5 ArrayList (java.util.ArrayList)4 BroadcastResult (io.nuls.network.entity.BroadcastResult)3 ByteBuf (io.netty.buffer.ByteBuf)2 NulsMessage (io.nuls.core.mesasge.NulsMessage)2 NetworkEventResult (io.nuls.network.message.NetworkEventResult)2 GetNodeEvent (io.nuls.network.message.entity.GetNodeEvent)2 GetNodesIpEvent (io.nuls.network.message.entity.GetNodesIpEvent)2 NodeEvent (io.nuls.network.message.entity.NodeEvent)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Block (io.nuls.core.chain.entity.Block)1 NodePo (io.nuls.db.entity.NodePo)1 GetVersionEvent (io.nuls.network.message.entity.GetVersionEvent)1 NodesIpEvent (io.nuls.network.message.entity.NodesIpEvent)1 InfoDto (io.nuls.rpc.entity.InfoDto)1 RpcResult (io.nuls.rpc.entity.RpcResult)1 HashSet (java.util.HashSet)1