Ravn::BDE::

BeaconListener class

A simple client for beacon messages.

Attributes

address R

The multicast address

callback R

The message callback

port R

The port of the multicast group

socket R

The UDP socket beacon messages will be sent to

Public Class Methods

new( address=Ravn::BDE::Beacon.multicast_address, port=Ravn::BDE::Beacon.multicast_port )

Create a new client that will listen for beacon messages.

# File lib/ravn/bde/beacon_listener.rb, line 22
def initialize( address=Ravn::BDE::Beacon.multicast_address, port=Ravn::BDE::Beacon.multicast_port )
        @address = address
        @port = port
        @socket = self.create_listener_socket( address, port )
        @running = false
        @callback = nil
end

Public Instance Methods

create_listener_socket( address, port )

Return a UDPSocket that’s set up with membership to the given multicast address.

# File lib/ravn/bde/beacon_listener.rb, line 75
def create_listener_socket( address, port )
        self.log.info "Setting up listener on %s[%s]:%d" % [ address, Socket::INADDR_ANY, self.port ]
        membership = IPAddr.new( address.to_s ).hton + IPAddr.new( '0.0.0.0' ).hton

        socket = UDPSocket.new
        socket.setsockopt( :IPPROTO_IP, :IP_ADD_MEMBERSHIP, membership )
        socket.setsockopt( :SOL_SOCKET, :SO_REUSEPORT, 1 )

        return socket
end
on_message( &block )

Register the given block to be called on each beacon message.

# File lib/ravn/bde/beacon_listener.rb, line 69
def on_message( &block )
        @callback = block
end
running()

True while the listener is running

# File lib/ravn/bde/beacon_listener.rb, line 53
attr_predicate_accessor :running
start()

Start listening for beacon messages.

# File lib/ravn/bde/beacon_listener.rb, line 57
def start
        @thread = Thread.new( &self.method(:start_listening) )
end
start_listening()

Start listening for beacon messages and calling the on_message handler for each one.

# File lib/ravn/bde/beacon_listener.rb, line 88
def start_listening
        Thread.current.name = "beacon listener" unless Thread.current == Thread.main

        self.socket.bind( Socket::INADDR_ANY, self.port )
        self.running = true

        while self.running?
                begin
                        message, _ = self.socket.recvfrom( 1024 )
                        info = MessagePack.unpack( message )

                        self.callback&.call( info )
                rescue => err
                        self.log.error "%p while reading from multicast: %s" % [ err.class, err.message ]
                end
        end
end
stop()

Stop the listener.

# File lib/ravn/bde/beacon_listener.rb, line 63
def stop
        @running = false
end