class FFWD::UDP::Connect

Constants

RESOLVE_TIMEOUT

Attributes

config[R]
log[R]
reporter_meta[R]

Public Class Methods

new(core, log, host, port, handler, config) click to toggle source
# File lib/ffwd/protocol/udp/connect.rb, line 40
def initialize core, log, host, port, handler, config
  @log = log
  @host = host
  @port = port
  @handler = handler

  ignored = config[:ignored]

  @bind_host = "0.0.0.0"
  @host_ip = nil
  @c = nil
  @peer = "#{host}:#{port}"
  @reporter_meta = {:component => @handler.plugin_type, :peer => @peer}

  info = "udp://#{@peer}"

  @subs = []

  r = FFWD.retry :timeout => RESOLVE_TIMEOUT do |a|
    unless @host_ip
      @host_ip = resolve_ip @host
      raise "Could not resolve: #{@host}" if @host_ip.nil?
    end

    @c = EM.open_datagram_socket @bind_host, nil, @handler, self, config

    unless ignored.include? :events
      @subs << core.output.event_subscribe{|e| handle_event e}
    end

    unless ignored.include? :metrics
      @subs << core.output.metric_subscribe{|m| handle_metric m}
    end

    log.info "Connect to #{info} (attempt #{a})"
    log.info "  config: #{config.inspect}"
  end

  r.error do |a, t, e|
    log.warning "Connect to #{info} failed, retry ##{a} in #{t}s: #{e}"
  end

  r.depend_on core

  core.stopping do
    if @c
      @c.close
      @c = nil
    end

    @subs.each(&:unsubscribe).clear
  end
end
prepare(config) click to toggle source
# File lib/ffwd/protocol/udp/connect.rb, line 25
def self.prepare config
  config[:ignored] = (config[:ignored] || []).map{|v| Utils.check_ignored v}
  config
end

Public Instance Methods

connection_completed() click to toggle source
# File lib/ffwd/protocol/udp/connect.rb, line 100
def connection_completed; end
send_data(data) click to toggle source
# File lib/ffwd/protocol/udp/connect.rb, line 94
def send_data data
  return unless @c
  @c.send_datagram data, @host_ip, @port
end
unbind() click to toggle source
# File lib/ffwd/protocol/udp/connect.rb, line 99
def unbind; end

Private Instance Methods

handle_event(event) click to toggle source
# File lib/ffwd/protocol/udp/connect.rb, line 104
def handle_event event
  unless @c
    increment :dropped_events, 1
    return
  end

  @c.send_event event
  increment :sent_events, 1
end
handle_metric(metric) click to toggle source
# File lib/ffwd/protocol/udp/connect.rb, line 114
def handle_metric metric
  unless @c
    increment :dropped_metrics, 1
    return
  end

  @c.send_metric metric
  increment :sent_metrics, 1
end
resolve_ip(host) click to toggle source
# File lib/ffwd/protocol/udp/connect.rb, line 124
def resolve_ip host
  Socket.getaddrinfo(@host, nil, nil, :DGRAM).each do |item|
    next if item[0] != "AF_INET"
    return item[3]
  end

  return nil
end