From cb3ead6cd00c19c69151d4c05b77ce51646e2435 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Thu, 21 Dec 2023 12:52:56 -0800 Subject: [PATCH] Add verbose flag --- cmd/bgammon-server/main.go | 4 +++- pkg/server/client_socket.go | 10 +++++++--- pkg/server/client_websocket.go | 10 +++++++--- pkg/server/server.go | 8 +++++--- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/cmd/bgammon-server/main.go b/cmd/bgammon-server/main.go index ff428c9..39f83e5 100644 --- a/cmd/bgammon-server/main.go +++ b/cmd/bgammon-server/main.go @@ -20,6 +20,7 @@ func main() { mailServer string passwordSalt string resetSalt string + verbose bool debug int debugCommands bool rollStatistics bool @@ -29,6 +30,7 @@ func main() { flag.StringVar(&tz, "tz", "", "Time zone used when calculating statistics") flag.StringVar(&dataSource, "db", "", "Database data source (postgres://username:password@localhost:5432/database_name") flag.StringVar(&mailServer, "smtp", "", "SMTP server address") + flag.BoolVar(&verbose, "verbose", false, "Print all client messages") flag.IntVar(&debug, "debug", 0, "print debug information and serve pprof on specified port") flag.BoolVar(&debugCommands, "debug-commands", false, "allow players to use restricted commands") flag.BoolVar(&rollStatistics, "statistics", false, "print dice roll statistics and exit") @@ -60,7 +62,7 @@ func main() { }() } - s := server.NewServer(tz, dataSource, mailServer, passwordSalt, resetSalt, false, debugCommands) + s := server.NewServer(tz, dataSource, mailServer, passwordSalt, resetSalt, false, verbose || debug > 0, debugCommands) if tcpAddress != "" { s.Listen("tcp", tcpAddress) } diff --git a/pkg/server/client_socket.go b/pkg/server/client_socket.go index 15244cb..2f8676d 100644 --- a/pkg/server/client_socket.go +++ b/pkg/server/client_socket.go @@ -19,13 +19,15 @@ type socketClient struct { commands chan<- []byte terminated bool wgEvents sync.WaitGroup + verbose bool } -func newSocketClient(conn net.Conn, commands chan<- []byte, events chan []byte) *socketClient { +func newSocketClient(conn net.Conn, commands chan<- []byte, events chan []byte, verbose bool) *socketClient { return &socketClient{ conn: conn, events: events, commands: commands, + verbose: verbose, } } @@ -76,7 +78,9 @@ func (c *socketClient) readCommands() { copy(buf, scanner.Bytes()) c.commands <- buf - logClientRead(scanner.Bytes()) + if c.verbose { + logClientRead(scanner.Bytes()) + } setTimeout() } @@ -120,7 +124,7 @@ func (c *socketClient) writeEvents(closeWrite chan struct{}) { continue } - if !bytes.HasPrefix(event, []byte(`{"Type":"ping"`)) && !bytes.HasPrefix(event, []byte(`{"Type":"list"`)) { + if c.verbose && !bytes.HasPrefix(event, []byte(`{"Type":"ping"`)) && !bytes.HasPrefix(event, []byte(`{"Type":"list"`)) { log.Printf("-> %s", event) } c.wgEvents.Done() diff --git a/pkg/server/client_websocket.go b/pkg/server/client_websocket.go index 40b4988..8ab73e4 100644 --- a/pkg/server/client_websocket.go +++ b/pkg/server/client_websocket.go @@ -21,9 +21,10 @@ type webSocketClient struct { commands chan<- []byte terminated bool wgEvents sync.WaitGroup + verbose bool } -func newWebSocketClient(r *http.Request, w http.ResponseWriter, commands chan<- []byte, events chan []byte) *webSocketClient { +func newWebSocketClient(r *http.Request, w http.ResponseWriter, commands chan<- []byte, events chan []byte, verbose bool) *webSocketClient { conn, _, _, err := ws.UpgradeHTTP(r, w) if err != nil { return nil @@ -33,6 +34,7 @@ func newWebSocketClient(r *http.Request, w http.ResponseWriter, commands chan<- conn: conn, events: events, commands: commands, + verbose: verbose, } } @@ -85,7 +87,9 @@ func (c *webSocketClient) readCommands() { copy(buf, msg) c.commands <- buf - logClientRead(msg) + if c.verbose { + logClientRead(msg) + } } } @@ -127,7 +131,7 @@ func (c *webSocketClient) writeEvents(closeWrite chan struct{}) { continue } - if !bytes.HasPrefix(event, []byte(`{"Type":"ping"`)) && !bytes.HasPrefix(event, []byte(`{"Type":"list"`)) { + if c.verbose && !bytes.HasPrefix(event, []byte(`{"Type":"ping"`)) && !bytes.HasPrefix(event, []byte(`{"Type":"list"`)) { log.Printf("-> %s", event) } c.wgEvents.Done() diff --git a/pkg/server/server.go b/pkg/server/server.go index 5dc3f59..c053a29 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -57,9 +57,10 @@ type server struct { tz *time.Location relayChat bool // Chats are not relayed normally. This option is only used by local servers. + verbose bool } -func NewServer(tz string, dataSource string, mailServer string, passwordSalt string, resetSalt string, relayChat bool, allowDebug bool) *server { +func NewServer(tz string, dataSource string, mailServer string, passwordSalt string, resetSalt string, relayChat bool, verbose bool, allowDebug bool) *server { const bufferSize = 10 s := &server{ newGameIDs: make(chan int), @@ -70,6 +71,7 @@ func NewServer(tz string, dataSource string, mailServer string, passwordSalt str passwordSalt: passwordSalt, resetSalt: resetSalt, relayChat: relayChat, + verbose: verbose, } if tz != "" { @@ -245,7 +247,7 @@ func (s *server) handleWebSocket(w http.ResponseWriter, r *http.Request) { commands := make(chan []byte, bufferSize) events := make(chan []byte, bufferSize) - wsClient := newWebSocketClient(r, w, commands, events) + wsClient := newWebSocketClient(r, w, commands, events, s.verbose) if wsClient == nil { return } @@ -411,7 +413,7 @@ func (s *server) handleConnection(conn net.Conn) { connected: now, lastActive: now, commands: commands, - Client: newSocketClient(conn, commands, events), + Client: newSocketClient(conn, commands, events, s.verbose), } s.sendHello(c) s.handleClient(c)