| 1 | // Copyright 2013 The GoMPD Authors. All rights reserved. |
| 2 | // Use of this source code is governed by the MIT |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package mpd |
| 6 | |
| 7 | // Watcher represents a MPD client connection that can be watched for events. |
| 8 | type Watcher struct { |
| 9 | conn *Client // client connection to MPD |
| 10 | exit chan bool // channel used to ask loop to terminate |
| 11 | done chan bool // channel indicating loop has terminated |
| 12 | names chan []string // channel to set new subsystems to watch |
| 13 | Event chan string // event channel |
| 14 | Error chan error // error channel |
| 15 | } |
| 16 | |
| 17 | // NewWatcher connects to MPD server and watches for changes in subsystems |
| 18 | // names. If no subsystem is specified, all changes are reported. |
| 19 | // |
| 20 | // See http://www.musicpd.org/doc/protocol/command_reference.html#command_idle |
| 21 | // for valid subsystem names. |
| 22 | func NewWatcher(net, addr, passwd string, names ...string) (w *Watcher, err error) { |
| 23 | conn, err := DialAuthenticated(net, addr, passwd) |
| 24 | if err != nil { |
| 25 | return |
| 26 | } |
| 27 | w = &Watcher{ |
| 28 | conn: conn, |
| 29 | Event: make(chan string), |
| 30 | Error: make(chan error), |
| 31 | done: make(chan bool), |
| 32 | // Buffer channels to avoid race conditions with noIdle |
| 33 | names: make(chan []string, 1), |
| 34 | exit: make(chan bool, 1), |
| 35 | } |
| 36 | go w.watch(names...) |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | func (w *Watcher) watch(names ...string) { |
| 41 | defer w.closeChans() |
| 42 | |
| 43 | // We can block in two places: idle and sending on Event/Error channels. |
| 44 | // We need to check w.exit and w.names after each. |
| 45 | for { |
| 46 | changed, err := w.conn.idle(names...) |
| 47 | select { |
| 48 | case <-w.exit: |
| 49 | // If Close interrupted idle with a noidle, and we don't |
| 50 | // exit now, we will block trying to send on Event/Error. |
| 51 | return |
| 52 | case names = <-w.names: |
| 53 | // Received new subsystems to watch. Ignore results. |
| 54 | changed = []string{} |
| 55 | err = nil |
| 56 | default: // continue |
| 57 | } |
| 58 | |
| 59 | switch { |
| 60 | case err != nil: |
| 61 | w.Error <- err |
| 62 | default: |
| 63 | for _, name := range changed { |
| 64 | w.Event <- name |
| 65 | } |
| 66 | } |
| 67 | select { |
| 68 | case <-w.exit: |
| 69 | // If Close unblocks us from sending on Event/Error channels, |
| 70 | // we should exit now because noidle might be sent out |
| 71 | // before we get to idle. |
| 72 | return |
| 73 | case names = <-w.names: |
| 74 | // If method Subsystems unblocks us from sending on Event/Error |
| 75 | // channels, the next call to idle should be on the new names. |
| 76 | default: // continue |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func (w *Watcher) closeChans() { |
| 82 | close(w.Event) |
| 83 | close(w.Error) |
| 84 | close(w.names) |
| 85 | close(w.exit) |
| 86 | close(w.done) |
| 87 | } |
| 88 | |
| 89 | func (w *Watcher) consume() { |
| 90 | for { |
| 91 | select { |
| 92 | case <-w.Event: |
| 93 | case <-w.Error: |
| 94 | default: |
| 95 | return |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Subsystems interrupts watching current subsystems, consumes all |
| 101 | // outstanding values from Event and Error channels, and then |
| 102 | // changes the subsystems to watch for to names. |
| 103 | func (w *Watcher) Subsystems(names ...string) { |
| 104 | w.names <- names |
| 105 | w.consume() |
| 106 | w.conn.noIdle() |
| 107 | } |
| 108 | |
| 109 | // Close closes Event and Error channels, and the connection to MPD server. |
| 110 | func (w *Watcher) Close() error { |
| 111 | w.exit <- true |
| 112 | w.consume() |
| 113 | w.conn.noIdle() |
| 114 | |
| 115 | <-w.done // wait for idle to finish and channels to close |
| 116 | // At this point, watch goroutine has ended, |
| 117 | // so it's safe to close connection. |
| 118 | return w.conn.Close() |
| 119 | } |