From 373d8be1a0b29dc3bb6b0279e15cf077813dab53 Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 25 Mar 2023 22:52:09 -0600 Subject: [PATCH] split button and parse packages --- src/device/input/button/parser_test.go | 11 ---------- src/device/input/input.go | 5 +++-- src/device/input/{button => parse}/parser.go | 9 ++++---- src/device/input/parse/parser_test.go | 11 ++++++++++ .../input/{button => parse}/plaintext.go | 15 +++++++------ .../input/{button => parse}/plaintext_test.go | 5 +++-- .../input/{button => parse}/testdata/v01.yaml | 0 src/device/input/{button => parse}/v01.go | 22 +++++++++++-------- .../{button => parse}/v01_exported_test.go | 15 +++++++------ .../input/{button => parse}/v01_test.go | 2 +- src/device/input/wrap/refresh.go | 4 ++-- src/device/input/wrap/refresh_test.go | 6 ++--- src/device/input/wrap/wrap.go | 4 ++-- 13 files changed, 59 insertions(+), 50 deletions(-) delete mode 100644 src/device/input/button/parser_test.go rename src/device/input/{button => parse}/parser.go (62%) create mode 100644 src/device/input/parse/parser_test.go rename src/device/input/{button => parse}/plaintext.go (59%) rename src/device/input/{button => parse}/plaintext_test.go (83%) rename src/device/input/{button => parse}/testdata/v01.yaml (100%) rename src/device/input/{button => parse}/v01.go (82%) rename src/device/input/{button => parse}/v01_exported_test.go (88%) rename src/device/input/{button => parse}/v01_test.go (98%) diff --git a/src/device/input/button/parser_test.go b/src/device/input/button/parser_test.go deleted file mode 100644 index af5db09..0000000 --- a/src/device/input/button/parser_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package button_test - -import ( - "mayhem-party/src/device/input/button" - "testing" -) - -func TestParser(t *testing.T) { - var _ button.Parser = button.Plaintext{} - var _ button.Parser = &button.V01{} -} diff --git a/src/device/input/input.go b/src/device/input/input.go index c3ca210..40ccc40 100644 --- a/src/device/input/input.go +++ b/src/device/input/input.go @@ -3,6 +3,7 @@ package input import ( "context" "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/parse" "mayhem-party/src/device/input/raw" "mayhem-party/src/device/input/wrap" ) @@ -14,7 +15,7 @@ type Input interface { func New(ctx context.Context) Input { src := raw.New(ctx) - return wrap.New(ctx, func() button.Parser { - return button.New(ctx, src) + return wrap.New(ctx, func() wrap.Wrap { + return parse.New(ctx, src) }) } diff --git a/src/device/input/button/parser.go b/src/device/input/parse/parser.go similarity index 62% rename from src/device/input/button/parser.go rename to src/device/input/parse/parser.go index cbdba8e..4d27bbf 100644 --- a/src/device/input/button/parser.go +++ b/src/device/input/parse/parser.go @@ -1,23 +1,24 @@ -package button +package parse import ( "context" + "mayhem-party/src/device/input/button" "mayhem-party/src/device/input/raw" "os" ) var ( - FlagButtonV01 = os.Getenv("BUTTON_V01") == "true" + FlagParseV01 = os.Getenv("PARSE_V01") == "true" ) type Parser interface { - Read() []Button + Read() []button.Button Close() CloseWrap() raw.Raw } func New(ctx context.Context, src raw.Raw) Parser { - if FlagButtonV01 { + if FlagParseV01 { return NewV01(ctx, src) } return NewPlaintext(src) diff --git a/src/device/input/parse/parser_test.go b/src/device/input/parse/parser_test.go new file mode 100644 index 0000000..f7676a4 --- /dev/null +++ b/src/device/input/parse/parser_test.go @@ -0,0 +1,11 @@ +package parse_test + +import ( + "mayhem-party/src/device/input/parse" + "testing" +) + +func TestParser(t *testing.T) { + var _ parse.Parser = parse.Plaintext{} + var _ parse.Parser = &parse.V01{} +} diff --git a/src/device/input/button/plaintext.go b/src/device/input/parse/plaintext.go similarity index 59% rename from src/device/input/button/plaintext.go rename to src/device/input/parse/plaintext.go index c6d7d79..72cc18c 100644 --- a/src/device/input/button/plaintext.go +++ b/src/device/input/parse/plaintext.go @@ -1,12 +1,13 @@ -package button +package parse import ( + "mayhem-party/src/device/input/button" "mayhem-party/src/device/input/raw" "os" ) var ( - FlagButtonPlaintextRelease = os.Getenv("BUTTON_PLAINTEXT_RELEASE") + FlagParsePlaintextRelease = os.Getenv("PARSE_PLAINTEXT_RELEASE") ) type Plaintext struct { @@ -16,8 +17,8 @@ type Plaintext struct { func NewPlaintext(src raw.Raw) Plaintext { releaseChar := byte('!') - if FlagButtonPlaintextRelease != "" { - releaseChar = byte(FlagButtonPlaintextRelease[0]) + if FlagParsePlaintextRelease != "" { + releaseChar = byte(FlagParsePlaintextRelease[0]) } return Plaintext{ src: src, @@ -29,16 +30,16 @@ func (p Plaintext) Close() { p.src.Close() } func (p Plaintext) CloseWrap() raw.Raw { return p.src } -func (p Plaintext) Read() []Button { +func (p Plaintext) Read() []button.Button { b := p.src.Read() - buttons := make([]Button, 0, len(b)) + buttons := make([]button.Button, 0, len(b)) down := true for i := range b { if b[i] == p.release { down = false } else { if b[i] != '\n' { - buttons = append(buttons, Button{Char: b[i], Down: down}) + buttons = append(buttons, button.Button{Char: b[i], Down: down}) } down = true } diff --git a/src/device/input/button/plaintext_test.go b/src/device/input/parse/plaintext_test.go similarity index 83% rename from src/device/input/button/plaintext_test.go rename to src/device/input/parse/plaintext_test.go index f293aab..8acaeaa 100644 --- a/src/device/input/button/plaintext_test.go +++ b/src/device/input/parse/plaintext_test.go @@ -1,13 +1,14 @@ -package button_test +package parse_test import ( "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/parse" "testing" ) func TestPlaintext(t *testing.T) { src := constSrc("c!b") - p := button.NewPlaintext(src) + p := parse.NewPlaintext(src) got := p.Read() if len(got) != 2 { t.Fatal(len(got)) diff --git a/src/device/input/button/testdata/v01.yaml b/src/device/input/parse/testdata/v01.yaml similarity index 100% rename from src/device/input/button/testdata/v01.yaml rename to src/device/input/parse/testdata/v01.yaml diff --git a/src/device/input/button/v01.go b/src/device/input/parse/v01.go similarity index 82% rename from src/device/input/button/v01.go rename to src/device/input/parse/v01.go index 8f8c059..761b748 100644 --- a/src/device/input/button/v01.go +++ b/src/device/input/parse/v01.go @@ -1,4 +1,4 @@ -package button +package parse import ( "context" @@ -6,6 +6,7 @@ import ( "io" "io/ioutil" "log" + "mayhem-party/src/device/input/button" "mayhem-party/src/device/input/raw" "net/http" "os" @@ -15,8 +16,8 @@ import ( ) var ( - FlagDebug = os.Getenv("DEBUG") == "true" - FlagButtonV01Config = os.Getenv("BUTTON_V01_CONFIG") + FlagDebug = os.Getenv("DEBUG") == "true" + FlagParseV01Config = os.Getenv("PARSE_V01_CONFIG") ) type ( @@ -49,7 +50,7 @@ type ( func NewV01(ctx context.Context, src raw.Raw) *V01 { var cfg v01Cfg - b, _ := ioutil.ReadFile(FlagButtonV01Config) + b, _ := ioutil.ReadFile(FlagParseV01Config) yaml.Unmarshal(b, &cfg) ctx, can := context.WithCancel(ctx) result := &V01{ @@ -81,6 +82,9 @@ func (v01 *V01) listen() { v := v01.cfg.Users["broadcast"] v.Message = string(b) v01.cfg.Users["broadcast"] = v + if _, ok := r.URL.Query()["refresh"]; ok { + //wrap.Signal <- syscall.SIGUSR1 + } } }), } @@ -106,7 +110,7 @@ func (v01 *V01) Close() { v01.src.Close() } -func (v01 *V01) Read() []Button { +func (v01 *V01) Read() []button.Button { line := v01.src.Read() var msg v01Msg if err := json.Unmarshal(line, &msg); err != nil { @@ -148,13 +152,13 @@ func (v01 *V01) telemetry(msg v01Msg) { } } -func (msg v01Msg) buttons() []Button { - buttons := make([]Button, len(msg.Y)+len(msg.N)) +func (msg v01Msg) buttons() []button.Button { + buttons := make([]button.Button, len(msg.Y)+len(msg.N)) for i := range msg.Y { - buttons[i] = Button{Char: msg.Y[i], Down: true} + buttons[i] = button.Button{Char: msg.Y[i], Down: true} } for i := range msg.N { - buttons[len(msg.Y)+i] = Button{Char: msg.N[i], Down: false} + buttons[len(msg.Y)+i] = button.Button{Char: msg.N[i], Down: false} } if FlagDebug { log.Printf("%+v", msg) diff --git a/src/device/input/button/v01_exported_test.go b/src/device/input/parse/v01_exported_test.go similarity index 88% rename from src/device/input/button/v01_exported_test.go rename to src/device/input/parse/v01_exported_test.go index e8b4501..7c44ba4 100644 --- a/src/device/input/button/v01_exported_test.go +++ b/src/device/input/parse/v01_exported_test.go @@ -1,10 +1,11 @@ -package button_test +package parse_test import ( "context" "fmt" "io" "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/parse" "net/http" "os" "path" @@ -16,7 +17,7 @@ import ( func TestV01(t *testing.T) { src := constSrc(fmt.Sprintf(`{"T":%v,"U":"bel","Y":"abc","N":"cde"}`, time.Now().UnixNano()/int64(time.Millisecond)-50)) t.Logf("(%v) %s", len(src), src.Read()) - v01 := button.NewV01(context.Background(), src) + v01 := parse.NewV01(context.Background(), src) defer v01.Close() got := v01.Read() want := []button.Button{ @@ -50,10 +51,10 @@ func TestV01WithCfg(t *testing.T) { - transformation: w: i `), os.ModePerm) - button.FlagButtonV01Config = p + parse.FlagParseV01Config = p t.Run("unknown user ignored", func(t *testing.T) { - v01 := button.NewV01(context.Background(), constSrc(`{"U":"qt","Y":"w"}`)) + v01 := parse.NewV01(context.Background(), constSrc(`{"U":"qt","Y":"w"}`)) defer v01.Close() got := v01.Read() if len(got) != 0 { @@ -62,7 +63,7 @@ func TestV01WithCfg(t *testing.T) { }) t.Run("player2", func(t *testing.T) { - v01 := button.NewV01(context.Background(), constSrc(`{"U":"bel","Y":"w","N":"w"}`)) + v01 := parse.NewV01(context.Background(), constSrc(`{"U":"bel","Y":"w","N":"w"}`)) defer v01.Close() got := v01.Read() if len(got) != 2 { @@ -95,11 +96,11 @@ func TestV01Feedback(t *testing.T) { - transformation: w: i `), os.ModePerm) - button.FlagButtonV01Config = p + parse.FlagParseV01Config = p ctx, can := context.WithCancel(context.Background()) defer can() - v01 := button.NewV01(ctx, constSrc(`{"U":"qt","Y":"w"}`)) + v01 := parse.NewV01(ctx, constSrc(`{"U":"qt","Y":"w"}`)) defer v01.Close() for { diff --git a/src/device/input/button/v01_test.go b/src/device/input/parse/v01_test.go similarity index 98% rename from src/device/input/button/v01_test.go rename to src/device/input/parse/v01_test.go index 8cf335f..42dd776 100644 --- a/src/device/input/button/v01_test.go +++ b/src/device/input/parse/v01_test.go @@ -1,4 +1,4 @@ -package button +package parse import ( "testing" diff --git a/src/device/input/wrap/refresh.go b/src/device/input/wrap/refresh.go index f6c83b9..6c80388 100644 --- a/src/device/input/wrap/refresh.go +++ b/src/device/input/wrap/refresh.go @@ -11,7 +11,7 @@ import ( ) var ( - chSigUsr1 = func() chan os.Signal { + ChSigUsr1 = func() chan os.Signal { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGUSR1) return c @@ -24,7 +24,7 @@ type Refresh struct { } func NewRefresh(ctx context.Context, newWrap func() Wrap) *Refresh { - return NewRefreshWith(ctx, newWrap, chSigUsr1) + return NewRefreshWith(ctx, newWrap, ChSigUsr1) } func NewRefreshWith(ctx context.Context, newWrap func() Wrap, ch <-chan os.Signal) *Refresh { diff --git a/src/device/input/wrap/refresh_test.go b/src/device/input/wrap/refresh_test.go index 8a4f596..e0ee5b8 100644 --- a/src/device/input/wrap/refresh_test.go +++ b/src/device/input/wrap/refresh_test.go @@ -2,7 +2,7 @@ package wrap import ( "context" - "mayhem-party/src/device/input/button" + "mayhem-party/src/device/input/parse" "os" "syscall" "testing" @@ -50,7 +50,7 @@ func TestRefreshDoesntCloseSources(t *testing.T) { newParsers := 0 newParser := func() Wrap { newParsers += 1 - return button.NewPlaintext(src) + return parse.NewPlaintext(src) } ctx, can := context.WithCancel(context.Background()) defer can() @@ -69,7 +69,7 @@ func TestRefreshDoesntCloseSources(t *testing.T) { } for i := 0; i < 5; i++ { - chSigUsr1 <- syscall.SIGINT + ChSigUsr1 <- syscall.SIGINT } time.Sleep(time.Millisecond * 250) if want := (telemetrySrc{reads: 5}); *src != want { diff --git a/src/device/input/wrap/wrap.go b/src/device/input/wrap/wrap.go index 65814eb..aa31897 100644 --- a/src/device/input/wrap/wrap.go +++ b/src/device/input/wrap/wrap.go @@ -19,9 +19,9 @@ type Wrap interface { CloseWrap() raw.Raw } -func New(ctx context.Context, srcFunc func() button.Parser) Wrap { +func New(ctx context.Context, parserFunc func() Wrap) Wrap { maker := func() Wrap { - return srcFunc() + return parserFunc() } if FlagBuffered { oldMaker := maker