Sha256: 75cf12d932645c1b9ddf220d14f32ff933e42bb1aca15d34969b279eb7fef612

Contents?: true

Size: 794 Bytes

Versions: 3

Compression:

Stored size: 794 Bytes

Contents

module Propono
  class UdpListenerError < StandardError
  end

  class UdpListener

    def self.listen(host, port, &processor)
      new(host, port, &processor).listen
    end

    def initialize(host, port, &processor)
      raise UdpListenerError.new("Please provide a block to call for each message") unless block_given?
      @host = host
      @port = port
      @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.6 lib/propono/services/udp_listener.rb
propono-0.5.5 lib/propono/services/udp_listener.rb
propono-0.5.4 lib/propono/services/udp_listener.rb