Search in sources :

Example 1 with NulsMessage

use of io.nuls.core.mesasge.NulsMessage 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 2 with NulsMessage

use of io.nuls.core.mesasge.NulsMessage in project nuls by nuls-io.

the class BroadcastHandler method broadcastToNode.

public BroadcastResult broadcastToNode(BaseEvent event, String nodeId, boolean asyn) {
    try {
        NulsMessage message = new NulsMessage(network.packetMagic(), event.serialize());
        Node node = nodesManager.getNode(nodeId);
        if (node == null) {
            return new BroadcastResult(false, "node not found");
        }
        return broadcast(message, node, asyn);
    } catch (IOException e) {
        return new BroadcastResult(false, "event.serialize() error");
    }
}
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 3 with NulsMessage

use of io.nuls.core.mesasge.NulsMessage in project nuls by nuls-io.

the class ConnectionManager method receiveMessage.

public void receiveMessage(ByteBuffer buffer, Node node) {
    List<NulsMessage> list;
    try {
        buffer.flip();
        if (!node.isAlive()) {
            buffer.clear();
            return;
        }
        list = new ArrayList<>();
        while (buffer.hasRemaining()) {
            NulsMessage message = new NulsMessage(buffer);
            list.add(message);
        }
        for (NulsMessage message : list) {
            if (MessageFilterChain.getInstance().doFilter(message)) {
                BaseEvent event = EventManager.getInstance(message.getData());
                MsgLog.info("get(" + node.getId() + "):\n" + Hex.encode(message.getHeader().serialize()) + "--" + Hex.encode(message.getData()));
                processMessage(event, node);
            }
        }
    } catch (NulsException e) {
        // todo
        Log.error(e);
    } catch (Exception e) {
        // todo
        Log.error(e);
        return;
    } finally {
        buffer.clear();
    }
}
Also used : NulsMessage(io.nuls.core.mesasge.NulsMessage) NulsException(io.nuls.core.exception.NulsException) BaseEvent(io.nuls.core.event.BaseEvent) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException)

Aggregations

NulsMessage (io.nuls.core.mesasge.NulsMessage)3 IOException (java.io.IOException)3 BroadcastResult (io.nuls.network.entity.BroadcastResult)2 Node (io.nuls.network.entity.Node)2 BaseEvent (io.nuls.core.event.BaseEvent)1 NulsException (io.nuls.core.exception.NulsException)1