Ejemplo n.º 1
0
  public void handleDownstream(final ChannelHandlerContext context, final ChannelEvent evt)
      throws Exception {
    if (evt instanceof ChannelStateEvent) {
      ChannelStateEvent e = (ChannelStateEvent) evt;
      switch (e.getState()) {
        case OPEN:
        case CONNECTED:
        case BOUND:
          if (Boolean.FALSE.equals(e.getValue()) || e.getValue() == null) {
            closeOutboundAndChannel(context, e);
            return;
          }
      }
    }
    if (!(evt instanceof MessageEvent)) {
      context.sendDownstream(evt);
      return;
    }

    MessageEvent e = (MessageEvent) evt;
    if (!(e.getMessage() instanceof ChannelBuffer)) {
      context.sendDownstream(evt);
      return;
    }

    // Do not encrypt the first write request if this handler is
    // created with startTLS flag turned on.
    if (startTls && sentFirstMessage.compareAndSet(false, true)) {
      context.sendDownstream(evt);
      return;
    }

    // Otherwise, all messages are encrypted.
    ChannelBuffer msg = (ChannelBuffer) e.getMessage();
    PendingWrite pendingWrite;

    if (msg.readable()) {
      pendingWrite =
          new PendingWrite(
              evt.getFuture(), msg.toByteBuffer(msg.readerIndex(), msg.readableBytes()));
    } else {
      pendingWrite = new PendingWrite(evt.getFuture(), null);
    }
    synchronized (pendingUnencryptedWrites) {
      boolean offered = pendingUnencryptedWrites.offer(pendingWrite);
      assert offered;
    }

    wrap(context, evt.getChannel());
  }
Ejemplo n.º 2
0
 @Override
 public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
   if (_encodeCipher != null) {
     MessageEvent m = Util.code(_encodeCipher, e, false);
     ctx.sendDownstream(m);
   }
 }
 private void handleApplicationShutdown(ChannelHandlerContext ctx, MessageEvent me) {
   synchronized (monitor) {
     this.shutdown = true;
     emptyAcknowledgementSchedule.clear();
     ongoingMessageExchanges.clear();
   }
   ctx.sendDownstream(me);
 }
 @Override
 public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent channelEvent)
     throws Exception {
   if (channelEvent instanceof MessageEvent) {
     final MessageEvent msgEvent = (MessageEvent) channelEvent;
     this.outgoingMessage(ctx, msgEvent);
   }
   ctx.sendDownstream(channelEvent);
 }
  /**
   * If the message to be written is a {@link CoapResponse} this method decides whether the message
   * type is {@link MessageType.Name#ACK} (if there wasn't an empty acknowledgement sent yet) or
   * {@link MessageType.Name#CON} (if there was already an empty acknowledgement sent). In the
   * latter case it additionally cancels the sending of an empty acknowledgement (which was
   * scheduled by the <code>messageReceived</code> method when the request was received).
   *
   * @param ctx The {@link ChannelHandlerContext} connecting relating this class (which implements
   *     the {@link ChannelUpstreamHandler} interface) to the datagramChannel that received the
   *     message.
   * @param me the {@link MessageEvent} containing the actual message
   * @throws Exception if an error occurred
   */
  @Override
  public void writeRequested(ChannelHandlerContext ctx, MessageEvent me) throws Exception {
    if (isShutdown()) return;

    if (me.getMessage() instanceof CoapResponse) handleOutgoingCoapResponse(ctx, me);
    else if (me.getMessage() instanceof InternalApplicationShutdownMessage)
      handleApplicationShutdown(ctx, me);
    else ctx.sendDownstream(me);
  }
Ejemplo n.º 6
0
  private void closeOutboundAndChannel(
      final ChannelHandlerContext context, final ChannelStateEvent e) throws SSLException {
    if (!e.getChannel().isConnected()) {
      context.sendDownstream(e);
      return;
    }

    unwrap(context, e.getChannel(), ChannelBuffers.EMPTY_BUFFER, 0, 0);
    if (!engine.isInboundDone()) {
      if (sentCloseNotify.compareAndSet(false, true)) {
        engine.closeOutbound();
        ChannelFuture closeNotifyFuture = wrapNonAppData(context, e.getChannel());
        closeNotifyFuture.addListener(new ClosingChannelFutureListener(context, e));
        return;
      }
    }

    context.sendDownstream(e);
  }
Ejemplo n.º 7
0
  @Override
  public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    // Log all channel state changes.
    if (e instanceof ChannelStateEvent) {
      //			logger.trace("server : channel state changed: " + e);
    }

    // Sending the event downstream (outbound)
    ctx.sendDownstream(e);
    //		super.handleDownstream(ctx, e);
  }
  @Override
  public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent evt) throws Exception {
    if (!(evt instanceof MessageEvent)) {
      ctx.sendDownstream(evt);
      return;
    }

    MessageEvent e = (MessageEvent) evt;
    Object originalMessage = e.getMessage();

    if (originalMessage instanceof ChannelBuffer) {
      // 对于ChannelBuffer不尝试编码
      ctx.sendDownstream(evt);
    } else {
      ChannelBuffer encodedMessage = encode(ctx, e.getChannel(), originalMessage);
      if (encodedMessage != null) {
        Channels.write(ctx, e.getFuture(), encodedMessage, e.getRemoteAddress());
      }
    }
  }
  private void sendHeader(ChannelHandlerContext ctx) throws IOException {
    headerSent = true;
    logger.info(
        "Authenticating with Found Elasticsearch at [{}] on connection [{}]",
        ctx.getChannel().getRemoteAddress(),
        ctx.getChannel().getLocalAddress());
    ChannelBuffer message = new FoundTransportHeader(clusterName.value(), apiKey).getHeaderBuffer();

    ctx.sendDownstream(
        new DownstreamMessageEvent(
            ctx.getChannel(),
            Channels.succeededFuture(ctx.getChannel()),
            message,
            ctx.getChannel().getRemoteAddress()));
  }
Ejemplo n.º 10
0
  private void flushPendingEncryptedWrites(ChannelHandlerContext ctx) {
    // Avoid possible dead lock and data integrity issue
    // which is caused by cross communication between more than one channel
    // in the same VM.
    if (!pendingEncryptedWritesLock.tryLock()) {
      return;
    }

    try {
      MessageEvent e;
      while ((e = pendingEncryptedWrites.poll()) != null) {
        ctx.sendDownstream(e);
      }
    } finally {
      pendingEncryptedWritesLock.unlock();
    }
  }
Ejemplo n.º 11
0
  /* (non-Javadoc)
   * @see org.jboss.netty.channel.SimpleChannelHandler#writeRequested(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.MessageEvent)
   */
  @Override
  public void writeRequested(final ChannelHandlerContext context, final MessageEvent e)
      throws Exception {

    // extract the string from the message event
    final Object message = e.getMessage();
    if (!(message instanceof String)) {
      context.sendDownstream(e);
      return;
    }
    String string = (String) message;

    // encode the string into a buffer
    ByteOrder byteOrder = context.getChannel().getConfig().getBufferFactory().getDefaultOrder();
    ChannelBuffer buffer = copiedBuffer(byteOrder, string, Charset.forName("utf-8"));

    // send the buffer downstream
    Channels.write(context, e.getFuture(), buffer, e.getRemoteAddress());
  }
 @Override
 public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
   if (e instanceof MessageEvent) {
     MessageEvent event = (MessageEvent) e;
     ChannelBuffer buffer = (ChannelBuffer) event.getMessage();
     while (buffer.readableBytes() > 0) {
       ChannelBuffer newBuffer = ChannelBuffers.dynamicBuffer();
       if (buffer.readableBytes() >= chunkSize) {
         newBuffer.writeBytes(buffer.readBytes(chunkSize));
       } else {
         newBuffer.writeBytes(buffer.readBytes(buffer.readableBytes()));
       }
       Thread.sleep(latency);
       write(ctx, e.getFuture(), newBuffer, event.getRemoteAddress());
     }
   } else {
     ctx.sendDownstream(e);
   }
 }
  private void handleOutgoingCoapResponse(ChannelHandlerContext ctx, MessageEvent me) {

    CoapResponse coapResponse = (CoapResponse) me.getMessage();
    InetSocketAddress remoteEndpoint = (InetSocketAddress) me.getRemoteAddress();

    IncomingMessageExchange messageExchange;
    synchronized (monitor) {
      messageExchange = ongoingMessageExchanges.remove(remoteEndpoint, coapResponse.getMessageID());
    }

    if (messageExchange instanceof IncomingReliableMessageExchange) {

      // if the ongoing message exchange is reliable and the empty ACK was not yet sent make
      // response piggy-
      // backed and suppress scheduled empty ACK
      if (!((IncomingReliableMessageExchange) messageExchange).isAcknowledgementSent()) {
        coapResponse.setMessageType(MessageType.Name.ACK.getNumber());
        ((IncomingReliableMessageExchange) messageExchange).setAcknowledgementSent();
      }
    }

    ctx.sendDownstream(me);
  }
Ejemplo n.º 14
0
 @Override
 public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
   ctx.sendDownstream(e);
 }
Ejemplo n.º 15
0
 @Override
 public void writeRequested(ChannelHandlerContext ctx, MessageEvent me) {
   log.info("Message to be sent to {}", me.getRemoteAddress());
   ctx.sendDownstream(me);
 }