Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sldc;
- import io.netty.bootstrap.Bootstrap;
- import io.netty.channel.*;
- import io.netty.channel.oio.OioEventLoopGroup;
- import io.netty.handler.codec.string.StringDecoder;
- import io.netty.handler.codec.string.StringEncoder;
- import io.netty.handler.logging.LoggingHandler;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Component;
- import javax.annotation.PreDestroy;
- @Component
- @Slf4j
- public class TestService {
- private Channel channel;
- private ChannelFuture channelFuture;
- private OioEventLoopGroup group = new OioEventLoopGroup();
- // private CompletableFuture<Channel> completableFuture=new CompletableFuture<>();
- // final CountDownLatch connectLatch = new CountDownLatch(1);
- // final CountDownLatch closeLatch = new CountDownLatch(1);
- @Async
- public void connect() throws Exception{
- Bootstrap b=new Bootstrap();
- b.group(group)
- .channel(PureJavaCommChannel.class)
- .handler(new ChannelInitializer<PureJavaCommChannel>() {
- @Override
- public void initChannel(PureJavaCommChannel ch) throws Exception {
- ch.pipeline().addLast(
- new LoggingHandler(),
- new StringEncoder(),
- new StringDecoder(),
- new PureJavaCommClientHandler(),
- new CustomOutBoundHandler()
- );
- }
- });
- channelFuture = b.connect(new PureJavaCommDeviceAddress("COM10")).sync();
- channelFuture.addListener(future -> {
- channel=((ChannelFuture)future).channel();
- });
- channelFuture.channel().closeFuture().sync();
- }
- @Async
- public void stop() throws Exception{
- channel.close();
- }
- public class CustomOutBoundHandler extends ChannelOutboundHandlerAdapter{
- private ChannelHandlerContext channelHandlerContext;
- public void shutdown(){
- channelHandlerContext.close();
- }
- @Override
- public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
- this.channelHandlerContext=ctx;
- super.handlerAdded(ctx);
- }
- @Override
- public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
- super.write(ctx, msg, promise);
- }
- @Override
- public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
- super.close(ctx, promise);
- }
- }
- // @ChannelHandler.Sharable
- public class PureJavaCommClientHandler extends SimpleChannelInboundHandler<String> {
- private ResponseFuture responseFuture;
- private Channel channel;
- public void setChannel(Channel channel) {
- this.channel = channel;
- }
- public Channel getChannel() {
- return channel;
- }
- public void setResponseFuture(ResponseFuture future) {
- this.responseFuture = future;
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- cause.printStackTrace();
- super.exceptionCaught(ctx, cause);
- }
- @Override
- public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
- ctx.channel().close();
- super.userEventTriggered(ctx, evt);
- }
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- ctx.channel().write("testing");
- super.channelActive(ctx);
- }
- @Override
- public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
- super.handlerRemoved(ctx);
- }
- @Override
- public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
- if ("OK".equals(msg)) {
- log.info("Serial port responded to AT");
- } else {
- log.info("Serial port responded with not-OK: " + msg);
- }
- ctx.close();
- }
- @Override
- public void channelInactive(ChannelHandlerContext ctx) throws Exception {
- System.out.println("inactive called....");
- ctx.channel().close();
- super.channelInactive(ctx);
- }
- @Override
- public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
- super.channelRegistered(ctx);
- }
- @Override
- public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
- super.channelUnregistered(ctx);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement