use of io.nuls.network.model.BroadcastResult in project nuls by nuls-io.
the class MessageBusServiceImpl method getNodeIdListResult.
private Result<List<String>> getNodeIdListResult(BroadcastResult result) {
List<String> list = new ArrayList<>();
if (!result.isSuccess() || result.getBroadcastNodes() == null || result.getBroadcastNodes().isEmpty()) {
return Result.getFailed(result.getErrorCode()).setData(list);
}
for (Node node : result.getBroadcastNodes()) {
list.add(node.getId());
}
Result rs = new Result();
rs.setSuccess(true);
rs.setData(list);
return rs;
}
use of io.nuls.network.model.BroadcastResult in project nuls by nuls-io.
the class BroadcastHandler method broadcastToANode.
public BroadcastResult broadcastToANode(BaseMessage message, Node node, boolean asyn) {
if (!node.isAlive()) {
return new BroadcastResult(false, NetworkErrorCode.NET_NODE_DEAD);
}
if (node.getChannel() == null || !node.getChannel().isActive()) {
return new BroadcastResult(false, NetworkErrorCode.NET_NODE_MISS_CHANNEL);
}
try {
MessageHeader header = message.getHeader();
header.setMagicNumber(networkParam.getPacketMagic());
BaseNulsData body = message.getMsgBody();
header.setLength(body.size());
if (asyn) {
node.getChannel().eventLoop().execute(() -> {
try {
Channel channel = node.getChannel();
if (channel != null) {
channel.writeAndFlush(Unpooled.wrappedBuffer(message.serialize()));
}
} catch (IOException e) {
e.printStackTrace();
}
});
} else {
ChannelFuture future = node.getChannel().writeAndFlush(Unpooled.wrappedBuffer(message.serialize()));
future.await();
boolean success = future.isSuccess();
if (!success) {
return new BroadcastResult(false, NetworkErrorCode.NET_BROADCAST_FAIL);
}
}
} catch (Exception e) {
Log.error(e);
return new BroadcastResult(false, NetworkErrorCode.NET_MESSAGE_ERROR);
}
return new BroadcastResult(true, KernelErrorCode.SUCCESS);
}
use of io.nuls.network.model.BroadcastResult in project nuls by nuls-io.
the class BroadcastHandler method broadcastToHalfNode.
public BroadcastResult broadcastToHalfNode(BaseMessage msg, Node excludeNode, boolean asyn) {
Collection<Node> nodes = nodeManager.getAvailableNodes();
if (nodes.isEmpty()) {
return new BroadcastResult(false, NetworkErrorCode.NET_BROADCAST_NODE_EMPTY);
}
List<Node> nodeList = new ArrayList<>();
int i = 0;
for (Node node : nodes) {
i++;
if (i % 2 == 1) {
nodeList.add(node);
}
}
return broadcastToList(nodeList, msg, excludeNode, asyn, 50);
}
use of io.nuls.network.model.BroadcastResult 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