func main() { log.Println("Multicast discovery utility running...") mc := multicast.JoinMulticast(func(peer string, peers []multicast.PeerInfo) { log.Println("Added new peer:", peer) }, func(peer string, peers []multicast.PeerInfo) { log.Println("Removed peer:", peer) }) /* f, err := mc.conn.File() err = syscall.SetsockoptInt(int(f.Fd()), syscall.IPPROTO_IP, syscall.IP_MULTICAST_LOOP, 1) if err != nil { log.Fatal("Error setting up socket for multicast loop") } */ mc.SetPeriod(1) mc.StartMulticast() mc.ListenPeers() c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { <-c log.Println("Leaving multicast group...") mc.LeaveMulticast() log.Println("Quitting multicast discovery...") os.Exit(0) }() // Sleep forever in main goroutine select {} }
// Start begins the local Lantern discovery process func Start(advertise bool, portToAdvertise string) { if service != nil { // Dev error: this service shouldn't be started unless stopped panic("The " + messageType + " service is already registered") return } var err error service, err = ui.Register(messageType, nil, func(write func(interface{}) error) error { // When connecting the UI we push the current peer list. For this reason, we need // to hold this list beyond the add/remove events. return write(buildPeersList()) }) if err != nil { log.Errorf("Unable to register Local Discovery service: %q", err) return } addOrRemoveCb := func(peer string, peersInfo []multicast.PeerInfo) { peersMutex.Lock() lastPeers = peersInfo peersMutex.Unlock() service.Out <- buildPeersList() } mc = multicast.JoinMulticast(addOrRemoveCb, addOrRemoveCb) if advertise { mc.SetPayload(portToAdvertise) mc.StartMulticast() } mc.ListenPeers() }