<commit_before>/*
 * Copyright (c) 2012-2015 Spotify AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.spotify.netty4.handler.codec.zmtp;


import java.nio.ByteBuffer;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.CombinedChannelDuplexHandler;
import io.netty.handler.codec.ReplayingDecoder;

import static com.spotify.netty4.handler.codec.zmtp.ZMTPUtils.checkNotNull;
import static io.netty.util.CharsetUtil.UTF_8;

/**
 * An abstract base class for common functionality to the ZMTP codecs.
 */
public class ZMTPCodec extends ReplayingDecoder<Void> {

  private final ZMTPHandshaker handshaker;
  private final ZMTPSession session;

  private final ZMTPEncoder encoder;
  private final ZMTPDecoder decoder;

  public ZMTPCodec(final Builder builder) {
    this.session = ZMTPSession.builder()
        .localIdentity(builder.localIdentity)
        .socketType(builder.socketType)
        .type(builder.connectionType)
        .build();
    this.handshaker = checkNotNull(builder.protocol, "protocol").handshaker(session);
    this.encoder = (builder.encoder == null) ? new ZMTPMessageEncoder(session.isEnveloped()) : builder.encoder;
    this.decoder = (builder.decoder == null) ? new ZMTPMessageDecoder(session.isEnveloped()) : builder.decoder;
  }

  @Override
  public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);
    ctx.writeAndFlush(handshaker.onConnect());
  }

  @Override
  protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
      throws Exception {

    in.markReaderIndex();
    final ZMTPHandshake handshake = handshaker.inputOutput(in, ctx);
    if (handshake == null) {
      return;
    }

    session.handshakeDone(handshake);

    ctx.fireUserEventTriggered(session);

    updatePipeline(ctx.pipeline(), session);

    // This follows the pattern for dynamic pipelines documented in
    // http://netty.io/4.0/api/io/netty/handler/codec/ReplayingDecoder.html
    if (actualReadableBytes() > 0) {
      out.add(in.readBytes(actualReadableBytes()));
    }
  }

  private void updatePipeline(ChannelPipeline pipeline,
                              ZMTPSession session) {
    final ZMTPMessageParser parser = ZMTPMessageParser.create(session.actualVersion(), decoder);
    final ChannelHandler handler = new CombinedChannelDuplexHandler<ZMTPFramingDecoder, ZMTPFramingEncoder>(
        new ZMTPFramingDecoder(parser),
        new ZMTPFramingEncoder(session, encoder));
    pipeline.replace(this, "zmtp-codec", handler);<commit_msg>remove unnecessary index mark<commit_after>/*
 * Copyright (c) 2012-2015 Spotify AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.spotify.netty4.handler.codec.zmtp;


import java.nio.ByteBuffer;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.CombinedChannelDuplexHandler;
import io.netty.handler.codec.ReplayingDecoder;

import static com.spotify.netty4.handler.codec.zmtp.ZMTPUtils.checkNotNull;
import static io.netty.util.CharsetUtil.UTF_8;

/**
 * An abstract base class for common functionality to the ZMTP codecs.
 */
public class ZMTPCodec extends ReplayingDecoder<Void> {

  private final ZMTPHandshaker handshaker;
  private final ZMTPSession session;

  private final ZMTPEncoder encoder;
  private final ZMTPDecoder decoder;

  public ZMTPCodec(final Builder builder) {
    this.session = ZMTPSession.builder()
        .localIdentity(builder.localIdentity)
        .socketType(builder.socketType)
        .type(builder.connectionType)
        .build();
    this.handshaker = checkNotNull(builder.protocol, "protocol").handshaker(session);
    this.encoder = (builder.encoder == null) ? new ZMTPMessageEncoder(session.isEnveloped()) : builder.encoder;
    this.decoder = (builder.decoder == null) ? new ZMTPMessageDecoder(session.isEnveloped()) : builder.decoder;
  }

  @Override
  public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);
    ctx.writeAndFlush(handshaker.onConnect());
  }

  @Override
  protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
      throws Exception {

    final ZMTPHandshake handshake = handshaker.inputOutput(in, ctx);
    if (handshake == null) {
      return;
    }

    session.handshakeDone(handshake);

    ctx.fireUserEventTriggered(session);

    updatePipeline(ctx.pipeline(), session);

    // This follows the pattern for dynamic pipelines documented in
    // http://netty.io/4.0/api/io/netty/handler/codec/ReplayingDecoder.html
    if (actualReadableBytes() > 0) {
      out.add(in.readBytes(actualReadableBytes()));
    }
  }

  private void updatePipeline(ChannelPipeline pipeline,
                              ZMTPSession session) {
    final ZMTPMessageParser parser = ZMTPMessageParser.create(session.actualVersion(), decoder);
    final ChannelHandler handler = new CombinedChannelDuplexHandler<ZMTPFramingDecoder, ZMTPFramingEncoder>(
        new ZMTPFramingDecoder(parser),
        new ZMTPFramingEncoder(session, encoder));
    pipeline.replace(this, "zmtp-codec", handler);
{'fraction_non_alphanumeric': 0.09389525875426093, 'fraction_numerical': 0.005577936163619461, 'mean_word_length': 4.139331210191083, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4264418', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1827, 'n_words': 485}