@Override
  public void appendMessages(Tpp tpp, Collection<MessageBatchWithRawData> batches)
      throws Exception {
    ByteBuf bodyBuf = Unpooled.buffer();
    KafkaMessageBrokerSender sender = getSender(tpp.getTopic());
    try {
      for (MessageBatchWithRawData batch : batches) {
        List<PartialDecodedMessage> pdmsgs = batch.getMessages();
        for (PartialDecodedMessage pdmsg : pdmsgs) {
          m_messageCodec.encodePartial(pdmsg, bodyBuf);
          byte[] bytes = new byte[bodyBuf.readableBytes()];
          bodyBuf.readBytes(bytes);
          bodyBuf.clear();

          ByteBuf propertiesBuf = pdmsg.getDurableProperties();
          HermesPrimitiveCodec codec = new HermesPrimitiveCodec(propertiesBuf);
          Map<String, String> propertiesMap = codec.readStringStringMap();
          sender.send(tpp.getTopic(), propertiesMap.get("pK"), bytes);
          BrokerStatusMonitor.INSTANCE.kafkaSend(tpp.getTopic());
        }
      }
    } finally {
      bodyBuf.release();
    }
  }
 @Override
 protected void doFlushByteBuffer(ByteBuf buf) throws Exception {
   while (buf.readable()) {
     doWriteBytes(buf);
   }
   buf.clear();
 }
  @Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    ChannelPipeline p = ctx.pipeline();
    SocksProtocolVersion version = SocksProtocolVersion.valueOf(in.readByte());
    System.out.println(version);
    in.resetReaderIndex();
    switch (version) {
      case SOCKS4a:
        p.addLast(new Socks4CmdRequestDecoder());
        p.addLast(Socks4MessageEncoder.INSTANCE);

        break;
      case SOCKS5:
        p.addLast(new Socks5InitRequestDecoder());
        p.addLast(Socks5MessageEncoder.INSTANCE);

        break;
      case UNKNOWN:
        in.clear();
        ctx.close();
        return;
    }
    p.addLast(SocksServerHandler.INSTANCE);
    p.remove(this);
  }
 @Override
 public void eval() {
   buffer.clear();
   buffer.writeByte(in.value);
   out.buffer = buffer;
   out.start = 0;
   out.end = 1;
 }
Example #5
0
 /**
  * Decode the from one {@link ByteBuf} to an other. This method will be called till either the
  * input {@link ByteBuf} has nothing to read when return from this method or till nothing was read
  * from the input {@link ByteBuf}.
  *
  * @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to
  * @param in the {@link ByteBuf} from which to read data
  * @param out the {@link List} to which decoded messages should be added
  * @throws Exception is thrown if an error accour
  */
 @Override
 protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
   int readableBytes = in.readableBytes();
   if (readableBytes > maxFrameSize) {
     in.clear();
     throw new TooLongFrameException();
   }
   ByteBuf buf = in.readBytes(readableBytes);
   out.add(buf);
 }
  private void initHeaderQos(ByteBuf buff, int messageID, AbstractMessage.QOSType... qoss)
      throws IllegalAccessException {
    buff.clear()
        .writeByte(AbstractMessage.SUBACK << 4)
        .writeBytes(Utils.encodeRemainingLength(2 + qoss.length));

    buff.writeShort(messageID);
    for (AbstractMessage.QOSType qos : qoss) {
      buff.writeByte(qos.ordinal());
    }
  }
Example #7
0
 public void setSource(TreeNode tree) throws IOException {
   JsonFactory jsonFactory = new JsonFactory();
   if (buffer == null) {
     buffer = Unpooled.buffer();
   } else {
     buffer.clear();
   }
   type = QueueMessageType.JSON;
   try (ByteBufOutputStream outputStream = new ByteBufOutputStream(buffer)) {
     JsonGenerator generator = jsonFactory.createGenerator(outputStream);
     generator.setCodec(new ObjectMapper());
     generator.writeTree(tree);
     generator.close();
   }
 }
Example #8
0
 @Override
 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
   if (isRemote(ctx)) {
     byte[] data = getPayloadFromByteBuf(buffer);
     LOG.trace(
         "Read complete,send message to remote channel: "
             + routeId
             + ",message is "
             + new String(data));
     broker.sendRequest(new UscRemoteDataMessage(routeId, data, true));
     buffer.clear();
     return;
   }
   // propagate the data to rest of handlers in pipeline
   ctx.fireChannelReadComplete();
 }
  @Override
  protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
      throws Exception {
    if (in.readableBytes() >= CLIENT_HANDSHAKE_LEN) {
      byte[] handshake = new byte[20];
      in.readBytes(handshake);

      if (!Arrays.equals(handshake, HANDSHAKE_REQUEST)) {
        in.clear();
        LOGGER.warn("{} Received invalid handshake.", ctx.channel());
        ctx.close();
        return;
      }

      ctx.writeAndFlush(Unpooled.wrappedBuffer(response));
      ctx.channel().pipeline().remove(this);
    }
  }
Example #10
0
  private void writeToChannel() throws IOException {
    if (closed) {
      throw new IOException("Already closed: " + id);
    }

    chc.writeAndFlush(buf.copy()).addListener(listener);
    buf.clear();

    // if underlying channel is not writable (perhaps because of slow consumer) wait for
    // notification about writable state change
    synchronized (channelWritabilityMonitor) {
      // to prevent spurious wake up
      while (!chc.channel().isWritable()) {
        try {
          channelWritabilityMonitor.wait();
        } catch (InterruptedException e) {
          throw new IOException("Interrupted when waiting for channel writability state change", e);
        }
      }
    }
  }
 @Override
 public void clear() {
   buffer.clear();
 }
Example #12
0
 @Override
 public ByteBuf clear() {
   return buf.clear();
 }
Example #13
0
  @Override
  public void flush(final ChannelHandlerContext ctx, ChannelFuture future) throws Exception {
    final ByteBuf in = ctx.outboundByteBuffer();
    final ByteBuf out = ctx.nextOutboundByteBuffer();

    out.unsafe().discardSomeReadBytes();

    // Do not encrypt the first write request if this handler is
    // created with startTLS flag turned on.
    if (startTls && !sentFirstMessage) {
      sentFirstMessage = true;
      out.writeBytes(in);
      ctx.flush(future);
      return;
    }

    if (ctx.executor() == ctx.channel().eventLoop()) {
      flushFutureNotifier.addFlushFuture(future, in.readableBytes());
    } else {
      synchronized (flushFutureNotifier) {
        flushFutureNotifier.addFlushFuture(future, in.readableBytes());
      }
    }

    boolean unwrapLater = false;
    int bytesConsumed = 0;
    try {
      for (; ; ) {
        SSLEngineResult result = wrap(engine, in, out);
        bytesConsumed += result.bytesConsumed();
        if (result.getStatus() == Status.CLOSED) {
          // SSLEngine has been closed already.
          // Any further write attempts should be denied.
          if (in.readable()) {
            in.clear();
            SSLException e = new SSLException("SSLEngine already closed");
            future.setFailure(e);
            ctx.fireExceptionCaught(e);
            flush0(ctx, bytesConsumed, e);
            bytesConsumed = 0;
          }
          break;
        } else {
          switch (result.getHandshakeStatus()) {
            case NEED_WRAP:
              ctx.flush();
              continue;
            case NEED_UNWRAP:
              if (ctx.inboundByteBuffer().readable()) {
                unwrapLater = true;
              }
              break;
            case NEED_TASK:
              runDelegatedTasks();
              continue;
            case FINISHED:
              setHandshakeSuccess();
              continue;
            case NOT_HANDSHAKING:
              break;
            default:
              throw new IllegalStateException(
                  "Unknown handshake status: " + result.getHandshakeStatus());
          }

          if (result.bytesConsumed() == 0 && result.bytesProduced() == 0) {
            break;
          }
        }
      }

      if (unwrapLater) {
        inboundBufferUpdated(ctx);
      }
    } catch (SSLException e) {
      setHandshakeFailure(e);
      throw e;
    } finally {
      in.unsafe().discardSomeReadBytes();
      flush0(ctx, bytesConsumed);
    }
  }
 @Before
 public void setUp() {
   byteBuf.clear();
 }