Class: FFWD::UDP::Bind

Inherits:
Object
  • Object
show all
Includes:
Reporter
Defined in:
lib/ffwd/protocol/udp/bind.rb

Constant Summary collapse

DEFAULT_REBIND_TIMEOUT =
10
DEFAULT_RECEIVE_BUFFER_SIZE =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reporter

build_meta, included, #increment, map_meta, #report!, #reporter_data

Constructor Details

#initialize(core, log, host, port, connection, config) ⇒ Bind

Returns a new instance of Bind.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ffwd/protocol/udp/bind.rb', line 45

def initialize core, log, host, port, connection, config
  @log = log
  @peer = "#{host}:#{port}"
  @reporter_meta = {:component => connection.plugin_type, :listen => @peer}

  rebind_timeout = config[:rebind_timeout]

  @socket = nil

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

  r = FFWD.retry :timeout => rebind_timeout do |a|
    @socket = EM.open_datagram_socket host, port, connection, self, core, config

    if size = config[:receive_buffer_size]
      log.debug "Setting receive buffer size to #{size}"
      @socket.set_sock_opt Socket::SOL_SOCKET, Socket::SO_RCVBUF, size
    end

    log.info "Bind on #{info} (attempt #{a})"
    log.info "  config: #{config.inspect}"
  end

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

    if @socket
      @socket.close
      @socket = nil
    end
  end

  r.depend_on core

  core.stopping do
    if @socket
      @socket.unbind
      @socket = nil
    end

    log.info "Unbound #{info}"
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



43
44
45
# File 'lib/ffwd/protocol/udp/bind.rb', line 43

def config
  @config
end

#logObject (readonly)

Returns the value of attribute log.



43
44
45
# File 'lib/ffwd/protocol/udp/bind.rb', line 43

def log
  @log
end

#reporter_metaObject (readonly)

Returns the value of attribute reporter_meta.



43
44
45
# File 'lib/ffwd/protocol/udp/bind.rb', line 43

def reporter_meta
  @reporter_meta
end

Class Method Details

.prepare(opts) ⇒ Object



29
30
31
32
33
# File 'lib/ffwd/protocol/udp/bind.rb', line 29

def self.prepare opts
  opts[:rebind_timeout] ||= DEFAULT_REBIND_TIMEOUT
  opts[:receive_buffer_size] ||= DEFAULT_RECEIVE_BUFFER_SIZE
  opts
end