diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/controller/controller.go | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index fb5f5ca..2aa8023 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -26,7 +26,7 @@ import ( "container/list" "errors" "fmt" - "log" + // "log" "strings" "sync" @@ -125,15 +125,14 @@ func (c *Controller) publishEvent(ev Event) { select { case <-sub.unsubscribe: - log.Printf("controller: removing subscriber '%v', because it has unsubscribed", sub.publish) + // log.Printf("controller: removing subscriber '%v', because it has unsubscribed", sub.publish) close(sub.publish) c.subscribers.Remove(entry) default: select { case sub.publish <- ev: default: - // subscriber is not responding... - log.Printf("controller: removing subscriber '%v', because it is not responding", sub.publish) + // log.Printf("controller: removing subscriber '%v', because it is not responding", sub.publish) close(sub.publish) c.subscribers.Remove(entry) } @@ -163,8 +162,11 @@ func (c *Controller) Init() error { go func() { for { - // TODO: handle Errors (reopen the device!) - c.handleMidiPacket(<-ch) + p := <-ch + if p.Err != nil { + c.Shutdown() + } + c.handleMidiPacket(p) } }() return nil @@ -173,9 +175,20 @@ func (c *Controller) Init() error { func (c *Controller) Shutdown() error { if c.Dev != nil { c.Dev.Close() + c.Dev = nil + } + + c.subscribersLock.Lock() + defer c.subscribersLock.Unlock() + + for entry := c.subscribers.Front(); entry != nil; entry = entry.Next() { + sub, ok := (entry.Value).(subscriber) + if !ok { + panic(fmt.Sprintf("controller: subscriber list element value has wrong type: %T", entry.Value)) + } + close(sub.publish) } - // TODO: also close all subscribed channels - // terminate go-routine started by Init() + c.subscribers.Init() return nil } @@ -218,7 +231,7 @@ func (c *Controller) Subscribe(out chan<- Event) chan<- struct{} { c.subscribersLock.Lock() defer c.subscribersLock.Unlock() - log.Printf("controller: subscribing '%v' to events", out) + // log.Printf("controller: subscribing '%v' to events", out) unsubscribe := make(chan struct{}) c.subscribers.PushBack(subscriber{publish: out, unsubscribe: unsubscribe}) return unsubscribe |