<commit_before># Copyright (c) 2016-2017 <NAME> <<EMAIL>>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
#    https://eclipse.org/org/documents/epl-v10.php.
# and the Eclipse Distribution License is available at
#   https://eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
#    <NAME> - initial committer

module PahoMqtt
  class Handler

    attr_reader :registered_callback
    attr_reader :last_packet_received_at
    attr_accessor :clean_session

    def initialize
      @registered_callback = []
      @publisher           = nil
      @subscriber          = nil
    end

    def config_pubsub(publisher, subscriber)
      @publisher = publisher
      @subscriber = subscriber
    end

    def socket=(socket)
      @socket = socket
    end

    def receive_packet
      result = IO.select([@socket], nil, nil, SELECT_TIMEOUT) unless @socket.nil? || @socket.closed?
      unless result.nil?
        packet = PahoMqtt::Packet::Base.read(@socket)
        unless packet.nil?
          if packet.is_a?(PahoMqtt::Packet::Connack)
            @last_packet_received_at = Time.now
            return handle_connack(packet)
          else
            handle_packet(packet)
            @last_packet_received_at = Time.now
          end
        end
      end
      return result
    end

    def handle_packet(packet)
      PahoMqtt.logger.info("New packet #{packet.class} received.") if PahoMqtt.logger?
      type = packet_type(packet)
      self.send("handle_#{type}", packet)
    end

    def register_topic_callback(topic, callback, &block)
      if topic.nil?
        PahoMqtt.logger.error("The topics where the callback is trying to be registered have been found nil.") if PahoMqtt.logger?
        raise ArgumentError
      end
      clear_topic_callback(topic)
      if block_given?
        @registered_callback.push([topic, block])
      elsif !(callback.nil?) && callback.is_a?(Proc)
        @registered_callback.push([topic, callback])
      end
      MQTT_ERR_SUCCESS
    end

    def clear_topic_callback(topic)
      if topic.nil?
        PahoMqtt.logger.error("The topics where the callback is trying to be unregistered have been found nil.") if PahoMqtt.logger?
        raise ArgumentError
      end
      @registered_callback.delete_if { |pair| pair.first == topic }
      MQTT_ERR_SUCCESS
    end

    def handle_connack(packet)
      if packet.return_code == 0x00
        PahoMqtt.logger.debug(packet.return_msg) if PahoMqtt.logger?
        handle_connack_accepted(packet.session_present)
      else
        PahoMqtt.logger.warn(packet.return_msg) if PahoMqtt.logger?
        MQTT_CS_DISCONNECT
      end
      @on_connack.call(packet) unless @on_connack.nil?
      MQTT_CS_CONNECTED
    end

    def handle_connack_accepted(session_flag)
      clean_session?(session_flag)
      new_session?(session_flag)
      old_session?(session_flag)
    end

    def new_session?(session_flag)
      if !@clean_session && !session_flag
        PahoMqtt.logger.debug("New session created for the client.") if PahoMqtt.logger?
      end
    end

    def clean_session?(session_flag)
      if @clean_session && !session_flag
        PahoMqtt.logger.debug("No previous session found by server, starting a new one.") if PahoMqtt.logger?
      end
    end

    def old_session?(session_flag)
      if !@clean_session && session_flag
        PahoMqtt.logger.debug("Previous session restored by the server.") if PahoMqtt.logger?
      end
    end

    def handle_pingresp(_packet)
      @last_packet_received_at = Time.now
    end

    def handle_suback(packet)
      max_qos = packet.return_codes
      id      = packet.id
      topics  = []
      topics  = @subscriber.add_subscription(max_qos, id, topics)
      unless topics.empty?
        @on_suback.call(topics) unless @on_suback.nil?
      end
    end
<commit_msg>Set the last_packet_received_at variable only once and at a single place.<commit_after># Copyright (c) 2016-2017 <NAME> <<EMAIL>>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v1.0 which accompany this distribution.
#
# The Eclipse Public License is available at
#    https://eclipse.org/org/documents/epl-v10.php.
# and the Eclipse Distribution License is available at
#   https://eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
#    <NAME> - initial committer

module PahoMqtt
  class Handler

    attr_reader :registered_callback
    attr_reader :last_packet_received_at
    attr_accessor :clean_session

    def initialize
      @registered_callback = []
      @publisher           = nil
      @subscriber          = nil
    end

    def config_pubsub(publisher, subscriber)
      @publisher = publisher
      @subscriber = subscriber
    end

    def socket=(socket)
      @socket = socket
    end

    def receive_packet
      result = IO.select([@socket], nil, nil, SELECT_TIMEOUT) unless @socket.nil? || @socket.closed?
      unless result.nil?
        packet = PahoMqtt::Packet::Base.read(@socket)
        unless packet.nil?
          @last_packet_received_at = Time.now
          if packet.is_a?(PahoMqtt::Packet::Connack)
            return handle_connack(packet)
          else
            handle_packet(packet)
          end
        end
      end
      return result
    end

    def handle_packet(packet)
      PahoMqtt.logger.info("New packet #{packet.class} received.") if PahoMqtt.logger?
      type = packet_type(packet)
      self.send("handle_#{type}", packet)
    end

    def register_topic_callback(topic, callback, &block)
      if topic.nil?
        PahoMqtt.logger.error("The topics where the callback is trying to be registered have been found nil.") if PahoMqtt.logger?
        raise ArgumentError
      end
      clear_topic_callback(topic)
      if block_given?
        @registered_callback.push([topic, block])
      elsif !(callback.nil?) && callback.is_a?(Proc)
        @registered_callback.push([topic, callback])
      end
      MQTT_ERR_SUCCESS
    end

    def clear_topic_callback(topic)
      if topic.nil?
        PahoMqtt.logger.error("The topics where the callback is trying to be unregistered have been found nil.") if PahoMqtt.logger?
        raise ArgumentError
      end
      @registered_callback.delete_if { |pair| pair.first == topic }
      MQTT_ERR_SUCCESS
    end

    def handle_connack(packet)
      if packet.return_code == 0x00
        PahoMqtt.logger.debug(packet.return_msg) if PahoMqtt.logger?
        handle_connack_accepted(packet.session_present)
      else
        PahoMqtt.logger.warn(packet.return_msg) if PahoMqtt.logger?
        MQTT_CS_DISCONNECT
      end
      @on_connack.call(packet) unless @on_connack.nil?
      MQTT_CS_CONNECTED
    end

    def handle_connack_accepted(session_flag)
      clean_session?(session_flag)
      new_session?(session_flag)
      old_session?(session_flag)
    end

    def new_session?(session_flag)
      if !@clean_session && !session_flag
        PahoMqtt.logger.debug("New session created for the client.") if PahoMqtt.logger?
      end
    end

    def clean_session?(session_flag)
      if @clean_session && !session_flag
        PahoMqtt.logger.debug("No previous session found by server, starting a new one.") if PahoMqtt.logger?
      end
    end

    def old_session?(session_flag)
      if !@clean_session && session_flag
        PahoMqtt.logger.debug("Previous session restored by the server.") if PahoMqtt.logger?
      end
    end

    def handle_pingresp(_packet)
    end

    def handle_suback(packet)
      max_qos = packet.return_codes
      id      = packet.id
      topics  = []
      topics  = @subscriber.add_subscription(max_qos, id, topics)
      unless topics.empty?
        @on_suback.call(topics) unless @on_suback.nil?
      end
    end

{'fraction_non_alphanumeric': 0.08337475149105368, 'fraction_numerical': 0.004721669980119284, 'mean_word_length': 2.7402416356877324, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 4, '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': '205199', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2442, 'n_words': 602}