From 6ed8727e980d31475a91c0b1a35fd4aa5a3bbf75 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 30 Apr 2017 02:39:27 +0200 Subject: protocol version is not really part of the SourceId --- src/hub/src/spreadspace.org/sfive/s5cvt.go | 15 +++++--- src/hub/src/spreadspace.org/sfive/s5cvt_test.go | 18 +++++----- src/hub/src/spreadspace.org/sfive/s5store_test.go | 42 ++++++++++------------- src/hub/src/spreadspace.org/sfive/s5typesApi.go | 7 ++-- 4 files changed, 41 insertions(+), 41 deletions(-) (limited to 'src/hub') diff --git a/src/hub/src/spreadspace.org/sfive/s5cvt.go b/src/hub/src/spreadspace.org/sfive/s5cvt.go index 28162ba..48eac4b 100644 --- a/src/hub/src/spreadspace.org/sfive/s5cvt.go +++ b/src/hub/src/spreadspace.org/sfive/s5cvt.go @@ -37,6 +37,10 @@ import ( "fmt" ) +const ( + ProtocolVersion = 1 +) + type FullDecoder interface { Decode(jsonString []byte) (DataUpdateFull, error) } @@ -46,7 +50,8 @@ type FullEncoder interface { } type StatefulDecoder struct { - sourceId SourceId + Version uint + SourceId } type PlainDecoder struct{} @@ -55,12 +60,12 @@ type PlainEncoder struct{} func NewStatefulDecoder(jsonString []byte) (decoder FullDecoder, err error) { res := new(StatefulDecoder) - err = json.Unmarshal(jsonString, &res.sourceId) + err = json.Unmarshal(jsonString, &res) if err != nil { return } - if res.sourceId.Version != 1 { - err = fmt.Errorf("unsupported version, expected 1, actual %v", res.sourceId.Version) + if res.Version != ProtocolVersion { + err = fmt.Errorf("unsupported version, expected %d, actual %v", ProtocolVersion, res.Version) } decoder = res return @@ -71,7 +76,7 @@ func NewPlainDecoder() FullDecoder { } func (sd *StatefulDecoder) Decode(jsonString []byte) (dat DataUpdateFull, err error) { - dat.CopyFromSourceId(&sd.sourceId) + dat.CopyFromSourceId(&sd.SourceId) err = json.Unmarshal(jsonString, &dat) // like in PlainDecoder, let the client decide how to use partial results // (Unmarshal returns partial results in case of errors) diff --git a/src/hub/src/spreadspace.org/sfive/s5cvt_test.go b/src/hub/src/spreadspace.org/sfive/s5cvt_test.go index 94ddc85..cec0773 100644 --- a/src/hub/src/spreadspace.org/sfive/s5cvt_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5cvt_test.go @@ -39,24 +39,26 @@ import ( ) var ( - sourceIdFields = `"hostname": "localhost", "streamer-id": {"quality": "low", "content-id": "av", "format": "webm"}, "tags": ["elevate", "2014"], "version": 1` - sourceIdData = `{` + sourceIdFields + `}` - sourceIdDataStruct = SourceId{Hostname: "localhost", StreamId: StreamId{Quality: "low", ContentId: "av", Format: "webm"}, Tags: []string{"elevate", "2014"}, Version: 1} - updateFields = `"data": {"bytes-sent": 1, "client-count": 3, "bytes-received": 1}, "start-time": "2014-08-24T14:35:33.847282Z", "duration-ms": 5000` - updateData = "{" + updateFields + "}" - updateDataStruct = DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC), Duration: 5000} - testData = "{" + sourceIdFields + "," + updateFields + "}" + sourceIdFields = `"hostname": "localhost", "streamer-id": {"quality": "low", "content-id": "av", "format": "webm"}, "tags": ["elevate", "2014"], "version": 1` + sourceIdData = `{` + sourceIdFields + `}` + sourceIdDataStruct = SourceId{Hostname: "localhost", StreamId: StreamId{Quality: "low", ContentId: "av", Format: "webm"}, Tags: []string{"elevate", "2014"}} + updateFields = `"data": {"bytes-sent": 1, "client-count": 3, "bytes-received": 1}, "start-time": "2014-08-24T14:35:33.847282Z", "duration-ms": 5000` + updateData = "{" + updateFields + "}" + updateDataStruct = DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC), Duration: 5000} + testData = "{" + sourceIdFields + "," + updateFields + "}" + statefulInitMessage = `{ "version": 1, ` + sourceIdFields + `}` ) func GetExpected() *DataUpdateFull { expected := new(DataUpdateFull) + expected.Version = ProtocolVersion expected.CopyFromSourceId(&sourceIdDataStruct) expected.CopyFromUpdate(&updateDataStruct) return expected } func TestDecodeStateful(t *testing.T) { - dc, err := NewStatefulDecoder([]byte(sourceIdData)) + dc, err := NewStatefulDecoder([]byte(statefulInitMessage)) if err != nil { t.Errorf("Creating decoder failed with %v", err) return diff --git a/src/hub/src/spreadspace.org/sfive/s5store_test.go b/src/hub/src/spreadspace.org/sfive/s5store_test.go index bda0a5e..f0b9d1c 100644 --- a/src/hub/src/spreadspace.org/sfive/s5store_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5store_test.go @@ -43,12 +43,6 @@ import ( "github.com/boltdb/bolt" ) -const ( - QualityLow = "low" - QualityMedium = "medium" - QualityHigh = "high" -) - var ( __boltPath = "/run/s5hub_testing_db.bolt" ) @@ -192,25 +186,25 @@ func TestOpen(t *testing.T) { store.Close() } -// func TestAppend(t *testing.T) { -// os.Remove(__boltPath) -// store, err := NewStore(__boltPath, false) -// if err != nil { -// t.Fatalf("Failed to initialize: %v", err) -// } -// defer store.Close() +func TestAppend(t *testing.T) { + os.Remove(__boltPath) + store, err := NewStore(__boltPath, false) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + defer store.Close() -// startTime := time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC) -// update := DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: startTime, Duration: 5000} -// streamId := StreamId{ContentId: "content", Format: "7bitascii", Quality: QualityHigh} -// source := SourceId{Hostname: "localhost", Tags: []string{"tag1", "master"}, StreamId: streamId, Version: 1} -// dat := DataUpdateFull{"", -1, source, update} + startTime := time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC) + update := DataUpdate{Data: SourceData{ClientCount: 3, BytesReceived: 42, BytesSent: 3 * 42}, StartTime: startTime, Duration: 5000} + streamId := StreamId{ContentId: "talkingheads", Format: "7bitascii", Quality: "high"} + source := SourceId{Hostname: "streamer", Tags: []string{"tag1", "master"}, StreamId: streamId} + dat := DataUpdateFull{ProtocolVersion, "", -1, source, update} -// err = store.Append(dat) -// if err != nil { -// t.Fatalf("Failed to append: %v", err) -// } -// } + err = store.Append(dat) + if err != nil { + t.Fatalf("Failed to append: %v", err) + } +} // func TestGetUpdatesAfter(t *testing.T) { // os.Remove(__boltPath) @@ -256,7 +250,7 @@ func TestReadOnly(t *testing.T) { store.Close() // - // TODO: test append on read-only store + // TODO: test fetch and append on read-only store // } diff --git a/src/hub/src/spreadspace.org/sfive/s5typesApi.go b/src/hub/src/spreadspace.org/sfive/s5typesApi.go index 233659a..0cd5100 100644 --- a/src/hub/src/spreadspace.org/sfive/s5typesApi.go +++ b/src/hub/src/spreadspace.org/sfive/s5typesApi.go @@ -41,10 +41,9 @@ type StreamId struct { } type SourceId struct { - Version uint `json:"version" db:"-"` Hostname string `json:"hostname"` - StreamId StreamId `json:"streamer-id" db:"-"` - Tags []string `json:"tags" db:"-"` + StreamId StreamId `json:"streamer-id"` + Tags []string `json:"tags"` } type ClientData struct { @@ -67,6 +66,7 @@ type DataUpdate struct { } type DataUpdateFull struct { + Version uint `json:"version"` SourceHubUuid string `json:"SourceHubUuid,omitempty"` SourceHubDataUpdateId int `json:"SourceHubDataUpdateId,omitempty"` SourceId @@ -81,7 +81,6 @@ func (duf *DataUpdateFull) CopyFromSourceId(src *SourceId) { duf.Hostname = src.Hostname duf.StreamId = src.StreamId duf.Tags = src.Tags - duf.Version = src.Version } func (duf *DataUpdateFull) CopyFromUpdate(du *DataUpdate) { -- cgit v1.2.3