From a5c696c2dab9ab5cb5d93e17a6a6c1fd11a1e8f9 Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Sun, 21 Jan 2018 22:30:41 +0100 Subject: [PATCH] discovery: rework discovery handling --- discovery/listener.go | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/discovery/listener.go b/discovery/listener.go index 585cd3c..462a6d7 100644 --- a/discovery/listener.go +++ b/discovery/listener.go @@ -15,6 +15,8 @@ import ( "context" "net" + "github.com/pkg/errors" + "github.com/bboozzoo/mconnect/logger" "github.com/bboozzoo/mconnect/protocol/packet" ) @@ -39,26 +41,40 @@ func NewListener() (*Listener, error) { return listener, nil } -func (l *Listener) Receive(ctx context.Context) { +// Discovery conveys the received discovery information +type Discovery struct { + // Packet is the original packet received + Packet *packet.Packet + // Identity is the parsed identity data + Identity *packet.Identity + // From is the address the packet was received from + From *net.UDPAddr +} + +// Receive blocks waiting to receive a discovery packet. Once received, it will +// parse the packet and return a result. +func (l *Listener) Receive(ctx context.Context) (*Discovery, error) { log := logger.FromContext(ctx) buf := make([]byte, 4096) count, addr, err := l.conn.ReadFromUDP(buf) if err != nil { - log.Printf("listen failed: %v", err) + return nil, errors.Wrap(err, "failed to receive a packet") } - log.Printf("got %v bytes from %v", count, addr) - log.Printf("data:\n%s", string(buf)) + log.Debugf("got %v bytes from %v", count, addr) + log.Debugf("data:\n%s", string(buf)) p, err := packet.FromData(buf) if err != nil { - log.Errorf("failed to parse packet: %v", err) - return + return nil, errors.Wrap(err, "failed to parse packet") } - log.Printf("packet: %+v", p) identity, err := p.AsIdentity() if err != nil { - log.Errorf("failed to parse identity packet: %v", err) - return + return nil, errors.Wrap(err, "failed to parse as identity packet") + } + discovery := &Discovery{ + Packet: p, + Identity: identity, + From: addr, } - log.Printf("identity: %+v", identity) + return discovery, nil }