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

View file

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

9
hub.go
View file

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