func main() { me, err := url.Parse(*myEndpoint) if err != nil { panic(err) } log.Printf("my address is: %s", me.String()) multicast, err := url.Parse(*multicastEndpoint) if err != nil { panic(err) } log.Printf("joining multicast group on: %s", multicast.String()) go func() { http.HandleFunc(progol.ValidationPath, progol.ValidationHandler) log.Printf("starting HTTP server on %s", me.Host) log.Fatal(http.ListenAndServe(me.Host, nil)) }() d, err := progol.NewMulticastDiscovery(*myEndpoint, multicast.Host, 3*time.Second) if err != nil { log.Fatalf("creating Multicast Discovery: %s", err) } v := progol.NewValidator(d, 1*time.Second) c := make(chan []progol.Peer) go func() { for peers := range c { log.Printf("Peers: %v", peers) } }() v.Subscribe(c) select {} }
// newMulticastDiscovery creates a progol.MulticastDiscovery for this process, // choosing a random port. func newMulticastDiscovery(host, group string, ttl time.Duration) (progol.Discovery, int, error) { for i := 0; i < 10; i++ { port := 10000 + rand.Intn(9999) laddr := "http://" + net.JoinHostPort(host, fmt.Sprint(port)) discovery, err := progol.NewMulticastDiscovery(laddr, group, ttl) if err != nil { log.Printf("%s: %s, retrying...", laddr, err) time.Sleep(time.Duration(10+rand.Intn(40)) * time.Millisecond) continue } log.Printf("Multicast Discovery on %s", laddr) return discovery, port, nil } return nil, 0, fmt.Errorf("couldn't find free port") }