Module: EventMachine::SFlow::DatagramHandler

Defined in:
lib/em-sflow/datagram_handler.rb

Instance Method Summary collapse

Instance Method Details

#initialize(args) ⇒ Object



4
5
6
7
8
# File 'lib/em-sflow/datagram_handler.rb', line 4

def initialize args
  @host = args[:host]
  @callbacks = args[:callbacks]
  @proxy_targets = args[:proxy_targets]
end

#receive_data(data) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/em-sflow/datagram_handler.rb', line 10

def receive_data data
  unless @proxy_targets.empty?
    @proxy_socket ||= EventMachine.open_datagram_socket(@host, 0)
    
    @proxy_targets.each do |target|
      target_host = target.split(":")[0]
      target_port = target.split(":")[1] || "6343"

      @proxy_socket.send_datagram(data, target_host, target_port)
    end
  end

  datagram = Datagram.new(data)
  
  @callbacks.each do |callback|
    callback.call datagram
  end
end