Skip to content

Commit

Permalink
oscli metro command
Browse files Browse the repository at this point in the history
  • Loading branch information
glynternet committed Jun 11, 2018
1 parent 2977a3d commit 6d527a1
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 17 deletions.
25 changes: 10 additions & 15 deletions cmd/oscli/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"log"
"net"

"github.com/glynternet/oscli/internal"
"github.com/glynternet/oscli/models"
Expand All @@ -21,33 +20,24 @@ const (
)

var cmdOSCGen = &cobra.Command{
Use: "generate",
Use: "generate [ADDRESS] [MESSAGE]...",
Short: "generate a stream of osc messages",
Long: `generate a stream of osc messages
Generate an osc signal with values ranging from 0 to 1 as a sin wave.
The messages will be sent to the given address.`,
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("arguments required to form OSC message")
}
msgAddr, err := internal.CleanAddress(args[0])
if err != nil {
return errors.Wrap(err, "parsing OSC message address")
}

host, err := internal.GetRemoteHost(
viper.GetBool(keyLocal),
viper.GetString(keyRemoteHost),
)
host, err := initRemoteHost()
if err != nil {
log.Fatal(errors.Wrap(err, "getting remote host"))
return errors.Wrap(err, "initialising host")
}

_, err = net.LookupHost(host)
if err != nil {
log.Fatal(errors.Wrapf(err, "looking up %s host %s", keyRemoteHost, host))
}
client := osc.NewClient(
host,
viper.GetInt(keyRemotePort),
Expand All @@ -61,7 +51,11 @@ The messages will be sent to the given address.`,
var staticArgs []interface{}
if len(args) > 0 {
for _, arg := range args[1:] {
staticArgs = append(staticArgs, arg)
a, err := osc2.Parse(arg)
if err != nil {
return errors.Wrapf(err, "parsing arg '%s' as value", arg)
}
staticArgs = append(staticArgs, a)
}
}

Expand All @@ -75,6 +69,7 @@ The messages will be sent to the given address.`,
err := client.Send(msg)
if err != nil {
log.Print(errors.Wrap(err, "sending message to client"))
continue
}
log.Printf("Message (%+v) sent to client at %s:%d", msg, client.IP(), client.Port())
}
Expand Down
78 changes: 78 additions & 0 deletions cmd/oscli/cmd/metro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"fmt"
"log"

"github.com/glynternet/oscli/internal"
osc2 "github.com/glynternet/oscli/pkg/osc"
"github.com/glynternet/oscli/pkg/wave"
"github.com/hypebeast/go-osc/osc"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cmdMetro = &cobra.Command{
Use: "metro [ADDRESS] [MESSAGE]...",
Short: "generate a ticker of the same OSC message",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
msgAddr, err := internal.CleanAddress(args[0])
if err != nil {
return errors.Wrap(err, "parsing OSC message address")
}

host, err := initRemoteHost()
if err != nil {
return errors.Wrap(err, "initialising host")
}

client := osc.NewClient(
host,
viper.GetInt(keyRemotePort),
)

msgFreq := viper.GetFloat64(keyMsgFrequency)
if msgFreq <= 0 {
log.Fatal(fmt.Errorf("%s must be positive, received %f", keyMsgFrequency, msgFreq))
}

var staticArgs []interface{}
if len(args) > 0 {
for _, arg := range args[1:] {
a, err := osc2.Parse(arg)
if err != nil {
return errors.Wrapf(err, "parsing arg '%s' as value", arg)
}
staticArgs = append(staticArgs, a)
}
}

genFn := func() *osc.Message {
return osc.NewMessage(msgAddr, staticArgs...)
}

// TODO: the second argument to this could be a ticker or something?
msgCh := osc2.Generate(genFn, wave.Frequency(msgFreq).Period())
for {
select {
case msg := <-msgCh:
err := client.Send(msg)
if err != nil {
log.Print(errors.Wrap(err, "sending message to client"))
continue
}
log.Printf("Message (%+v) sent to client at %s:%d", msg, client.IP(), client.Port())
}
}
},
}

func init() {
rootCmd.AddCommand(cmdMetro)
err := viper.BindPFlags(cmdMetro.Flags())
if err != nil {
log.Fatal(err)
}
}
26 changes: 25 additions & 1 deletion cmd/oscli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cmd

import (
"log"
"net"
"strings"

"github.com/glynternet/oscli/internal"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -15,7 +17,7 @@ func Execute() error {
}

const (
appName = "osc"
appName = "oscli"

keyListenHost = "listen-host"
usageListenHost = "host address to listen on"
Expand Down Expand Up @@ -43,6 +45,9 @@ func init() {
rootCmd.PersistentFlags().Uint(keyListenPort, 9000, usageListenPort)
rootCmd.PersistentFlags().StringP(keyRemoteHost, "r", "", usageRemoteHost)
rootCmd.PersistentFlags().Uint(keyRemotePort, 9000, usageRemotePort)

rootCmd.PersistentFlags().Float64P(keyMsgFrequency, "m", 25, "frequency to send messages at")

err := viper.BindPFlags(rootCmd.PersistentFlags())
if err != nil {
log.Fatal(errors.Wrap(err, "binding PFlags"))
Expand All @@ -54,3 +59,22 @@ func initConfig() {
viper.AutomaticEnv() // read in environment variables that match
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
}

func initRemoteHost() (string, error) {
host, err := internal.GetRemoteHost(
viper.GetBool(keyLocal),
viper.GetString(keyRemoteHost),
)
if err != nil {
return "", errors.Wrap(err, "getting remote host")
}

return host, errors.Wrap(verifyHost(host), "verifying host")
}

// verifyHost checks that the given string can be resolved through the current
// DNS/networking state
func verifyHost(host string) error {
_, err := net.LookupHost(host)
return errors.Wrapf(err, "looking up %s host %s", keyRemoteHost, host)
}
3 changes: 2 additions & 1 deletion cmd/oscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
}

const (
appName = "osc"
appName = "oscli"

keyListenHost = "listen-host"
usageListenHost = "host address to listen on"
Expand All @@ -40,6 +40,7 @@ var rootCmd = &cobra.Command{
}

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().BoolP(keyLocal, "l", false, usageLocal)
rootCmd.PersistentFlags().String(keyListenHost, "", usageListenHost)
rootCmd.PersistentFlags().Uint(keyListenPort, 9000, usageListenPort)
Expand Down

0 comments on commit 6d527a1

Please sign in to comment.