diff options
author | Christian Pointner <equinox@spreadspace.org> | 2019-02-14 21:11:51 +0100 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2019-02-14 21:11:51 +0100 |
commit | bd12fc026dd7782244357e5bfe842f8ed9822702 (patch) | |
tree | 253925e8353a4ac2f615f24af2471f4641d1dc16 /pkg | |
parent | minor refactoring (diff) |
controller error handling
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 |