Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. //kode untuk menjalankan server
  2. public class CoreServer {
  3.  
  4.     private static final Logger LOGGER = LoggerFactory.getLogger(CoreServer.class);
  5.  
  6.     private Integer port;
  7.  
  8. //    @Getter
  9. //    Channel channel;
  10. //
  11. //    @Getter
  12. //    ChannelFuture channelFuture;
  13.     EventLoopGroup bossGroup = new NioEventLoopGroup(10);
  14.     EventLoopGroup workerGroup = new NioEventLoopGroup(10);
  15.  
  16.     public CoreServer(int port) {
  17.         this.port = port;
  18.     }
  19.  
  20.     public void start() throws Exception {
  21.         LOGGER.debug("\n\n------------------------------------------< STARTING SERVER >---------------------------------------");
  22.         try {
  23.             ServerBootstrap bootServer = new ServerBootstrap();
  24.             bootServer.group(bossGroup, workerGroup)
  25.                     .handler(new LoggingHandler(LogLevel.DEBUG))
  26.                     .channel(NioServerSocketChannel.class)
  27.                     .childHandler(new ServerInitializer())
  28.                     .option(ChannelOption.SO_BACKLOG, 128)
  29.                     .childOption(ChannelOption.SO_KEEPALIVE, true);
  30.  
  31.             ChannelFuture cFuture = bootServer.bind(port).sync();
  32.  
  33.             cFuture.channel().closeFuture().sync();
  34.  
  35.         } catch (InterruptedException e) {
  36.             e.printStackTrace();
  37.             LOGGER.error("Error Menjalankan Server [{}]", e);
  38.             System.out.println("Exception in server thread");
  39.         } finally {
  40.             shutdown();
  41.         }
  42.     }
  43.  
  44.     public void shutdown() {
  45.         LOGGER.debug("\n------------------------------------------< SHUTDOWN SERVER >---------------------------------------");
  46.         try {
  47.             workerGroup.shutdownGracefully().sync();
  48.             bossGroup.shutdownGracefully().sync();
  49.         } catch (InterruptedException ex) {
  50.             Exceptions.printStackTrace(ex);
  51.         }
  52.     }
  53. }
  54.  
  55. //listener untuk menerima dan mengirim data
  56. @Sharable
  57. public class ServerListener extends ChannelInboundHandlerAdapter {
  58.  
  59.     private static final Logger LOGGER = LoggerFactory.getLogger(ServerListener.class);
  60.     private ServerAction action = new ServerAction();
  61.  
  62.     @Override
  63.     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
  64.         ctx.flush();
  65.     }
  66.  
  67.     @Override
  68.     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  69.         if (msg != null) {
  70.             action.readMessage(ctx.channel(), msg);
  71.         }
  72.         //ReferenceCountUtil.release(msg);
  73.     }
  74.  
  75.     @Override
  76.     public void channelActive(ChannelHandlerContext ctx) throws Exception {
  77.         super.channelActive(ctx);
  78.         //action.addChannel(ctx.channel());
  79.         //action.viewConnectedChannel();
  80.         LOGGER.info("Client {} now connected", ctx.channel().remoteAddress());
  81.     }
  82.  
  83.     @Override
  84.     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  85.         super.channelInactive(ctx);
  86.         //action.removeChannel(ctx.channel());
  87.         //action.viewConnectedChannel();
  88.         ctx.flush();
  89.         ctx.close();
  90.         LOGGER.info("Client {} disconnect", ctx.channel().remoteAddress());
  91.     }
  92.  
  93. }
  94.  
  95. //server initializer
  96. public class ServerInitializer extends ChannelInitializer<SocketChannel> {
  97.  
  98.     @Override
  99.     protected void initChannel(SocketChannel socketChannel) throws Exception {
  100.  
  101.         socketChannel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
  102.         socketChannel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
  103.  
  104.         socketChannel.pipeline().addLast(new ServerListener());
  105.         socketChannel.pipeline().addLast(new ChannelExceptionHandler());
  106.     }
  107.  
  108. }
  109.  
  110.  
  111. //action ketika ada pesan dari server
  112. public class ServerAction {
  113.  
  114.     private List<Channel> channels = new ArrayList<Channel>();
  115.     private static final Logger LOGGER = LoggerFactory.getLogger(ServerAction.class);
  116.  
  117.     public void addChannel(Channel client) {
  118.         channels.add(client);
  119.     }
  120.  
  121.     public void viewConnectedChannel() {
  122.         LOGGER.info("------------------------< LIST CONNECTED CLIENT >-------------");
  123.         for (Channel channel : channels) {
  124.             LOGGER.info("Channel {} connected", channel.remoteAddress());
  125.         }
  126.         LOGGER.info("--------------------------------------------------------------");
  127.     }
  128.  
  129.     public void removeChannel(Channel client) {
  130.         channels.remove(client);
  131.     }
  132.  
  133.     public void readMessage(Channel client, Object msg) {
  134.         messageChecker(client, msg);
  135.     }
  136.  
  137.     private void messageChecker(Channel client, Object msg) {
  138.         LOGGER.info("Message From client {} : {} ", client.remoteAddress(), msg.toString());
  139.  
  140.         JSONObject object = new JSONObject(msg.toString());
  141.         String action = object.get("action").toString();
  142.  
  143.         if (action.equalsIgnoreCase(RequestAction.PRINTER.toString())) {
  144.             sendResponse(client, ServerUtils.getListPrinter());
  145.         } else if (action.equalsIgnoreCase(RequestAction.FLOOR.toString())) {
  146.             sendResponse(client, ServerUtils.getListFloor());
  147.         } else if (action.equalsIgnoreCase(RequestAction.TABLE.toString())) {
  148.             sendResponse(client, ServerUtils.getListTable());
  149.         } else if (action.equalsIgnoreCase(RequestAction.ITEM.toString())) {
  150.             sendResponse(client, ServerUtils.getListItem());
  151.         } else if (action.equalsIgnoreCase(RequestAction.UNIT.toString())) {
  152.             sendResponse(client, ServerUtils.getListUnit());
  153.         } else if (action.equalsIgnoreCase(RequestAction.PRICE.toString())) {
  154.             sendResponse(client, ServerUtils.getListPrice());
  155.         }
  156.     }
  157.  
  158.     public void sendResponse(Channel client, String msg) {
  159.         client.write(msg);
  160.         LOGGER.info("Response for {} complete", client.remoteAddress());
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement