Skip to content

Commit

Permalink
Add Finalize on SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 31, 2013
1 parent 0512cc7 commit bdd9b88
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
18 changes: 16 additions & 2 deletions master.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gobot

import "runtime"
import (
"os"
"os/signal"
"runtime"
)

type Master struct {
Robots []Robot
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit bdd9b88

Please sign in to comment.