11package api
22
33import (
4- "bytes"
54 "github.com/RoboCup-SSL/ssl-game-controller/internal/app/engine"
65 "github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
76 "github.com/RoboCup-SSL/ssl-game-controller/internal/app/statemachine"
8- "github.com/golang/protobuf/jsonpb"
9- "github.com/golang/protobuf/proto"
10- "github.com/golang/protobuf/ptypes"
7+ "github.com/google/uuid"
118 "github.com/gorilla/websocket"
12- "github.com/odeke-em/go-uuid"
9+ "google.golang.org/protobuf/encoding/protojson"
10+ "google.golang.org/protobuf/proto"
11+ "google.golang.org/protobuf/types/known/durationpb"
1312 "log"
1413 "net/http"
1514 "reflect"
@@ -29,7 +28,7 @@ type ServerConnection struct {
2928 lastGcState * engine.GcState
3029 lastProtocolId int32
3130 lastConfig * engine.Config
32- marshaler jsonpb. Marshaler
31+ marshaler protojson. MarshalOptions
3332}
3433
3534func NewServer (gcEngine * engine.Engine ) (s * Server ) {
@@ -45,7 +44,7 @@ func NewServerConnection(gcEngine *engine.Engine, conn *websocket.Conn) (s *Serv
4544 s .conn = conn
4645 s .gcEngine = gcEngine
4746 s .lastProtocolId = - 1
48- s .marshaler .EmitDefaults = true
47+ s .marshaler .EmitUnpopulated = true
4948 return
5049}
5150
@@ -73,7 +72,7 @@ func (a *Server) WsHandler(w http.ResponseWriter, r *http.Request) {
7372
7473func (s * ServerConnection ) publish () {
7574 hook := make (chan engine.HookOut , 10 )
76- hookId := "apiServer-" + uuid .New ()
75+ hookId := "apiServer-" + uuid .NewString ()
7776 s .gcEngine .RegisterHook (hookId , hook )
7877 defer func () {
7978 s .gcEngine .UnregisterHook (hookId )
@@ -175,28 +174,28 @@ func (s *ServerConnection) changesToProtocolEntries(changes []*statemachine.Stat
175174 for i , change := range changes {
176175 var matchTimeElapsed time.Duration
177176 if change .State .MatchTimeStart .Seconds != 0 {
178- tChange , _ := ptypes . Timestamp ( change .Timestamp )
179- tStart , _ := ptypes . Timestamp ( change .State .MatchTimeStart )
177+ tChange := change .Timestamp . AsTime ( )
178+ tStart := change .State .MatchTimeStart . AsTime ( )
180179 matchTimeElapsed = tChange .Sub (tStart )
181180 }
182181
183182 entries [len (changes )- 1 - i ] = & ProtocolEntry {
184183 Id : change .Id ,
185184 Change : change .Change ,
186- MatchTimeElapsed : ptypes . DurationProto (matchTimeElapsed ),
185+ MatchTimeElapsed : durationpb . New (matchTimeElapsed ),
187186 StageTimeElapsed : change .State .StageTimeElapsed ,
188187 }
189188 }
190189 return entries
191190}
192191
193192func (s * ServerConnection ) publishOutput (wrapper * Output ) {
194- b , err := s .marshaler .MarshalToString (wrapper )
193+ b , err := s .marshaler .Marshal (wrapper )
195194 if err != nil {
196195 log .Println ("Marshal error:" , err )
197196 }
198197
199- err = s .conn .WriteMessage (websocket .TextMessage , [] byte ( b ) )
198+ err = s .conn .WriteMessage (websocket .TextMessage , b )
200199 if err != nil {
201200 log .Println ("Could not write message to api client:" , err )
202201 }
@@ -231,7 +230,7 @@ func (a *Server) listenForNewEvents(conn *websocket.Conn) {
231230
232231func (a * Server ) handleNewEventMessage (b []byte ) {
233232 in := Input {}
234- err := jsonpb .Unmarshal (bytes . NewReader ( b ) , & in )
233+ err := protojson .Unmarshal (b , & in )
235234 if err != nil {
236235 log .Println ("Could not read input:" , string (b ), err )
237236 return
0 commit comments