Exemplo n.º 1
0
// Publish announces the service for the machine's ip address on a random port using mDNS.
func (s *MDNSService) Publish() error {
	ip, err := GetFirstLocalIPAddress()
	if err != nil {
		return err
	}
	log.Println("[INFO] Accessory IP is", ip)

	// Host should end with '.'
	hostname, _ := os.Hostname()
	host := fmt.Sprintf("%s.", strings.Trim(hostname, "."))
	text := s.txtRecords()
	server, err := bonjour.RegisterProxy(s.name, "_hap._tcp.", "", s.port, host, ip.String(), text, nil)
	if err != nil {
		log.Fatal(err)
	}

	s.server = server
	return err
}
Exemplo n.º 2
0
// Publish announces the service for the machine's ip address on a random port using mDNS.
func (s *MDNSService) Publish() error {
	// Host should end with '.'
	hostname, _ := os.Hostname()
	host := fmt.Sprintf("%s.", strings.Trim(hostname, "."))
	text := s.config.txtRecords()

	// 2016-03-14(brutella): Replace whitespaces (" ") from service name
	// with underscores ("_")to fix invalid http host header field value
	// produces by iOS.
	//
	// [Radar] http://openradar.appspot.com/radar?id=4931940373233664
	stripped := strings.Replace(s.config.name, " ", "_", -1)

	server, err := bonjour.RegisterProxy(stripped, "_hap._tcp.", "", s.config.servePort, host, s.config.IP, text, nil)
	if err != nil {
		log.Info.Panic(err)
	}

	s.server = server
	return err
}
Exemplo n.º 3
0
func (s *Service) start() {
	defer s.wg.Done()

	var bonj *bonjour.Server
	defer func(b **bonjour.Server) {
		s.stopBonjour(*b)
	}(&bonj)

	for {
		select {
		case msg := <-s.messages:
			if msg != s.currentMsg {
				s.currentMsg = msg

				s.stopBonjour(bonj)
				bonj = nil

				if msg != "" {
					log.Info("Registering service", "name", msg, "host", s.host, "port", s.port)
					var err error
					bonj, err = bonjour.RegisterProxy(
						msg,
						"_afpovertcp._tcp", "local",
						s.port, s.host, s.host,
						nil, nil)
					if err != nil || bonj == nil {
						log.Error("Failed to register service with bonjour", "err", err)
						bonj = nil
					}
				}
			}
		case <-s.stop:
			log.Info("Ending bonjour-updating routine")
			return
		}
	}
}