Skip to content

Commit

Permalink
Robot is now a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Apr 15, 2014
1 parent 48b2eed commit afa6cf1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Api(bot *Master) {
m.Get("/robots", func() string {
jsonRobots := make([]*jsonRobot, 0)
for _, robot := range bot.Robots {
jsonRobots = append(jsonRobots, a.formatJsonRobot(&robot))
jsonRobots = append(jsonRobots, a.formatJsonRobot(robot))
}
return toJson(jsonRobots)
})
Expand Down
18 changes: 9 additions & 9 deletions master.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Master struct {
Robots []Robot
Robots []*Robot
NumCPU int
}

Expand All @@ -17,15 +17,19 @@ func GobotMaster() *Master {
return m
}

var startRobots = func(m *Master) {
var trap = func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
}

func (m *Master) Start() {
runtime.GOMAXPROCS(m.NumCPU)

for s := range m.Robots {
m.Robots[s].startRobot()
}

c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
var c = make(chan os.Signal, 1)
trap(c)

for _ = range c {
for r := range m.Robots {
Expand All @@ -36,14 +40,10 @@ var startRobots = func(m *Master) {
}
}

func (m *Master) Start() {
startRobots(m)
}

func (m *Master) FindRobot(name string) *Robot {
for _, robot := range m.Robots {
if robot.Name == name {
return &robot
return robot
}
}
return nil
Expand Down
10 changes: 3 additions & 7 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ type Robot struct {
devices []*device `json:"-"`
}

var start = func(r *Robot) {
func (r *Robot) Start() {
m := GobotMaster()
m.Robots = []Robot{*r}
m.Robots = []*Robot{r}
m.Start()
}

func (r *Robot) Start() {
start(r)
}

func (r *Robot) startRobot() {
r.initName()
r.initCommands()
Expand All @@ -50,7 +46,7 @@ func (r *Robot) initName() {
if r.Name == "" {
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Int()
r.Name = fmt.Sprintf("Robot %v", i)
r.Name = fmt.Sprintf("Robot%v", i)
}
}

Expand Down

0 comments on commit afa6cf1

Please sign in to comment.