Repository: incubator-mynewt-newtmgr
Updated Branches:
  refs/heads/master f573ad10e -> a5c9d0b3f


nmxact - ble_loop example: graceful shutdown


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/commit/a5c9d0b3
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/a5c9d0b3
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/a5c9d0b3

Branch: refs/heads/master
Commit: a5c9d0b3f9ac7df16492e5dd39a3b9aac13a72a6
Parents: f573ad1
Author: Christopher Collins <ccoll...@apache.org>
Authored: Fri Apr 7 14:08:15 2017 -0700
Committer: Christopher Collins <ccoll...@apache.org>
Committed: Fri Apr 7 14:08:15 2017 -0700

----------------------------------------------------------------------
 nmxact/example/ble_loop/ble_loop.go | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/a5c9d0b3/nmxact/example/ble_loop/ble_loop.go
----------------------------------------------------------------------
diff --git a/nmxact/example/ble_loop/ble_loop.go 
b/nmxact/example/ble_loop/ble_loop.go
index c76b11c..7c6140a 100644
--- a/nmxact/example/ble_loop/ble_loop.go
+++ b/nmxact/example/ble_loop/ble_loop.go
@@ -22,13 +22,40 @@ package main
 import (
        "fmt"
        "os"
+       "os/signal"
+       "syscall"
 
        "mynewt.apache.org/newtmgr/nmxact/bledefs"
        "mynewt.apache.org/newtmgr/nmxact/nmble"
        "mynewt.apache.org/newtmgr/nmxact/sesn"
        "mynewt.apache.org/newtmgr/nmxact/xact"
+       "mynewt.apache.org/newtmgr/nmxact/xport"
 )
 
+func configExitHandler(x xport.Xport, s sesn.Sesn) {
+       onExit := func() {
+               if s.IsOpen() {
+                       s.Close()
+               }
+
+               x.Stop()
+       }
+
+       sigChan := make(chan os.Signal, 1)
+       signal.Notify(sigChan)
+
+       go func() {
+               for {
+                       s := <-sigChan
+                       switch s {
+                       case os.Interrupt, syscall.SIGTERM:
+                               onExit()
+                               os.Exit(0)
+                       }
+               }
+       }()
+}
+
 func main() {
        // Initialize the BLE transport.
        params := nmble.NewXportCfg()
@@ -66,6 +93,8 @@ func main() {
                os.Exit(1)
        }
 
+       configExitHandler(x, s)
+
        // Repeatedly:
        //     * Connect to peer if unconnected.
        //     * Send an echo command to peer.

Reply via email to