revid: fix manual input reader and writer

The reader and writer pipes should be created on call to Start
and not in NewManualInput. This is so that if Close is called,
which will close the reader and the writer, a call to start will
create them again.

Approved-by: Alan Noble
Approved-by: David Sutton
This commit is contained in:
Saxon Milton 2023-10-14 00:34:08 +00:00
parent 072f069d1a
commit c98fffe722
1 changed files with 9 additions and 2 deletions

View File

@ -31,6 +31,7 @@ package device
import ( import (
"fmt" "fmt"
"io" "io"
"errors"
"bitbucket.org/ausocean/av/revid/config" "bitbucket.org/ausocean/av/revid/config"
) )
@ -89,12 +90,14 @@ type ManualInput struct {
// NewManualInput provides a new ManualInput. // NewManualInput provides a new ManualInput.
func NewManualInput() *ManualInput { func NewManualInput() *ManualInput {
r, w := io.Pipe() return &ManualInput{}
return &ManualInput{reader: r, writer: w}
} }
// Read reads from the manual input and puts the bytes into p. // Read reads from the manual input and puts the bytes into p.
func (m *ManualInput) Read(p []byte) (int, error) { func (m *ManualInput) Read(p []byte) (int, error) {
if !m.isRunning {
return 0, errors.New("manual input has not been started, can't read")
}
return m.reader.Read(p) return m.reader.Read(p)
} }
@ -109,6 +112,7 @@ func (m *ManualInput) Set(c config.Config) error { return nil }
// to satisfy the Device interface. // to satisfy the Device interface.
func (m *ManualInput) Start() error { func (m *ManualInput) Start() error {
m.isRunning = true m.isRunning = true
m.reader, m.writer = io.Pipe()
return nil return nil
} }
@ -131,5 +135,8 @@ func (m *ManualInput) IsRunning() bool { return m.isRunning }
// Write writes p to the ManualInput's writer side of its pipe. // Write writes p to the ManualInput's writer side of its pipe.
func (m *ManualInput) Write(p []byte) (int, error) { func (m *ManualInput) Write(p []byte) (int, error) {
if !m.isRunning {
return 0, errors.New("manual input has not been started, can't write")
}
return m.writer.Write(p) return m.writer.Write(p)
} }