Commit 9afea098 authored by Vesa-Pekka Palmu's avatar Vesa-Pekka Palmu

Improvement pass on the mitti/millumi bridge

Renamed the bridge to cmd/clock-bridge, ported the interface
scanning logic from the clocks and added descriptions for parameters
parent ea0a8433
......@@ -2,7 +2,7 @@ PROJECT_NAME := "clock-8001"
PKG := "gitlab.com/Depili/$(PROJECT_NAME)"
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
BINARIES := clock-8001 matrix-clock sdl-clock
BINARIES := clock-bridge matrix-clock sdl-clock
GOLINT := "$(GOPATH)/bin/golint"
GIT_TAG := $(shell git describe --tags --abbrev=0)
GIT_COMMIT := $(shell git rev-list -1 HEAD)
......@@ -36,7 +36,7 @@ dep: ## Get the dependencies
@go get -v -d ./...
build: dep ## Build the binary file
@go build -ldflags $(GO_LD_FLAGS) gitlab.com/Depili/clock-8001
@go build -ldflags $(GO_LD_FLAGS) gitlab.com/Depili/clock-8001/cmd/clock-bridge
@go build -ldflags $(GO_LD_FLAGS) gitlab.com/Depili/clock-8001/cmd/matrix-clock
@go build -ldflags $(GO_LD_FLAGS) gitlab.com/Depili/clock-8001/cmd/sdl-clock
......
......@@ -3,47 +3,116 @@ package clock
import (
"fmt"
"github.com/hypebeast/go-osc/osc"
"log"
"net"
"strings"
"time"
)
// ClientOptions common options for client instances
type ClientOptions struct {
Connect string `long:"clock-client-connect"` // Address to connect to with OSC
Connect string `long:"clock-client-connect" description:"Address to send clock osc messages to" default:"255.255.255.255:1245"` // Address to connect to with OSC
}
// MakeClient Create a clock OSC client
func (options ClientOptions) MakeClient() (*Client, error) {
var client = Client{}
if udpAddr, err := net.ResolveUDPAddr("udp", options.Connect); err != nil {
return nil, err
} else if udpConn, err := net.DialUDP("udp", nil, udpAddr); err != nil {
return nil, err
} else {
client.udpConn = udpConn
}
client.udpDest = options.Connect
// Poll for network interface changes
go client.interfaceMonitor()
return &client, nil
}
// Client A clock osc client
type Client struct {
udpConn *net.UDPConn
udpDest string
oscDests *feedbackDestinations
}
// Monitor for interface address changes and update broadcast destinations
func (client *Client) interfaceMonitor() {
log.Printf("Monitoring network interface changes\n")
port := strings.Join(strings.Split(client.udpDest, ":")[1:], "")
log.Printf("OSC feedback port: %v", port)
for {
time.Sleep(interfacePollTime)
log.Printf("Updating feedback connections\n")
conns := feedbackDestinations{
udpConns: make([]*net.UDPConn, 0),
}
if !strings.Contains(client.udpDest, "255.255.255.255") {
log.Printf(" -> Trying single address: %v\n", client.udpDest)
if udpAddr, err := net.ResolveUDPAddr("udp", client.udpDest); err != nil {
log.Printf(" -> Failed to resolve OSC feedback address: %v", err)
} else if udpConn, err := net.DialUDP("udp", nil, udpAddr); err != nil {
log.Printf(" -> Failed to open OSC feedback address: %v", err)
} else {
log.Printf("OSC feedback: sending to %v", client.udpDest)
conns.udpConns = append(conns.udpConns, udpConn)
}
continue
}
addrs, _ := net.InterfaceAddrs()
for _, addr := range addrs {
ip, n, err := net.ParseCIDR(addr.String())
if err != nil {
log.Printf(" -> error parsing network\n")
} else {
if ip.IsLoopback() {
// Ignore loopback interfaces
continue
} else if ip.To4() != nil {
broadcast := net.IP(make([]byte, 4))
for i := range n.IP {
broadcast[i] = n.IP[i] | (^n.Mask[i])
}
log.Printf(" -> using broadcast address %v", broadcast)
dest := fmt.Sprintf("%v:%v", broadcast, port)
if udpAddr, err := net.ResolveUDPAddr("udp", dest); err != nil {
log.Printf(" -> Failed to resolve OSC broadcast address %v: %v", dest, err)
} else if udpConn, err := net.DialUDP("udp", nil, udpAddr); err != nil {
log.Printf(" -> Failed to open OSC broadcast address %v: %v", dest, err)
} else {
log.Printf("OSC feedback: sending to %v", dest)
conns.udpConns = append(conns.udpConns, udpConn)
}
}
}
}
client.oscDests = &conns
}
}
// Print the connection info of a Client
func (client *Client) String() string {
return fmt.Sprintf("%v", client.udpConn.RemoteAddr())
return fmt.Sprintf("%v", client.udpDest)
}
func (client *Client) send(packet osc.Packet) error {
if data, err := packet.MarshalBinary(); err != nil {
return err
} else if _, err := client.udpConn.Write(data); err != nil {
return err
} else {
if client.oscDests == nil {
// No osc connection
return nil
}
data, err := packet.MarshalBinary()
if err != nil {
return err
}
for _, conn := range client.oscDests.udpConns {
if _, err := conn.Write(data); err != nil {
return err
}
}
return nil
}
// SendDisplay Send a /clock/display message
......
......@@ -8,15 +8,16 @@ import (
"gitlab.com/Depili/clock-8001/millumin"
"gitlab.com/Depili/clock-8001/mitti"
"log"
"os"
"regexp"
)
var options struct {
ClockClientOptions clock.ClientOptions `group:"qmsk/osc-tally clock client"`
ClockRemainingThreshold float32 `long:"clock-remaining-threshold" default:"20"`
ClockRemainingThreshold float32 `long:"clock-remaining-threshold" description:"Remaining time highlight threshold" default:"20"`
Ignore string `long:"millumin-ignore-layer" value-name:"REGEXP" description:"Ignore matching millumin layers (case-insensitive regexp)" default:"ignore"`
ignoreRegexp *regexp.Regexp
ListenAddr string `long:"osc-listen"`
ListenAddr string `long:"osc-listen" description:"Address to listen for mitti/millumin osc messages" default:"0.0.0.0:1234"`
Debug bool `long:"osc-debug"`
}
......@@ -139,11 +140,15 @@ func run(oscServer *osc.Server) error {
func main() {
if _, err := parser.Parse(); err != nil {
log.Fatalf("parse flags: %v", err)
} else {
log.Printf("options: %#v", options)
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
panic(err)
}
}
fmt.Printf("fooo")
regexp, err := regexp.Compile("(?i)" + options.Ignore)
if err != nil {
log.Fatalf("Invalid --millumin-ignore-layer=%v: %v", options.Ignore, err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment