Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an option to change the listen-on address and exposes the UDP connection #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/jsimonetti/go-artnet/packet/code"
)

var broadcastAddr = net.UDPAddr{
var defaultBroadcastAddr = net.UDPAddr{
IP: []byte{0x02, 0xff, 0xff, 0xff},
Port: int(packet.ArtNetPort),
}
Expand Down Expand Up @@ -89,6 +89,8 @@ type Controller struct {
InputAddress map[Address]*ControlledNode
nodeLock sync.Mutex

broadcastAddr net.UDPAddr

shutdownCh chan struct{}

maxFPS int
Expand All @@ -101,9 +103,10 @@ type Controller struct {
// NewController return a Controller
func NewController(name string, ip net.IP, log Logger, opts ...Option) *Controller {
c := &Controller{
cNode: NewNode(name, code.StController, ip, log),
log: log,
maxFPS: 1000,
cNode: NewNode(name, code.StController, ip, log),
log: log,
maxFPS: 1000,
broadcastAddr: defaultBroadcastAddr,
}

for _, opt := range opts {
Expand Down Expand Up @@ -161,7 +164,7 @@ func (c *Controller) pollLoop() {

// send ArtPollPacket
c.cNode.sendCh <- netPayload{
address: broadcastAddr,
address: c.broadcastAddr,
data: b,
}
c.cNode.pollCh <- packet.ArtPollPacket{}
Expand All @@ -172,7 +175,7 @@ func (c *Controller) pollLoop() {
case <-c.pollTicker.C:
// send ArtPollPacket
c.cNode.sendCh <- netPayload{
address: broadcastAddr,
address: c.broadcastAddr,
data: b,
}
c.cNode.pollCh <- packet.ArtPollPacket{}
Expand Down
37 changes: 27 additions & 10 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ type Node struct {
// Config holds the configuration of this node
Config NodeConfig

broadcastAddr net.UDPAddr

// conn is the UDP connection this node will listen on
conn *net.UDPConn
localAddr net.UDPAddr
sendCh chan netPayload
recvCh chan netPayload
conn *net.UDPConn
localAddr net.UDPAddr
listenAddr net.UDPAddr
sendCh chan netPayload
recvCh chan netPayload

// shutdownCh will be closed on shutdown of the node
shutdownCh chan struct{}
Expand All @@ -49,15 +52,25 @@ type netPayload struct {
}

// NewNode return a Node
func NewNode(name string, style code.StyleCode, ip net.IP, log Logger) *Node {
func NewNode(name string, style code.StyleCode, ip net.IP, log Logger, opts ...NodeOption) *Node {
n := &Node{
Config: NodeConfig{
Name: name,
Type: style,
},
conn: nil,
shutdown: true,
log: log.With(Fields{"type": "Node"}),
listenAddr: net.UDPAddr{Port: packet.ArtNetPort},
broadcastAddr: defaultBroadcastAddr,
conn: nil,
shutdown: true,
log: log.With(Fields{"type": "Node"}),
}

for _, opt := range opts {
n.SetOption(opt)
}

for _, opt := range opts {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this code block duplicated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Must have slipped in while merging. I don't quite know why it thought that was a merge issue on that line... I'll fix it.

n.SetOption(opt)
}

// initialize required node callbacks
Expand All @@ -80,6 +93,10 @@ func NewNode(name string, style code.StyleCode, ip net.IP, log Logger) *Node {
return n
}

func (n *Node) Connection() *net.UDPConn {
return n.conn
}

// Stop will stop all running routines and close the network connection
func (n *Node) Stop() {
n.shutdownLock.Lock()
Expand Down Expand Up @@ -110,7 +127,7 @@ func (n *Node) Start() error {
n.shutdownCh = make(chan struct{})
n.shutdown = false

c, err := net.ListenPacket("udp4", fmt.Sprintf(":%d", packet.ArtNetPort))
c, err := net.ListenPacket("udp4", n.listenAddr.String())
if err != nil {
n.shutdownErr = fmt.Errorf("error net.ListenPacket: %s", err)
n.log.With(Fields{"error": err}).Error("error net.ListenPacket")
Expand Down Expand Up @@ -150,7 +167,7 @@ func (n *Node) pollReplyLoop() {
n.log.With(nil).Debug("sending ArtPollReply")

n.sendCh <- netPayload{
address: broadcastAddr,
address: n.broadcastAddr,
data: me,
}

Expand Down
60 changes: 60 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package artnet

import (
"net"

"github.com/jsimonetti/go-artnet/packet"
)

// Option is a functional option handler for Controller.
type Option func(*Controller) error

Expand All @@ -15,3 +21,57 @@ func MaxFPS(fps int) Option {
return nil
}
}

// BroadcastAddr sets the broadcast address to use; defaults to 2.255.255.255:6454
func BroadcastAddr(addr net.UDPAddr) Option {
return func(c *Controller) error {
c.broadcastAddr = addr
return nil
}
}

// ListenAddr sets the listen address and port to use; defaults to :6454 if unset
func ListenAddress(addr net.UDPAddr) Option {
return func(c *Controller) error {
return NodeListenAddress(addr)(c.cNode)
}
}

// ListenIP sets the listen IP to use; defaults to :6454 if unset
func ListenIP(ip net.IP) Option {
return func(c *Controller) error {
return NodeListenIP(ip)(c.cNode)
}
}

// NodeOption is a functional option handler for Node.
type NodeOption func(*Node) error

// SetOption runs a functional option against Node.
func (n *Node) SetOption(option NodeOption) error {
return option(n)
}

// NodeBroadcastAddress sets the broadcast address to use; defaults to 2.255.255.255:6454
func NodeBroadcastAddress(addr net.UDPAddr) NodeOption {
return func(n *Node) error {
n.broadcastAddr = addr
return nil
}
}

// NodeListenAddress sets the listen address and port to use; defaults to :6454 if unset
func NodeListenAddress(addr net.UDPAddr) NodeOption {
return func(n *Node) error {
n.listenAddr = addr
return nil
}
}

// NodeListenIP sets the listen IP to use; defaults to :6454 if unset
func NodeListenIP(ip net.IP) NodeOption {
return func(n *Node) error {
n.listenAddr = net.UDPAddr{IP: ip, Port: packet.ArtNetPort}
return nil
}
}