<commit_before>require 'timeout'
require 'socket'
require 'json'

require 'spf/common/extensions/fixnum'
require 'spf/common/exceptions'
require 'spf/common/loop_connector'


module SPF
  module Gateway

    class ConfigurationAgent < SPF::Common::LoopConnector

      # Timeouts
      DEFAULT_OPTIONS = {
        header_read_timeout:      5.seconds,
        request_read_timeout:     10.seconds,
        reprogram_read_timeout:   10.seconds
      }

      # Get ASCII/UTF-8 code for newline character
      # NOTE: the following code is uglier but should be more portable and
      # robust than a simple '\n'.ord
      NEWLINE = '\n'.unpack('C').first

      def initialize(service_manager, remote_host, remote_port, configuration, opts = {})
        super(remote_host, remote_port, self.class.name)

        @service_manager = service_manager
        @pig_conf = configuration # PIGConfiguration object
        @ca_conf = DEFAULT_OPTIONS.merge(opts)
      end


      private

        def handle_connection(socket, host, port)
          # set Socket KEEP_ALIVE: after 60s inactivity send up to 10 probes with 5s interval
          socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
          socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE, 60)
          socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL, 10)
          socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT, 5)

          logger.info "*** #{self.class.name}: begin registration with the SPF Controller ***"

          # create registration object
          registration = {}

          registration[:alias_name] = @pig_conf.alias_name
          registration[:gps_lat] = @pig_conf.location[:gps_lat]
          registration[:gps_lon] = @pig_conf.location[:gps_lon]
          registration = registration.to_json
<commit_msg>Fixed error caused by undefined constants in Socket module<commit_after>require 'timeout'
require 'socket'
require 'json'

require 'spf/common/extensions/fixnum'
require 'spf/common/exceptions'
require 'spf/common/loop_connector'


module SPF
  module Gateway

    class ConfigurationAgent < SPF::Common::LoopConnector
      
      include Socket::Constants

      # Timeouts
      DEFAULT_OPTIONS = {
        header_read_timeout:      5.seconds,
        request_read_timeout:     10.seconds,
        reprogram_read_timeout:   10.seconds
      }

      # Get ASCII/UTF-8 code for newline character
      # NOTE: the following code is uglier but should be more portable and
      # robust than a simple '\n'.ord
      NEWLINE = '\n'.unpack('C').first

      def initialize(service_manager, remote_host, remote_port, configuration, opts = {})
        super(remote_host, remote_port, self.class.name)

        @service_manager = service_manager
        @pig_conf = configuration # PIGConfiguration object
        @ca_conf = DEFAULT_OPTIONS.merge(opts)
      end


      private

        def handle_connection(socket, host, port)
          # set Socket KEEP_ALIVE: after 60s inactivity send up to 10 probes with 5s interval
          if [:SOL_SOCKET, :SO_KEEPALIVE].all? {|c| Socket.const_defined? c}
            socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
            if [:SOL_TCP, :TCP_KEEPIDLE, :TCP_KEEPINTVL, :TCP_KEEPCNT].all? {|c| Socket.const_defined? c}
              socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE, 60)
              socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL, 10)
              socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT, 5)
            end
            logger.info "*** #{self.class.name}: set keep-alive options for the TCP connection with the SPF Controller ***"
          end

          logger.info "*** #{self.class.name}: begin registration with the SPF Controller ***"

          # create registration object
          registration = {}

          registration[:alias_name] = @pig_conf.alias_name
          registration[:gps_lat] = @pig_conf.location[:gps_lat]
          registration[:gps_lon] = @pig_conf.location[:gps_lon]
          registration = registration.to_json

{'fraction_non_alphanumeric': 0.09561166952684481, 'fraction_numerical': 0.007845060063741112, 'mean_word_length': 2.9344262295081966, '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': '1359049', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1200, 'n_words': 281}