Ejemplo n.º 1
0
 public void runServer() throws Exception {
   final ServerBootstrap sb = new ServerBootstrap();
   try {
     sb.group(new NioEventLoopGroup(), new NioEventLoopGroup())
         .channel(NioServerSocketChannel.class)
         .childHandler(
             new ChannelInitializer<SocketChannel>() {
               @Override
               public void initChannel(final SocketChannel ch) throws Exception {
                 ch.pipeline()
                     .addLast(
                         new HttpRequestDecoder(),
                         new HttpObjectAggregator(65536),
                         new HttpResponseEncoder(),
                         new WebSocketServerProtocolHandler("/websocket"));
               }
             });
     final Channel ch = sb.bind(8080).sync().channel();
     context
         .getScheduler()
         .scheduleWithFixedDelay(
             new Runnable() {
               @Override
               public void run() {
                 if (ch != null) {
                   if (ch.isActive()) {
                     String report = reporter.makeReport();
                     ch.write(new TextWebSocketFrame(report));
                     ch.flush();
                     log.info("Report sent");
                   } else {
                     log.warn(
                         "Cannot send report on channel (A,O,R) ("
                             + ch.isActive()
                             + ", "
                             + ch.isOpen()
                             + ", "
                             + ch.isRegistered()
                             + ")");
                   }
                 } else {
                   log.error("Channel is null!");
                 }
               }
             },
             15,
             context.getProps().getReportLogInterval(),
             TimeUnit.SECONDS);
     log.info("Web socket server started at port " + 8080);
     ch.closeFuture().sync();
     log.info("Web socket server stoped");
   } finally {
     sb.shutdown();
     log.info("Web socket server shutdown");
   }
 }
Ejemplo n.º 2
0
 @Override
 public void run() {
   try {
     while (true) {
       String message = null;
       try {
         message = context.getQueue().take();
       } catch (InterruptedException e) {
         log.warn("Can't read queue");
       }
       try {
         writeMessage(socket, message);
         readMessage(socket);
       } catch (Throwable e) {
         String warning = "Client disconected";
         try {
           warning += " ::: " + socket.getRemoteAddress();
         } catch (Throwable ex) {
           log.error("Unexpected error : " + ex.getMessage());
         }
         log.warn(warning);
         break;
       }
     }
   } catch (Throwable ex) {
     log.error("Unexpected error : " + ex.getMessage());
   } finally {
     shutdown();
   }
 }
Ejemplo n.º 3
0
 private void processData(byte[] data) {
   Message message = converter.read(data);
   if (message != null) {
     context.addMessage(message);
   }
 }