|
| 1 | +package peer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/grandcat/zeroconf" |
| 8 | +) |
| 9 | + |
| 10 | +// Server defines the server that registers the peer and discover other peers. |
| 11 | +// It wrappes the logic for zeroconf. |
| 12 | +// |
| 13 | +// Done is a channel that is closed when the Server finish the execution. |
| 14 | +type Server struct { |
| 15 | + Done chan interface{} |
| 16 | + name string |
| 17 | + port int |
| 18 | +} |
| 19 | + |
| 20 | +// NewServer will create a new peer discover server. |
| 21 | +// |
| 22 | +// The port is the port number used for the TCP connections |
| 23 | +// from where the files will be transfered. |
| 24 | +func NewServer(name string, port int) *Server { |
| 25 | + return &Server{ |
| 26 | + name: name, |
| 27 | + port: port, |
| 28 | + Done: make(chan interface{}), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Run will start the Server that will register the peer and start looking for |
| 33 | +// peers on the local network with zeroconf. |
| 34 | +// |
| 35 | +// Arguments: |
| 36 | +// |
| 37 | +// - ctx is a context with cancel, what will be used to terminate the server. |
| 38 | +// |
| 39 | +// - peers is a channel of Peer that will stream each peer discovered on the |
| 40 | +// network. |
| 41 | +// |
| 42 | +// Errors: |
| 43 | +// |
| 44 | +// - on registering the service |
| 45 | +// |
| 46 | +// - on start listening |
| 47 | +// |
| 48 | +// - on discover |
| 49 | +func (s *Server) Run(ctx context.Context, peers chan Peer) error { |
| 50 | + instance := fmt.Sprintf("catch-%s", s.name) |
| 51 | + sv, err := zeroconf.Register(instance, serviceName, serviceDomain, s.port, nil, nil) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("peer server fail to register: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + resolver, err := zeroconf.NewResolver(zeroconf.SelectIPTraffic(zeroconf.IPv4)) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("peer server fail to start listening: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + entries := make(chan *zeroconf.ServiceEntry) |
| 62 | + err = resolver.Browse(ctx, serviceName, serviceDomain, entries) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("peer server fail to discover: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + go func(results <-chan *zeroconf.ServiceEntry) { |
| 68 | + for entry := range results { |
| 69 | + peers <- newPeer(entry.HostName, entry.AddrIPv4[0], entry.Port) |
| 70 | + } |
| 71 | + sv.Shutdown() |
| 72 | + close(s.Done) |
| 73 | + // entries is already closed by the context cancelation |
| 74 | + }(entries) |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
0 commit comments