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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"mayhem-party/src/device/input/button"
|
"mayhem-party/src/device/input/button"
|
||||||
|
"mayhem-party/src/device/input/parse"
|
||||||
"mayhem-party/src/device/input/raw"
|
"mayhem-party/src/device/input/raw"
|
||||||
"mayhem-party/src/device/input/wrap"
|
"mayhem-party/src/device/input/wrap"
|
||||||
)
|
)
|
||||||
|
|
@ -14,7 +15,7 @@ type Input interface {
|
||||||
|
|
||||||
func New(ctx context.Context) Input {
|
func New(ctx context.Context) Input {
|
||||||
src := raw.New(ctx)
|
src := raw.New(ctx)
|
||||||
return wrap.New(ctx, func() button.Parser {
|
return wrap.New(ctx, func() wrap.Wrap {
|
||||||
return button.New(ctx, src)
|
return parse.New(ctx, src)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,24 @@
|
||||||
package button
|
package parse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"mayhem-party/src/device/input/button"
|
||||||
"mayhem-party/src/device/input/raw"
|
"mayhem-party/src/device/input/raw"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
FlagButtonV01 = os.Getenv("BUTTON_V01") == "true"
|
FlagParseV01 = os.Getenv("PARSE_V01") == "true"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Parser interface {
|
type Parser interface {
|
||||||
Read() []Button
|
Read() []button.Button
|
||||||
Close()
|
Close()
|
||||||
CloseWrap() raw.Raw
|
CloseWrap() raw.Raw
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, src raw.Raw) Parser {
|
func New(ctx context.Context, src raw.Raw) Parser {
|
||||||
if FlagButtonV01 {
|
if FlagParseV01 {
|
||||||
return NewV01(ctx, src)
|
return NewV01(ctx, src)
|
||||||
}
|
}
|
||||||
return NewPlaintext(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 (
|
import (
|
||||||
|
"mayhem-party/src/device/input/button"
|
||||||
"mayhem-party/src/device/input/raw"
|
"mayhem-party/src/device/input/raw"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
FlagButtonPlaintextRelease = os.Getenv("BUTTON_PLAINTEXT_RELEASE")
|
FlagParsePlaintextRelease = os.Getenv("PARSE_PLAINTEXT_RELEASE")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Plaintext struct {
|
type Plaintext struct {
|
||||||
|
|
@ -16,8 +17,8 @@ type Plaintext struct {
|
||||||
|
|
||||||
func NewPlaintext(src raw.Raw) Plaintext {
|
func NewPlaintext(src raw.Raw) Plaintext {
|
||||||
releaseChar := byte('!')
|
releaseChar := byte('!')
|
||||||
if FlagButtonPlaintextRelease != "" {
|
if FlagParsePlaintextRelease != "" {
|
||||||
releaseChar = byte(FlagButtonPlaintextRelease[0])
|
releaseChar = byte(FlagParsePlaintextRelease[0])
|
||||||
}
|
}
|
||||||
return Plaintext{
|
return Plaintext{
|
||||||
src: src,
|
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) CloseWrap() raw.Raw { return p.src }
|
||||||
|
|
||||||
func (p Plaintext) Read() []Button {
|
func (p Plaintext) Read() []button.Button {
|
||||||
b := p.src.Read()
|
b := p.src.Read()
|
||||||
buttons := make([]Button, 0, len(b))
|
buttons := make([]button.Button, 0, len(b))
|
||||||
down := true
|
down := true
|
||||||
for i := range b {
|
for i := range b {
|
||||||
if b[i] == p.release {
|
if b[i] == p.release {
|
||||||
down = false
|
down = false
|
||||||
} else {
|
} else {
|
||||||
if b[i] != '\n' {
|
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
|
down = true
|
||||||
}
|
}
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
package button_test
|
package parse_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"mayhem-party/src/device/input/button"
|
"mayhem-party/src/device/input/button"
|
||||||
|
"mayhem-party/src/device/input/parse"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPlaintext(t *testing.T) {
|
func TestPlaintext(t *testing.T) {
|
||||||
src := constSrc("c!b")
|
src := constSrc("c!b")
|
||||||
p := button.NewPlaintext(src)
|
p := parse.NewPlaintext(src)
|
||||||
got := p.Read()
|
got := p.Read()
|
||||||
if len(got) != 2 {
|
if len(got) != 2 {
|
||||||
t.Fatal(len(got))
|
t.Fatal(len(got))
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package button
|
package parse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"mayhem-party/src/device/input/button"
|
||||||
"mayhem-party/src/device/input/raw"
|
"mayhem-party/src/device/input/raw"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -16,7 +17,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
FlagDebug = os.Getenv("DEBUG") == "true"
|
FlagDebug = os.Getenv("DEBUG") == "true"
|
||||||
FlagButtonV01Config = os.Getenv("BUTTON_V01_CONFIG")
|
FlagParseV01Config = os.Getenv("PARSE_V01_CONFIG")
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|
@ -49,7 +50,7 @@ type (
|
||||||
|
|
||||||
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
||||||
var cfg v01Cfg
|
var cfg v01Cfg
|
||||||
b, _ := ioutil.ReadFile(FlagButtonV01Config)
|
b, _ := ioutil.ReadFile(FlagParseV01Config)
|
||||||
yaml.Unmarshal(b, &cfg)
|
yaml.Unmarshal(b, &cfg)
|
||||||
ctx, can := context.WithCancel(ctx)
|
ctx, can := context.WithCancel(ctx)
|
||||||
result := &V01{
|
result := &V01{
|
||||||
|
|
@ -81,6 +82,9 @@ func (v01 *V01) listen() {
|
||||||
v := v01.cfg.Users["broadcast"]
|
v := v01.cfg.Users["broadcast"]
|
||||||
v.Message = string(b)
|
v.Message = string(b)
|
||||||
v01.cfg.Users["broadcast"] = v
|
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()
|
v01.src.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) Read() []Button {
|
func (v01 *V01) Read() []button.Button {
|
||||||
line := v01.src.Read()
|
line := v01.src.Read()
|
||||||
var msg v01Msg
|
var msg v01Msg
|
||||||
if err := json.Unmarshal(line, &msg); err != nil {
|
if err := json.Unmarshal(line, &msg); err != nil {
|
||||||
|
|
@ -148,13 +152,13 @@ func (v01 *V01) telemetry(msg v01Msg) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg v01Msg) buttons() []Button {
|
func (msg v01Msg) buttons() []button.Button {
|
||||||
buttons := make([]Button, len(msg.Y)+len(msg.N))
|
buttons := make([]button.Button, len(msg.Y)+len(msg.N))
|
||||||
for i := range msg.Y {
|
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 {
|
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 {
|
if FlagDebug {
|
||||||
log.Printf("%+v", msg)
|
log.Printf("%+v", msg)
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
package button_test
|
package parse_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mayhem-party/src/device/input/button"
|
"mayhem-party/src/device/input/button"
|
||||||
|
"mayhem-party/src/device/input/parse"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
@ -16,7 +17,7 @@ import (
|
||||||
func TestV01(t *testing.T) {
|
func TestV01(t *testing.T) {
|
||||||
src := constSrc(fmt.Sprintf(`{"T":%v,"U":"bel","Y":"abc","N":"cde"}`, time.Now().UnixNano()/int64(time.Millisecond)-50))
|
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())
|
t.Logf("(%v) %s", len(src), src.Read())
|
||||||
v01 := button.NewV01(context.Background(), src)
|
v01 := parse.NewV01(context.Background(), src)
|
||||||
defer v01.Close()
|
defer v01.Close()
|
||||||
got := v01.Read()
|
got := v01.Read()
|
||||||
want := []button.Button{
|
want := []button.Button{
|
||||||
|
|
@ -50,10 +51,10 @@ func TestV01WithCfg(t *testing.T) {
|
||||||
- transformation:
|
- transformation:
|
||||||
w: i
|
w: i
|
||||||
`), os.ModePerm)
|
`), os.ModePerm)
|
||||||
button.FlagButtonV01Config = p
|
parse.FlagParseV01Config = p
|
||||||
|
|
||||||
t.Run("unknown user ignored", func(t *testing.T) {
|
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()
|
defer v01.Close()
|
||||||
got := v01.Read()
|
got := v01.Read()
|
||||||
if len(got) != 0 {
|
if len(got) != 0 {
|
||||||
|
|
@ -62,7 +63,7 @@ func TestV01WithCfg(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("player2", func(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()
|
defer v01.Close()
|
||||||
got := v01.Read()
|
got := v01.Read()
|
||||||
if len(got) != 2 {
|
if len(got) != 2 {
|
||||||
|
|
@ -95,11 +96,11 @@ func TestV01Feedback(t *testing.T) {
|
||||||
- transformation:
|
- transformation:
|
||||||
w: i
|
w: i
|
||||||
`), os.ModePerm)
|
`), os.ModePerm)
|
||||||
button.FlagButtonV01Config = p
|
parse.FlagParseV01Config = p
|
||||||
ctx, can := context.WithCancel(context.Background())
|
ctx, can := context.WithCancel(context.Background())
|
||||||
defer can()
|
defer can()
|
||||||
|
|
||||||
v01 := button.NewV01(ctx, constSrc(`{"U":"qt","Y":"w"}`))
|
v01 := parse.NewV01(ctx, constSrc(`{"U":"qt","Y":"w"}`))
|
||||||
defer v01.Close()
|
defer v01.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package button
|
package parse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
chSigUsr1 = func() chan os.Signal {
|
ChSigUsr1 = func() chan os.Signal {
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, syscall.SIGUSR1)
|
signal.Notify(c, syscall.SIGUSR1)
|
||||||
return c
|
return c
|
||||||
|
|
@ -24,7 +24,7 @@ type Refresh struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRefresh(ctx context.Context, newWrap func() Wrap) *Refresh {
|
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 {
|
func NewRefreshWith(ctx context.Context, newWrap func() Wrap, ch <-chan os.Signal) *Refresh {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package wrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"mayhem-party/src/device/input/button"
|
"mayhem-party/src/device/input/parse"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -50,7 +50,7 @@ func TestRefreshDoesntCloseSources(t *testing.T) {
|
||||||
newParsers := 0
|
newParsers := 0
|
||||||
newParser := func() Wrap {
|
newParser := func() Wrap {
|
||||||
newParsers += 1
|
newParsers += 1
|
||||||
return button.NewPlaintext(src)
|
return parse.NewPlaintext(src)
|
||||||
}
|
}
|
||||||
ctx, can := context.WithCancel(context.Background())
|
ctx, can := context.WithCancel(context.Background())
|
||||||
defer can()
|
defer can()
|
||||||
|
|
@ -69,7 +69,7 @@ func TestRefreshDoesntCloseSources(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
chSigUsr1 <- syscall.SIGINT
|
ChSigUsr1 <- syscall.SIGINT
|
||||||
}
|
}
|
||||||
time.Sleep(time.Millisecond * 250)
|
time.Sleep(time.Millisecond * 250)
|
||||||
if want := (telemetrySrc{reads: 5}); *src != want {
|
if want := (telemetrySrc{reads: 5}); *src != want {
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,9 @@ type Wrap interface {
|
||||||
CloseWrap() raw.Raw
|
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 {
|
maker := func() Wrap {
|
||||||
return srcFunc()
|
return parserFunc()
|
||||||
}
|
}
|
||||||
if FlagBuffered {
|
if FlagBuffered {
|
||||||
oldMaker := maker
|
oldMaker := maker
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue