예제 #1
0
func run() error {
	interfaces, err := net.Interfaces()
	if err != nil {
		return err
	}

	zone := zone.NewDockerMachineZone()

	for _, iface := range interfaces {
		if iface.Flags&net.FlagBroadcast == 0 {
			continue
		}

		addrs, err := iface.Addrs()
		if err != nil {
			return err
		}

		if len(addrs) == 0 {
			continue
		}

		log.Printf("Starting server for iface %s", iface.Name)

		_, err = mdns.NewServer(&mdns.Config{Zone: zone, Iface: &iface})

		if err != nil {
			log.Fatal(err)
		}
	}

	return nil
}
예제 #2
0
파일: main.go 프로젝트: Gaboose/mdns
func main() {

	serviceTag := "_foobar._tcp"
	if len(os.Args) > 1 {
		serviceTag = os.Args[1]
	}

	// Setup our service export
	host, err := os.Hostname()
	if err != nil {
		log.Fatal(err)
	}

	info := []string{"My awesome service"}
	service, err := mdns.NewMDNSService(host, serviceTag, "", "", 8000, nil, info)
	if err != nil {
		log.Fatal(err)
	}

	// Create the mDNS server, defer shutdown
	server, err := mdns.NewServer(&mdns.Config{Zone: service})
	if err != nil {
		log.Fatal(err)
	}

	defer server.Shutdown()

	wait()
}
예제 #3
0
파일: networking.go 프로젝트: ArturoVM/flow
func loop(input <-chan common.Command) {
	host, err := os.Hostname()
	if err != nil {
		log.Fatal("unable to announce on mDNS: unable to obtain hostname")
	}
	info := []string{"Flow distributed computing peer"}
	service, err := mdns.NewMDNSService(host, "_flow._tcp", "", "", 3569, nil, info)
	if err != nil {
		log.Fatal("unable to create mDNS service")
	}
	server, err := mdns.NewServer(&mdns.Config{Zone: service})
	if err != nil {
		log.Fatal("unable to start mDNS service")
	}
	defer server.Shutdown()

	for c := range input {
		switch c.Cmd {
		case "lookup-peers":
			p := LookupPeers()
			peerTable := <-p
			if len(peerTable) < 1 {
				out <- Event{
					Type: Error,
					Data: "no peers found",
				}
			} else {
				out <- Event{
					Type: PeersFound,
					Data: peerTable,
				}
			}
		case "select-peer":
			p, err := selectPeer()
			if err != nil {
				out <- Event{
					Type: Error,
					Data: fmt.Sprintf("error selecting peer: %s", err),
				}
			} else {
				out <- Event{
					Type: PeerSelected,
					Data: p,
				}
			}
		case "reply":
			ipTable[c.Args["peer"]] <- c.Args["msg"]
			close(ipTable[c.Args["peer"]])
			delete(ipTable, c.Args["peer"])
		case "eval":
			evalInst := "eval::|:|flow-code|:|" + c.Args["code"] + "|:|flow-code|:|"
			SendEval(evalInst)
		default:
		}
	}
}
예제 #4
0
파일: web.go 프로젝트: ruo91/go-http-mdns
func main() {
	c.Hostname, _ = os.Hostname()
	for _, e := range os.Environ() {
		pair := strings.Split(e, "=")
		c.Env = append(c.Env, EnvVar{pair[0], pair[1]})
	}
	fmt.Println(c)
	static := web.New()
	static.Get("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.Handle("/static/", static)

	goji.Get("/", renderIndex)

	// Setup our service export
	host, _ := os.Hostname()
	info := []string{"simple peer discovery test"}
	service, _ := mdns.NewMDNSService(host, "_http._tcp", "", "", 8000, nil, info)

	// Create the mDNS server, defer shutdown
	fmt.Println("Creating mDNS server")
	server, _ := mdns.NewServer(&mdns.Config{Zone: service})
	defer server.Shutdown()

	// Make a channel for results and start listening
	peers := make(chan *mdns.ServiceEntry, 4)
	go func() {
		fmt.Println("Waiting for peers")
		for entry := range peers {
			known := false
			for _, e := range c.Peers {
				if entry.Name == e {
					known = true
				}
			}
			if known == false {
				fmt.Println("Got new peer", entry.Name)
				c.Peers = append(c.Peers, entry.Name)
			}
		}
	}()

	fmt.Println("Creating mDNS listener")
	//mdns.Lookup("_googlecast._tcp", peers)

	// this will panic without IF_MULTICAST, causing the program to exit
	mdns.Lookup("_http._tcp", peers)

	fmt.Println("Creating HTTP server")
	goji.Serve() // port 8000 by default
	close(peers)
}
예제 #5
0
func loop(input <-chan common.Command) {
	host, err := os.Hostname()
	if err != nil {
		log.Fatal("imposible obtener hostname para publicar servicio mDNS")
	}
	info := []string{"Flow distributed computing peer"}
	service, err := mdns.NewMDNSService(host, "_flow._tcp", "", "", 3569, nil, info)
	if err != nil {
		log.Fatal("imposible crear servicio de mDNS")
	}
	server, err := mdns.NewServer(&mdns.Config{Zone: service})
	if err != nil {
		log.Fatal("imposible iniciar servicio de mDNS")
	}
	defer server.Shutdown()

	for c := range input {
		switch c.Cmd {
		case "lookup-peers":
			p := LookupPeers()
			peerTable := <-p
			out <- Event{
				Type: PeersFound,
				Data: peerTable,
			}
		case "select-peer":
			p, err := SelectPeer()
			if err != nil {
				out <- Event{
					Type: Error,
					Data: fmt.Sprintf("error selecting peer: %s", err),
				}
				fmt.Printf("\nerror selecting peer:\n %s\n", err)
			} else {
				out <- Event{
					Type: PeerSelected,
					Data: p,
				}
			}
		case "send":
			ipTable[c.Args["peer"]] <- c.Args["msg"]
			close(ipTable[c.Args["peer"]])
			delete(ipTable, c.Args["peer"])
		default:
		}
	}
}
예제 #6
0
파일: mdns.go 프로젝트: vladshub/hydra-node
func (dir *Mdns) Register() error {
	service, err := mdns.NewMDNSService("hydra_client",
		"_hydra._tcp",
		"",
		"",
		8000,
		nil,
		[]string{dir.serviceName, dir.componentName},
	)
	fmt.Println(service, err)
	// Create the mDNS server, defer shutdown
	server, err := mdns.NewServer(&mdns.Config{Zone: service})
	if err != nil {
		return err
	}
	defer server.Shutdown()
	return nil
}
예제 #7
0
파일: mdns.go 프로젝트: ZhuJingfa/serf
// NewAgentMDNS is used to create a new AgentMDNS
func NewAgentMDNS(agent *Agent, logOutput io.Writer, replay bool,
	node, discover string, iface *net.Interface, bind net.IP, port int) (*AgentMDNS, error) {
	// Create the service
	service, err := mdns.NewMDNSService(
		node,
		mdnsName(discover),
		"",
		"",
		port,
		[]net.IP{bind},
		[]string{fmt.Sprintf("Serf '%s' cluster", discover)})
	if err != nil {
		return nil, err
	}

	// Configure mdns server
	conf := &mdns.Config{
		Zone:  service,
		Iface: iface,
	}

	// Create the server
	server, err := mdns.NewServer(conf)
	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,
		iface:    iface,
	}

	// Start the background workers
	go m.run()
	return m, nil
}