|
@@ -24,10 +24,25 @@ type ClientOptions struct {
|
|
|
// The channel "register" and "unregister" can be seen as "command"
|
|
|
// keep in mind that "registering" is what you do also to get messages in a one-time fashion. In fact, Client
|
|
|
// has "options", such as Nofollow, to explain the Hub what should be given
|
|
|
+
|
|
|
+// An HubCommand is an "enum" of different commands
|
|
|
+type HubCommand int
|
|
|
+
|
|
|
+const (
|
|
|
+ CommandClear = iota
|
|
|
+ CommandPauseToggle = iota
|
|
|
+)
|
|
|
+
|
|
|
+// An HubFullCommand is a Command, complete with arguments
|
|
|
+type HubFullCommand struct {
|
|
|
+ Command HubCommand
|
|
|
+}
|
|
|
+
|
|
|
type Hub struct {
|
|
|
Register chan Client
|
|
|
Unregister chan Client
|
|
|
LogMessages chan format.LogParts
|
|
|
+ Commands chan HubFullCommand
|
|
|
|
|
|
clients map[Client]bool
|
|
|
circbuf *ring.Ring
|
|
@@ -39,6 +54,7 @@ func NewHub(ringBufSize int) Hub {
|
|
|
Register: make(chan Client),
|
|
|
Unregister: make(chan Client),
|
|
|
LogMessages: make(chan format.LogParts),
|
|
|
+ Commands: make(chan HubFullCommand),
|
|
|
circbuf: ring.New(ringBufSize),
|
|
|
}
|
|
|
}
|
|
@@ -77,6 +93,7 @@ func (h *Hub) register(cl Client) {
|
|
|
|
|
|
// Run is hub main loop; keeps everything going
|
|
|
func (h *Hub) Run() {
|
|
|
+ active := true
|
|
|
for {
|
|
|
select {
|
|
|
case cl := <-h.Register:
|
|
@@ -88,22 +105,31 @@ func (h *Hub) Run() {
|
|
|
delete(h.clients, cl)
|
|
|
}
|
|
|
case msg := <-h.LogMessages:
|
|
|
- h.circbuf.Value = msg
|
|
|
- h.circbuf = h.circbuf.Next()
|
|
|
- for client := range h.clients {
|
|
|
- select { // send without blocking
|
|
|
- case client.Messages <- msg:
|
|
|
- break
|
|
|
- default:
|
|
|
- break
|
|
|
+ if active == true {
|
|
|
+ h.circbuf.Value = msg
|
|
|
+ h.circbuf = h.circbuf.Next()
|
|
|
+ for client := range h.clients {
|
|
|
+ select { // send without blocking
|
|
|
+ case client.Messages <- msg:
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ case cmd := <-h.Commands:
|
|
|
+ if cmd.Command == CommandClear {
|
|
|
+ h.clear()
|
|
|
+ }
|
|
|
+ if cmd.Command == CommandPauseToggle {
|
|
|
+ active = !active
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Clear removes every all elements from the buffer
|
|
|
-func (h *Hub) Clear() {
|
|
|
+func (h *Hub) clear() {
|
|
|
buf := h.circbuf
|
|
|
for i := 0; i < buf.Len(); i++ {
|
|
|
buf.Value = nil
|