command responses

This commit is contained in:
boyska 2018-11-11 19:10:53 +01:00
parent 3f216f12f8
commit 7c17c971a0
3 changed files with 16 additions and 2 deletions

View file

@ -13,6 +13,7 @@ import (
"gopkg.in/mcuadros/go-syslog.v2/format"
)
// TODO: return a server
func setupHTTP(hub circolog.Hub) {
http.HandleFunc("/", getHTTPHandler(hub))
http.HandleFunc("/ws", getWSHandler(hub))

View file

@ -89,12 +89,18 @@ func main() {
select {
case sig := <-interrupt:
if sig == syscall.SIGHUP {
log.Println("Clearing queue")
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear}
<-hub.Responses
log.Println("Queue cleared")
}
if sig == syscall.SIGUSR1 {
log.Println("Pause/resume")
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle}
resp := <-hub.Responses
if resp.Value.(bool) {
log.Println("resumed")
} else {
log.Println("paused")
}
}
if sig == syscall.SIGTERM || sig == syscall.SIGINT {
log.Println("Quitting because of signal", sig)

7
hub.go
View file

@ -37,12 +37,16 @@ const (
type HubFullCommand struct {
Command HubCommand
}
type CommandResponse struct {
Value interface{}
}
type Hub struct {
Register chan Client
Unregister chan Client
LogMessages chan format.LogParts
Commands chan HubFullCommand
Responses chan CommandResponse
clients map[Client]bool
circbuf *ring.Ring
@ -55,6 +59,7 @@ 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),
}
}
@ -120,9 +125,11 @@ func (h *Hub) Run() {
case cmd := <-h.Commands:
if cmd.Command == CommandClear {
h.clear()
h.Responses <- CommandResponse{Value: true}
}
if cmd.Command == CommandPauseToggle {
active = !active
h.Responses <- CommandResponse{Value: active}
}
}
}