Class: Pants::Readers::UDPReaderConnection

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
LogSwitch::Mixin, NetworkHelpers
Defined in:
lib/pants/readers/udp_reader.rb

Overview

This is the EventMachine connection that reads on the source IP and UDP port. It places all read data onto the data channel. Allows for unicast or multicast addresses; it’ll detect which to use from the IP you pass in.

Instance Method Summary collapse

Constructor Details

#initialize(write_to_channel, starter_callback) ⇒ UDPReaderConnection

Returns a new instance of UDPReaderConnection.

Parameters:

  • write_to_channel (EventMachine::Channel)

    The data channel to write read data to.

  • starter_callback (EventMachine::Callback)

    The callback that should get called when the connection has been fully initialized.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pants/readers/udp_reader.rb', line 21

def initialize(write_to_channel, starter_callback)
  @write_to_channel = write_to_channel
  @starter_callback = starter_callback
  port, ip = Socket.unpack_sockaddr_in(get_sockname)

  if Addrinfo.ip(ip).ipv4_multicast? || Addrinfo.ip(ip).ipv6_multicast?
    log "Got a multicast address: #{ip}:#{port}"
    setup_multicast_socket(ip)
  else
    log "Got a unicast address: #{ip}:#{port}"
  end
end

Instance Method Details

#post_initObject



34
35
36
# File 'lib/pants/readers/udp_reader.rb', line 34

def post_init
  @starter_callback.call
end

#receive_data(data) ⇒ Object

Reads the data and writes it to the data channel.

Parameters:

  • data (String)

    The socket data to write to the channel.



41
42
43
# File 'lib/pants/readers/udp_reader.rb', line 41

def receive_data(data)
  @write_to_channel << data
end