Skip to content

Commit 1f3a9a7

Browse files
committed
sed -i '' 's;MessageHandlers;PatternMatching;' *.go
1 parent 53342a9 commit 1f3a9a7

6 files changed

+25
-24
lines changed

dispatcher.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ type Dispatcher interface {
3131
Invoke(msg Message, exactMatch bool) error
3232
}
3333

34-
// MessageHandlers is a map from OSC address pattern to message handler.
35-
type MessageHandlers map[string]MessageHandler
34+
// PatternMatching is a dispatcher that implements OSC 1.0 pattern matching.
35+
// See http://opensoundcontrol.org/spec-1_0 "OSC Message Dispatching and Pattern Matching"
36+
type PatternMatching map[string]MessageHandler
3637

3738
// Dispatch invokes an OSC bundle's messages.
38-
func (h MessageHandlers) Dispatch(b Bundle, exactMatch bool) error {
39+
func (h PatternMatching) Dispatch(b Bundle, exactMatch bool) error {
3940
var (
4041
now = time.Now()
4142
tt = b.Timetag.Time()
@@ -48,7 +49,7 @@ func (h MessageHandlers) Dispatch(b Bundle, exactMatch bool) error {
4849
}
4950

5051
// immediately invokes an OSC bundle immediately.
51-
func (h MessageHandlers) immediately(b Bundle, exactMatch bool) error {
52+
func (h PatternMatching) immediately(b Bundle, exactMatch bool) error {
5253
for _, p := range b.Packets {
5354
errs := []string{}
5455
if err := h.invoke(p, exactMatch); err != nil {
@@ -63,7 +64,7 @@ func (h MessageHandlers) immediately(b Bundle, exactMatch bool) error {
6364
}
6465

6566
// invoke invokes an OSC packet, which could be a message or a bundle of messages.
66-
func (h MessageHandlers) invoke(p Packet, exactMatch bool) error {
67+
func (h PatternMatching) invoke(p Packet, exactMatch bool) error {
6768
switch x := p.(type) {
6869
case Message:
6970
return h.Invoke(x, exactMatch)
@@ -75,7 +76,7 @@ func (h MessageHandlers) invoke(p Packet, exactMatch bool) error {
7576
}
7677

7778
// Invoke invokes an OSC message.
78-
func (h MessageHandlers) Invoke(msg Message, exactMatch bool) error {
79+
func (h PatternMatching) Invoke(msg Message, exactMatch bool) error {
7980
for address, handler := range h {
8081
matched, err := msg.Match(address, exactMatch)
8182
if err != nil {

dispatcher_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// Test a successful method invocation.
1111
func TestDispatcherDispatchOK(t *testing.T) {
1212
c := make(chan struct{})
13-
d := MessageHandlers{
13+
d := PatternMatching{
1414
"/bar": Method(func(msg Message) error {
1515
close(c)
1616
return nil
@@ -31,7 +31,7 @@ func TestDispatcherDispatchOK(t *testing.T) {
3131

3232
// Test a method that returns an error.
3333
func TestDispatcherDispatchError(t *testing.T) {
34-
d := MessageHandlers{
34+
d := PatternMatching{
3535
"/foo": Method(func(msg Message) error {
3636
return errors.New("oops")
3737
}),
@@ -50,7 +50,7 @@ func TestDispatcherDispatchError(t *testing.T) {
5050

5151
func TestDispatcherDispatchNestedBundle(t *testing.T) {
5252
c := make(chan struct{})
53-
d := MessageHandlers{
53+
d := PatternMatching{
5454
"/foo": Method(func(msg Message) error {
5555
close(c)
5656
return nil
@@ -75,7 +75,7 @@ func TestDispatcherDispatchNestedBundle(t *testing.T) {
7575
}
7676

7777
func TestDispatcherMiss(t *testing.T) {
78-
d := MessageHandlers{
78+
d := PatternMatching{
7979
"/foo": Method(func(msg Message) error {
8080
return nil
8181
}),
@@ -89,7 +89,7 @@ func TestDispatcherMiss(t *testing.T) {
8989
}
9090

9191
func TestDispatcherInvoke(t *testing.T) {
92-
d := MessageHandlers{
92+
d := PatternMatching{
9393
"/foo": Method(func(msg Message) error {
9494
return errors.New("foo error")
9595
}),

osc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func checkDispatcher(dispatcher Dispatcher) error {
112112
if dispatcher == nil {
113113
return ErrNilDispatcher
114114
}
115-
messageHandlers, ok := dispatcher.(MessageHandlers)
115+
messageHandlers, ok := dispatcher.(PatternMatching)
116116
if ok {
117117
for addr := range messageHandlers {
118118
if err := ValidateAddress(addr); err != nil {

pingpong_example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Example_pingPong() {
6666
}
6767

6868
func serverDispatch(server *UDPConn, errChan chan error) {
69-
errChan <- server.Serve(1, MessageHandlers{
69+
errChan <- server.Serve(1, PatternMatching{
7070
"/ping": Method(func(msg Message) error {
7171
fmt.Println("Server received ping.")
7272
return server.SendTo(msg.Sender, Message{Address: "/pong"})
@@ -83,7 +83,7 @@ func serverDispatch(server *UDPConn, errChan chan error) {
8383
}
8484

8585
func clientDispatch(client *UDPConn, errChan chan error, pongChan chan struct{}, closeChan chan struct{}) {
86-
errChan <- client.Serve(1, MessageHandlers{
86+
errChan <- client.Serve(1, PatternMatching{
8787
"/pong": Method(func(msg Message) error {
8888
fmt.Println("Client received pong.")
8989
close(pongChan)

udp_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestInvalidAddress(t *testing.T) {
2121
}
2222
defer func() { _ = server.Close() }() // Best effort.
2323

24-
if err := server.Serve(1, MessageHandlers{
24+
if err := server.Serve(1, PatternMatching{
2525
"/[": Method(func(msg Message) error {
2626
return nil
2727
}),
@@ -72,7 +72,7 @@ func TestDialUDPContext(t *testing.T) {
7272
if c.Context() != ctxTimeout {
7373
t.Fatalf("expected %+v to be %+v", ctxTimeout, c.Context())
7474
}
75-
if err := c.Serve(1, MessageHandlers{}); err != context.DeadlineExceeded {
75+
if err := c.Serve(1, PatternMatching{}); err != context.DeadlineExceeded {
7676
t.Fatalf("expected context.DeadlineExceeded, got %+v", err)
7777
}
7878
}
@@ -83,7 +83,7 @@ func TestDialUDPContext(t *testing.T) {
8383
// For clients that are interested in closing the server with an OSC
8484
// message, a method is automatically added to the provided dispatcher
8585
// at the "/server/close" address that closes the server.
86-
func testUDPServer(t *testing.T, dispatcher MessageHandlers) (*UDPConn, *UDPConn, chan error) {
86+
func testUDPServer(t *testing.T, dispatcher PatternMatching) (*UDPConn, *UDPConn, chan error) {
8787
laddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
8888
if err != nil {
8989
t.Fatal(err)
@@ -93,7 +93,7 @@ func testUDPServer(t *testing.T, dispatcher MessageHandlers) (*UDPConn, *UDPConn
9393
t.Fatal(err)
9494
}
9595
if dispatcher == nil {
96-
dispatcher = MessageHandlers{}
96+
dispatcher = PatternMatching{}
9797
}
9898
dispatcher["/server/close"] = Method(func(msg Message) error {
9999
return server.Close()
@@ -158,7 +158,7 @@ func TestUDPConnServe_ContextTimeout(t *testing.T) {
158158
}
159159
errChan := make(chan error)
160160
go func() {
161-
errChan <- server.Serve(1, MessageHandlers{})
161+
errChan <- server.Serve(1, PatternMatching{})
162162
}()
163163
select {
164164
case <-time.After(200 * time.Millisecond):
@@ -187,7 +187,7 @@ func TestUDPConnServe_ReadError(t *testing.T) {
187187
ctx: context.Background(),
188188
}
189189
go func() {
190-
errChan <- server.Serve(1, MessageHandlers{
190+
errChan <- server.Serve(1, PatternMatching{
191191
"/close": Method(func(msg Message) error {
192192
return server.Close()
193193
}),
@@ -233,7 +233,7 @@ func TestUDPConnServe_BadInboundAddr(t *testing.T) {
233233
badPacket{},
234234
} {
235235
// Send a message with a bad address.
236-
_, conn, errChan := testUDPServer(t, MessageHandlers{
236+
_, conn, errChan := testUDPServer(t, PatternMatching{
237237
"/foo": Method(func(msg Message) error {
238238
return nil
239239
}),
@@ -305,7 +305,7 @@ func TestUDPConnSendBundle_DispatchError(t *testing.T) {
305305
Message{Address: "/foo"},
306306
},
307307
}
308-
_, conn, errChan := testUDPServer(t, MessageHandlers{
308+
_, conn, errChan := testUDPServer(t, PatternMatching{
309309
"/foo": Method(func(msg Message) error {
310310
return errors.New("oops")
311311
}),

unix_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func tmpListener(t *testing.T, dispatcher Dispatcher) (*UnixConn, chan error) {
3030
func TestUnixSend(t *testing.T) {
3131
fooch := make(chan struct{})
3232

33-
server, errChan := tmpListener(t, MessageHandlers{
33+
server, errChan := tmpListener(t, PatternMatching{
3434
"/foo": Method(func(m Message) error {
3535
close(fooch)
3636
return nil
@@ -113,7 +113,7 @@ func TestDialUnixSetWriteBufferError(t *testing.T) {
113113
func TestUnixSendTo(t *testing.T) {
114114
fooch := make(chan struct{})
115115

116-
server, errChan := tmpListener(t, MessageHandlers{
116+
server, errChan := tmpListener(t, PatternMatching{
117117
"/foo": Method(func(m Message) error {
118118
close(fooch)
119119
return nil

0 commit comments

Comments
 (0)