Skip to content

Commit

Permalink
Merge 7b957ff into 85fee7f
Browse files Browse the repository at this point in the history
  • Loading branch information
daveyarwood authored Jan 17, 2020
2 parents 85fee7f + 7b957ff commit 97c8e6c
Show file tree
Hide file tree
Showing 7 changed files with 648 additions and 160 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

## Features

- UDP (default) or TCP
- OSC Bundles, including timetags
- OSC Messages
- OSC Client
Expand Down
145 changes: 96 additions & 49 deletions examples/basic_server/basic_server.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,114 @@
package main

import (
"bufio"
"fmt"
"net"
"math/rand"
"os"
"strconv"
"strings"
"time"

"github.com/hypebeast/go-osc/osc"
)

func main() {
addr := "127.0.0.1:8765"
server := &osc.Server{}
conn, err := net.ListenPacket("udp", addr)
if err != nil {
fmt.Println("Couldn't listen: ", err)
}
defer conn.Close()
func indent(str string, indentLevel int) string {
indentation := strings.Repeat(" ", indentLevel)

fmt.Println("### Welcome to go-osc receiver demo")
fmt.Println("Press \"q\" to exit")

go func() {
fmt.Println("Start listening on", addr)

for {
packet, err := server.ReceivePacket(conn)
if err != nil {
fmt.Println("Server error: " + err.Error())
os.Exit(1)
}

if packet != nil {
switch packet.(type) {
default:
fmt.Println("Unknow packet type!")

case *osc.Message:
fmt.Printf("-- OSC Message: ")
osc.PrintMessage(packet.(*osc.Message))

case *osc.Bundle:
fmt.Println("-- OSC Bundle:")
bundle := packet.(*osc.Bundle)
for i, message := range bundle.Messages {
fmt.Printf(" -- OSC Message #%d: ", i+1)
osc.PrintMessage(message)
}
}
}
result := ""

for i, line := range strings.Split(str, "\n") {
if i != 0 {
result += "\n"
}
}()

reader := bufio.NewReader(os.Stdin)
result += indentation + line
}

return result
}

func debug(packet osc.Packet, indentLevel int) string {
switch packet.(type) {
default:
return "Unknown packet type!"

for {
c, err := reader.ReadByte()
if err != nil {
os.Exit(0)
case *osc.Message:
return fmt.Sprintf("-- OSC Message: %s", packet.(*osc.Message))

case *osc.Bundle:
bundle := packet.(*osc.Bundle)

result := fmt.Sprintf("-- OSC Bundle (%s):", bundle.Timetag.Time())

for i, message := range bundle.Messages {
result += "\n" + indent(
fmt.Sprintf("-- OSC Message #%d: %s", i+1, message),
indentLevel+1,
)
}

if c == 'q' {
os.Exit(0)
for _, bundle := range bundle.Bundles {
result += "\n" + indent(debug(bundle, 0), indentLevel+1)
}

return result
}
}

// Debugger is a simple Dispatcher that prints all messages and bundles as they
// are received.
type Debugger struct{}

// Dispatch implements Dispatcher.Dispatch by printing the packet received.
func (Debugger) Dispatch(packet osc.Packet) {
if packet != nil {
fmt.Println(debug(packet, 0) + "\n")
}
}

func printUsage() {
fmt.Printf("Usage: %s PROTOCOL PORT\n", os.Args[0])
}

func main() {
rand.Seed(time.Now().Unix())

numArgs := len(os.Args[1:])

if numArgs != 2 {
printUsage()
os.Exit(1)
}

var protocol osc.NetworkProtocol
switch strings.ToLower(os.Args[1]) {
case "udp":
protocol = osc.UDP
case "tcp":
protocol = osc.TCP
default:
fmt.Println("Invalid protocol: " + os.Args[1])
printUsage()
os.Exit(1)
}

port, err := strconv.ParseInt(os.Args[2], 10, 32)
if err != nil {
fmt.Println(err)
printUsage()
os.Exit(1)
}

addr := fmt.Sprintf("127.0.0.1:%d", port)

server := osc.NewServer(addr, Debugger{}, 0)
server.SetNetworkProtocol(protocol)

fmt.Println("### Welcome to go-osc receiver demo")
fmt.Printf("Listening via %s on port %d...\n", protocol, port)

if err := server.ListenAndServe(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
125 changes: 101 additions & 24 deletions examples/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,110 @@ import (
"bufio"
"fmt"
"io"
"math/rand"
"os"
"strconv"
"strings"
"time"

"github.com/hypebeast/go-osc/osc"
)

// TODO: Revise the client!
func testString() string {
return "test string " + strconv.Itoa(rand.Intn(1000))
}

func testBlob() []byte {
return []byte(testString())
}

var argFunctions = []func(*osc.Message){
func(msg *osc.Message) {
msg.Append(int32(rand.Intn(100000)))
},
func(msg *osc.Message) {
msg.Append(rand.Float32() * 100000)
},
func(msg *osc.Message) {
msg.Append(testString())
},
func(msg *osc.Message) {
msg.Append(testBlob())
},
func(msg *osc.Message) {
msg.Append(*osc.NewTimetag(time.Now()))
},
func(msg *osc.Message) {
msg.Append(true)
},
func(msg *osc.Message) {
msg.Append(false)
},
func(msg *osc.Message) {
msg.Append(nil)
},
}

func randomMessage(address string) *osc.Message {
message := osc.NewMessage(address)

for i := 0; i < 1+rand.Intn(5); i++ {
argFunctions[rand.Intn(len(argFunctions))](message)
}

return message
}

func randomBundle() *osc.Bundle {
bundle := osc.NewBundle(time.Now())

for i := 0; i < 1+rand.Intn(5); i++ {
if rand.Float32() < 0.25 {
bundle.Append(randomBundle())
} else {
bundle.Append(randomMessage("/bundle/message/" + strconv.Itoa(i+1)))
}
}

return bundle
}

func printUsage() {
fmt.Printf("Usage: %s PROTOCOL PORT\n", os.Args[0])
}

func main() {
rand.Seed(time.Now().Unix())

numArgs := len(os.Args[1:])

if numArgs != 2 {
printUsage()
os.Exit(1)
}

var protocol osc.NetworkProtocol
switch strings.ToLower(os.Args[1]) {
case "udp":
protocol = osc.UDP
case "tcp":
protocol = osc.TCP
default:
fmt.Println("Invalid protocol: " + os.Args[1])
printUsage()
os.Exit(1)
}

port, err := strconv.ParseInt(os.Args[2], 10, 32)
if err != nil {
fmt.Println(err)
printUsage()
os.Exit(1)
}

ip := "localhost"
port := 8765
client := osc.NewClient(ip, port)
client := osc.NewClient(ip, int(port))
client.SetNetworkProtocol(protocol)

fmt.Println("### Welcome to go-osc transmitter demo")
fmt.Println("Please, select the OSC packet type you would like to send:")
Expand All @@ -38,28 +130,13 @@ func main() {

sline := strings.TrimRight(string(line), "\n")
if sline == "m" {
message := osc.NewMessage("/message/address")
message.Append(int32(12345))
message.Append("teststring")
message.Append(true)
message.Append(false)
client.Send(message)
if err := client.Send(randomMessage("/message/address")); err != nil {
fmt.Println(err)
}
} else if sline == "b" {
bundle := osc.NewBundle(time.Now())
message1 := osc.NewMessage("/bundle/message/1")
message1.Append(int32(12345))
message1.Append("teststring")
message1.Append(true)
message1.Append(false)
message2 := osc.NewMessage("/bundle/message/2")
message2.Append(int32(3344))
message2.Append(float32(101.9))
message2.Append("string1")
message2.Append("string2")
message2.Append(true)
bundle.Append(message1)
bundle.Append(message2)
client.Send(bundle)
if err := client.Send(randomBundle()); err != nil {
fmt.Println(err)
}
} else if sline == "q" {
fmt.Println("Exit!")
os.Exit(0)
Expand Down
65 changes: 57 additions & 8 deletions examples/dispatching_server/dispatching_server.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,67 @@
package main

import "github.com/hypebeast/go-osc/osc"
import (
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"

"github.com/hypebeast/go-osc/osc"
)

func printUsage() {
fmt.Printf("Usage: %s PROTOCOL PORT\n", os.Args[0])
}

func main() {
addr := "127.0.0.1:8765"
rand.Seed(time.Now().Unix())

numArgs := len(os.Args[1:])

if numArgs != 2 {
printUsage()
os.Exit(1)
}

var protocol osc.NetworkProtocol
switch strings.ToLower(os.Args[1]) {
case "udp":
protocol = osc.UDP
case "tcp":
protocol = osc.TCP
default:
fmt.Println("Invalid protocol: " + os.Args[1])
printUsage()
os.Exit(1)
}

port, err := strconv.ParseInt(os.Args[2], 10, 32)
if err != nil {
fmt.Println(err)
printUsage()
os.Exit(1)
}

addr := fmt.Sprintf("127.0.0.1:%d", port)

d := osc.NewStandardDispatcher()
d.AddMsgHandler("/message/address", func(msg *osc.Message) {

if err := d.AddMsgHandler("/message/address", func(msg *osc.Message) {
osc.PrintMessage(msg)
})
server := &osc.Server{
Addr: addr,
Dispatcher: d,
}); err != nil {
fmt.Println(err)
os.Exit(1)
}

server.ListenAndServe()
server := osc.NewServer(addr, d, 0)
server.SetNetworkProtocol(protocol)

fmt.Printf("Listening via %s on port %d...\n", protocol, port)

if err := server.ListenAndServe(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Loading

0 comments on commit 97c8e6c

Please sign in to comment.