Skip to content

Commit 7f559f0

Browse files
committed
[refactoring] Cleanup publisher code, make it optional
1 parent 394384a commit 7f559f0

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

internal/app/controller/publisher.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ import (
1010

1111
const maxDatagramSize = 8192
1212

13-
type RefBoxPublisher struct {
14-
Conn *net.UDPConn
15-
Message sslproto.SSL_Referee
13+
type Publisher struct {
14+
conn *net.UDPConn
15+
message sslproto.SSL_Referee
1616
}
1717

18-
// NewBroadcaster creates a new UDP multicast connection on which to broadcast
19-
func NewRefBoxPublisher(address string) RefBoxPublisher {
18+
// Create a new publisher that publishes referee messages via UDP to the teams
19+
func NewPublisher(address string) (publisher Publisher, err error) {
2020
addr, err := net.ResolveUDPAddr("udp", address)
2121
if err != nil {
22-
log.Fatalln(err)
22+
return
2323
}
2424

2525
conn, err := net.DialUDP("udp", nil, addr)
2626
if err != nil {
27-
log.Fatalln(err)
27+
return
2828
}
29+
2930
conn.SetReadBuffer(maxDatagramSize)
3031
log.Println("Connected to", address)
3132

32-
publisher := RefBoxPublisher{}
33-
publisher.Conn = conn
33+
publisher.conn = conn
3434

35-
initRefereeMessage(&publisher.Message)
35+
initRefereeMessage(&publisher.message)
3636

37-
return publisher
37+
return
3838
}
3939

4040
func initRefereeMessage(m *sslproto.SSL_Referee) {
@@ -61,15 +61,20 @@ func initTeamInfo(t *sslproto.SSL_Referee_TeamInfo) {
6161
t.Goalie = new(uint32)
6262
}
6363

64-
func (p *RefBoxPublisher) Publish(state *RefBoxState, command *RefBoxEventCommand) {
64+
// Publish the state and command
65+
func (p *Publisher) Publish(state *RefBoxState, command *RefBoxEventCommand) {
6566

66-
updateMessage(&p.Message, state, command)
67-
bytes, err := proto.Marshal(&p.Message)
67+
if p.conn == nil {
68+
return
69+
}
70+
71+
updateMessage(&p.message, state, command)
72+
bytes, err := proto.Marshal(&p.message)
6873
if err != nil {
6974
log.Printf("Could not marshal referee message: %v\nError: %v", state, err)
7075
return
7176
}
72-
_, err = p.Conn.Write(bytes)
77+
_, err = p.conn.Write(bytes)
7378
if err != nil {
7479
log.Printf("Could not write message: %v", err)
7580
}
@@ -90,6 +95,7 @@ func updateMessage(r *sslproto.SSL_Referee, state *RefBoxState, command *RefBoxE
9095
*r.CommandTimestamp = uint64(time.Now().UnixNano() / 1000)
9196
}
9297
}
98+
9399
func mapCommand(c *RefBoxEventCommand) sslproto.SSL_Referee_Command {
94100
switch c.Type {
95101
case CommandHalt:

internal/app/controller/refBox.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type RefBox struct {
2525
stateHistoryFile *os.File
2626
lastStateFile *os.File
2727
StageTimes map[RefBoxStage]time.Duration
28-
Publisher RefBoxPublisher
28+
Publisher Publisher
2929
}
3030

3131
func NewRefBox() (refBox *RefBox) {
@@ -36,11 +36,19 @@ func NewRefBox() (refBox *RefBox) {
3636
refBox.notifyUpdateState = make(chan struct{})
3737
refBox.MatchTimeStart = time.Unix(0, 0)
3838
refBox.State = NewRefBoxState(refBox.Config)
39-
refBox.Publisher = NewRefBoxPublisher(refBox.Config.Publish.Address)
39+
refBox.Publisher = loadPublisher(refBox.Config)
4040

4141
return
4242
}
4343

44+
func loadPublisher(config Config) Publisher {
45+
publisher, err := NewPublisher(config.Publish.Address)
46+
if err != nil {
47+
log.Printf("Could not start publisher on %v. %v", config.Publish.Address, err)
48+
}
49+
return publisher
50+
}
51+
4452
func loadConfig() Config {
4553
config, err := LoadConfig(configFileName)
4654
if err != nil {

0 commit comments

Comments
 (0)