Add Finalize on SIGINT

This commit is contained in:
Adrian Zankich 2013-12-30 20:38:14 -08:00
parent 0512cc756d
commit bdd9b88327
3 changed files with 25 additions and 2 deletions

View File

@ -39,14 +39,17 @@ func (c *connection) Connect() bool {
}
func (c *connection) Disconnect() bool {
fmt.Println("Disconnecting " + c.Name + "...")
return c.Adaptor.Disconnect()
}
func (c *connection) Finalize() bool {
fmt.Println("Finalizing " + c.Name + "...")
return c.Adaptor.Finalize()
}
func (c *connection) Reconnect() bool {
fmt.Println("Reconnecting to " + c.Name + " on port " + c.Port + "...")
return c.Adaptor.Reconnect()
}

View File

@ -1,6 +1,10 @@
package gobot
import "runtime"
import (
"os"
"os/signal"
"runtime"
)
type Master struct {
Robots []Robot
@ -15,10 +19,20 @@ func GobotMaster() *Master {
func (m *Master) Start() {
runtime.GOMAXPROCS(m.NumCPU)
for s := range m.Robots {
go m.Robots[s].startRobot()
}
select {}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
for _ = range c {
for r := range m.Robots {
m.Robots[r].finalizeConnections()
}
break
}
}
func (m *Master) FindRobot(name string) *Robot {

View File

@ -97,6 +97,12 @@ func (r *Robot) startDevices() bool {
return success
}
func (r *Robot) finalizeConnections() {
for i := range r.connections {
r.connections[i].Finalize()
}
}
func (r *Robot) GetDevices() []*device {
return r.devices
}