From 5a67e71fe4d30b095539de49f01b13a6cddc4fee Mon Sep 17 00:00:00 2001 From: Saxon Date: Mon, 1 Apr 2019 12:02:15 +1030 Subject: [PATCH] revid: made minimalHttpSender implement io.Writer --- revid/senders.go | 20 ++++++-------------- revid/senders_test.go | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/revid/senders.go b/revid/senders.go index db122293..6cd4e9bd 100644 --- a/revid/senders.go +++ b/revid/senders.go @@ -45,14 +45,6 @@ import ( "bitbucket.org/ausocean/utils/logger" ) -// Sender is intended to provided functionality for the sending of a byte slice -// to an implemented destination. -type Sender interface { - // send takes the bytes slice d and sends to a particular destination as - // implemented. - send(d []byte) error -} - // Log is used by the multiSender. type Log func(level int8, message string, params ...interface{}) @@ -105,8 +97,8 @@ func newMinimalHttpSender(ns *netsender.Sender, log func(lvl int8, msg string, a } // send takes a bytes slice d and sends to http using s' http client. -func (s *minimalHttpSender) send(d []byte) error { - return httpSend(d, s.client, s.log) +func (s *minimalHttpSender) Write(d []byte) (int, error) { + return len(d), httpSend(d, s.client, s.log) } // loadSender is a destination to send a *ring.Chunk to. @@ -181,7 +173,7 @@ func (s *fileSender) close() error { return s.file.Close() } // lengthed clips based on PSI. It also fixes accounts for discontinuities by // setting the discontinuity indicator for the first packet of a clip. type mtsSender struct { - sender Sender + dst io.Writer buf []byte next []byte pkt packet.Packet @@ -190,9 +182,9 @@ type mtsSender struct { } // newMtsSender returns a new mtsSender. -func newMtsSender(s Sender, log func(lvl int8, msg string, args ...interface{})) *mtsSender { +func newMtsSender(dst io.Writer, log func(lvl int8, msg string, args ...interface{})) *mtsSender { return &mtsSender{ - sender: s, + dst: dst, repairer: mts.NewDiscontinuityRepairer(), } } @@ -210,7 +202,7 @@ func (s *mtsSender) Write(d []byte) (int, error) { if s.curPid == mts.PatPid && len(s.buf) > 0 { err := s.repairer.Repair(s.buf) if err == nil { - err = s.sender.send(s.buf) + _, err = s.dst.Write(s.buf) if err == nil { goto done } diff --git a/revid/senders_test.go b/revid/senders_test.go index d6620002..f1eb7a4c 100644 --- a/revid/senders_test.go +++ b/revid/senders_test.go @@ -59,7 +59,7 @@ var ( // sender simulates sending of video data, creating discontinuities if // testDiscontinuities is set to true. -type sender struct { +type destination struct { buf [][]byte testDiscontinuities bool discontinuityAt int @@ -68,17 +68,17 @@ type sender struct { // send takes d and neglects if testDiscontinuities is true, returning an error, // otherwise d is appended to senders buf. -func (ts *sender) send(d []byte) error { +func (ts *destination) Write(d []byte) (int, error) { //fmt.Println("sending") if ts.testDiscontinuities && ts.currentPkt == ts.discontinuityAt { ts.currentPkt++ - return errSendFailed + return 0, errSendFailed } cpy := make([]byte, len(d)) copy(cpy, d) ts.buf = append(ts.buf, cpy) ts.currentPkt++ - return nil + return len(d), nil } // log implements the required logging func for some of the structs in use @@ -110,8 +110,8 @@ func TestMtsSenderSegment(t *testing.T) { mts.Meta = meta.New() // Create ringBuffer, sender, loadsender and the MPEGTS encoder. - tstSender := &sender{} - loadSender := newMtsSender(tstSender, log) + tstDst := &destination{} + loadSender := newMtsSender(tstDst, log) rb := ring.NewBuffer(rbSize, rbElementSize, wTimeout) encoder := mts.NewEncoder((*buffer)(rb), 25) @@ -141,7 +141,7 @@ func TestMtsSenderSegment(t *testing.T) { } } - result := tstSender.buf + result := tstDst.buf expectData := 0 for clipNo, clip := range result { t.Logf("Checking clip: %v\n", clipNo) @@ -194,8 +194,8 @@ func TestMtsSenderDiscontinuity(t *testing.T) { // Create ringBuffer sender, loadSender and the MPEGTS encoder. const clipWithDiscontinuity = 3 - tstSender := &sender{testDiscontinuities: true, discontinuityAt: clipWithDiscontinuity} - loadSender := newMtsSender(tstSender, log) + tstDst := &destination{testDiscontinuities: true, discontinuityAt: clipWithDiscontinuity} + loadSender := newMtsSender(tstDst, log) rb := ring.NewBuffer(rbSize, rbElementSize, wTimeout) encoder := mts.NewEncoder((*buffer)(rb), 25) @@ -224,7 +224,7 @@ func TestMtsSenderDiscontinuity(t *testing.T) { } } - result := tstSender.buf + result := tstDst.buf // First check that we have less clips as expected. expectedLen := (((noOfPacketsToWrite/psiSendCount)*2 + noOfPacketsToWrite) / psiSendCount) - 1