<commit_before>// NIOServer.java

package ed.net;

import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;

public abstract class NIOServer extends Thread {

    final static boolean D = Boolean.getBoolean( "DEBUG.NIO" );
    final static long SELECT_TIMEOUT = 10;
    
    public NIOServer( int port )
        throws IOException {
        super( "NIOServer  port:" + port  );
        
        _port = port;
        
        _ssChannel = ServerSocketChannel.open();
        _ssChannel.configureBlocking( false );
        _ssChannel.socket().bind( new InetSocketAddress( port ) );
        
        _initSelector();

        setDaemon( true );
    }

    public Selector getSelector(){
        return _selector;
    }

    private void _initSelector()
        throws IOException {

        if ( _selector != null ){
            _selector.close();
        }

        Selector s = Selector.open();
        _ssChannel.register( s , SelectionKey.OP_ACCEPT );

        _selector = s;
    }
    
    public void run(){
        
        System.out.println( "Listening on port: " + _port );

        final ByteBuffer readBuf = ByteBuffer.allocateDirect(1024);
        final ByteBuffer writeBuf = ByteBuffer.allocateDirect(1024);
        
        int deadSelectorCount = 0;

        while ( true ){

            final int numKeys;
            final long selectTime;

            try {
                final long start = System.currentTimeMillis();
                numKeys = _selector.select( 10 );
                selectTime = System.currentTimeMillis() - start;
            }
            catch ( IOException ioe ){
                ed.log.Logger.getLogger( "nio" ).error( "couldn't select on port : " + _port , ioe );
                continue;
            }
            
            final Iterator<SelectionKey> i = _selector.selectedKeys().iterator();

            if ( numKeys == 0 && ! i.hasNext() ){
                if ( selectTime == 0 ){
                    deadSelectorCount++;
                    System.err.println( "got 0 keys after waiting :" + selectTime + " " + deadSelectorCount + " in a row." );
                    
                    if ( deadSelectorCount > 1000 ){
                        System.err.println( "****  SHOULD KILL SELECTOR AND START OVER " );
                        try {
                            _initSelector();
                        }
                        catch ( Throwable t ){
                            System.err.println( "couldn't re-init selector after issue.  dying" );
                            t.printStackTrace();
                            System.exit( -1 );
                        }
                    }
                }
                continue;
            }
            
            deadSelectorCount = 0;

            for ( ; i.hasNext() ;  ){
                SelectionKey key = i.next();
                
                SocketChannel sc = null;
                SocketHandler sh = null;
                
                try {
                    <commit_msg>some more work on linux nio issue<commit_after>// NIOServer.java

package ed.net;

import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;

public abstract class NIOServer extends Thread {

    final static boolean D = Boolean.getBoolean( "DEBUG.NIO" );
    final static long SELECT_TIMEOUT = 10;
    
    public NIOServer( int port )
        throws IOException {
        super( "NIOServer  port:" + port  );
        
        _port = port;
        
        _ssChannel = ServerSocketChannel.open();
        _ssChannel.configureBlocking( false );
        _ssChannel.socket().bind( new InetSocketAddress( port ) );
        
        _initSelector();

        setDaemon( true );
    }

    public Selector getSelector(){
        return _selector;
    }

    private void _initSelector()
        throws IOException {

        Selector s = Selector.open();

        if ( _selector != null ){
            for ( SelectionKey sk : _selector.keys() ){

                if ( sk.channel() == _ssChannel )
                    continue;

                if ( sk.interestOps() == 0 )
                    continue;

                System.out.println( "moving : " + sk.channel() + " : " + sk.interestOps() );
                sk.channel().register( s , sk.interestOps() );
            }
            _selector.close();
        }

        _ssChannel.register( s , SelectionKey.OP_ACCEPT );
        _selector = s;
    }
    
    public void run(){
        
        System.out.println( "Listening on port: " + _port );

        final ByteBuffer readBuf = ByteBuffer.allocateDirect(1024);
        final ByteBuffer writeBuf = ByteBuffer.allocateDirect(1024);
        
        int deadSelectorCount = 0;

        while ( true ){

            final int numKeys;
            final long selectTime;

            try {
                final long start = System.currentTimeMillis();
                numKeys = _selector.select( 10 );
                selectTime = System.currentTimeMillis() - start;
            }
            catch ( IOException ioe ){
                ed.log.Logger.getLogger( "nio" ).error( "couldn't select on port : " + _port , ioe );
                continue;
            }
            
            final Iterator<SelectionKey> i = _selector.selectedKeys().iterator();

            if ( numKeys == 0 && ! i.hasNext() ){
                if ( selectTime == 0 ){
                    deadSelectorCount++;
                    
                    if ( deadSelectorCount > 50 ){
                        System.err.println( "got 0 keys after waiting :" + selectTime + " " + deadSelectorCount + " in a row." );
                        
                        if ( deadSelectorCount > 1000 ){
                            System.err.println( "****  KILLING SELECTOR AND STARTING OVER - should be taking good channels " );
                            try {
                                _initSelector();
                            }
                            catch ( Throwable t ){
                                System.err.println( "couldn't re-init selector after issue.  dying" );
                                t.printStackTrace();
                                System.exit( -1 );
                            }
                        }
                    }
                }
                continue;
            }
            
            deadSelectorCount = 0;

            for ( ; i.hasNext() ;  ){
                SelectionKey key = i.next();
                
                SocketChannel sc = null;
                SocketHandler sh = null;
                
                try {
                    
{'fraction_non_alphanumeric': 0.09579856972586412, 'fraction_numerical': 0.00700238379022646, 'mean_word_length': 1.2169749009247028, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '3721030', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1593, 'n_words': 412}