Skip to content

Commit

Permalink
[feature] Increase the update rate of multicast network packets
Browse files Browse the repository at this point in the history
  • Loading branch information
g3force committed Aug 25, 2018
1 parent 433c8a7 commit d9f3d54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
30 changes: 20 additions & 10 deletions internal/app/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"github.com/RoboCup-SSL/ssl-game-controller/pkg/timer"
"log"
"time"
)

const configFileName = "config/ssl-game-controller.yaml"
Expand Down Expand Up @@ -52,15 +53,25 @@ func (c *GameController) Run() {
c.ApiServer.PublishState(*c.Engine.State)
c.ApiServer.PublishRefereeEvents(c.Engine.RefereeEvents)

go func() {
defer c.historyPreserver.Close()
for {
c.timer.WaitTillNextFullSecond()
c.Engine.Tick(c.timer.Delta())
c.historyPreserver.Save(c.Engine.History)
c.publish()
}
}()
go c.publishApi()
go c.publishNetwork()
}

func (c *GameController) publishApi() {
defer c.historyPreserver.Close()
for {
c.timer.WaitTillNextFullSecond()
c.Engine.Tick(c.timer.Delta())
c.historyPreserver.Save(c.Engine.History)
c.publish()
}
}

func (c *GameController) publishNetwork() {
for {
c.timer.WaitTillNextFull(100 * time.Millisecond)
c.Publisher.Publish(c.Engine.State)
}
}

func (c *GameController) OnNewEvent(event Event) {
Expand Down Expand Up @@ -97,5 +108,4 @@ func (c *GameController) publish() {
}

c.ApiServer.PublishState(*c.Engine.State)
c.Publisher.Publish(c.Engine.State)
}
7 changes: 7 additions & 0 deletions pkg/timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ func (t *Timer) WaitTillNextFullSecond() {
nextDuration := elapsed.Truncate(time.Second) + time.Second
t.WaitTill(nextDuration)
}

// WaitTillNextFull waits until the internal timer has reached the next full given duration
func (t *Timer) WaitTillNextFull(d time.Duration) {
elapsed := t.Elapsed()
nextDuration := elapsed.Truncate(d) + d
t.WaitTill(nextDuration)
}

0 comments on commit d9f3d54

Please sign in to comment.