Esempio n. 1
0
func (z *zeroconf) updatePrinterTXT(name, ty, url, id string, online bool) error {
	z.spMutex.Lock()
	defer z.spMutex.Unlock()

	r, exists := z.printers[name]
	if !exists {
		return fmt.Errorf("printer %s cannot be updated for Avahi publishing; it was never added", name)
	}

	r.ty = ty
	r.url = url
	r.id = id
	r.online = online

	if z.state == C.AVAHI_CLIENT_S_RUNNING && r.group != nil {
		txt := prepareTXT(ty, url, id, online)
		defer C.avahi_string_list_free(txt)

		C.avahi_threaded_poll_lock(z.threadedPoll)
		defer C.avahi_threaded_poll_unlock(z.threadedPoll)

		if errstr := C.updateAvahiGroup(z.threadedPoll, r.group, r.name, txt); errstr != nil {
			err := fmt.Errorf("Failed to update Avahi group: %s", C.GoString(errstr))
			return err
		}
	}

	z.printers[name] = r
	return nil
}
Esempio n. 2
0
func (z *zeroconf) updatePrinterTXT(name, ty, url, id string, online bool) error {
	z.spMutex.Lock()
	defer z.spMutex.Unlock()

	r, exists := z.printers[name]
	if !exists {
		return fmt.Errorf("printer %s cannot be updated for Avahi publishing; it was never added", name)
	}

	r.ty = ty
	r.url = url
	r.id = id
	r.online = online

	if z.state == C.AVAHI_CLIENT_S_RUNNING && r.group != nil {
		tyC := C.CString(ty)
		defer C.free(unsafe.Pointer(tyC))
		urlC := C.CString(url)
		defer C.free(unsafe.Pointer(urlC))
		idC := C.CString(id)
		defer C.free(unsafe.Pointer(idC))
		var onlineC *C.char
		if online {
			onlineC = C.CString("online")
		} else {
			onlineC = C.CString("offline")
		}
		defer C.free(unsafe.Pointer(onlineC))

		C.avahi_threaded_poll_lock(z.threadedPoll)
		defer C.avahi_threaded_poll_unlock(z.threadedPoll)

		var errstr *C.char
		C.updateAvahiGroup(z.threadedPoll, r.group, r.name, tyC, urlC, idC, onlineC, &errstr)
		if errstr != nil {
			err := errors.New(C.GoString(errstr))
			C.free(unsafe.Pointer(errstr))
			return err
		}
	}

	z.printers[name] = r
	return nil
}