diff --git a/registry/encoding.go b/registry/encoding.go deleted file mode 100644 index 750f88f8..00000000 --- a/registry/encoding.go +++ /dev/null @@ -1,73 +0,0 @@ -package registry - -import ( - "bytes" - "compress/zlib" - "encoding/hex" - "encoding/json" - "io/ioutil" - "strings" -) - -func encode(txt *mdnsTxt) ([]string, error) { - b, err := json.Marshal(txt) - if err != nil { - return nil, err - } - - var buf bytes.Buffer - defer buf.Reset() - - w := zlib.NewWriter(&buf) - if _, err := w.Write(b); err != nil { - return nil, err - } - w.Close() - - encoded := hex.EncodeToString(buf.Bytes()) - - // individual txt limit - if len(encoded) <= 255 { - return []string{encoded}, nil - } - - // split encoded string - var record []string - - for len(encoded) > 255 { - record = append(record, encoded[:255]) - encoded = encoded[255:] - } - - record = append(record, encoded) - - return record, nil -} - -func decode(record []string) (*mdnsTxt, error) { - encoded := strings.Join(record, "") - - hr, err := hex.DecodeString(encoded) - if err != nil { - return nil, err - } - - br := bytes.NewReader(hr) - zr, err := zlib.NewReader(br) - if err != nil { - return nil, err - } - - rbuf, err := ioutil.ReadAll(zr) - if err != nil { - return nil, err - } - - var txt *mdnsTxt - - if err := json.Unmarshal(rbuf, &txt); err != nil { - return nil, err - } - - return txt, nil -} diff --git a/registry/encoding_test.go b/registry/encoding_test.go deleted file mode 100644 index ff9ae97e..00000000 --- a/registry/encoding_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package registry - -import ( - "testing" -) - -func TestEncoding(t *testing.T) { - testData := []*mdnsTxt{ - { - Version: "1.0.0", - Metadata: map[string]string{ - "foo": "bar", - }, - Endpoints: []*Endpoint{ - { - Name: "endpoint1", - Request: &Value{ - Name: "request", - Type: "request", - }, - Response: &Value{ - Name: "response", - Type: "response", - }, - Metadata: map[string]string{ - "foo1": "bar1", - }, - }, - }, - }, - } - - for _, d := range testData { - encoded, err := encode(d) - if err != nil { - t.Fatal(err) - } - - for _, txt := range encoded { - if len(txt) > 255 { - t.Fatalf("One of parts for txt is %d characters", len(txt)) - } - } - - decoded, err := decode(encoded) - if err != nil { - t.Fatal(err) - } - - if decoded.Version != d.Version { - t.Fatalf("Expected version %s got %s", d.Version, decoded.Version) - } - - if len(decoded.Endpoints) != len(d.Endpoints) { - t.Fatalf("Expected %d endpoints, got %d", len(d.Endpoints), len(decoded.Endpoints)) - } - - for k, v := range d.Metadata { - if val := decoded.Metadata[k]; val != v { - t.Fatalf("Expected %s=%s got %s=%s", k, v, k, val) - } - } - } - -} diff --git a/registry/mdns_registry.go b/registry/mdns_registry.go index 9dbca6de..7e614840 100644 --- a/registry/mdns_registry.go +++ b/registry/mdns_registry.go @@ -2,8 +2,13 @@ package registry import ( + "bytes" + "compress/zlib" "context" + "encoding/hex" + "encoding/json" "fmt" + "io/ioutil" "net" "strconv" "strings" @@ -49,6 +54,68 @@ type mdnsRegistry struct { listener chan *mdns.ServiceEntry } +func encode(txt *mdnsTxt) ([]string, error) { + b, err := json.Marshal(txt) + if err != nil { + return nil, err + } + + var buf bytes.Buffer + defer buf.Reset() + + w := zlib.NewWriter(&buf) + if _, err := w.Write(b); err != nil { + return nil, err + } + w.Close() + + encoded := hex.EncodeToString(buf.Bytes()) + + // individual txt limit + if len(encoded) <= 255 { + return []string{encoded}, nil + } + + // split encoded string + var record []string + + for len(encoded) > 255 { + record = append(record, encoded[:255]) + encoded = encoded[255:] + } + + record = append(record, encoded) + + return record, nil +} + +func decode(record []string) (*mdnsTxt, error) { + encoded := strings.Join(record, "") + + hr, err := hex.DecodeString(encoded) + if err != nil { + return nil, err + } + + br := bytes.NewReader(hr) + zr, err := zlib.NewReader(br) + if err != nil { + return nil, err + } + + rbuf, err := ioutil.ReadAll(zr) + if err != nil { + return nil, err + } + + var txt *mdnsTxt + + if err := json.Unmarshal(rbuf, &txt); err != nil { + return nil, err + } + + return txt, nil +} func newRegistry(opts ...Option) Registry { options := Options{ Context: context.Background(), diff --git a/registry/mdns_test.go b/registry/mdns_test.go index a5630c67..c9979d5c 100644 --- a/registry/mdns_test.go +++ b/registry/mdns_test.go @@ -137,3 +137,63 @@ func TestMDNS(t *testing.T) { } } + +func TestEncoding(t *testing.T) { + testData := []*mdnsTxt{ + { + Version: "1.0.0", + Metadata: map[string]string{ + "foo": "bar", + }, + Endpoints: []*Endpoint{ + { + Name: "endpoint1", + Request: &Value{ + Name: "request", + Type: "request", + }, + Response: &Value{ + Name: "response", + Type: "response", + }, + Metadata: map[string]string{ + "foo1": "bar1", + }, + }, + }, + }, + } + + for _, d := range testData { + encoded, err := encode(d) + if err != nil { + t.Fatal(err) + } + + for _, txt := range encoded { + if len(txt) > 255 { + t.Fatalf("One of parts for txt is %d characters", len(txt)) + } + } + + decoded, err := decode(encoded) + if err != nil { + t.Fatal(err) + } + + if decoded.Version != d.Version { + t.Fatalf("Expected version %s got %s", d.Version, decoded.Version) + } + + if len(decoded.Endpoints) != len(d.Endpoints) { + t.Fatalf("Expected %d endpoints, got %d", len(d.Endpoints), len(decoded.Endpoints)) + } + + for k, v := range d.Metadata { + if val := decoded.Metadata[k]; val != v { + t.Fatalf("Expected %s=%s got %s=%s", k, v, k, val) + } + } + } + +}