<commit_before>require 'socket'
require 'ionian/socket'

module Ionian
  
  # A convenient wrapper for TCP, UDP, and Unix server sockets.
  class Server
    
    def initialize **kwargs, &block
      @accept_listeners = []
      register_accept_listener &block if block_given?
      
      @interface = kwargs.fetch :interface, ''
      @port      = kwargs.fetch :port, nil
      
      
      # Automatically select UDP for the multicast range. Otherwise default to TCP.
      default_protocol = :tcp
      # TODO: This ivar may be incorrect for UDP -- bound interface is not destination.
      default_protocol = :udp  if Ionian::Extension::Socket.multicast? @interface
      default_protocol = :unix if @interface.start_with? '/'
      
      @protocol  = kwargs.fetch :protocol, default_protocol
      
      # TODO: Move this to #listen.
      case @protocol
      when :tcp
        @server = TCPServer.new @interface, @port
        @server.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, [1].pack('i')
      when :udp
        @server = Ionian::Socket.new host: @interface, port: @port, protocol: :udp
      when :unix
        @server = UNIXServer.new @interface
      end
    end
    
    # Starts the socket server listening for connections.
    # Blocks registered with #register_accept_listener will
    # be run when a connection is accepted.
    def listen &block
      register_accept_listener &block if block_given?
      
      @accept_thread = Thread.new do<commit_msg>Server listener accept thread only instantiates if not already running.<commit_after>require 'socket'
require 'ionian/socket'

module Ionian
  
  # A convenient wrapper for TCP, UDP, and Unix server sockets.
  class Server
    
    def initialize **kwargs, &block
      @accept_listeners = []
      register_accept_listener &block if block_given?
      
      @interface = kwargs.fetch :interface, ''
      @port      = kwargs.fetch :port, nil
      
      
      # Automatically select UDP for the multicast range. Otherwise default to TCP.
      default_protocol = :tcp
      # TODO: This ivar may be incorrect for UDP -- bound interface is not destination.
      default_protocol = :udp  if Ionian::Extension::Socket.multicast? @interface
      default_protocol = :unix if @interface.start_with? '/'
      
      @protocol  = kwargs.fetch :protocol, default_protocol
      
      # TODO: Move this to #listen.
      case @protocol
      when :tcp
        @server = TCPServer.new @interface, @port
        @server.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, [1].pack('i')
      when :udp
        @server = Ionian::Socket.new host: @interface, port: @port, protocol: :udp
      when :unix
        @server = UNIXServer.new @interface
      end
    end
    
    # Starts the socket server listening for connections.
    # Blocks registered with #register_accept_listener will
    # be run when a connection is accepted.
    def listen &block
      register_accept_listener &block if block_given?
      
      @accept_thread ||= Thread.new do
{'fraction_non_alphanumeric': 0.0850295469468155, 'fraction_numerical': 0.0006565988181221273, 'mean_word_length': 2.6666666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7180012', 'n_tokens_mistral': 867, 'n_tokens_neox': 849, 'n_words': 280}