Sha256: 175a869f7d199462af5aebe7369cae96f1886d34f22625c03cd663bfa8ab5903

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Hooray
  # Node representing a device
  class Node
    attr_accessor :host, :name, :nick, :mac, :ip, :ports

    def initialize(params = {})
      self.ip = params[:ip]
      @mac = params[:mac]
      @mac ||= Mac.addr if @ip == Hooray::Local.lan_ip
      @name = params[:name] || find_name
      return unless params[:ports]
      @ports = params[:ports].reject(&:nil?).map { |n| Hooray::Port.new(n) }
    end

    def ip=(param)
      return unless param
      @ip = param.is_a?(IPAddr) ? param : IPAddr.new(param)
    end

    def find_name
      return unless mac
      if [Mac.addr].flatten.include?(mac) # Hack until macaddr get fix
        Socket.gethostname
      else
        Settings.device(mac) || Settings.manufacturer(mac)
      end
    end

    def ports
      @ports.sort.map(&:number).join(', ')
    end

    def <=>(other)
      ip <=> other.ip
    end

    # The same node must have the same ip and mac
    def eql?(other)
      ip == other.ip || mac == other.mac
    end

    def hash
      [ip, mac].hash
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hooray-0.3.0 lib/hooray/node.rb