answer to the right command

This commit is contained in:
Blallo 2018-12-24 15:54:22 +01:00
parent 9ef425d827
commit 8865335515
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F
3 changed files with 16 additions and 13 deletions

View file

@ -26,8 +26,9 @@ func togglePause(hub circolog.Hub, verbose bool) http.HandlerFunc {
if verbose { if verbose {
log.Printf("[%s] %s - toggled pause", r.Method, r.RemoteAddr) log.Printf("[%s] %s - toggled pause", r.Method, r.RemoteAddr)
} }
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle} response := make(chan circolog.CommandResponse)
resp := <-hub.Responses hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle, Response: response}
resp := <-response
active := resp.Value.(bool) active := resp.Value.(bool)
w.Header().Set("content-type", "application/json") w.Header().Set("content-type", "application/json")
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
@ -40,8 +41,9 @@ func clearQueue(hub circolog.Hub, verbose bool) http.HandlerFunc {
if verbose { if verbose {
log.Printf("[%s] %s - cleared queue", r.Method, r.RemoteAddr) log.Printf("[%s] %s - cleared queue", r.Method, r.RemoteAddr)
} }
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear} response := make(chan circolog.CommandResponse)
resp := <-hub.Responses hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear, Response: response}
resp := <-response
success := resp.Value.(bool) success := resp.Value.(bool)
w.Header().Set("content-type", "application/json") w.Header().Set("content-type", "application/json")
enc := json.NewEncoder(w) enc := json.NewEncoder(w)

View file

@ -108,8 +108,9 @@ func main() {
select { select {
case sig := <-interrupt: case sig := <-interrupt:
if sig == syscall.SIGUSR1 { if sig == syscall.SIGUSR1 {
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle} response := make(chan circolog.CommandResponse)
resp := <-hub.Responses hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle, Response: response}
resp := <-response
if resp.Value.(bool) { if resp.Value.(bool) {
log.Println("resumed") log.Println("resumed")
} else { } else {
@ -117,8 +118,9 @@ func main() {
} }
} }
if sig == syscall.SIGUSR2 { if sig == syscall.SIGUSR2 {
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear} response := make(chan circolog.CommandResponse)
resp := <-hub.Responses hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear, Response: response}
resp := <-response
if resp.Value.(bool) { if resp.Value.(bool) {
log.Println("buffer cleaned") log.Println("buffer cleaned")
} else { } else {

9
hub.go
View file

@ -35,7 +35,8 @@ const (
// An HubFullCommand is a Command, complete with arguments // An HubFullCommand is a Command, complete with arguments
type HubFullCommand struct { type HubFullCommand struct {
Command HubCommand Command HubCommand
Response chan CommandResponse
} }
type CommandResponse struct { type CommandResponse struct {
Value interface{} Value interface{}
@ -46,7 +47,6 @@ type Hub struct {
Unregister chan Client Unregister chan Client
LogMessages chan format.LogParts LogMessages chan format.LogParts
Commands chan HubFullCommand Commands chan HubFullCommand
Responses chan CommandResponse
clients map[Client]bool clients map[Client]bool
circbuf *ring.Ring circbuf *ring.Ring
@ -59,7 +59,6 @@ func NewHub(ringBufSize int) Hub {
Unregister: make(chan Client), Unregister: make(chan Client),
LogMessages: make(chan format.LogParts), LogMessages: make(chan format.LogParts),
Commands: make(chan HubFullCommand), Commands: make(chan HubFullCommand),
Responses: make(chan CommandResponse),
circbuf: ring.New(ringBufSize), circbuf: ring.New(ringBufSize),
} }
} }
@ -125,11 +124,11 @@ func (h *Hub) Run() {
case cmd := <-h.Commands: case cmd := <-h.Commands:
if cmd.Command == CommandClear { if cmd.Command == CommandClear {
h.clear() h.clear()
h.Responses <- CommandResponse{Value: true} cmd.Response <- CommandResponse{Value: true}
} }
if cmd.Command == CommandPauseToggle { if cmd.Command == CommandPauseToggle {
active = !active active = !active
h.Responses <- CommandResponse{Value: active} cmd.Response <- CommandResponse{Value: active}
} }
} }
} }