-
Notifications
You must be signed in to change notification settings - Fork 9
/
message.go
164 lines (145 loc) · 4.09 KB
/
message.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package osc
import (
"bytes"
"fmt"
"io"
"net"
"regexp"
"strings"
"github.com/pkg/errors"
)
// Common errors.
var (
ErrIndexOutOfBounds = errors.New("index out of bounds")
ErrInvalidTypeTag = errors.New("invalid type tag")
ErrNilWriter = errors.New("writer must not be nil")
ErrParse = errors.New("error parsing message")
)
// Message is an OSC message.
// An OSC message consists of an OSC address pattern and zero or more arguments.
type Message struct {
Address string `json:"address"`
Arguments []Argument
Sender net.Addr
}
// ParseMessage parses an OSC message from a slice of bytes.
func ParseMessage(data []byte, sender net.Addr) (Message, error) {
address, idx := ReadString(data)
msg := Message{
Address: address,
Sender: sender,
}
data = data[idx:]
typetags, idx := ReadString(data)
data = data[idx:]
// Read all arguments.
args, err := ReadArguments([]byte(typetags), data)
if err != nil {
return Message{}, errors.Wrap(err, "parse message")
}
msg.Arguments = args
return msg, nil
}
// Bytes returns the contents of the message as a slice of bytes.
func (msg Message) Bytes() []byte {
b := [][]byte{
ToBytes(msg.Address),
msg.Typetags(),
}
for _, a := range msg.Arguments {
b = append(b, a.Bytes())
}
return bytes.Join(b, []byte{})
}
// Equal returns true if the messages are equal, false otherwise.
func (msg Message) Equal(other Packet) bool {
msg2, ok := other.(Message)
if !ok {
return false
}
if msg.Address != msg2.Address {
return false
}
if len(msg.Arguments) != len(msg2.Arguments) {
return false
}
for i, a := range msg.Arguments {
if !a.Equal(msg2.Arguments[i]) {
return false
}
}
return true
}
// Match returns true if the address of the OSC Message matches the given address.
func (msg Message) Match(address string, exactMatch bool) (bool, error) {
if exactMatch {
return address == msg.Address, nil
}
// Verify same number of parts.
if !VerifyParts(address, msg.Address) {
return false, nil
}
exp, err := GetRegex(msg.Address)
if err != nil {
return false, err
}
return exp.MatchString(address), nil
}
// Typetags returns a padded byte slice of the message's type tags.
func (msg Message) Typetags() []byte {
tt := make([]byte, len(msg.Arguments)+1)
tt[0] = TypetagPrefix
for i, a := range msg.Arguments {
tt[i+1] = a.Typetag()
}
return Pad(append(tt, 0))
}
// WriteTo writes the Message to an io.Writer.
func (msg Message) WriteTo(w io.Writer) (int64, error) {
var bytesWritten int
nw, err := fmt.Fprintf(w, "%s%s", msg.Address, msg.Typetags())
if err != nil {
return 0, err
}
bytesWritten += nw
for _, a := range msg.Arguments {
nw, err := a.WriteTo(w)
if err != nil {
return 0, err
}
bytesWritten += int(nw)
}
return int64(bytesWritten), nil
}
// GetRegex compiles and returns a regular expression object for the given address pattern.
func GetRegex(pattern string) (*regexp.Regexp, error) {
pattern = strings.Replace(pattern, ".", "\\.", -1) // Escape all '.' in the pattern
pattern = strings.Replace(pattern, "(", "\\(", -1) // Escape all '(' in the pattern
pattern = strings.Replace(pattern, ")", "\\)", -1) // Escape all ')' in the pattern
pattern = strings.Replace(pattern, "*", ".*", -1) // Replace a '*' with '.*' that matches zero or more characters
pattern = strings.Replace(pattern, "{", "(", -1) // Change a '{' to '('
pattern = strings.Replace(pattern, ",", "|", -1) // Change a ',' to '|'
pattern = strings.Replace(pattern, "}", ")", -1) // Change a '}' to ')'
pattern = strings.Replace(pattern, "?", ".", -1) // Change a '?' to '.'
pattern = "^" + pattern + "$"
return regexp.Compile(pattern)
}
// VerifyParts verifies that m1 and m2 have the same number of parts,
// where a part is a nonempty string between pairs of '/' or a nonempty
// string at the end.
func VerifyParts(m1, m2 string) bool {
if m1 == m2 {
return true
}
mc := string(MessageChar)
p1, p2 := strings.Split(m1, mc), strings.Split(m2, mc)
if len(p1) != len(p2) || len(p1) == 0 {
return false
}
for i, p := range p1[1:] {
if len(p) == 0 || len(p2[i+1]) == 0 {
return false
}
}
return true
}