Пример #1
0
// LookupPeers busca peers en la red
func LookupPeers() <-chan []string {
	entriesCh := make(chan *mdns.ServiceEntry, 4)
	resChan := make(chan []string, 1)
	go func(ch chan<- []string) {
		var entries []string             // peers
		addrs, _ := net.InterfaceAddrs() // interfaces locales
		for entry := range entriesCh {
			addr := entry.AddrV4 // dirección de host encontrado
			skip := false        // saltar o no esta dirección
			// buscar si peer encontrado es máquina propia
			for i := range addrs {
				n := addrs[i].(*net.IPNet) // hacer type assertion
				if n.IP.Equal(addr) {
					skip = true
				}
			}
			if !skip {
				entries = append(entries, net.JoinHostPort(addr.String(),
					fmt.Sprintf("%d", entry.Port)))
			}
		}
		resChan <- entries
	}(resChan)
	mdns.Lookup("_flow._tcp", entriesCh)
	close(entriesCh)
	return resChan
}
Пример #2
0
func main() {
	entriesCh := make(chan *mdns.ServiceEntry, 4)
	go func() {
		for entry := range entriesCh {
			fmt.Printf("Got new entry: %v\n", entry)
		}
	}()

	// Start the lookup
	mdns.Lookup("_hydra._tcp", entriesCh)
	close(entriesCh)
}
Пример #3
0
func main() {
	// Make a channel for results and start listening
	entriesCh := make(chan *mdns.ServiceEntry, 4)
	go func() {
		for entry := range entriesCh {
			fmt.Printf("Got new entry: %v\n", entry)
		}
	}()

	// Start the lookup
	mdns.Lookup("_googlecast._tcp", entriesCh)
	close(entriesCh)
}
Пример #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)
}
Пример #5
0
func main() {

	// Make a channel for results and start listening
	entriesCh := make(chan *mdns.ServiceEntry, 4)
	go func() {
		for entry := range entriesCh {
			fmt.Printf("Gots new entry: %v\n", entry.Name)

			if strings.Contains(entry.Name, "LDLN\\ Basestation") {

				clientConnect(entry.Host, entry.Port)
			}
		}
	}()

	// Start the lookup
	for {
		mdns.Lookup("_http._tcp", entriesCh)
		time.Sleep(60000 * time.Millisecond)
	}
	defer close(entriesCh)
}
Пример #6
0
func (c *client) findPeers() {

	query := "_ninja-homecloud-mqtt._tcp"

	// Make a channel for results and start listening
	entriesCh := make(chan *mdns.ServiceEntry, 4)
	go func() {
		for entry := range entriesCh {

			if !strings.Contains(entry.Name, query) {
				continue
			}
			nodeInfo := parseMdnsInfo(entry.Info)

			id, ok := nodeInfo["ninja.sphere.node_id"]

			if !ok {
				log.Warningf("Found a node, but couldn't get it's node id. %v", entry)
				continue
			}

			if id == config.Serial() {
				// It's me.
				continue
			}

			user, ok := nodeInfo["ninja.sphere.user_id"]
			if !ok {
				log.Warningf("Found a node, but couldn't get it's user id. %v", entry)
				continue
			}

			site, ok := nodeInfo["ninja.sphere.site_id"]
			siteUpdated, ok := nodeInfo["ninja.sphere.site_updated"]
			masterNodeID, ok := nodeInfo["ninja.sphere.master_node_id"]

			if user == config.MustString("userId") {

				if site == config.MustString("siteId") {
					log.Infof("Found a sibling node (%s) - %s", id, entry.Addr)

					siteUpdatedInt, err := strconv.ParseInt(siteUpdated, 10, 64)

					if err != nil {
						log.Warningf("Failed to read the site_updated field (%s) on node %s - %s", siteUpdated, id, entry.Addr)
					} else {
						if int(siteUpdatedInt) > config.MustInt("siteUpdated") {

							log.Infof("Found node (%s - %s) with a newer site update time (%s).", id, entry.Addr, siteUpdated)

							info := &meshInfo{
								MasterNodeID: masterNodeID,
								SiteID:       config.MustString("siteId"),
								SiteUpdated:  int(siteUpdatedInt),
							}

							err := saveMeshInfo(info)
							if err != nil {
								log.Warningf("Failed to save updated mesh info from node: %s - %+v", err, info)
							}

							if masterNodeID == config.MustString("masterNodeId") {
								log.Infof("Updated master id is the same (%s). Moving on with our lives.", masterNodeID)
							} else {
								log.Infof("Master id has changed (was %s now %s). Rebooting", config.MustString("masterNodeId"), masterNodeID)

								reboot()
								return
							}
						}
					}

				} else {
					log.Warningf("Found a node owned by the same user (%s) but from a different site (%s) - ID:%s - %s", user, site, id, entry.Addr)
				}

			} else {
				log.Infof("Found a node owned by another user (%s) (%s) - %s", user, id, entry.Addr)
			}

			if id == config.MustString("masterNodeId") {
				log.Infof("Found the master node (%s) - %s", id, entry.Addr)

				select {
				case c.foundMaster <- true:
				default:
				}

				if !c.bridged {
					c.bridgeToMaster(entry.Addr, entry.Port)
					c.bridged = true
					c.exportNodeDevice()
				}
			}

		}
	}()

	// Start the lookup
	mdns.Lookup(query, entriesCh)
	close(entriesCh)
}