Sha256: 915787f1868f861fb9841f2c945019e2bb9439d74b2c4f352bc98e8003092de7

Contents?: true

Size: 720 Bytes

Versions: 3

Compression:

Stored size: 720 Bytes

Contents

module Propono
  class UdpListenerError < StandardError
  end

  class UdpListener

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

    def initialize(&processor)
      raise UdpListenerError.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
      text = socket.recvfrom(1024)[0]
      Thread.new { @processor.call(text) }
    end

    def socket
      @socket ||= begin
        socket = UDPSocket.new
        socket.bind(@host, @port)
        socket
      end
    end

    def config
      Configuration.instance
    end

  end
end


Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
propono-0.5.3 lib/propono/services/udp_listener.rb
propono-0.5.2 lib/propono/services/udp_listener.rb
propono-0.5.1 lib/propono/services/udp_listener.rb