Sha256: c253cef60c4728cc4b98315c0c1721959fd3e366c81adbcbc6f585c0a42db8c4

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'socket'
require 'json'

module Noam
  module Message
    class Marco
      attr_reader :port

      def initialize(room_name, lemma_name)
        @room_name = room_name
        @lemma_name = lemma_name
      end

      def start
        bcast_socket = UDPSocket.new
        reply_socket = UDPSocket.new
        reply_socket.bind("0.0.0.0", 0)

        begin
          bcast_socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)

          loop do
            bcast_socket.send(noam_encode, 0, "255.255.255.255", Noam::BEACON_PORT)
            if message_received?(bcast_socket)
              break
            end
          end

          get_polo_response(bcast_socket)
        ensure
          reply_socket.close
        end
      end

      def noam_encode
        ["marco", @lemma_name, @room_name, Noam::DEVICE_TYPE, Noam::VERSION].to_json
      end

      private

      MAX_RESPONSE_LENGTH = 1600
      WAIT_TIME = 5.0

      def message_received?(socket)
        IO.select([socket], [], [], WAIT_TIME)
      end

      def get_polo_response(socket)
        message, sockaddr = socket.recvfrom(MAX_RESPONSE_LENGTH)
        _, _, noam_port = JSON.parse(message)
        _, _, addr, _ = sockaddr

        Polo.new(addr, noam_port)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
noam_lemma-0.2.1.2 lib/noam_lemma/message/marco.rb
noam_lemma-0.2.1.1 lib/noam_lemma/message/marco.rb
noam_lemma-0.2.1 lib/noam_lemma/message/marco.rb