Skip to content

Commit

Permalink
Implement EAAS discovery message.
Browse files Browse the repository at this point in the history
  • Loading branch information
icedream committed Mar 15, 2024
1 parent 6988f0d commit a1ba9cb
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 5 deletions.
67 changes: 67 additions & 0 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,70 @@ func (m *discoveryMessage) writeTo(w io.Writer) (err error) {
}
return
}

type eaasDiscoveryMessage struct {
tokenPrefixedMessage
Hostname string
SoftwareVersion string
URL string
Extra string // usually just _
}

var eaasDiscoveryMagic = []byte("EAAS")

func (m *eaasDiscoveryMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
var readMagic []byte
if readMagic, err = r.Peek(4); err != nil {
return
}
ok = bytes.Equal(readMagic, discoveryMagic)
return
}

func (m *eaasDiscoveryMessage) readFrom(r io.Reader) (err error) {
readMagic := make([]byte, 4)
if _, err = r.Read(readMagic); err != nil {
return
} else if !bytes.Equal(readMagic, discoveryMagic) {
err = ErrInvalidMessageReceived
return
}
if err = m.tokenPrefixedMessage.readFrom(r); err != nil {
return
}
if err = readNetworkString(r, &m.Hostname); err != nil {
return
}
if err = readLengthPrefixedNetworkString(r, &m.URL, utf8StringEncoding); err != nil {
return
}
if err = readNetworkString(r, &m.SoftwareVersion); err != nil {
return
}
if err = readNetworkString(r, &m.Extra); err != nil {
return
}
return
}

func (m *eaasDiscoveryMessage) writeTo(w io.Writer) (err error) {
if _, err = w.Write(discoveryMagic); err != nil {
return
}
if err = m.tokenPrefixedMessage.writeTo(w); err != nil {
return
}
if err = writeNetworkString(w, m.Hostname); err != nil {
return
}
if err = writeLengthPrefixedNetworkString(w, m.URL, utf8StringEncoding); err != nil {
return
}
if err = writeNetworkString(w, m.SoftwareVersion); err != nil {
return
}
if err = writeNetworkString(w, m.Extra); err != nil {
return
}
return
}
33 changes: 33 additions & 0 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,39 @@ var testMessages = []struct {
Timelines: []float64{6865523.501393173, 7171186.184697615, 0, 0},
},
},
{
Name: "EAAS discovery",
Bytes: []byte{
0x45, 0x41, 0x41, 0x53, 0x01, 0x01, 0x79, 0x9b,
0x2d, 0xab, 0xf7, 0xc7, 0x43, 0x63, 0xb4, 0x9c,
0x59, 0xe1, 0x91, 0x16, 0x89, 0x9e, 0x00, 0x00,
0x00, 0x24, 0x00, 0x69, 0x00, 0x63, 0x00, 0x65,
0x00, 0x64, 0x00, 0x72, 0x00, 0x65, 0x00, 0x61,
0x00, 0x6d, 0x00, 0x2d, 0x00, 0x66, 0x00, 0x72,
0x00, 0x61, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x77,
0x00, 0x6f, 0x00, 0x72, 0x00, 0x6b, 0x00, 0x00,
0x00, 0x1c, 0x67, 0x72, 0x70, 0x63, 0x3a, 0x2f,
0x2f, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38,
0x2e, 0x31, 0x38, 0x38, 0x2e, 0x31, 0x32, 0x30,
0x3a, 0x35, 0x30, 0x30, 0x31, 0x30, 0x00, 0x00,
0x00, 0x20, 0x00, 0x33, 0x00, 0x2e, 0x00, 0x34,
0x00, 0x2e, 0x00, 0x30, 0x00, 0x2e, 0x00, 0x66,
0x00, 0x36, 0x00, 0x62, 0x00, 0x33, 0x00, 0x64,
0x00, 0x63, 0x00, 0x32, 0x00, 0x63, 0x00, 0x32,
0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00,
0x5f,
},
CreateMessage: func() message { return new(eaasDiscoveryMessage) },
Message: &eaasDiscoveryMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: Token{0x79, 0x9b, 0x2d, 0xab, 0xf7, 0xc7, 0x43, 0x63, 0xb4, 0x9c, 0x59, 0xe1, 0x91, 0x16, 0x89, 0x9e},
},
Hostname: "icedream-framework",
SoftwareVersion: "3.4.0.f6b3dc2c20",
URL: "grpc://192.168.188.120:50010",
Extra: "_",
},
},
}

func Test_Messages_Read(t *testing.T) {
Expand Down
22 changes: 17 additions & 5 deletions network_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"encoding/binary"
"io"

"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
)

var networkStringEncoding = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
var (
utf16StringEncoding = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
utf8StringEncoding = unicode.UTF8
)

func writeNetworkString(w io.Writer, v string) (err error) {
converted, err := networkStringEncoding.NewEncoder().Bytes([]byte(v))
func writeLengthPrefixedNetworkString(w io.Writer, v string, enc encoding.Encoding) (err error) {
converted, err := enc.NewEncoder().Bytes([]byte(v))
if err != nil {
return
}
Expand All @@ -21,7 +25,7 @@ func writeNetworkString(w io.Writer, v string) (err error) {
return
}

func readNetworkString(r io.Reader, v *string) (err error) {
func readLengthPrefixedNetworkString(r io.Reader, v *string, enc encoding.Encoding) (err error) {
var expectedLength uint32
if err = binary.Read(r, binary.BigEndian, &expectedLength); err != nil {
return
Expand All @@ -38,10 +42,18 @@ func readNetworkString(r io.Reader, v *string) (err error) {
}
offset += n
}
vBytes, err := networkStringEncoding.NewDecoder().Bytes(buf)
vBytes, err := enc.NewDecoder().Bytes(buf)
if err != nil {
return
}
*v = string(vBytes)
return
}

func writeNetworkString(w io.Writer, v string) (err error) {
return writeLengthPrefixedNetworkString(w, v, utf16StringEncoding)
}

func readNetworkString(r io.Reader, v *string) (err error) {
return readLengthPrefixedNetworkString(r, v, utf16StringEncoding)
}

0 comments on commit a1ba9cb

Please sign in to comment.