Class: Ahoy::Contact

Inherits:
Object
  • Object
show all
Defined in:
lib/ahoy/contact.rb

Overview

Ahoy::Contact represents another user or system, available to recieve messages, or who may send them to our user.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, domain = "local.") ⇒ Contact

:call-seq: Contact.new(name, domain=“local.”) -> contact

Create a new Ahoy::Contact. name should be in name@location format.



18
19
20
21
22
23
24
25
# File 'lib/ahoy/contact.rb', line 18

def initialize(name, domain="local.")
  @name = name
  @domain = domain
  @target = nil
  @port = nil
  @interface_addresses = {}
  @online = true
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



10
11
12
# File 'lib/ahoy/contact.rb', line 10

def domain
  @domain
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/ahoy/contact.rb', line 10

def name
  @name
end

#onlineObject Also known as: online?

Returns the value of attribute online.



11
12
13
# File 'lib/ahoy/contact.rb', line 11

def online
  @online
end

Instance Method Details

#==(other) ⇒ Object

:call-seq: contact == other_contact -> bool

Equality. Two contacts are equal if they have the same fullname (and therefore name, location, service, and domain).



94
95
96
# File 'lib/ahoy/contact.rb', line 94

def ==(other)
  other.is_a?(self.class) && other.fullname == fullname
end

#add_interface(name) ⇒ Object

Internal use only.



67
68
69
# File 'lib/ahoy/contact.rb', line 67

def add_interface(name) # :nodoc:
  @interface_addresses[name] = [] unless @interface_addresses.key?(name)
end

#fullnameObject

:call-seq: contact.fullname -> string

Returns the contact’s full name in [email protected] format



31
32
33
# File 'lib/ahoy/contact.rb', line 31

def fullname
  [name, Ahoy::SERVICE_TYPE, domain].join(".")
end

#getaddrinfo(interface = nil, resolve_cache = nil) ⇒ Object

:call-seq: contact.getaddrinfo(interface=nil) -> self

Determine and set the contact’s IP addresses. If an interface is passed, only lookup the IP addresses for that interface.

Pass true as the second argument to prevent a resolve.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ahoy/contact.rb', line 129

def getaddrinfo(interface=nil, resolve_cache=nil)
  unless interface
    interfaces(resolve_cache).each {|inter| getaddrinfo(inter, true)}
    return self
  end
  service = DNSSD::Service.new
  main = Thread.current
  service.getaddrinfo(target(resolve_cache), 0, 0, interface) do |addressed|
    @interface_addresses[addressed.interface].push(addressed.address)
    unless addressed.flags.more_coming?
      service.stop unless service.stopped?
      main.run
    end
  end
  Thread.stop unless service.stopped?
  self
end

#interfaces(use_cache = nil) ⇒ Object

:call-seq: contact.interfaces -> array

Return the contact’s interfaces. Pass true as the argument to use the cached value rather than looking it up.



60
61
62
63
# File 'lib/ahoy/contact.rb', line 60

def interfaces(use_cache=nil)
  resolve(use_cache)
  @interface_addresses.keys
end

#ip_addresses(interface = nil, resolve_cache = nil, use_cache = nil) ⇒ Object

:call-seq: contact.ip_addresses(interface=nil)

Returns all of contact’s IP addresses, or if an interface is supplied as an argument, just the IP addresses for that interface.

Pass true as the second argument to prevent a lookup of interfaces, pass true as the third argument to prevent a lookup of IP addresses, and instead use the cached value.



80
81
82
83
84
85
86
87
# File 'lib/ahoy/contact.rb', line 80

def ip_addresses(interface=nil, resolve_cache=nil, use_cache=nil)
  getaddrinfo(resolve_cache) unless use_cache
  if interface
    @interface_addresses[interface]
  else
    @interface_addresses.values.flatten
  end
end

#port(use_cache = nil) ⇒ Object

:call-seq: contact.port -> string

Return the contact’s port attribute. Pass true as the argument to use the cached value rather than looking it up.



50
51
52
53
# File 'lib/ahoy/contact.rb', line 50

def port(use_cache=nil)
  resolve(use_cache)
  @port
end

#resolve(use_cache = nil) ⇒ Object

:call-seq: contact.resolve -> contact

Determine and set the contact’s target, port, and interfaces.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ahoy/contact.rb', line 102

def resolve(use_cache=nil)
  if use_cache && @target && @port && @interface_addresses.keys.any?
    return self
  end
  service = DNSSD::Service.new
  main = Thread.current
  @interface_addresses.clear
  service.resolve(name, Ahoy::SERVICE_TYPE, domain) do |resolved|
    @target = resolved.target
    @port = resolved.port
    @interface_addresses[resolved.interface] = []
    unless resolved.flags.more_coming?
      service.stop unless service.stopped?
      main.run
    end
  end
  Thread.stop unless service.stopped?
  self
end

#target(use_cache = nil) ⇒ Object

:call-seq: contact.target -> string

Return the contact’s target attribute. Pass true as the argument to use the cached value rather than looking it up.



40
41
42
43
# File 'lib/ahoy/contact.rb', line 40

def target(use_cache=nil)
  resolve(use_cache)
  @target
end