From e0a8ff52180c5e11e32897cb6fcb13c839c02495 Mon Sep 17 00:00:00 2001 From: saji Date: Wed, 3 May 2023 00:26:53 -0500 Subject: [PATCH] get rid of messages, we use RPC now --- internal/gotelem/messages.go | 60 ---- internal/gotelem/messages_gen.go | 491 -------------------------- internal/gotelem/messages_gen_test.go | 349 ------------------ 3 files changed, 900 deletions(-) delete mode 100644 internal/gotelem/messages.go delete mode 100644 internal/gotelem/messages_gen.go delete mode 100644 internal/gotelem/messages_gen_test.go diff --git a/internal/gotelem/messages.go b/internal/gotelem/messages.go deleted file mode 100644 index 0f0cd74..0000000 --- a/internal/gotelem/messages.go +++ /dev/null @@ -1,60 +0,0 @@ -package gotelem - -import ( - "github.com/tinylib/msgp/msgp" -) - -// a body is a thing that can get a type, which we put in the header. -// we use the header to store metadata too -type Body interface { - GetType() string - msgp.Marshaler -} - -//go:generate msgp -type Data struct { - Header map[string]string `msg:"header"` - Body msgp.Raw `msg:"body"` -} - -type CanBody struct { - Id uint32 `msg:"id"` - Payload []byte `msg:"data"` - Source string `msg:"src"` -} - -func (*CanBody) GetType() string { - return "canp" -} - -// A status contains information about the running application. -// mainly internal battery percentage. -type StatusBody struct { - BatteryPct float32 `msg:"batt"` - ErrCode int16 `msg:"err"` // 0 is good. -} - -func (*StatusBody) GetType() string { - return "status" -} - -// takes anything that has a GetType() string method and packs it up. -func NewData(body Body) (*Data, error) { - data := &Data{} - - data.Header["type"] = body.GetType() - - // add other metadata here. - data.Header["ver"] = "0.0.1" - - data.Header["test"] = "mesg" - - rawBody, err := body.MarshalMsg(nil) - if err != nil { - return nil, err - } - - data.Body = rawBody - - return data, nil -} diff --git a/internal/gotelem/messages_gen.go b/internal/gotelem/messages_gen.go deleted file mode 100644 index e6445fd..0000000 --- a/internal/gotelem/messages_gen.go +++ /dev/null @@ -1,491 +0,0 @@ -package gotelem - -// Code generated by github.com/tinylib/msgp DO NOT EDIT. - -import ( - "github.com/tinylib/msgp/msgp" -) - -// DecodeMsg implements msgp.Decodable -func (z *CanBody) DecodeMsg(dc *msgp.Reader) (err error) { - var field []byte - _ = field - var zb0001 uint32 - zb0001, err = dc.ReadMapHeader() - if err != nil { - err = msgp.WrapError(err) - return - } - for zb0001 > 0 { - zb0001-- - field, err = dc.ReadMapKeyPtr() - if err != nil { - err = msgp.WrapError(err) - return - } - switch msgp.UnsafeString(field) { - case "id": - z.Id, err = dc.ReadUint32() - if err != nil { - err = msgp.WrapError(err, "Id") - return - } - case "data": - z.Payload, err = dc.ReadBytes(z.Payload) - if err != nil { - err = msgp.WrapError(err, "Payload") - return - } - case "src": - z.Source, err = dc.ReadString() - if err != nil { - err = msgp.WrapError(err, "Source") - return - } - default: - err = dc.Skip() - if err != nil { - err = msgp.WrapError(err) - return - } - } - } - return -} - -// EncodeMsg implements msgp.Encodable -func (z *CanBody) EncodeMsg(en *msgp.Writer) (err error) { - // map header, size 3 - // write "id" - err = en.Append(0x83, 0xa2, 0x69, 0x64) - if err != nil { - return - } - err = en.WriteUint32(z.Id) - if err != nil { - err = msgp.WrapError(err, "Id") - return - } - // write "data" - err = en.Append(0xa4, 0x64, 0x61, 0x74, 0x61) - if err != nil { - return - } - err = en.WriteBytes(z.Payload) - if err != nil { - err = msgp.WrapError(err, "Payload") - return - } - // write "src" - err = en.Append(0xa3, 0x73, 0x72, 0x63) - if err != nil { - return - } - err = en.WriteString(z.Source) - if err != nil { - err = msgp.WrapError(err, "Source") - return - } - return -} - -// MarshalMsg implements msgp.Marshaler -func (z *CanBody) MarshalMsg(b []byte) (o []byte, err error) { - o = msgp.Require(b, z.Msgsize()) - // map header, size 3 - // string "id" - o = append(o, 0x83, 0xa2, 0x69, 0x64) - o = msgp.AppendUint32(o, z.Id) - // string "data" - o = append(o, 0xa4, 0x64, 0x61, 0x74, 0x61) - o = msgp.AppendBytes(o, z.Payload) - // string "src" - o = append(o, 0xa3, 0x73, 0x72, 0x63) - o = msgp.AppendString(o, z.Source) - return -} - -// UnmarshalMsg implements msgp.Unmarshaler -func (z *CanBody) UnmarshalMsg(bts []byte) (o []byte, err error) { - var field []byte - _ = field - var zb0001 uint32 - zb0001, bts, err = msgp.ReadMapHeaderBytes(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - for zb0001 > 0 { - zb0001-- - field, bts, err = msgp.ReadMapKeyZC(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - switch msgp.UnsafeString(field) { - case "id": - z.Id, bts, err = msgp.ReadUint32Bytes(bts) - if err != nil { - err = msgp.WrapError(err, "Id") - return - } - case "data": - z.Payload, bts, err = msgp.ReadBytesBytes(bts, z.Payload) - if err != nil { - err = msgp.WrapError(err, "Payload") - return - } - case "src": - z.Source, bts, err = msgp.ReadStringBytes(bts) - if err != nil { - err = msgp.WrapError(err, "Source") - return - } - default: - bts, err = msgp.Skip(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - } - } - o = bts - return -} - -// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message -func (z *CanBody) Msgsize() (s int) { - s = 1 + 3 + msgp.Uint32Size + 5 + msgp.BytesPrefixSize + len(z.Payload) + 4 + msgp.StringPrefixSize + len(z.Source) - return -} - -// DecodeMsg implements msgp.Decodable -func (z *Data) DecodeMsg(dc *msgp.Reader) (err error) { - var field []byte - _ = field - var zb0001 uint32 - zb0001, err = dc.ReadMapHeader() - if err != nil { - err = msgp.WrapError(err) - return - } - for zb0001 > 0 { - zb0001-- - field, err = dc.ReadMapKeyPtr() - if err != nil { - err = msgp.WrapError(err) - return - } - switch msgp.UnsafeString(field) { - case "header": - var zb0002 uint32 - zb0002, err = dc.ReadMapHeader() - if err != nil { - err = msgp.WrapError(err, "Header") - return - } - if z.Header == nil { - z.Header = make(map[string]string, zb0002) - } else if len(z.Header) > 0 { - for key := range z.Header { - delete(z.Header, key) - } - } - for zb0002 > 0 { - zb0002-- - var za0001 string - var za0002 string - za0001, err = dc.ReadString() - if err != nil { - err = msgp.WrapError(err, "Header") - return - } - za0002, err = dc.ReadString() - if err != nil { - err = msgp.WrapError(err, "Header", za0001) - return - } - z.Header[za0001] = za0002 - } - case "body": - err = z.Body.DecodeMsg(dc) - if err != nil { - err = msgp.WrapError(err, "Body") - return - } - default: - err = dc.Skip() - if err != nil { - err = msgp.WrapError(err) - return - } - } - } - return -} - -// EncodeMsg implements msgp.Encodable -func (z *Data) EncodeMsg(en *msgp.Writer) (err error) { - // map header, size 2 - // write "header" - err = en.Append(0x82, 0xa6, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72) - if err != nil { - return - } - err = en.WriteMapHeader(uint32(len(z.Header))) - if err != nil { - err = msgp.WrapError(err, "Header") - return - } - for za0001, za0002 := range z.Header { - err = en.WriteString(za0001) - if err != nil { - err = msgp.WrapError(err, "Header") - return - } - err = en.WriteString(za0002) - if err != nil { - err = msgp.WrapError(err, "Header", za0001) - return - } - } - // write "body" - err = en.Append(0xa4, 0x62, 0x6f, 0x64, 0x79) - if err != nil { - return - } - err = z.Body.EncodeMsg(en) - if err != nil { - err = msgp.WrapError(err, "Body") - return - } - return -} - -// MarshalMsg implements msgp.Marshaler -func (z *Data) MarshalMsg(b []byte) (o []byte, err error) { - o = msgp.Require(b, z.Msgsize()) - // map header, size 2 - // string "header" - o = append(o, 0x82, 0xa6, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72) - o = msgp.AppendMapHeader(o, uint32(len(z.Header))) - for za0001, za0002 := range z.Header { - o = msgp.AppendString(o, za0001) - o = msgp.AppendString(o, za0002) - } - // string "body" - o = append(o, 0xa4, 0x62, 0x6f, 0x64, 0x79) - o, err = z.Body.MarshalMsg(o) - if err != nil { - err = msgp.WrapError(err, "Body") - return - } - return -} - -// UnmarshalMsg implements msgp.Unmarshaler -func (z *Data) UnmarshalMsg(bts []byte) (o []byte, err error) { - var field []byte - _ = field - var zb0001 uint32 - zb0001, bts, err = msgp.ReadMapHeaderBytes(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - for zb0001 > 0 { - zb0001-- - field, bts, err = msgp.ReadMapKeyZC(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - switch msgp.UnsafeString(field) { - case "header": - var zb0002 uint32 - zb0002, bts, err = msgp.ReadMapHeaderBytes(bts) - if err != nil { - err = msgp.WrapError(err, "Header") - return - } - if z.Header == nil { - z.Header = make(map[string]string, zb0002) - } else if len(z.Header) > 0 { - for key := range z.Header { - delete(z.Header, key) - } - } - for zb0002 > 0 { - var za0001 string - var za0002 string - zb0002-- - za0001, bts, err = msgp.ReadStringBytes(bts) - if err != nil { - err = msgp.WrapError(err, "Header") - return - } - za0002, bts, err = msgp.ReadStringBytes(bts) - if err != nil { - err = msgp.WrapError(err, "Header", za0001) - return - } - z.Header[za0001] = za0002 - } - case "body": - bts, err = z.Body.UnmarshalMsg(bts) - if err != nil { - err = msgp.WrapError(err, "Body") - return - } - default: - bts, err = msgp.Skip(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - } - } - o = bts - return -} - -// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message -func (z *Data) Msgsize() (s int) { - s = 1 + 7 + msgp.MapHeaderSize - if z.Header != nil { - for za0001, za0002 := range z.Header { - _ = za0002 - s += msgp.StringPrefixSize + len(za0001) + msgp.StringPrefixSize + len(za0002) - } - } - s += 5 + z.Body.Msgsize() - return -} - -// DecodeMsg implements msgp.Decodable -func (z *StatusBody) DecodeMsg(dc *msgp.Reader) (err error) { - var field []byte - _ = field - var zb0001 uint32 - zb0001, err = dc.ReadMapHeader() - if err != nil { - err = msgp.WrapError(err) - return - } - for zb0001 > 0 { - zb0001-- - field, err = dc.ReadMapKeyPtr() - if err != nil { - err = msgp.WrapError(err) - return - } - switch msgp.UnsafeString(field) { - case "batt": - z.BatteryPct, err = dc.ReadFloat32() - if err != nil { - err = msgp.WrapError(err, "BatteryPct") - return - } - case "err": - z.ErrCode, err = dc.ReadInt16() - if err != nil { - err = msgp.WrapError(err, "ErrCode") - return - } - default: - err = dc.Skip() - if err != nil { - err = msgp.WrapError(err) - return - } - } - } - return -} - -// EncodeMsg implements msgp.Encodable -func (z StatusBody) EncodeMsg(en *msgp.Writer) (err error) { - // map header, size 2 - // write "batt" - err = en.Append(0x82, 0xa4, 0x62, 0x61, 0x74, 0x74) - if err != nil { - return - } - err = en.WriteFloat32(z.BatteryPct) - if err != nil { - err = msgp.WrapError(err, "BatteryPct") - return - } - // write "err" - err = en.Append(0xa3, 0x65, 0x72, 0x72) - if err != nil { - return - } - err = en.WriteInt16(z.ErrCode) - if err != nil { - err = msgp.WrapError(err, "ErrCode") - return - } - return -} - -// MarshalMsg implements msgp.Marshaler -func (z StatusBody) MarshalMsg(b []byte) (o []byte, err error) { - o = msgp.Require(b, z.Msgsize()) - // map header, size 2 - // string "batt" - o = append(o, 0x82, 0xa4, 0x62, 0x61, 0x74, 0x74) - o = msgp.AppendFloat32(o, z.BatteryPct) - // string "err" - o = append(o, 0xa3, 0x65, 0x72, 0x72) - o = msgp.AppendInt16(o, z.ErrCode) - return -} - -// UnmarshalMsg implements msgp.Unmarshaler -func (z *StatusBody) UnmarshalMsg(bts []byte) (o []byte, err error) { - var field []byte - _ = field - var zb0001 uint32 - zb0001, bts, err = msgp.ReadMapHeaderBytes(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - for zb0001 > 0 { - zb0001-- - field, bts, err = msgp.ReadMapKeyZC(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - switch msgp.UnsafeString(field) { - case "batt": - z.BatteryPct, bts, err = msgp.ReadFloat32Bytes(bts) - if err != nil { - err = msgp.WrapError(err, "BatteryPct") - return - } - case "err": - z.ErrCode, bts, err = msgp.ReadInt16Bytes(bts) - if err != nil { - err = msgp.WrapError(err, "ErrCode") - return - } - default: - bts, err = msgp.Skip(bts) - if err != nil { - err = msgp.WrapError(err) - return - } - } - } - o = bts - return -} - -// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message -func (z StatusBody) Msgsize() (s int) { - s = 1 + 5 + msgp.Float32Size + 4 + msgp.Int16Size - return -} diff --git a/internal/gotelem/messages_gen_test.go b/internal/gotelem/messages_gen_test.go deleted file mode 100644 index 51fa3e1..0000000 --- a/internal/gotelem/messages_gen_test.go +++ /dev/null @@ -1,349 +0,0 @@ -package gotelem - -// Code generated by github.com/tinylib/msgp DO NOT EDIT. - -import ( - "bytes" - "testing" - - "github.com/tinylib/msgp/msgp" -) - -func TestMarshalUnmarshalCanBody(t *testing.T) { - v := CanBody{} - bts, err := v.MarshalMsg(nil) - if err != nil { - t.Fatal(err) - } - left, err := v.UnmarshalMsg(bts) - if err != nil { - t.Fatal(err) - } - if len(left) > 0 { - t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left) - } - - left, err = msgp.Skip(bts) - if err != nil { - t.Fatal(err) - } - if len(left) > 0 { - t.Errorf("%d bytes left over after Skip(): %q", len(left), left) - } -} - -func BenchmarkMarshalMsgCanBody(b *testing.B) { - v := CanBody{} - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - v.MarshalMsg(nil) - } -} - -func BenchmarkAppendMsgCanBody(b *testing.B) { - v := CanBody{} - bts := make([]byte, 0, v.Msgsize()) - bts, _ = v.MarshalMsg(bts[0:0]) - b.SetBytes(int64(len(bts))) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - bts, _ = v.MarshalMsg(bts[0:0]) - } -} - -func BenchmarkUnmarshalCanBody(b *testing.B) { - v := CanBody{} - bts, _ := v.MarshalMsg(nil) - b.ReportAllocs() - b.SetBytes(int64(len(bts))) - b.ResetTimer() - for i := 0; i < b.N; i++ { - _, err := v.UnmarshalMsg(bts) - if err != nil { - b.Fatal(err) - } - } -} - -func TestEncodeDecodeCanBody(t *testing.T) { - v := CanBody{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - - m := v.Msgsize() - if buf.Len() > m { - t.Log("WARNING: TestEncodeDecodeCanBody Msgsize() is inaccurate") - } - - vn := CanBody{} - err := msgp.Decode(&buf, &vn) - if err != nil { - t.Error(err) - } - - buf.Reset() - msgp.Encode(&buf, &v) - err = msgp.NewReader(&buf).Skip() - if err != nil { - t.Error(err) - } -} - -func BenchmarkEncodeCanBody(b *testing.B) { - v := CanBody{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - b.SetBytes(int64(buf.Len())) - en := msgp.NewWriter(msgp.Nowhere) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - v.EncodeMsg(en) - } - en.Flush() -} - -func BenchmarkDecodeCanBody(b *testing.B) { - v := CanBody{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - b.SetBytes(int64(buf.Len())) - rd := msgp.NewEndlessReader(buf.Bytes(), b) - dc := msgp.NewReader(rd) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - err := v.DecodeMsg(dc) - if err != nil { - b.Fatal(err) - } - } -} - -func TestMarshalUnmarshalData(t *testing.T) { - v := Data{} - bts, err := v.MarshalMsg(nil) - if err != nil { - t.Fatal(err) - } - left, err := v.UnmarshalMsg(bts) - if err != nil { - t.Fatal(err) - } - if len(left) > 0 { - t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left) - } - - left, err = msgp.Skip(bts) - if err != nil { - t.Fatal(err) - } - if len(left) > 0 { - t.Errorf("%d bytes left over after Skip(): %q", len(left), left) - } -} - -func BenchmarkMarshalMsgData(b *testing.B) { - v := Data{} - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - v.MarshalMsg(nil) - } -} - -func BenchmarkAppendMsgData(b *testing.B) { - v := Data{} - bts := make([]byte, 0, v.Msgsize()) - bts, _ = v.MarshalMsg(bts[0:0]) - b.SetBytes(int64(len(bts))) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - bts, _ = v.MarshalMsg(bts[0:0]) - } -} - -func BenchmarkUnmarshalData(b *testing.B) { - v := Data{} - bts, _ := v.MarshalMsg(nil) - b.ReportAllocs() - b.SetBytes(int64(len(bts))) - b.ResetTimer() - for i := 0; i < b.N; i++ { - _, err := v.UnmarshalMsg(bts) - if err != nil { - b.Fatal(err) - } - } -} - -func TestEncodeDecodeData(t *testing.T) { - v := Data{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - - m := v.Msgsize() - if buf.Len() > m { - t.Log("WARNING: TestEncodeDecodeData Msgsize() is inaccurate") - } - - vn := Data{} - err := msgp.Decode(&buf, &vn) - if err != nil { - t.Error(err) - } - - buf.Reset() - msgp.Encode(&buf, &v) - err = msgp.NewReader(&buf).Skip() - if err != nil { - t.Error(err) - } -} - -func BenchmarkEncodeData(b *testing.B) { - v := Data{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - b.SetBytes(int64(buf.Len())) - en := msgp.NewWriter(msgp.Nowhere) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - v.EncodeMsg(en) - } - en.Flush() -} - -func BenchmarkDecodeData(b *testing.B) { - v := Data{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - b.SetBytes(int64(buf.Len())) - rd := msgp.NewEndlessReader(buf.Bytes(), b) - dc := msgp.NewReader(rd) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - err := v.DecodeMsg(dc) - if err != nil { - b.Fatal(err) - } - } -} - -func TestMarshalUnmarshalStatusBody(t *testing.T) { - v := StatusBody{} - bts, err := v.MarshalMsg(nil) - if err != nil { - t.Fatal(err) - } - left, err := v.UnmarshalMsg(bts) - if err != nil { - t.Fatal(err) - } - if len(left) > 0 { - t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left) - } - - left, err = msgp.Skip(bts) - if err != nil { - t.Fatal(err) - } - if len(left) > 0 { - t.Errorf("%d bytes left over after Skip(): %q", len(left), left) - } -} - -func BenchmarkMarshalMsgStatusBody(b *testing.B) { - v := StatusBody{} - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - v.MarshalMsg(nil) - } -} - -func BenchmarkAppendMsgStatusBody(b *testing.B) { - v := StatusBody{} - bts := make([]byte, 0, v.Msgsize()) - bts, _ = v.MarshalMsg(bts[0:0]) - b.SetBytes(int64(len(bts))) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - bts, _ = v.MarshalMsg(bts[0:0]) - } -} - -func BenchmarkUnmarshalStatusBody(b *testing.B) { - v := StatusBody{} - bts, _ := v.MarshalMsg(nil) - b.ReportAllocs() - b.SetBytes(int64(len(bts))) - b.ResetTimer() - for i := 0; i < b.N; i++ { - _, err := v.UnmarshalMsg(bts) - if err != nil { - b.Fatal(err) - } - } -} - -func TestEncodeDecodeStatusBody(t *testing.T) { - v := StatusBody{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - - m := v.Msgsize() - if buf.Len() > m { - t.Log("WARNING: TestEncodeDecodeStatusBody Msgsize() is inaccurate") - } - - vn := StatusBody{} - err := msgp.Decode(&buf, &vn) - if err != nil { - t.Error(err) - } - - buf.Reset() - msgp.Encode(&buf, &v) - err = msgp.NewReader(&buf).Skip() - if err != nil { - t.Error(err) - } -} - -func BenchmarkEncodeStatusBody(b *testing.B) { - v := StatusBody{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - b.SetBytes(int64(buf.Len())) - en := msgp.NewWriter(msgp.Nowhere) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - v.EncodeMsg(en) - } - en.Flush() -} - -func BenchmarkDecodeStatusBody(b *testing.B) { - v := StatusBody{} - var buf bytes.Buffer - msgp.Encode(&buf, &v) - b.SetBytes(int64(buf.Len())) - rd := msgp.NewEndlessReader(buf.Bytes(), b) - dc := msgp.NewReader(rd) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - err := v.DecodeMsg(dc) - if err != nil { - b.Fatal(err) - } - } -}