Exemplo n.º 1
0
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    final Channel channel = ctx.getChannel();
    SocketMessage sm = (SocketMessage) e.getMessage();
    if (sm.getKind() == Kind.REQUEST) {
      final Request request = Request.newBuilder().mergeFrom(sm.getBody()).build();
      if (request.getOperate() == Operate.HeartBeat) {
        beHeartBeat.beHeartBeat(context, channel, request);
      }
    } else if (sm.getKind() == Kind.WEB_REUQEST) {
      final WebRequest request = WebRequest.newBuilder().mergeFrom(sm.getBody()).build();
      if (request.getOperate() == WebOperate.ExecuteJob) {
        completionService.submit(
            new Callable<ChannelResponse>() {
              public ChannelResponse call() throws Exception {
                return new ChannelResponse(channel, beWebExecute.beWebExecute(context, request));
              }
            });
      } else if (request.getOperate() == WebOperate.CancelJob) {
        completionService.submit(
            new Callable<ChannelResponse>() {
              public ChannelResponse call() throws Exception {
                return new ChannelResponse(channel, beWebCancel.beWebCancel(context, request));
              }
            });
      } else if (request.getOperate() == WebOperate.UpdateJob) {
        completionService.submit(
            new Callable<ChannelResponse>() {
              public ChannelResponse call() throws Exception {
                return new ChannelResponse(channel, beUpdate.beWebUpdate(context, request));
              }
            });
      } else if (request.getOperate() == WebOperate.ExecuteDebug) {
        completionService.submit(
            new Callable<ChannelResponse>() {
              public ChannelResponse call() throws Exception {
                return new ChannelResponse(channel, beDebug.beWebExecute(context, request));
              }
            });
      }
    } else if (sm.getKind() == Kind.RESPONSE) {
      for (ResponseListener lis : new ArrayList<ResponseListener>(listeners)) {
        lis.onResponse(Response.newBuilder().mergeFrom(sm.getBody()).build());
      }
    } else if (sm.getKind() == Kind.WEB_RESPONSE) {
      for (ResponseListener lis : new ArrayList<ResponseListener>(listeners)) {
        lis.onWebResponse(WebResponse.newBuilder().mergeFrom(sm.getBody()).build());
      }
    }

    super.messageReceived(ctx, e);
  }
 @Override
 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
   if (e.getMessage() == null) {
     return;
   } else if (e.getMessage() instanceof byte[]) {
     byte[] bytes = (byte[]) e.getMessage();
     Object msg;
     if (bytes.length == 0) {
       msg = Heartbeat.getSingleton();
     } else {
       try {
         msg = serializer.deserialize(bytes);
       } catch (Exception ex) {
         throw ex;
       }
     }
     UpstreamMessageEvent event =
         new UpstreamMessageEvent(e.getChannel(), msg, e.getRemoteAddress());
     super.messageReceived(ctx, event);
   } else {
     super.messageReceived(ctx, e);
   }
 }
Exemplo n.º 3
0
  /* (non-Javadoc)
   * @see org.jboss.netty.channel.SimpleChannelUpstreamHandler#messageReceived(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.MessageEvent)
   */
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Message message = (Message) e.getMessage();
    long commandId = ((Long) message.getHeader().getCommandId()).longValue();
    if (commandId != packetType.getCommandId()) {
      super.messageReceived(ctx, e);
      return;
    }

    ReportRequestMessage requestMessage = (ReportRequestMessage) message;
    ReportResponseMessage responseMessage = new ReportResponseMessage();

    responseMessage.setRequest(requestMessage);

    ctx.getChannel().write(responseMessage);

    ((Session) ctx.getChannel().getAttachment())
        .receive(requestMessage.setResponse(responseMessage));
  }
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    ImapSession session = (ImapSession) attributes.get(ctx.getChannel());
    ImapResponseComposer response = (ImapResponseComposer) ctx.getAttachment();
    ImapMessage message = (ImapMessage) e.getMessage();
    ChannelPipeline cp = ctx.getPipeline();

    try {
      if (cp.get(NettyConstants.EXECUTION_HANDLER) != null) {
        cp.addBefore(
            NettyConstants.EXECUTION_HANDLER, NettyConstants.HEARTBEAT_HANDLER, heartbeatHandler);
      } else {
        cp.addBefore(
            NettyConstants.CORE_HANDLER, NettyConstants.HEARTBEAT_HANDLER, heartbeatHandler);
      }
      final ResponseEncoder responseEncoder = new ResponseEncoder(encoder, response, session);
      processor.process(message, responseEncoder, session);

      if (session.getState() == ImapSessionState.LOGOUT) {
        // Make sure we close the channel after all the buffers were flushed out
        Channel channel = ctx.getChannel();
        if (channel.isConnected()) {
          channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
        }
      }
      final IOException failure = responseEncoder.getFailure();

      if (failure != null) {
        final Logger logger = session.getLog();
        logger.info(failure.getMessage());
        if (logger.isDebugEnabled()) {
          logger.debug("Failed to write " + message, failure);
        }
        throw failure;
      }
    } finally {
      ctx.getPipeline().remove(NettyConstants.HEARTBEAT_HANDLER);
    }

    super.messageReceived(ctx, e);
  }