forked from schollz/sendosc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (99 loc) · 2.29 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"flag"
"fmt"
"log"
"strconv"
"time"
"github.com/hypebeast/go-osc/osc"
)
var flagRecvHost, flagRecvAddress, flagHost, flagAddress string
var flagRecvPort, flagPort int
type arrayFlags []string
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var flagInts, flagStrings, flagFloats arrayFlags
func init() {
flag.StringVar(&flagHost, "host", "localhost", "osc host")
flag.IntVar(&flagPort, "port", 57120, "port to use")
flag.StringVar(&flagAddress, "addr", "/osc/address", "osc address")
flag.Var(&flagInts, "i", "integer to send")
flag.Var(&flagFloats, "f", "float to send")
flag.Var(&flagStrings, "s", "integer to send")
flag.StringVar(&flagRecvHost, "recv-host", "localhost", "osc host")
flag.IntVar(&flagRecvPort, "recv-port", -1, "port to use (active if port>0)")
flag.StringVar(&flagRecvAddress, "recv-addr", "/osc/address", "osc address")
}
var done chan bool
var serverReady bool
func main() {
flag.Parse()
var err error
done = make(chan bool)
if flagRecvPort > 0 {
go runServer()
for {
if serverReady {
break
}
time.Sleep(5 * time.Millisecond)
client := osc.NewClient(flagRecvHost, flagRecvPort)
msg := osc.NewMessage("/is_ready")
client.Send(msg)
}
err = run()
<-done
} else {
err = run()
}
if err != nil {
log.Printf("error: %s", err.Error())
}
}
func runServer() {
d := osc.NewStandardDispatcher()
d.AddMsgHandler(flagRecvAddress, func(msg *osc.Message) {
osc.PrintMessage(msg)
})
d.AddMsgHandler("/quit", func(msg *osc.Message) {
done <- true
})
d.AddMsgHandler("/is_ready", func(msg *osc.Message) {
serverReady = true
})
server := &osc.Server{
Addr: fmt.Sprintf("%s:%d", flagRecvHost, flagRecvPort),
Dispatcher: d,
}
server.ListenAndServe()
}
func run() (err error) {
client := osc.NewClient(flagHost, flagPort)
msg := osc.NewMessage(flagAddress)
for _, u := range flagInts {
v, err := strconv.Atoi(u)
if err == nil {
msg.Append(int32(v))
} else {
return err
}
}
for _, u := range flagFloats {
v, err := strconv.ParseFloat(u, 64)
if err == nil {
msg.Append(v)
} else {
return err
}
}
for _, u := range flagStrings {
msg.Append(u)
}
err = client.Send(msg)
return
}