Skip to content

Commit

Permalink
Add option to change broadcast address (#35)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Murray <brian@bmurray.ca>
  • Loading branch information
bmurray committed Jan 5, 2024
1 parent 1e9a3d5 commit 6247bc8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
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
17 changes: 12 additions & 5 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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
Expand Down Expand Up @@ -49,15 +51,20 @@ 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"}),
broadcastAddr: defaultBroadcastAddr,
conn: nil,
shutdown: true,
log: log.With(Fields{"type": "Node"}),
}

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

// initialize required node callbacks
Expand Down Expand Up @@ -150,7 +157,7 @@ func (n *Node) pollReplyLoop() {
n.log.With(nil).Debug("sending ArtPollReply")

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

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

import "net"

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

Expand All @@ -15,3 +17,27 @@ 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
}
}

// 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
}
}

0 comments on commit 6247bc8

Please sign in to comment.