1
1
package main
2
2
3
3
import (
4
+ "bufio"
4
5
"crypto/rsa"
5
6
"flag"
7
+ "fmt"
6
8
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/client"
7
9
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
8
10
"github.com/RoboCup-SSL/ssl-go-tools/pkg/sslconn"
9
11
"log"
10
12
"net"
13
+ "os"
14
+ "strings"
11
15
"time"
12
16
)
13
17
@@ -48,12 +52,37 @@ func main() {
48
52
c .conn = conn
49
53
50
54
c .register ()
51
- c .sendGameEvent ()
52
55
53
- c .sendAutoRefMessage ("Hello World" )
56
+ go func () {
57
+ for {
58
+ time .Sleep (1 * time .Second )
59
+ c .sendEmptyMessage ()
60
+ }
61
+ }()
54
62
63
+ reader := bufio .NewReader (os .Stdin )
55
64
for {
56
- time .Sleep (1 * time .Second )
65
+ fmt .Print ("-> " )
66
+ text , err := reader .ReadString ('\n' )
67
+ if err != nil {
68
+ log .Print ("Can not read from stdin: " , err )
69
+ for {
70
+ time .Sleep (1 * time .Second )
71
+ }
72
+ }
73
+ // convert CRLF to LF
74
+ text = strings .Replace (text , "\n " , "" , - 1 )
75
+
76
+ if strings .Compare ("ballLeftField" , text ) == 0 {
77
+ c .sendBallLeftField ()
78
+ } else if strings .Compare ("botCrashUnique" , text ) == 0 {
79
+ c .sendBotCrashUnique ()
80
+ } else {
81
+ fmt .Println ("Available commands: " )
82
+ fmt .Printf (" %-20s: %s\n " , "help" , "Show this help text" )
83
+ fmt .Printf (" %-20s: %s\n " , "ballLeftField" , "Send game event" )
84
+ fmt .Printf (" %-20s: %s\n " , "botCrashUnique" , "Send game event" )
85
+ }
57
86
}
58
87
}
59
88
@@ -96,7 +125,7 @@ func (c *Client) register() {
96
125
}
97
126
}
98
127
99
- func (c * Client ) sendGameEvent () {
128
+ func (c * Client ) sendBallLeftField () {
100
129
event := refproto.GameEvent_BallLeftFieldTouchLine {}
101
130
event .BallLeftFieldTouchLine = new (refproto.GameEvent_BallLeftField )
102
131
event .BallLeftFieldTouchLine .ByBot = new (uint32 )
@@ -109,33 +138,50 @@ func (c *Client) sendGameEvent() {
109
138
gameEvent := refproto.GameEvent {Event : & event , Type : new (refproto.GameEventType )}
110
139
* gameEvent .Type = refproto .GameEventType_BALL_LEFT_FIELD_TOUCH_LINE
111
140
request := refproto.AutoRefToController {GameEvent : & gameEvent }
112
- c .sendRequest (& request )
141
+ c .sendRequest (& request , true )
113
142
}
114
143
115
- func (c * Client ) sendAutoRefMessage (msg string ) {
116
- message := refproto.AutoRefMessage {Message : & refproto.AutoRefMessage_Custom {Custom : msg }}
117
- request := refproto.AutoRefToController {AutoRefMessage : & message }
118
- c .sendRequest (& request )
144
+ func (c * Client ) sendBotCrashUnique () {
145
+ event := refproto.GameEvent_BotCrashUnique_ {}
146
+ event .BotCrashUnique = new (refproto.GameEvent_BotCrashUnique )
147
+ event .BotCrashUnique .Violator = new (uint32 )
148
+ * event .BotCrashUnique .Violator = 2
149
+ event .BotCrashUnique .Victim = new (uint32 )
150
+ * event .BotCrashUnique .Victim = 5
151
+ event .BotCrashUnique .ByTeam = new (refproto.Team )
152
+ * event .BotCrashUnique .ByTeam = refproto .Team_BLUE
153
+ event .BotCrashUnique .Location = & refproto.Location {X : new (float32 ), Y : new (float32 )}
154
+ * event .BotCrashUnique .Location .X = 1
155
+ * event .BotCrashUnique .Location .Y = 4.5
156
+ gameEvent := refproto.GameEvent {Event : & event , Type : new (refproto.GameEventType )}
157
+ * gameEvent .Type = refproto .GameEventType_BALL_LEFT_FIELD_TOUCH_LINE
158
+ request := refproto.AutoRefToController {GameEvent : & gameEvent }
159
+ c .sendRequest (& request , true )
119
160
}
120
161
121
- func (c * Client ) sendRequest (request * refproto.AutoRefToController ) {
162
+ func (c * Client ) sendEmptyMessage () {
163
+ request := refproto.AutoRefToController {}
164
+ c .sendRequest (& request , false )
165
+ }
166
+
167
+ func (c * Client ) sendRequest (request * refproto.AutoRefToController , doLog bool ) {
122
168
if privateKey != nil {
123
169
request .Signature = & refproto.Signature {Token : & c .token , Pkcs1V15 : []byte {}}
124
170
request .Signature .Pkcs1V15 = client .Sign (privateKey , request )
125
171
}
126
172
127
- log . Print ( "Sending " , request )
173
+ logIf ( doLog , "Sending " , request )
128
174
129
175
if err := sslconn .SendMessage (c .conn , request ); err != nil {
130
176
log .Fatalf ("Failed sending request: %v (%v)" , request , err )
131
177
}
132
178
133
- log . Print ( "Waiting for reply..." )
179
+ logIf ( doLog , "Waiting for reply..." )
134
180
reply := refproto.ControllerToAutoRef {}
135
181
if err := sslconn .ReceiveMessage (c .conn , & reply ); err != nil {
136
182
log .Fatal ("Failed receiving controller reply: " , err )
137
183
}
138
- log . Print ( "Received reply: " , reply )
184
+ logIf ( doLog , "Received reply: " , reply )
139
185
if reply .GetControllerReply () == nil || reply .GetControllerReply ().StatusCode == nil || * reply .GetControllerReply ().StatusCode != refproto .ControllerReply_OK {
140
186
log .Fatal ("Message rejected: " , * reply .GetControllerReply ().Reason )
141
187
}
@@ -145,3 +191,9 @@ func (c *Client) sendRequest(request *refproto.AutoRefToController) {
145
191
c .token = ""
146
192
}
147
193
}
194
+
195
+ func logIf (doLog bool , v ... interface {}) {
196
+ if doLog {
197
+ log .Print (v )
198
+ }
199
+ }
0 commit comments