Class: DNSSD::Service

Inherits:
Object
  • Object
show all
Defined in:
ext/rdnssd_service.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#threadObject (readonly)

Access the services underlying thread. Returns nil if the service is synchronous.

Class Method Details

.DNSSD::Service.fullname(name, type, domain) ⇒ String

Concatenate a three-part domain name (as seen in DNSSD::Reply#fullname()) into a properly-escaped full domain name.

Any dots or slashes in the name must NOT be escaped. May be nil (to construct a PTR record name, e.g. “_ftp._tcp.apple.com”).

The type is the service type followed by the protocol, separated by a dot (e.g. “_ftp._tcp”).

The domain is the domain name, e.g. “apple.com”. Any literal dots or backslashes must be escaped.

Raises a ArgumentError if the full service name cannot be constructed from the arguments.

Returns:

  • (String)


77
78
79
80
81
82
# File 'ext/rdnssd_service.c', line 77

static VALUE
dnssd_service_s_fullname(VALUE klass, VALUE name, VALUE type, VALUE domain)
{
	return dnssd_create_fullname( StringValueCStr(name), StringValueCStr(type),
																StringValueCStr(domain), 1 );
}

.DNSSD::Service.newraises a RuntimeError

Services can only be instantiated using DNSSD.enumerate_domains(), DNSSD.browse(), DNSSD.register(), and DNSSD.resolve().

Returns:

  • (raises a RuntimeError)


107
108
109
110
111
112
# File 'ext/rdnssd_service.c', line 107

static VALUE
dnssd_service_new(int argc, VALUE *argv, VALUE klass)
{
	dnssd_instantiation_error(rb_class2name(klass));
	return Qnil;
}

.DNSSD::Service.split(fullname) ⇒ Array .DNSSD::Service.split_fullname(fullname) ⇒ Array

Split a properly escaped multi-part domain name (as seen in DNSSD::Reply#fullname()) into an array of names.

DNSSD::Service.split('_http._tcp.local.') #=> ["_http.", "_tcp.", "local."]

Overloads:

  • .DNSSD::Service.split(fullname) ⇒ Array

    Returns:

    • (Array)
  • .DNSSD::Service.split_fullname(fullname) ⇒ Array

    Returns:

    • (Array)


94
95
96
97
98
# File 'ext/rdnssd_service.c', line 94

static VALUE
dnssd_service_s_split(VALUE klass, VALUE fullname)
{
	return dnssd_split_fullname(fullname);
}

.DNSSD::Service.split(fullname) ⇒ Array .DNSSD::Service.split_fullname(fullname) ⇒ Array

Split a properly escaped multi-part domain name (as seen in DNSSD::Reply#fullname()) into an array of names.

DNSSD::Service.split('_http._tcp.local.') #=> ["_http.", "_tcp.", "local."]

Overloads:

  • .DNSSD::Service.split(fullname) ⇒ Array

    Returns:

    • (Array)
  • .DNSSD::Service.split_fullname(fullname) ⇒ Array

    Returns:

    • (Array)


94
95
96
97
98
# File 'ext/rdnssd_service.c', line 94

static VALUE
dnssd_service_s_split(VALUE klass, VALUE fullname)
{
	return dnssd_split_fullname(fullname);
}

Instance Method Details

#inspectString

Returns:

  • (String)


259
260
261
262
263
264
265
266
267
268
269
270
# File 'ext/rdnssd_service.c', line 259

static VALUE
dnssd_service_inspect(VALUE self)
{
	VALUE buf = rb_str_buf_new(32);
	rb_str_buf_cat2(buf, "<#");
	rb_str_buf_cat2(buf, rb_obj_classname(self));
	if (dnssd_service_is_stopped(self)) {
		rb_str_buf_cat2(buf, " (stopped)");
	}
	rb_str_buf_cat2(buf, ">");
	return buf;
}

#stopObject

Stops service closing the underlying socket and killing the underlying thread.

It is good practice to all stop running services before exit.

service = DNSSD.browse('_http._tcp') do |r|
    # found a service ...
end
sleep(2)
service.stop


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/rdnssd_service.c', line 171

static VALUE
dnssd_service_stop(VALUE service)
{
	VALUE thread;
	DNSServiceRef *client = (DNSServiceRef*)RDATA(service)->data;
	/* set to null right away for a bit more thread safety */
	RDATA(service)->data = NULL;
	if (client == NULL) rb_raise(rb_eRuntimeError, "service is already stopped");
	dnssd_service_free_client(client);
	thread = rb_ivar_get(service, dnssd_iv_thread);
  rb_ivar_set(service, dnssd_iv_block, Qnil);
  rb_ivar_set(service, dnssd_iv_thread, Qnil);
	
	if (!NIL_P(thread)) {
		/* will raise error if thread is not a Ruby Thread */
		rb_thread_kill(thread);
	}
  return service;
}

#stopped?Boolean

Returns true if service has been stopped, false otherwise.

Returns:

  • (Boolean)


148
149
150
151
152
153
# File 'ext/rdnssd_service.c', line 148

static VALUE
dnssd_service_is_stopped(VALUE service)
{
	DNSServiceRef *client = (DNSServiceRef*)RDATA(service)->data;
  return client == NULL ? Qtrue : Qfalse;
}