<commit_before>require 'gserver'
require 'logger'

module Kamerling class Server < GServer
  def initialize(handler: Handler.new, host: DEFAULT_HOST,
                 logger: Logger.new('/dev/null'), tcp_port: 0, udp_port: 0)
    super tcp_port, host
    @audit      = true
    @handler    = handler
    @logger     = logger
    @udp_server = UDPSocket.new.tap { |server| server.bind host, udp_port }
  end

  def start
    @udp_server_thread = start_udp_server
    super
  end

  def tcp_addr
    Addr[host, port, :TCP]
  end

  def udp_addr
    Addr[udp_server.addr[3], udp_server.addr[1], :UDP]
  end

  attr_reader :handler, :logger, :udp_server
  private     :handler, :logger, :udp_server
  private     :host, :port

  private

  def connecting client
    logger.info "connect #{Addr[*client.remote_address.ip_unpack, :TCP]}"
    true
  end

  def disconnecting *_
  end

  def serve io
    addr  = Addr[*io.remote_address.ip_unpack, :TCP]
    input = io.read
    logger.debug "received #{addr} #{input}"
    handler.handle input, addr
  end

  def start_udp_server
    logger.info "start #{udp_addr}"
    Thread.new do
      loop do
        if IO.select [udp_server]
          input, conn = udp_server.recvfrom 2**16
          addr = Addr[conn[3], conn[1], :UDP]
          logger.info "connect #{addr}"
          logger.debug "received #{addr} #{input}"
          handler.handle input, addr
        end
      end
    end
  end

  def starting
    logger.info "start #{tcp_addr}"
  end

  def stopping
  end<commit_msg>Server: use UDPServer<commit_after>require 'gserver'
require 'logger'

module Kamerling class Server < GServer
  def initialize(handler: Handler.new, host: DEFAULT_HOST,
                 logger: Logger.new('/dev/null'), tcp_port: 0, udp_port: 0)
    super tcp_port, host
    @audit      = true
    @handler    = handler
    @logger     = logger
    @udp_server = UDPServer.new handler: handler, host: host, logger: logger,
      port: udp_port
  end

  def start
    udp_server.start
    super
  end

  def tcp_addr
    Addr[host, port, :TCP]
  end

  def udp_addr
    udp_server.addr
  end

  attr_reader :handler, :logger, :udp_server
  private     :handler, :logger, :udp_server
  private     :host, :port

  private

  def connecting client
    logger.info "connect #{Addr[*client.remote_address.ip_unpack, :TCP]}"
    true
  end

  def disconnecting *_
  end

  def serve io
    addr  = Addr[*io.remote_address.ip_unpack, :TCP]
    input = io.read
    logger.debug "received #{addr} #{input}"
    handler.handle input, addr
  end

  def starting
    logger.info "start #{tcp_addr}"
  end

  def stopping
  end
{'fraction_non_alphanumeric': 0.10003789314134141, 'fraction_numerical': 0.004168245547555892, 'mean_word_length': 2.6717663421418636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '7022661', 'n_tokens_mistral': 925, 'n_tokens_neox': 882, 'n_words': 242}