Class: Salut::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/Salut/Service.rb

Overview

Advertises its service on the local network using Bonjour or is a service that was found using Sault::Browser.

Note that an instance of the service should be used for one or the other, but not both. Some methods are meant for when you are using Service to advertise a service and other methods are meant for when you are working with services that have been discovered.

Instance Attribute Summary collapse

Adding callback extensions collapse

Advertising a service collapse

Working with discovered services collapse

Delegate methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Service

Returns a new instance of Service.

Examples:

Initializing with properties

service = Advertiser.new({
  service_type:'_http._tcp.',
  instance_name:'Test',
  port:3000
})

Initializing with an existing service

service = Advertiser.new({
  service:NSNetService.alloc.initWithDomain('',
                                       type:'_http._tcp.',
                                       name:`hostname -s`.chomp,
                                       port:3000)
})

Parameters:

  • params (Hash{Symbol=>(String,Fixnum,NSNetService)}) (defaults to: {})


42
43
44
45
46
47
48
49
# File 'lib/Salut/Service.rb', line 42

def initialize params = {}
  @service_type  = params[:service_type]
  @instance_name = params[:instance_name]
  @port          = params[:port]
  @service       = params[:service]
  @delegates     = {}
  @advertising   = false
end

Instance Attribute Details

#advertisingBoolean (readonly) Also known as: advertising?

Returns:

  • (Boolean)


13
14
15
# File 'lib/Salut/Service.rb', line 13

def advertising
  @advertising
end

#instance_nameString

Returns:

  • (String)


23
24
25
# File 'lib/Salut/Service.rb', line 23

def instance_name
  @instance_name
end

#portFixnum

Returns:

  • (Fixnum)


26
27
28
# File 'lib/Salut/Service.rb', line 26

def port
  @port
end

#serviceNSNetService (readonly)

Returns:

  • (NSNetService)


17
18
19
# File 'lib/Salut/Service.rb', line 17

def service
  @service
end

#service_typeString

Returns:

  • (String)


20
21
22
# File 'lib/Salut/Service.rb', line 20

def service_type
  @service_type
end

Instance Method Details

#delegate(method) ⇒ Proc

A shortcut for reading/writing from the delegate methods

Parameters:

  • method (Symbol)

Returns:

  • (Proc)


57
58
59
60
61
62
63
# File 'lib/Salut/Service.rb', line 57

def delegate method
  if block_given?
    @delegates[method] = Proc.new
  else
    @delegates[method]
  end
end

#netService(sender, didUpdateTXTRecordData: data) {|sender, data| ... } ⇒ Object

TODO:

should I process the TXT record before giving it to the proc?

Yield Parameters:

  • sender (Salut::Service)

    a reference to self

  • data (NSData)

    the new TXT record



115
116
117
118
119
# File 'lib/Salut/Service.rb', line 115

def netService sender, didNotPublish:error_dict
  @advertising = false
  @delegates[__method__].call self, error_dict if @delegates[__method__]
  Salut.log.info "ERROR: could not advertise service (#{sender.description})\n\t the problem was\n#{error_dict.description}"
end

#netServiceDidPublish(sender) {|sender| ... } ⇒ Object

Yield Parameters:



122
123
124
125
126
# File 'lib/Salut/Service.rb', line 122

def netServiceDidPublish sender
  @advertising = true
  @delegates[__method__].call self if @delegates[__method__]
  Salut.log.info "Successfully advertising service (#{sender.description})"
end

#netServiceDidResolveAddress(sender) {|sender| ... } ⇒ Object

Yield Parameters:



142
143
144
145
# File 'lib/Salut/Service.rb', line 142

def netServiceDidResolveAddress sender
  @delegates[__method__].call self if @delegates[__method__]
  Salut.log.info "Resolved address for service (#{sender.description})"
end

#netServiceDidStop(sender) {|sender| ... } ⇒ Object

Yield Parameters:



156
157
158
159
160
# File 'lib/Salut/Service.rb', line 156

def netServiceDidStop sender
  @advertising = false
  @delegates[__method__].call self if @delegates[__method__]
  Salut.log.info "Stopped advertising/resolving service (#{sender.description})"
end

#netServiceWillPublish(sender) {|sender| ... } ⇒ Object

Yield Parameters:



108
109
110
111
# File 'lib/Salut/Service.rb', line 108

def netServiceWillPublish sender
  @delegates[__method__].call self if @delegates[__method__]
  Salut.log.info "Starting to advertise service (#{sender.description})"
end

#netServiceWillResolve(sender) {|sender| ... } ⇒ Object

Yield Parameters:



129
130
131
132
# File 'lib/Salut/Service.rb', line 129

def netServiceWillResolve sender
  @delegates[__method__].call self if @delegates[__method__]
  Salut.log.info "Resolving service (#{sender.description})"
end

#resolve(timeout = 60.0) ⇒ Object

A more Ruby-like #resolveWithTimeout by supporting a default argument

Parameters:

  • timeout (Float) (defaults to: 60.0)

    number of seconds to wait before timing out



99
100
101
102
# File 'lib/Salut/Service.rb', line 99

def resolve timeout = 60.0
  @service.delegate = self
  @service.resolveWithTimeout timeout
end

#start_advertising(domain = '') ⇒ Object

Start advertising the service. If you want to change the service type, instance name, or port, you will have to #stop_advertising first.

If there is an error creating the underlying NSNetService object (usually because one of @service_type, @instance_name, and @port are not specified) then you will get a NilClass NoMethodError when the method tries to set the delegate.

Parameters:

  • domain (String) (defaults to: '')

    defaults to all domains



77
78
79
80
81
82
83
84
# File 'lib/Salut/Service.rb', line 77

def start_advertising domain = ''
  @service = NSNetService.alloc.initWithDomain domain,
                                          type:@service_type,
                                          name:@instance_name,
                                          port:@port
  @service.delegate = self
  @service.publish
end

#stop_advertisingObject

Stop advertising the service, which is a nice thing to do when you are cleaning up before exiting your code, but the script/program exiting will also cause the service to stop being published.



89
90
91
92
# File 'lib/Salut/Service.rb', line 89

def stop_advertising
  @service.stop
  @service = nil
end