Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UDP multicast triggering to worker #1

Open
catphish opened this issue Dec 10, 2018 · 0 comments
Open

Add UDP multicast triggering to worker #1

catphish opened this issue Dec 10, 2018 · 0 comments

Comments

@catphish
Copy link
Contributor

The sleep in a worker can be replaced with the following, which will stop sleeping when a UDP packet is received.

require 'socket'
require 'ipaddr'

MULTICAST_ADDR = "ff82::44:1202:901:222"
PORT = 3003
BIND_ADDR = '::'
socket = UDPSocket.new(Socket::AF_INET6)
membership = IPAddr.new(MULTICAST_ADDR).hton + IPAddr.new(BIND_ADDR).hton
socket.setsockopt(:IPPROTO_IPV6, :IPV6_JOIN_GROUP, membership)
socket.setsockopt(:SOL_SOCKET, :SO_REUSEPORT, 1)
socket.bind(BIND_ADDR, PORT)
loop do
  s = Kernel.select([socket],[],[],10)
  if s
    socket.recvfrom(1)
    puts 'triggered'
  else
    puts 'timed out'
  end
end

This can be triggered as follows.

require 'socket'

MULTICAST_ADDR = "ff82::44:1202:901:222"
PORT = 3003

socket = UDPSocket.new(Socket::AF_INET6)
socket.setsockopt(:IPPROTO_IP, :IP_MULTICAST_TTL, 1)
socket.send('GO! GO NOW! DO IT!!!!', 0, MULTICAST_ADDR, PORT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant