-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
218 lines (195 loc) · 7.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"bufio"
"crypto/rsa"
"flag"
"fmt"
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/client"
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
"github.com/RoboCup-SSL/ssl-go-tools/pkg/sslconn"
"log"
"net"
"os"
"strings"
"time"
)
var udpAddress = flag.String("udpAddress", "224.5.23.1:10003", "The multicast address of ssl-game-controller")
var autoDetectAddress = flag.Bool("autoDetectHost", true, "Automatically detect the game-controller host and replace it with the host given in address")
var refBoxAddr = flag.String("address", "localhost:10007", "Address to connect to")
var privateKeyLocation = flag.String("privateKey", "", "A private key to be used to sign messages")
var clientIdentifier = flag.String("identifier", "test", "The identifier of the client")
var privateKey *rsa.PrivateKey
type Client struct {
conn net.Conn
token string
}
func main() {
flag.Parse()
privateKey = client.LoadPrivateKey(*privateKeyLocation)
if *autoDetectAddress {
log.Print("Trying to detect host based on incoming referee messages...")
host := client.DetectHost(*udpAddress)
if host != "" {
log.Print("Detected game-controller host: ", host)
*refBoxAddr = client.GetConnectionString(*refBoxAddr, host)
}
}
conn, err := net.Dial("tcp", *refBoxAddr)
if err != nil {
log.Fatal("could not connect to game-controller at ", *refBoxAddr)
}
defer conn.Close()
log.Printf("Connected to game-controller at %v", *refBoxAddr)
c := Client{}
c.conn = conn
c.register()
go func() {
for {
time.Sleep(1 * time.Second)
c.sendEmptyMessage()
}
}()
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("-> ")
text, err := reader.ReadString('\n')
if err != nil {
log.Print("Can not read from stdin: ", err)
for {
time.Sleep(1 * time.Second)
}
}
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)
if strings.Compare("ballLeftField", text) == 0 {
c.sendBallLeftField()
} else if strings.Compare("botCrashUnique", text) == 0 {
c.sendBotCrashUnique()
} else if strings.Compare("doubleTouch", text) == 0 {
c.sendDoubleTouch()
} else {
fmt.Println("Available commands: ")
fmt.Printf(" %-20s: %s\n", "help", "Show this help text")
fmt.Printf(" %-20s: %s\n", "ballLeftField", "Send game event")
fmt.Printf(" %-20s: %s\n", "botCrashUnique", "Send game event")
fmt.Printf(" %-20s: %s\n", "doubleTouch", "Send game event")
}
}
}
func (c *Client) register() {
reply := refproto.ControllerToAutoRef{}
if err := sslconn.ReceiveMessage(c.conn, &reply); err != nil {
log.Fatal("Failed receiving controller reply: ", err)
}
if reply.GetControllerReply() == nil || reply.GetControllerReply().NextToken == nil {
log.Fatal("Missing next token")
}
registration := refproto.AutoRefRegistration{}
registration.Identifier = clientIdentifier
if privateKey != nil {
registration.Signature = &refproto.Signature{Token: reply.GetControllerReply().NextToken, Pkcs1V15: []byte{}}
registration.Signature.Pkcs1V15 = client.Sign(privateKey, ®istration)
}
log.Print("Sending registration")
if err := sslconn.SendMessage(c.conn, ®istration); err != nil {
log.Fatal("Failed sending registration: ", err)
}
log.Print("Sent registration, waiting for reply")
reply = refproto.ControllerToAutoRef{}
if err := sslconn.ReceiveMessage(c.conn, &reply); err != nil {
log.Fatal("Failed receiving controller reply: ", err)
}
if reply.GetControllerReply().StatusCode == nil || *reply.GetControllerReply().StatusCode != refproto.ControllerReply_OK {
reason := ""
if reply.GetControllerReply().Reason != nil {
reason = *reply.GetControllerReply().Reason
}
log.Fatal("Registration rejected: ", reason)
}
log.Printf("Successfully registered as %v", *clientIdentifier)
if reply.GetControllerReply().NextToken != nil {
c.token = *reply.GetControllerReply().NextToken
} else {
c.token = ""
}
}
func (c *Client) sendBallLeftField() {
event := refproto.GameEvent_BallLeftFieldTouchLine{}
event.BallLeftFieldTouchLine = new(refproto.GameEvent_BallLeftField)
event.BallLeftFieldTouchLine.ByBot = new(uint32)
*event.BallLeftFieldTouchLine.ByBot = 2
event.BallLeftFieldTouchLine.ByTeam = new(refproto.Team)
*event.BallLeftFieldTouchLine.ByTeam = refproto.Team_BLUE
event.BallLeftFieldTouchLine.Location = &refproto.Location{X: new(float32), Y: new(float32)}
*event.BallLeftFieldTouchLine.Location.X = 1
*event.BallLeftFieldTouchLine.Location.Y = 4.5
gameEvent := refproto.GameEvent{Event: &event, Type: new(refproto.GameEventType)}
*gameEvent.Type = refproto.GameEventType_BALL_LEFT_FIELD_TOUCH_LINE
request := refproto.AutoRefToController{GameEvent: &gameEvent}
c.sendRequest(&request, true)
}
func (c *Client) sendDoubleTouch() {
event := refproto.GameEvent_AttackerDoubleTouchedBall_{}
event.AttackerDoubleTouchedBall = new(refproto.GameEvent_AttackerDoubleTouchedBall)
event.AttackerDoubleTouchedBall.ByBot = new(uint32)
*event.AttackerDoubleTouchedBall.ByBot = 2
event.AttackerDoubleTouchedBall.ByTeam = new(refproto.Team)
*event.AttackerDoubleTouchedBall.ByTeam = refproto.Team_BLUE
event.AttackerDoubleTouchedBall.Location = &refproto.Location{X: new(float32), Y: new(float32)}
*event.AttackerDoubleTouchedBall.Location.X = 1
*event.AttackerDoubleTouchedBall.Location.Y = 4.5
gameEvent := refproto.GameEvent{Event: &event, Type: new(refproto.GameEventType)}
*gameEvent.Type = refproto.GameEventType_ATTACKER_DOUBLE_TOUCHED_BALL
request := refproto.AutoRefToController{GameEvent: &gameEvent}
c.sendRequest(&request, true)
}
func (c *Client) sendBotCrashUnique() {
event := refproto.GameEvent_BotCrashUnique_{}
event.BotCrashUnique = new(refproto.GameEvent_BotCrashUnique)
event.BotCrashUnique.Violator = new(uint32)
*event.BotCrashUnique.Violator = 2
event.BotCrashUnique.Victim = new(uint32)
*event.BotCrashUnique.Victim = 5
event.BotCrashUnique.ByTeam = new(refproto.Team)
*event.BotCrashUnique.ByTeam = refproto.Team_BLUE
event.BotCrashUnique.Location = &refproto.Location{X: new(float32), Y: new(float32)}
*event.BotCrashUnique.Location.X = 1
*event.BotCrashUnique.Location.Y = 4.5
gameEvent := refproto.GameEvent{Event: &event, Type: new(refproto.GameEventType)}
*gameEvent.Type = refproto.GameEventType_BOT_CRASH_UNIQUE
request := refproto.AutoRefToController{GameEvent: &gameEvent}
c.sendRequest(&request, true)
}
func (c *Client) sendEmptyMessage() {
request := refproto.AutoRefToController{}
c.sendRequest(&request, false)
}
func (c *Client) sendRequest(request *refproto.AutoRefToController, doLog bool) {
if privateKey != nil {
request.Signature = &refproto.Signature{Token: &c.token, Pkcs1V15: []byte{}}
request.Signature.Pkcs1V15 = client.Sign(privateKey, request)
}
logIf(doLog, "Sending ", request)
if err := sslconn.SendMessage(c.conn, request); err != nil {
log.Fatalf("Failed sending request: %v (%v)", request, err)
}
logIf(doLog, "Waiting for reply...")
reply := refproto.ControllerToAutoRef{}
if err := sslconn.ReceiveMessage(c.conn, &reply); err != nil {
log.Fatal("Failed receiving controller reply: ", err)
}
logIf(doLog, "Received reply: ", reply)
if reply.GetControllerReply() == nil || reply.GetControllerReply().StatusCode == nil || *reply.GetControllerReply().StatusCode != refproto.ControllerReply_OK {
log.Fatal("Message rejected: ", *reply.GetControllerReply().Reason)
}
if reply.GetControllerReply().NextToken != nil {
c.token = *reply.GetControllerReply().NextToken
} else {
c.token = ""
}
}
func logIf(doLog bool, v ...interface{}) {
if doLog {
log.Print(v...)
}
}