Class: Salut::Service
- Inherits:
-
Object
- Object
- Salut::Service
- 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
- #advertising ⇒ Boolean (also: #advertising?) readonly
- #instance_name ⇒ String
- #port ⇒ Fixnum
- #service ⇒ NSNetService readonly
- #service_type ⇒ String
Adding callback extensions collapse
-
#delegate(method) ⇒ Proc
A shortcut for reading/writing from the delegate methods.
Advertising a service collapse
-
#start_advertising(domain = '') ⇒ Object
Start advertising the service.
-
#stop_advertising ⇒ Object
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.
Working with discovered services collapse
-
#resolve(timeout = 60.0) ⇒ Object
A more Ruby-like #resolveWithTimeout by supporting a default argument.
Delegate methods collapse
- #netService(sender, didUpdateTXTRecordData: data) {|sender, data| ... } ⇒ Object
- #netServiceDidPublish(sender) {|sender| ... } ⇒ Object
- #netServiceDidResolveAddress(sender) {|sender| ... } ⇒ Object
- #netServiceDidStop(sender) {|sender| ... } ⇒ Object
- #netServiceWillPublish(sender) {|sender| ... } ⇒ Object
- #netServiceWillResolve(sender) {|sender| ... } ⇒ Object
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Service
constructor
A new instance of Service.
Constructor Details
#initialize(params = {}) ⇒ Service
Returns a new instance of Service.
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
#advertising ⇒ Boolean (readonly) Also known as: advertising?
13 14 15 |
# File 'lib/Salut/Service.rb', line 13 def advertising @advertising end |
#instance_name ⇒ String
23 24 25 |
# File 'lib/Salut/Service.rb', line 23 def instance_name @instance_name end |
#port ⇒ Fixnum
26 27 28 |
# File 'lib/Salut/Service.rb', line 26 def port @port end |
#service ⇒ NSNetService (readonly)
17 18 19 |
# File 'lib/Salut/Service.rb', line 17 def service @service end |
#service_type ⇒ 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
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
should I process the TXT record before giving it to the proc?
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
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
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
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
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
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
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.
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_advertising ⇒ Object
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 |