p2plocate

package module
v0.0.0-...-f317c5a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2017 License: MIT Imports: 9 Imported by: 0

README

p2plocate

Package p2plocate is a peer to peer discovery tool that uses UDP broadcasts on the local network to discover services running on other computers within that network and to determine what functions those services support.

Installation

go get github.com/brumawen/p2plocate

Quick Start

s := p2plocate.P2PServer{
    PortNo: 20401,
    Functions: []string{"Function1", "Function2"},
}
s.OnDiscover(func() {
    fmt.Println("Devices discovered on ", s.LastDiscover.String())
    fmt.Println("-----------------------------------------------------------------------")
    fmt.Println(s.Devices)
    fmt.Println("")
})
s.Start()

Documentation

Overview

Package p2plocate is a peer to peer discovery tool that uses UDP broadcasts on the local network to discover services running on other computers within that network and to determine what functions those services support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBroadcastAddress

func GetBroadcastAddress(ip string) string

GetBroadcastAddress returns the broadcast address for the specified IP address's network.

func GetClientID

func GetClientID() string

GetClientID returns the unique client id (UUID) for this application.

func GetLocalBroadcastAddress

func GetLocalBroadcastAddress() string

GetLocalBroadcastAddress returns the broadcast address for the local network

func GetLocalIPAddresses

func GetLocalIPAddresses() ([]string, error)

GetLocalIPAddresses gets a list of valid IPv4 addresses for the local machine

Types

type Device

type Device struct {
	ClientID   string
	Functions  []string
	IPAddress  string
	Discovered time.Time
}

Device ... Holds data about a discovered device

func (*Device) HasFunction

func (d *Device) HasFunction(function string) bool

HasFunction ... Returns whether or not the device offers the specified function

type Msg

type Msg struct {
	MsgType   string
	ClientID  string
	Functions []string
	Data      string
}

Msg ... The structure containing the data that is sent with a broadcast message

func (*Msg) FromJSON

func (m *Msg) FromJSON(j []byte) error

FromJSON ... Loads the data from the specified JSON string

func (*Msg) ToJSON

func (m *Msg) ToJSON() ([]byte, error)

ToJSON ... Converts the data into a JSON string

type P2PServer

type P2PServer struct {
	PortNo        int
	ClientID      string
	BroadcastAddr string

	IsRunning      bool
	LastDiscoverOK bool
	LastDiscover   time.Time
	LastError      error

	Functions []string
	Devices   []Device
	// contains filtered or unexported fields
}

P2PServer is used to locate services running on other devices and determine their functions.

func (*P2PServer) Discover

func (s *P2PServer) Discover() error

Discover broadcasts a discovery message and rebuilds the list of devices from the replies received from the other devices on the local network

func (*P2PServer) GetDevice

func (s *P2PServer) GetDevice(clientID string) (bool, Device)

GetDevice returns the specified device, if it has been discovered.

func (*P2PServer) GetDevicesForFunction

func (s *P2PServer) GetDevicesForFunction(function string) []Device

GetDevicesForFunction returns a list of devices that offer the specified function.

func (*P2PServer) OnDiscover

func (s *P2PServer) OnDiscover(f func())

OnDiscover allows a func to be set that will be called when a device is discovered or updated

func (*P2PServer) Start

func (s *P2PServer) Start() error

Start starts listening for broadcasted messages from other devices.

func (*P2PServer) Stop

func (s *P2PServer) Stop()

Stop stops listening for broadcasted messages from other devices.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL