use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project jMiniLang by bajdcc.
the class ModuleNetClient method run.
public void run() {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
if (addr.startsWith(":")) {
addr = "127.0.0.1" + addr;
}
URI uri = new URI("http://" + addr);
if (uri.getPort() < 0)
throw new URISyntaxException(addr, "invalid address");
Bootstrap b = new Bootstrap();
b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)).addLast("encoder", new LengthFieldPrepender(4, false)).addLast(new StringDecoder(Charset.forName("utf-8"))).addLast(new StringEncoder(Charset.forName("utf-8"))).addLast(new ModuleNetClientHandler(msgQueue));
}
});
ChannelFuture f = b.connect(uri.getHost(), uri.getPort());
CHANNEL_GROUP.add(f.channel());
f = f.sync();
if (f.isDone()) {
status = Status.RUNNING;
}
this.addr = f.channel().localAddress().toString();
f.channel().closeFuture().sync();
status = Status.ERROR;
error = "Client closed.";
} catch (Exception e) {
status = Status.ERROR;
error = "Error: " + e.getMessage();
if (error == null)
error = e.getClass().getSimpleName();
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project jMiniLang by bajdcc.
the class ModuleNetServer method run.
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)).addLast("encoder", new LengthFieldPrepender(4, false)).addLast(new StringDecoder(Charset.forName("utf-8"))).addLast(new StringEncoder(Charset.forName("utf-8"))).addLast(new ModuleNetServerHandler(msgQueue));
}
}).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port);
CHANNEL_GROUP.add(f.channel());
f = f.sync();
if (f.isDone()) {
status = Status.RUNNING;
}
f.channel().closeFuture().sync();
status = Status.ERROR;
error = "Server closed.";
} catch (Exception e) {
status = Status.ERROR;
error = "Error: " + e.getMessage();
if (error == null)
error = e.getClass().getSimpleName();
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project netty by netty.
the class RxtxClient method main.
public static void main(String[] args) throws Exception {
EventLoopGroup group = new OioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() {
@Override
public void initChannel(RxtxChannel ch) throws Exception {
ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(), new StringDecoder(), new RxtxClientHandler());
}
});
ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project netty by netty.
the class SecureChatServerInitializer method initChannel.
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// Add SSL handler first to encrypt and decrypt everything.
// In this example, we use a bogus certificate in the server side
// and accept any invalid certificates in the client side.
// You will need something more complicated to identify both
// and server in the real world.
pipeline.addLast(sslCtx.newHandler(ch.alloc()));
// On top of the SSL handler, add the text line codec.
pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
// and then business logic.
pipeline.addLast(new SecureChatServerHandler());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project netty by netty.
the class SocketStringEchoTest method testStringEcho.
private static void testStringEcho(ServerBootstrap sb, Bootstrap cb, boolean autoRead) throws Throwable {
sb.childOption(ChannelOption.AUTO_READ, autoRead);
cb.option(ChannelOption.AUTO_READ, autoRead);
Promise<Void> serverDonePromise = ImmediateEventExecutor.INSTANCE.newPromise();
Promise<Void> clientDonePromise = ImmediateEventExecutor.INSTANCE.newPromise();
final StringEchoHandler sh = new StringEchoHandler(autoRead, serverDonePromise);
final StringEchoHandler ch = new StringEchoHandler(autoRead, clientDonePromise);
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel sch) throws Exception {
sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
sch.pipeline().addAfter("decoder", "handler", sh);
}
});
cb.handler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel sch) throws Exception {
sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
sch.pipeline().addAfter("decoder", "handler", ch);
}
});
Channel sc = sb.bind().sync().channel();
Channel cc = cb.connect(sc.localAddress()).sync().channel();
for (String element : data) {
String delimiter = random.nextBoolean() ? "\r\n" : "\n";
cc.writeAndFlush(element + delimiter);
}
ch.donePromise.sync();
sh.donePromise.sync();
sh.channel.close().sync();
ch.channel.close().sync();
sc.close().sync();
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
throw sh.exception.get();
}
if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
throw ch.exception.get();
}
if (sh.exception.get() != null) {
throw sh.exception.get();
}
if (ch.exception.get() != null) {
throw ch.exception.get();
}
}
Aggregations