split button and parse packages
parent
bd5654128e
commit
373d8be1a0
|
|
@ -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{}
|
||||
}
|
||||
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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{}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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))
|
||||
|
|
@ -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)
|
||||
|
|
@ -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 {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package button
|
||||
package parse
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue