Sha256: 63c9a9fa1852f7789f737864a9352aaa337dc13198bdbcff1955bc7c40173ddc

Contents?: true

Size: 1000 Bytes

Versions: 9

Compression:

Stored size: 1000 Bytes

Contents

require 'socket'
require 'fcntl'

module PulseMeter
  module CommandAggregator
    class UDP

      def initialize(host, port = nil)
        @servers = if host.is_a?(Array)
          host
        else
          [[host, port]]
        end
        raise ArgumentError, "No servers specified" if @servers.empty?
        @buffer = []
        @in_multi = false
        @sock = UDPSocket.new
        @sock.fcntl(Fcntl::F_SETFL, @sock.fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)
      end

      def multi
        @in_multi = true
        yield
      ensure
        @in_multi = false
        send_buffer
      end

      def method_missing(*args)
        @buffer << args
        send_buffer unless @in_multi
      end

      private

      def send_buffer
        data = @buffer.to_json
        @sock.send(data, 0, *@servers.sample)
      rescue StandardError => e
        PulseMeter.error "error sending data: #{e}, #{e.backtrace.join("\n")}"
      ensure
        @buffer = []
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
pulse_meter_core-0.5.5 lib/pulse_meter/command_aggregator/udp.rb
pulse_meter_core-0.5.4 lib/pulse_meter/command_aggregator/udp.rb
pulse_meter_core-0.5.3 lib/pulse_meter/command_aggregator/udp.rb
pulse_meter_core-0.5.2 lib/pulse_meter/command_aggregator/udp.rb
pulse_meter_core-0.5.1 lib/pulse_meter/command_aggregator/udp.rb
pulse_meter_core-0.5.0 lib/pulse_meter/command_aggregator/udp.rb
pulse_meter_core-0.4.13 lib/pulse_meter/command_aggregator/udp.rb
pulse-meter-0.4.9 lib/pulse-meter/command_aggregator/udp.rb
pulse-meter-0.4.8 lib/pulse-meter/command_aggregator/udp.rb