use of io.netty.channel.socket.SocketChannel in project java by wavefrontHQ.
the class StreamIngester method run.
public void run() {
activeListeners.inc();
// Configure the server.
ServerBootstrap b = new ServerBootstrap();
EventLoopGroup parentGroup;
EventLoopGroup childGroup;
Class<? extends ServerChannel> socketChannelClass;
if (Epoll.isAvailable()) {
logger.fine("Using native socket transport for port " + listeningPort);
parentGroup = new EpollEventLoopGroup(1);
childGroup = new EpollEventLoopGroup();
socketChannelClass = EpollServerSocketChannel.class;
} else {
logger.fine("Using NIO socket transport for port " + listeningPort);
parentGroup = new NioEventLoopGroup(1);
childGroup = new NioEventLoopGroup();
socketChannelClass = NioServerSocketChannel.class;
}
try {
b.group(parentGroup, childGroup).channel(socketChannelClass).option(ChannelOption.SO_BACKLOG, 1024).localAddress(listeningPort).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frame decoder", frameDecoderFactory.getDecoder());
pipeline.addLast("byte array decoder", new ByteArrayDecoder());
pipeline.addLast(commandHandler);
}
});
if (parentChannelOptions != null) {
for (Map.Entry<ChannelOption<?>, ?> entry : parentChannelOptions.entrySet()) {
b.option((ChannelOption<Object>) entry.getKey(), entry.getValue());
}
}
if (childChannelOptions != null) {
for (Map.Entry<ChannelOption<?>, ?> entry : childChannelOptions.entrySet()) {
b.childOption((ChannelOption<Object>) entry.getKey(), entry.getValue());
}
}
// Start the server.
ChannelFuture f = b.bind().sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} catch (final InterruptedException e) {
logger.log(Level.WARNING, "Interrupted");
parentGroup.shutdownGracefully();
childGroup.shutdownGracefully();
logger.info("Listener on port " + String.valueOf(listeningPort) + " shut down");
} catch (Exception e) {
// ChannelFuture throws undeclared checked exceptions, so we need to handle it
if (e instanceof BindException) {
logger.severe("Unable to start listener - port " + String.valueOf(listeningPort) + " is already in use!");
} else {
logger.log(Level.SEVERE, "StreamIngester exception: ", e);
}
} finally {
activeListeners.dec();
}
}
use of io.netty.channel.socket.SocketChannel 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.netty.channel.socket.SocketChannel 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.netty.channel.socket.SocketChannel 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);
}
}
use of io.netty.channel.socket.SocketChannel 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);
}
Aggregations