From 07741b358ba75350e7be36ee76f45afa18b0461b Mon Sep 17 00:00:00 2001 From: xegineering Date: Wed, 2 Oct 2024 21:18:51 +0200 Subject: Rename Soundbox to Client Since the module name is `soundbox` naming the primary type of the module also `Soundbox` is redundant. Following similar code from the Go standard library this struct is renamed to `Client`. --- client.go | 31 +++++++++++++++++++++++++++++++ client_test.go | 25 +++++++++++++++++++++++++ examples_test.go | 12 ++++++------ soundbox.go | 31 ------------------------------- soundbox_test.go | 25 ------------------------- 5 files changed, 62 insertions(+), 62 deletions(-) create mode 100644 client.go create mode 100644 client_test.go delete mode 100644 soundbox.go delete mode 100644 soundbox_test.go diff --git a/client.go b/client.go new file mode 100644 index 0000000..27e5d54 --- /dev/null +++ b/client.go @@ -0,0 +1,31 @@ +package soundbox + +import ( + "fmt" + "net" +) + +// Port is the default network port a soundbox is listening to for incoming +// audio stream data. +const Port = 5316 + +type Client struct { + HardwareAddr net.HardwareAddr +} + +func NewClient(mac string) (Client, error) { + hardwareAddr, err := net.ParseMAC(mac) + if err != nil { + return Client{}, err + } + + return fromHardwareAddr(hardwareAddr) +} + +func fromHardwareAddr(addr net.HardwareAddr) (Client, error) { + if len(addr) != 6 { + return Client{}, fmt.Errorf("Only IEEE 802 MAC-48 addresses supported") + } + + return Client{HardwareAddr: addr}, nil +} diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..35e3624 --- /dev/null +++ b/client_test.go @@ -0,0 +1,25 @@ +package soundbox + +import ( + "testing" +) + +func TestNewClient(t *testing.T) { + // valid MAC should succeed + _, err := NewClient("12:34:56:ab:cd:ef") + if err != nil { + t.Fatalf("NewClient() failed: %v", err) + } + + // invalid MAC should fail + _, err = NewClient("12:34:56:ab:cd") + if err == nil { + t.Fatalf("NewClient() failed: %v", err) + } + + // No EUI-64 supported + _, err = NewClient("12:34:56:ab:cd:ef:12:45") + if err == nil { + t.Fatalf("NewClient() failed: %v", err) + } +} diff --git a/examples_test.go b/examples_test.go index 1320cd6..777ae52 100644 --- a/examples_test.go +++ b/examples_test.go @@ -7,15 +7,15 @@ import ( "xengineering.eu/soundbox" ) -// ExampleSoundbox demonstrates how to instantiate a Soundbox struct. The used -// MAC address if from a private range according to +// ExampleClient demonstrates how to instantiate a Client struct. The used MAC +// address if from a private range according to // https://www.rfc-editor.org/rfc/rfc9542.html#name-48-bit-mac-documentation-va. -func ExampleSoundbox() { - s, err := soundbox.NewSoundbox("00:00:5E:00:53:01") +func ExampleClient() { + s, err := soundbox.NewClient("00:00:5E:00:53:01") if err != nil { log.Fatal(err) } - fmt.Printf("Soundbox with MAC address %v", s.HardwareAddr) - // Output: Soundbox with MAC address 00:00:5e:00:53:01 + fmt.Printf("Client with MAC address %v", s.HardwareAddr) + // Output: Client with MAC address 00:00:5e:00:53:01 } diff --git a/soundbox.go b/soundbox.go deleted file mode 100644 index d6a0a34..0000000 --- a/soundbox.go +++ /dev/null @@ -1,31 +0,0 @@ -package soundbox - -import ( - "fmt" - "net" -) - -// Port is the default network port a soundbox is listening to for incoming -// audio stream data. -const Port = 5316 - -type Soundbox struct { - HardwareAddr net.HardwareAddr -} - -func NewSoundbox(mac string) (Soundbox, error) { - hardwareAddr, err := net.ParseMAC(mac) - if err != nil { - return Soundbox{}, err - } - - return fromHardwareAddr(hardwareAddr) -} - -func fromHardwareAddr(addr net.HardwareAddr) (Soundbox, error) { - if len(addr) != 6 { - return Soundbox{}, fmt.Errorf("Only IEEE 802 MAC-48 addresses supported") - } - - return Soundbox{HardwareAddr: addr}, nil -} diff --git a/soundbox_test.go b/soundbox_test.go deleted file mode 100644 index ee9d885..0000000 --- a/soundbox_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package soundbox - -import ( - "testing" -) - -func TestNewSoundbox(t *testing.T) { - // valid MAC should succeed - _, err := NewSoundbox("12:34:56:ab:cd:ef") - if err != nil { - t.Fatalf("NewSoundbox() failed: %v", err) - } - - // invalid MAC should fail - _, err = NewSoundbox("12:34:56:ab:cd") - if err == nil { - t.Fatalf("NewSoundbox() failed: %v", err) - } - - // No EUI-64 supported - _, err = NewSoundbox("12:34:56:ab:cd:ef:12:45") - if err == nil { - t.Fatalf("NewSoundbox() failed: %v", err) - } -} -- cgit v1.2.3-70-g09d2