Ejemplo n.º 1
0
Archivo: main.go Proyecto: 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()
}
Ejemplo n.º 2
0
// findService finds a mdns service by its fully qualified name.
func (dm *dockerMachineZone) findService(fqn string) (*mdns.MDNSService, error) {
	dm.lock.Lock()
	defer dm.lock.Unlock()

	entry, present := dm.services[fqn]
	if present {
		if time.Now().Before(entry.expiryDate) {
			return entry.service, nil
		}

		dm.services[fqn] = nil
	}

	machineName := fqn[0 : len(fqn)-len(".local.")]

	ip, err := findIP(machineName)
	if err != nil {
		return nil, err
	}

	service, err := mdns.NewMDNSService("DockerMachine", "_ssh._tcp", "local.", fqn, 22, []net.IP{ip}, []string{"DockerMachine " + machineName})
	if err != nil {
		return nil, err
	}

	dm.services[fqn] = &serviceEntry{
		expiryDate: time.Now().Add(time.Duration(dm.Ttl) * time.Second),
		service:    service,
	}

	return service, nil
}
Ejemplo n.º 3
0
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:
		}
	}
}
Ejemplo n.º 4
0
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)
}
Ejemplo n.º 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:
		}
	}
}
Ejemplo n.º 6
0
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
}
Ejemplo n.º 7
0
// 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
}