From 264dfdffb3be93416441091e1b389948f29a83c8 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 24 Apr 2018 06:01:35 -0700 Subject: [PATCH] simplify api --- README.md | 5 ++--- examples/main.go | 7 ++----- peerdiscovery.go | 31 +++++++++++++++++++++---------- peerdiscovery_test.go | 24 ++---------------------- 4 files changed, 27 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 35a4ab1..8d7b142 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![travis](https://travis-ci.org/schollz/peerdiscovery.svg?branch=master)](https://travis-ci.org/schollz/peerdiscovery) [![go report card](https://goreportcard.com/badge/github.com/schollz/peerdiscovery)](https://goreportcard.com/report/github.com/schollz/peerdiscovery) -[![coverage](https://img.shields.io/badge/coverage-83%25-brightgreen.svg)](https://gocover.io/github.com/schollz/peerdiscovery) +[![coverage](https://img.shields.io/badge/coverage-76%25-brightgreen.svg)](https://gocover.io/github.com/schollz/peerdiscovery) [![godocs](https://godoc.org/github.com/schollz/peerdiscovery?status.svg)](https://godoc.org/github.com/schollz/peerdiscovery) Pure-go library for cross-platform thread-safe local peer discovery using UDP broadcast. I needed a peer discovery for [croc](https://github.com/schollz/croc) and everything I tried had problems, so I made another one. @@ -21,8 +21,7 @@ go get -u github.com/schollz/peerdiscovery The following is a code to find the first peer on the local network and print it out. ```golang -p, _ := peerdiscovery.New(peerdiscovery.Settings{Limit: 1}) -discoveries, _ := p.Discover() +discoveries, _ := peerdiscovery.Discover(peerdiscovery.Settings{Limit: 1}) for _, d := range discoveries { fmt.Printf("discovered '%s'\n", d.Address) } diff --git a/examples/main.go b/examples/main.go index 98dd58b..5013308 100644 --- a/examples/main.go +++ b/examples/main.go @@ -23,17 +23,14 @@ func main() { }() // discover peers - p, err := peerdiscovery.New(peerdiscovery.Settings{ + discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{ Limit: -1, Payload: []byte(randStringBytesMaskImprSrc(10)), Delay: 500 * time.Millisecond, TimeLimit: 10 * time.Second, }) - if err != nil { - log.Fatal(err) - } - discoveries, err := p.Discover() + // print out results if err != nil { log.Fatal(err) } else { diff --git a/peerdiscovery.go b/peerdiscovery.go index bb63681..9c178dd 100644 --- a/peerdiscovery.go +++ b/peerdiscovery.go @@ -44,23 +44,25 @@ type Settings struct { multicastAddressNumbers []uint8 } -// PeerDiscovery is the object that can do the discovery for finding LAN peers. -type PeerDiscovery struct { +// peerDiscovery is the object that can do the discovery for finding LAN peers. +type peerDiscovery struct { settings Settings + received map[string][]byte sync.RWMutex } -// New returns a new PeerDiscovery object which can be used to discover peers. +// initialize returns a new peerDiscovery object which can be used to discover peers. // The settings are optional. If any setting is not supplied, then defaults are used. // See the Settings for more information. -func New(settings ...Settings) (p *PeerDiscovery, err error) { - p = new(PeerDiscovery) +func initialize(settings Settings) (p *peerDiscovery, err error) { + p = new(peerDiscovery) p.Lock() defer p.Unlock() - if len(settings) > 0 { - p.settings = settings[0] - } + + // initialize settings + p.settings = settings + // defaults if p.settings.Port == "" { p.settings.Port = "9999" @@ -97,7 +99,16 @@ func New(settings ...Settings) (p *PeerDiscovery, err error) { // Discover will use the created settings to scan for LAN peers. It will return // an array of the discovered peers and their associate payloads. It will not // return broadcasts sent to itself. -func (p *PeerDiscovery) Discover() (discoveries []Discovered, err error) { +func Discover(settings ...Settings) (discoveries []Discovered, err error) { + s := Settings{} + if len(settings) > 0 { + s = settings[0] + } + p, err := initialize(s) + if err != nil { + return + } + p.RLock() address := p.settings.MulticastAddress + ":" + p.settings.Port portNum := p.settings.portNum @@ -193,7 +204,7 @@ const ( // Listen binds to the UDP address and port given and writes packets received // from that address to a buffer which is passed to a hander -func (p *PeerDiscovery) listen() (recievedBytes []byte, err error) { +func (p *peerDiscovery) listen() (recievedBytes []byte, err error) { p.RLock() address := p.settings.MulticastAddress + ":" + p.settings.Port portNum := p.settings.portNum diff --git a/peerdiscovery_test.go b/peerdiscovery_test.go index 2f40223..abc8c88 100644 --- a/peerdiscovery_test.go +++ b/peerdiscovery_test.go @@ -7,34 +7,14 @@ import ( "github.com/stretchr/testify/assert" ) -func TestSettings(t *testing.T) { - _, err := New() - assert.Nil(t, err) - - _, err = New(Settings{ - Limit: -1, - Payload: []byte("payload"), - Delay: 500 * time.Millisecond, - TimeLimit: 10 * time.Second, - }) - assert.Nil(t, err) - - _, err = New(Settings{ - MulticastAddress: "assd.asdf.asdf.asfd", - }) - assert.NotNil(t, err) -} - func TestDiscovery(t *testing.T) { - p, _ := New(Settings{ + // should not be able to "discover" itself + discoveries, err := Discover(Settings{ Limit: -1, Payload: []byte("payload"), Delay: 500 * time.Millisecond, TimeLimit: 5 * time.Second, }) - - // should not be able to "discover" itself - discoveries, err := p.Discover() assert.Nil(t, err) assert.Zero(t, len(discoveries)) }