<commit_before>require 'thread'
require 'socket'
require 'trace_log'
require 'heatmiser_status'
require 'command_queue'

class HeatmiserClient

  def initialize client
    @client = client
    @clientIP = client.getpeername
    @clientIP = (4 ... 8).map{|pos|@clientIP[pos]}.join('.')
  end

  def session
    Thread.new @client, @clientIP do | client, clientIP |
      status = HeatmiserStatus.instance
      log = TraceLog.instance
      log.info "client #{clientIP} connected"
      loop do
        begin
          timeout 10 do
            command = read 5
            log.debug "header: #{TraceLog.hex command}" if log.debug?
            packetSize = (command[1] & 0xFF) | ((command[2] << 8) & 0x0F00)
            command += read(packetSize - 5)
            CommandQueue.instance.push clientIP, command unless (command[0] & 0xFF) == 0x93
            status.get { client.write status.raw.pack('c*') if status.valid }
            log.info "command received from client #{clientIP} so it is alive"
          end
        rescue Timeout::Error
          log.info "no command received from client #{clientIP} which might be dormant"
          break
        rescue => error
          log.error error
          break
        end
      end
      client.close
      log.info "client #{clientIP} disconnected"
    end
  end

  def read size
    data = @client.read size
    raise "unable to read #{size} bytes from client #{@clientIP} - presuming it has disconnected" unless data and data.size == size
    data.unpack('c*')<commit_msg>A bit more tolerance of errors.<commit_after>require 'thread'
require 'socket'
require 'trace_log'
require 'heatmiser_status'
require 'command_queue'

class HeatmiserClient

  def initialize client
    @client = client
    @clientIP = client.getpeername
    @clientIP = (4 ... 8).map{|pos|@clientIP[pos]}.join('.')
  end

  def session
    Thread.new @client, @clientIP do | client, clientIP |
      status = HeatmiserStatus.instance
      log = TraceLog.instance
      log.info "client #{clientIP} connected"
      loop do
        begin
          timeout 15 do
            command = read 5
            log.debug "header: #{TraceLog.hex command}" if log.debug?
            packetSize = (command[1] & 0xFF) | ((command[2] << 8) & 0x0F00)
            command += read(packetSize - 5)
            CommandQueue.instance.push clientIP, command unless (command[0] & 0xFF) == 0x93
            status.get { client.write status.raw.pack('c*') if status.valid }
            log.info "command received from client #{clientIP} so it is alive"
          end
        rescue Timeout::Error
          log.info "no command received from client #{clientIP} which might be dormant"
          break
        rescue => error
          log.error error
          break
        end
      end
      client.close
      log.info "client #{clientIP} disconnected"
    end
  end

  def read size
    data = @client.read size
    raise "unable to read #{size} bytes from client #{@clientIP} - presuming it has disconnected" unless data and data.size == size
    data.unpack('c*')
{'fraction_non_alphanumeric': 0.1017555266579974, 'fraction_numerical': 0.01235370611183355, 'mean_word_length': 2.3409337676438655, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4201514', 'n_tokens_mistral': 910, 'n_tokens_neox': 875, 'n_words': 282}