<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malhar.netlet;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public interface Listener
{
  public void handleException(Exception cce, DefaultEventLoop el);

  public void registered(SelectionKey key);

  public void unregistered(SelectionKey key);

  public static interface ServerListener extends Listener
  {
    public ClientListener getClientConnection(SocketChannel sc, ServerSocketChannel ssc);

  }

  public static interface ClientListener extends Listener
  {
    public void read() throws IOException;

    public void write() throws IOException;

  }

  public static final Listener NOOP_LISTENER = new Listener()
  {
    @Override
    public void handleException(Exception cce, DefaultEventLoop el)
    {
    }

    @Override
    public void registered(SelectionKey key)
    {
    }

    @Override
    public void unregistered(SelectionKey key)
    {
    }

  };

  static class DisconnectingListener implements ClientListener
  {
    private final ClientListener previous;
    private final SelectionKey key;

    public DisconnectingListener(SelectionKey key)
    {
      this.key = key;
      previous = (ClientListener)key.attachment();
    }

    @Override
    public void read() throws IOException
    {
      disconnect();
    }

    /**
     * Disconnect if there is no write interest on this socket.
     */
    private void disconnect()
    {
      if ((key.interestOps() & SelectionKey.OP_WRITE) == 0)  {
        if (key.isValid()) {
          key.cancel();
        }

        try {
          key.channel().close();
        }
        catch (IOException ie) {
          logger.warn("exception while closing socket", ie);
        }
      }
    }

    @Override
    public void write() throws IOException
    {
      previous.write();
      disconnect();
    }

    @Override
    public void handleException(Exception cce, DefaultEventLoop el)
    {
      previous.handleException(cce, el);
    }
<commit_msg>explicitly remote attachment from the key after it's disconnected.<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package malhar.netlet;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public interface Listener
{
  public void handleException(Exception cce, DefaultEventLoop el);

  public void registered(SelectionKey key);

  public void unregistered(SelectionKey key);

  public static interface ServerListener extends Listener
  {
    public ClientListener getClientConnection(SocketChannel sc, ServerSocketChannel ssc);

  }

  public static interface ClientListener extends Listener
  {
    public void read() throws IOException;

    public void write() throws IOException;

  }

  public static final Listener NOOP_LISTENER = new Listener()
  {
    @Override
    public void handleException(Exception cce, DefaultEventLoop el)
    {
    }

    @Override
    public void registered(SelectionKey key)
    {
    }

    @Override
    public void unregistered(SelectionKey key)
    {
    }

  };

  static class DisconnectingListener implements ClientListener
  {
    private final ClientListener previous;
    private final SelectionKey key;

    public DisconnectingListener(SelectionKey key)
    {
      this.key = key;
      previous = (ClientListener)key.attachment();
    }

    @Override
    public void read() throws IOException
    {
      disconnect();
    }

    /**
     * Disconnect if there is no write interest on this socket.
     */
    private void disconnect()
    {
      if ((key.interestOps() & SelectionKey.OP_WRITE) == 0)  {
        if (key.isValid()) {
          key.cancel();
        }

        try {
          key.channel().close();
        }
        catch (IOException ie) {
          logger.warn("exception while closing socket", ie);
        }

        key.attach(null);
      }
    }

    @Override
    public void write() throws IOException
    {
      previous.write();
      disconnect();
    }

    @Override
    public void handleException(Exception cce, DefaultEventLoop el)
    {
      previous.handleException(cce, el);
    }

{'fraction_non_alphanumeric': 0.08794079233783196, 'fraction_numerical': 0.00130605137135394, 'mean_word_length': 2.8069594034797016, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '239934', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1225, 'n_words': 359}