Sha256: 8b66448048fe609b56373eaad84b3e9d06f89c70e2904f024cc179b96ac7be59

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

require 'json'

module Propono
  class TcpListenerError < ProponoError
  end

  class TcpListener

    def self.listen(&processor)
      new(&processor).listen
    end

    def initialize(&processor)
      raise TcpListenerError.new("Please provide a block to call for each message") unless block_given?
      @processor = processor
    end

    def listen
      loop { receive_and_process }
    end

    private

    def receive_and_process
      client = server.accept
      tcp_data = client.recvfrom(1024)[0]
      client.close
      Thread.new { process_tcp_data(tcp_data) }
    end

    def process_tcp_data(tcp_data)
      json = Propono::Utils.symbolize_keys JSON.parse(tcp_data)

      # Legacy syntax is covered in the else statement
      # This conditional and the else block will be removed in v1.
      if json[:id]
        @processor.call(json[:topic], json[:message], id: json[:id])
      else
        Propono.config.logger.info("Sending and receiving messages without ids is deprecated")
        @processor.call(json[:topic], json[:message])
      end
    end

    def server
      @server ||= TCPServer.open(Propono.config.tcp_port)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
propono-1.7.0 lib/propono/services/tcp_listener.rb
propono-1.6.0 lib/propono/services/tcp_listener.rb
propono-1.5.0 lib/propono/services/tcp_listener.rb
propono-1.4.0 lib/propono/services/tcp_listener.rb