Add documentation, fix package local's name, add examples, and LICENSE

This commit is contained in:
Tony Blyler 2018-07-16 19:34:28 -04:00
parent 251638f59b
commit 6786d67033
8 changed files with 161 additions and 4 deletions

14
LICENSE Normal file
View file

@ -0,0 +1,14 @@
BSD-3-Clause License
Copyright (c) 2018, Tony Blyler
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

24
README.md Normal file
View file

@ -0,0 +1,24 @@
# Discovery
[![Documentation](https://godoc.org/gitlab.com/tblyler/discovery?status.svg)](https://godoc.org/gitlab.com/tblyler/discovery)
`discovery` is a pure [Go](https://golang.org) library to provide simple abstractions to discover other hosts on networks.
## Documentation
The [`discovery` documentation for source code is available on GoDoc](https://godoc.org/gitlab.com/tblyler/discovery) or you can run:
```bash
godoc gitlab.com/tblyler/discovery
```
## Testing
Most tests do not require an active network. However, for the ones that do, they only utilize the loopback device and bind to an automatically assigned port via the `0` port.
```bash
go test -cover gitlab.com/tblyler/discovery
```
## Examples
Usable examples are [located here](./examples) or run:
```bash
go get gitlab.com/tblyler/discovery/examples/...
```

Binary file not shown.

View file

@ -0,0 +1,112 @@
package main
import (
"context"
"fmt"
"net"
"os"
"os/signal"
"sync"
"time"
"gitlab.com/tblyler/discovery/local"
)
func main() {
b, err := local.NewBroadcast(
&net.UDPAddr{
IP: net.IPv4zero,
Port: 50000,
},
nil,
)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create new broadcast:", err)
os.Exit(1)
}
msgChan := make(chan []byte, 5)
errChan := make(chan error, 2)
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
errChan <- b.Listen(ctx, msgChan)
}()
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case msg := <-msgChan:
fmt.Println("Got message:", string(msg))
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(time.Second * time.Duration(5))
defer ticker.Stop()
hostname, _ := os.Hostname()
msg := []byte(hostname)
interfaces, _ := net.InterfaceAddrs()
for _, inter := range interfaces {
msg = append(msg, []byte("\n"+inter.String())...)
}
msg = append(msg, []byte("\n")...)
for {
select {
case <-ctx.Done():
return
case tTime := <-ticker.C:
fmt.Println("Sending message at", tTime)
err := b.Send(msg)
if err != nil {
errChan <- err
return
}
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
defer cancel()
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt)
for {
select {
case err := <-errChan:
fmt.Println("Got error:", err)
return
case <-sigChan:
fmt.Println("Got signal to quit, quitting")
return
}
}
}()
wg.Wait()
}

View file

@ -1,4 +1,4 @@
package broadcast package local
import ( import (
"context" "context"
@ -34,7 +34,7 @@ var (
ErrBroadcastSendBadSize = fmt.Errorf("broadcast message size must be > 0B and <= %dB", MaxUDPPacketSize) ErrBroadcastSendBadSize = fmt.Errorf("broadcast message size must be > 0B and <= %dB", MaxUDPPacketSize)
) )
// Broadcast implements Seeker with local network broadcasting with IPv4 broadcasting // Broadcast implements Seeker with local network broadcasting via IPv4 broadcasting
type Broadcast struct { type Broadcast struct {
listenAddr *net.UDPAddr listenAddr *net.UDPAddr
bcastIPs []net.IP bcastIPs []net.IP
@ -66,7 +66,10 @@ func NewBroadcast(listenAddr *net.UDPAddr, bcastIPs []net.IP) (b *Broadcast, err
return return
} }
// Listen for incoming Send requests from other Broadcast implementations // Listen for incoming Send requests from other Broadcast implementations.
// It is very likely that if this instance is listening on the same broadcast IP as one that
// it is also sending from, that it will receive what it is sent. It is up to the user to
// decide whether or not they care about receiving messages they are also sending.
func (b *Broadcast) Listen(ctx context.Context, msgChan chan<- []byte) error { func (b *Broadcast) Listen(ctx context.Context, msgChan chan<- []byte) error {
b.lconnLock.Lock() b.lconnLock.Lock()
defer b.lconnLock.Unlock() defer b.lconnLock.Unlock()

View file

@ -1,4 +1,4 @@
package broadcast package local
import ( import (
"context" "context"

3
local/local.go Normal file
View file

@ -0,0 +1,3 @@
// Package local provides Seeker implementations that are designed to only work on
// LAN interfaces.
package local

View file

@ -1,3 +1,4 @@
// Package discovery provides discovery interfaces to find other users of discovery on networks.
package discovery package discovery
import "context" import "context"