Sha256: b3f2ea5b9df99736a4fa4fb6bd02ed4f6426519e14840d1025cf91f1dc8de41d

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Lxi
  class NetFinder
    include FFI

    def init
      @devices = []
      Lxi.init_session
    end

    def search(timeout: 1000, type: :vxi11)
      if type == :mdns
        raise Error, 'mDNS library not found' unless check_mdns_lib?
      end

      @devices = []
      info = FFIFunctions::LxiInfo.new
      info[:broadcast] = broadcast_callback
      info[:device] = device_callback
      info[:service] = service_callback

      result = Lxi.discover(info, timeout, type)
      raise(Error, "Discovery error: #{result}") unless result == LXI_OK

      sleep(0.25)
      @devices
    end

    private

    def check_mdns_lib?
      Lxi::LibChecker.installed?(Lxi::MDNS_PATHS)
    end

    def broadcast_callback
      # FFI::Function.new(:void, %i[pointer pointer]) do |service, interface|
      #   puts("Broadcast: #{service.read_string} on #{interface.read_string}\n\n")
      #   @devices << { service: service.read_string, interface: interface.read_string }
      # end
    end

    def device_callback
      FFI::Function.new(:void, %i[pointer pointer]) do |address, id|
        # puts("Device: #{id.read_string} at #{address.read_string}")
        @devices << { address: address.read_string, id: id.read_string }
      end
    end

    def service_callback
      FFI::Function.new(:void, %i[pointer pointer pointer int]) do |address, id, service, port|
        address = address.read_string
        id = id.read_string
        service = service.read_string
        @devices << { address:, id:, service:, port: }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lxi_rb-0.9.0 lib/lxi/net_finder.rb