Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. package org.sldc;
  2.  
  3. import io.netty.bootstrap.Bootstrap;
  4. import io.netty.channel.*;
  5. import io.netty.channel.oio.OioEventLoopGroup;
  6. import io.netty.handler.codec.string.StringDecoder;
  7. import io.netty.handler.codec.string.StringEncoder;
  8. import io.netty.handler.logging.LoggingHandler;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.stereotype.Component;
  11.  
  12. import javax.annotation.PreDestroy;
  13.  
  14. @Component
  15. @Slf4j
  16. public class TestService {
  17.  
  18. private Channel channel;
  19. private ChannelFuture channelFuture;
  20. private OioEventLoopGroup group = new OioEventLoopGroup();
  21. // private CompletableFuture<Channel> completableFuture=new CompletableFuture<>();
  22. // final CountDownLatch connectLatch = new CountDownLatch(1);
  23. // final CountDownLatch closeLatch = new CountDownLatch(1);
  24.  
  25. @Async
  26. public void connect() throws Exception{
  27. Bootstrap b=new Bootstrap();
  28. b.group(group)
  29. .channel(PureJavaCommChannel.class)
  30. .handler(new ChannelInitializer<PureJavaCommChannel>() {
  31. @Override
  32. public void initChannel(PureJavaCommChannel ch) throws Exception {
  33. ch.pipeline().addLast(
  34. new LoggingHandler(),
  35. new StringEncoder(),
  36. new StringDecoder(),
  37. new PureJavaCommClientHandler(),
  38. new CustomOutBoundHandler()
  39. );
  40. }
  41. });
  42.  
  43. channelFuture = b.connect(new PureJavaCommDeviceAddress("COM10")).sync();
  44. channelFuture.addListener(future -> {
  45. channel=((ChannelFuture)future).channel();
  46. });
  47.  
  48. channelFuture.channel().closeFuture().sync();
  49.  
  50. }
  51.  
  52. @Async
  53. public void stop() throws Exception{
  54. channel.close();
  55. }
  56.  
  57. public class CustomOutBoundHandler extends ChannelOutboundHandlerAdapter{
  58.  
  59. private ChannelHandlerContext channelHandlerContext;
  60. public void shutdown(){
  61. channelHandlerContext.close();
  62. }
  63.  
  64. @Override
  65. public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
  66. this.channelHandlerContext=ctx;
  67. super.handlerAdded(ctx);
  68. }
  69.  
  70. @Override
  71. public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
  72. super.write(ctx, msg, promise);
  73. }
  74.  
  75. @Override
  76. public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
  77. super.close(ctx, promise);
  78. }
  79. }
  80.  
  81. // @ChannelHandler.Sharable
  82. public class PureJavaCommClientHandler extends SimpleChannelInboundHandler<String> {
  83. private ResponseFuture responseFuture;
  84. private Channel channel;
  85.  
  86. public void setChannel(Channel channel) {
  87. this.channel = channel;
  88. }
  89.  
  90. public Channel getChannel() {
  91. return channel;
  92. }
  93.  
  94. public void setResponseFuture(ResponseFuture future) {
  95. this.responseFuture = future;
  96. }
  97.  
  98. @Override
  99. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  100. cause.printStackTrace();
  101.  
  102. super.exceptionCaught(ctx, cause);
  103. }
  104.  
  105. @Override
  106. public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
  107. ctx.channel().close();
  108. super.userEventTriggered(ctx, evt);
  109. }
  110.  
  111. @Override
  112. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  113. ctx.channel().write("testing");
  114. super.channelActive(ctx);
  115. }
  116.  
  117.  
  118. @Override
  119. public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
  120. super.handlerRemoved(ctx);
  121. }
  122.  
  123. @Override
  124. public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
  125. if ("OK".equals(msg)) {
  126. log.info("Serial port responded to AT");
  127. } else {
  128. log.info("Serial port responded with not-OK: " + msg);
  129. }
  130. ctx.close();
  131. }
  132.  
  133. @Override
  134. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  135. System.out.println("inactive called....");
  136. ctx.channel().close();
  137. super.channelInactive(ctx);
  138. }
  139.  
  140. @Override
  141. public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
  142. super.channelRegistered(ctx);
  143. }
  144.  
  145. @Override
  146. public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
  147. super.channelUnregistered(ctx);
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement