-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34c62a0
commit a9e668f
Showing
10 changed files
with
794 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# buttplughttp | ||
|
||
An example application using the buttplug API that exposes a REST API to | ||
control devices. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module github.com/diamondburned/go-buttplug/cmd/buttplughttp | ||
|
||
go 1.17 | ||
|
||
replace github.com/diamondburned/go-buttplug => ../../ | ||
|
||
require github.com/diamondburned/go-buttplug v0.0.0-00010101000000-000000000000 | ||
|
||
require ( | ||
github.com/go-chi/chi v1.5.4 // indirect | ||
github.com/gorilla/schema v1.2.0 // indirect | ||
github.com/gorilla/websocket v1.4.2 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs= | ||
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg= | ||
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= | ||
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= | ||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= | ||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64= | ||
github.com/qri-io/jsonschema v0.2.1/go.mod h1:g7DPkiOsK1xv6T/Ao5scXRkd+yTFygcANPBaaqW+VrI= | ||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/diamondburned/go-buttplug" | ||
"github.com/diamondburned/go-buttplug/device" | ||
"github.com/diamondburned/go-buttplug/intiface" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
wsPort = 20000 | ||
httpAddr = "localhost:8080" | ||
intifaceCLI = "intiface-cli" | ||
) | ||
|
||
func main() { | ||
flag.IntVar(&wsPort, "ws-port", wsPort, "websocket port to start from") | ||
flag.StringVar(&httpAddr, "http-addr", httpAddr, "http address to listen to") | ||
flag.StringVar(&intifaceCLI, "exec", intifaceCLI, "command to execute if no --ws-addr") | ||
flag.Parse() | ||
|
||
if err := run(); err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
|
||
func run() error { | ||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) | ||
defer cancel() | ||
|
||
ws := intiface.NewWebsocket(wsPort, intifaceCLI) | ||
broadcaster := buttplug.NewBroadcaster() | ||
|
||
manager := device.NewManager() | ||
manager.Listen(broadcaster.Listen()) | ||
|
||
msgCh := broadcaster.Listen() | ||
|
||
// Start connecting and broadcasting messages at the same time. | ||
broadcaster.Start(ws.Open(ctx)) | ||
|
||
httpErr := make(chan error) | ||
go func() { | ||
server := newServer(ws.Websocket, manager) | ||
httpErr <- serveHTTP(ctx, httpAddr, server) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case msg := <-msgCh: | ||
switch msg := msg.(type) { | ||
case *buttplug.ServerInfo: | ||
// Server is ready. Start scanning and ask for the list of | ||
// devices. The device manager will pick up the device messages | ||
// for us. | ||
ws.Send(ctx, | ||
&buttplug.StartScanning{}, | ||
&buttplug.RequestDeviceList{}, | ||
) | ||
case *buttplug.DeviceList: | ||
for _, device := range msg.Devices { | ||
log.Printf("listed device %s (index %d)", device.DeviceName, device.DeviceIndex) | ||
} | ||
case *buttplug.DeviceAdded: | ||
log.Printf("added device %s (index %d)", msg.DeviceName, msg.DeviceIndex) | ||
case *buttplug.DeviceRemoved: | ||
log.Println("removed device", msg.DeviceIndex) | ||
case error: | ||
log.Println("buttplug error:", msg) | ||
} | ||
case err := <-httpErr: | ||
return errors.Wrap(err, "HTTP error") | ||
} | ||
} | ||
} | ||
|
||
func serveHTTP(ctx context.Context, addr string, h http.Handler) error { | ||
server := http.Server{ | ||
Addr: addr, | ||
Handler: h, | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
server.Shutdown(ctx) | ||
}() | ||
|
||
log.Print("starting HTTP server at http://", addr) | ||
|
||
if err := server.ListenAndServe(); err != nil { | ||
if errors.Is(err, http.ErrServerClosed) { | ||
return nil | ||
} | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.