Пример #1
0
// NewAgentMDNS is used to create a new AgentMDNS
func NewAgentMDNS(agent *Agent, logOutput io.Writer, replay bool,
	node, discover string, bind net.IP, port int) (*AgentMDNS, error) {
	// Create the service
	service := &mdns.MDNSService{
		Instance: node,
		Service:  mdnsName(discover),
		Addr:     bind,
		Port:     port,
		Info:     fmt.Sprintf("Serf '%s' cluster", discover),
	}
	if err := service.Init(); err != nil {
		return nil, err
	}

	// Create the server
	server, err := mdns.NewServer(&mdns.Config{Zone: service})
	if err != nil {
		return nil, err
	}

	// Initialize the AgentMDNS
	m := &AgentMDNS{
		agent:    agent,
		discover: discover,
		logger:   log.New(logOutput, "", log.LstdFlags),
		seen:     make(map[string]struct{}),
		server:   server,
		replay:   replay,
	}

	// Start the background workers
	go m.run()
	return m, nil
}
Пример #2
0
func advertiseQOTDService(advertiseTcp bool, advertiseUdp bool, port string) *mdns.Server {
	host, _ := os.Hostname()
	println(host)
	service := &mdns.MDNSService{
		Instance: host,
		Service:  "_qotd._tcp",
		Addr:     []byte{0, 0, 0, 0},
		Port:     3333,
		Info:     "QOTD Service",
	}
	service.Init()

	server, _ := mdns.NewServer(&mdns.Config{Zone: service})
	return server
}
Пример #3
0
func HostSync(s *SyncManifest, port uint16) {
	var service *mdns.MDNSService
	var server *mdns.Server

	host, err := os.Hostname()
	if err != nil {
		log.Println("Could not get hostname: ", err)
		goto listen
	}
	service = &mdns.MDNSService{
		Instance: host,
		Service:  "_gosync._tcp",
		Port:     int(port),
		Info:     "gosync hosted directory",
	}
	err = service.Init()
	if err != nil {
		log.Println("Could not init mdns service: ", err)
		goto listen
	}

	server, err = mdns.NewServer(&mdns.Config{Zone: service})
	if err != nil {
		log.Println("Could not create mdns service: ", err)
	}
	defer server.Shutdown()

listen:

	listener, err := net.Listen("tcp", ":"+strconv.Itoa(int(port)))
	defer listener.Close()
	if err != nil {
		log.Fatalln("Could not start server: ", err)
	}

	buf := new(bytes.Buffer)
	w, err := gzip.NewWriterLevel(buf, 9)
	if err != nil {
		log.Fatalln("Could not create compression stream: ", err)
	}

	fmt.Print("Compressing manifest...")
	enc := gob.NewEncoder(w)
	enc.Encode(s)
	w.Flush()
	w.Close()
	fmt.Printf("\r%30s", "")

	manifestEncoded := buf.Bytes()
	buf.Reset()

	fmt.Printf("\ngosync online and listening at :%d\n", port)

	for {
		c, err := listener.Accept()
		if err != nil {
			log.Fatalln("Failed to accept connection: ", err)
		}

		go SendSync(s, c, manifestEncoded)
	}
}