Skip to content

Commit

Permalink
simplify api
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Apr 24, 2018
1 parent 099b52a commit 264dfdf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down
7 changes: 2 additions & 5 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 21 additions & 10 deletions peerdiscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 2 additions & 22 deletions peerdiscovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 264dfdf

Please sign in to comment.