use of io.nuls.network.entity.Node in project nuls by nuls-io.
the class NodesManager method start.
/**
* get nodes from database
* connect other nodes
* running ping/pong thread
* running node discovery thread
*/
public void start() {
List<Node> nodes = discoverHandler.getLocalNodes(network.maxOutCount());
if (nodes == null || nodes.isEmpty()) {
nodes = getSeedNodes();
}
for (Node node : nodes) {
node.setType(Node.OUT);
node.setStatus(Node.WAIT);
addNodeToGroup(NetworkConstant.NETWORK_NODE_OUT_GROUP, node);
}
running = true;
TaskManager.createAndRunThread(NulsConstant.MODULE_ID_NETWORK, "NetworkNodeManager", this);
discoverHandler.start();
}
use of io.nuls.network.entity.Node in project nuls by nuls-io.
the class NodesManager method removeNode.
public void removeNode(String nodeId, Integer type) {
if (nodes.containsKey(nodeId)) {
Node node = nodes.get(nodeId);
if (null != type && type != node.getType()) {
return;
}
// When other modules call the interface, close channel first
if (StringUtils.isNotBlank(node.getChannelId())) {
SocketChannel channel = NioChannelMap.get(node.getChannelId());
if (channel != null) {
channel.close();
return;
}
}
node.destroy();
for (String groupName : node.getGroupSet()) {
removeNodeFromGroup(groupName, nodeId);
}
nodes.remove(nodeId);
getNodeDao().removeNode(NodeTransferTool.toPojo(node));
}
}
use of io.nuls.network.entity.Node in project nuls by nuls-io.
the class NodesManager method blackNode.
public void blackNode(String nodeId, int status) {
if (nodes.containsKey(nodeId)) {
Node node = nodes.get(nodeId);
node.setStatus(status);
getNodeDao().removeNode(NodeTransferTool.toPojo(node));
removeNode(node.getId(), null);
}
}
use of io.nuls.network.entity.Node in project nuls by nuls-io.
the class ClientChannelHandler method channelInactive.
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Log.debug("----------------------client channelInactive ------------------------- ");
String channelId = ctx.channel().id().asLongText();
SocketChannel channel = (SocketChannel) ctx.channel();
NioChannelMap.remove(channelId);
Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
if (node != null) {
if (node.getChannelId() == null || channelId.equals(node.getChannelId())) {
getNetworkService().removeNode(node.getId());
}
}
}
use of io.nuls.network.entity.Node in project nuls by nuls-io.
the class ClientChannelHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {
SocketChannel channel = (SocketChannel) ctx.channel();
Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
if (node != null && node.isAlive()) {
ByteBuf buf = (ByteBuf) msg;
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
buf.release();
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.put(bytes);
getNetworkService().receiveMessage(buffer, node);
}
}
Aggregations