From b943c9d6eb45b7970e1842e13509a4649d4dfaeb Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 30 Oct 2024 08:28:22 +0100 Subject: [PATCH 1/4] feat: refactor connection handling to use models.Client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - introduce `Client` model - modify all websocket operations to use the new `Client` model - update function signatures to accept `models.Client` instead of `*websocket.Conn` - ensure consistent usage of `client` across all related functions 🤖 --- cmd/client/main.go | 8 ++++---- internal/frontend/keyhandler.go | 9 ++++----- internal/frontend/main.go | 5 ++--- internal/websocket/connect.go | 10 ++++++---- internal/websocket/receive.go | 5 +++-- internal/websocket/send.go | 13 +++++++------ 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 6d65d5e..392ec5b 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -35,11 +35,11 @@ func Start() { config = helper.ParseConfig(*configfile) } - conn := websocket.Connect(config.URL) + client := websocket.Connect(config.URL) channel := make(chan GoTomato.ServerMessage) - go websocket.ProcessServerMessages(conn, channel) - frontend.UpdateLoop(conn, config, channel) + go websocket.ProcessServerMessages(client, channel) + frontend.UpdateLoop(client, config, channel) - websocket.Disconnect(conn) + websocket.Disconnect(client) } diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index 6de3860..213a957 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -2,7 +2,6 @@ package frontend import ( "github.com/eiannone/keyboard" - ws "github.com/gorilla/websocket" "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" @@ -21,16 +20,16 @@ func start_pause_resume(message GoTomato.ServerMessage) string { } } -func keyhandler(key keyboard.KeyEvent, conn *ws.Conn, config ChronoTomato.Config, message GoTomato.ServerMessage) bool { +func keyhandler(key keyboard.KeyEvent, client ChronoTomato.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool { switch key.Rune { case 0: // space cmd := start_pause_resume(message) - websocket.SendCmd(conn, config.Password, cmd) + websocket.SendCmd(client, config.Password, cmd) case 115: // s - websocket.SendCmd(conn, config.Password, "stop") + websocket.SendCmd(client, config.Password, "stop") case 114: // r if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) { - websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig) + websocket.Send_updateSettings(client, config.Password, config.PomodoroConfig) } case 113: // q return false diff --git a/internal/frontend/main.go b/internal/frontend/main.go index 3da0a08..0b51401 100644 --- a/internal/frontend/main.go +++ b/internal/frontend/main.go @@ -2,7 +2,6 @@ package frontend import ( "github.com/eiannone/keyboard" - ws "github.com/gorilla/websocket" "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" @@ -10,7 +9,7 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -func UpdateLoop(conn *ws.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { +func UpdateLoop(client ChronoTomato.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { var message GoTomato.ServerMessage keysEvents, _ := keyboard.GetKeys(1) @@ -22,7 +21,7 @@ func UpdateLoop(conn *ws.Conn, config ChronoTomato.Config, channel <-chan GoToma desktopNotifications(message) terminalOutput(message) case keypress := <-keysEvents: - if !keyhandler(keypress, conn, config, message) { + if !keyhandler(keypress, client, config, message) { return } case <-websocket.Done: diff --git a/internal/websocket/connect.go b/internal/websocket/connect.go index fd3c7ee..7a98d28 100644 --- a/internal/websocket/connect.go +++ b/internal/websocket/connect.go @@ -4,9 +4,11 @@ import ( "github.com/charmbracelet/log" "github.com/gorilla/websocket" "time" + + "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ) -func Connect(url string) *websocket.Conn { +func Connect(url string) models.Client { log.Info("Connected 󰖟 ", "host", url) conn, _, err := websocket.DefaultDialer.Dial(url, nil) @@ -14,10 +16,10 @@ func Connect(url string) *websocket.Conn { log.Fatal("Dial error!", "reason", err) } - return conn + return models.Client{Conn: conn} } -func Disconnect(conn *websocket.Conn) { +func Disconnect(client models.Client) { select { case <-Done: // session closed by remote @@ -25,7 +27,7 @@ func Disconnect(conn *websocket.Conn) { default: // Cleanly close the connection by sending a close message and then // waiting (with timeout) for the server to close the connection. - conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + client.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) select { case <-Done: case <-time.After(time.Second): diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index d17cac6..8c1b07a 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -6,18 +6,19 @@ import ( "github.com/charmbracelet/log" "github.com/gorilla/websocket" + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) var Done = make(chan struct{}) -func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerMessage) { +func ProcessServerMessages(client ChronoTomato.Client, channel chan<- GoTomato.ServerMessage) { var serverMessage GoTomato.ServerMessage defer close(Done) for { - _, message, err := conn.ReadMessage() + _, message, err := client.Conn.ReadMessage() if err != nil { if websocket.IsCloseError(err, websocket.CloseNormalClosure) { // Ignore normal closure and exit gracefully diff --git a/internal/websocket/send.go b/internal/websocket/send.go index 942b4b6..c0cc6ba 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -5,38 +5,39 @@ import ( "github.com/charmbracelet/log" "github.com/gorilla/websocket" + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) { +func sendClientCommand(client ChronoTomato.Client, msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) if err != nil { log.Error("Error marshalling!", "reason", err) return } - err = conn.WriteMessage(websocket.TextMessage, messageBytes) + err = client.Conn.WriteMessage(websocket.TextMessage, messageBytes) if err != nil { log.Error("Write error!", "reason", err) return } } -func SendCmd(conn *websocket.Conn, pwd string, cmd string) { +func SendCmd(client ChronoTomato.Client, pwd string, cmd string) { message := GoTomato.ClientCommand{ Command: cmd, Password: pwd, } - sendClientCommand(conn, message) + sendClientCommand(client, message) } -func Send_updateSettings(conn *websocket.Conn, pwd string, settings GoTomato.PomodoroConfig) { +func Send_updateSettings(client ChronoTomato.Client, pwd string, settings GoTomato.PomodoroConfig) { message := GoTomato.ClientCommand{ Command: "updateSettings", Password: pwd, Settings: settings, } - sendClientCommand(conn, message) + sendClientCommand(client, message) } From 0ca90b50a5cfe0a7d0c103aad0afba7b2402f5f3 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 30 Oct 2024 08:54:58 +0100 Subject: [PATCH 2/4] feat: refactor client handling and introduce websocket client type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add new websocket.Client type - change Connect and Disconnect functions to use the new Client type - implement methods for sending commands and updating settings on Client - update keyhandler to use websocket.Client instead of ChronoTomato.Client - modify UpdateLoop to accept websocket.Client - refactor ProcessServerMessages to accept the new Client type 🤖 --- internal/frontend/keyhandler.go | 8 ++++---- internal/frontend/main.go | 2 +- internal/websocket/connect.go | 8 +++----- internal/websocket/receive.go | 3 +-- internal/websocket/send.go | 14 ++++++++------ pkg/models/client.go | 8 ++++++++ 6 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 pkg/models/client.go diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index 213a957..bd0805b 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -20,16 +20,16 @@ func start_pause_resume(message GoTomato.ServerMessage) string { } } -func keyhandler(key keyboard.KeyEvent, client ChronoTomato.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool { +func keyhandler(key keyboard.KeyEvent, client websocket.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool { switch key.Rune { case 0: // space cmd := start_pause_resume(message) - websocket.SendCmd(client, config.Password, cmd) + client.SendCmd(config.Password, cmd) case 115: // s - websocket.SendCmd(client, config.Password, "stop") + client.SendCmd(config.Password, "stop") case 114: // r if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) { - websocket.Send_updateSettings(client, config.Password, config.PomodoroConfig) + client.SendSettingsUpdate(config.Password, config.PomodoroConfig) } case 113: // q return false diff --git a/internal/frontend/main.go b/internal/frontend/main.go index 0b51401..6a5888e 100644 --- a/internal/frontend/main.go +++ b/internal/frontend/main.go @@ -9,7 +9,7 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -func UpdateLoop(client ChronoTomato.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { +func UpdateLoop(client websocket.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { var message GoTomato.ServerMessage keysEvents, _ := keyboard.GetKeys(1) diff --git a/internal/websocket/connect.go b/internal/websocket/connect.go index 7a98d28..26d84a6 100644 --- a/internal/websocket/connect.go +++ b/internal/websocket/connect.go @@ -4,11 +4,9 @@ import ( "github.com/charmbracelet/log" "github.com/gorilla/websocket" "time" - - "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ) -func Connect(url string) models.Client { +func Connect(url string) Client { log.Info("Connected 󰖟 ", "host", url) conn, _, err := websocket.DefaultDialer.Dial(url, nil) @@ -16,10 +14,10 @@ func Connect(url string) models.Client { log.Fatal("Dial error!", "reason", err) } - return models.Client{Conn: conn} + return Client{Conn: conn} } -func Disconnect(client models.Client) { +func Disconnect(client Client) { select { case <-Done: // session closed by remote diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index 8c1b07a..547607b 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -6,13 +6,12 @@ import ( "github.com/charmbracelet/log" "github.com/gorilla/websocket" - ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) var Done = make(chan struct{}) -func ProcessServerMessages(client ChronoTomato.Client, channel chan<- GoTomato.ServerMessage) { +func ProcessServerMessages(client Client, channel chan<- GoTomato.ServerMessage) { var serverMessage GoTomato.ServerMessage defer close(Done) diff --git a/internal/websocket/send.go b/internal/websocket/send.go index c0cc6ba..fa736dd 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -9,35 +9,37 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -func sendClientCommand(client ChronoTomato.Client, msg GoTomato.ClientCommand) { +type Client ChronoTomato.PomodoroClient // New websocket client + +func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) if err != nil { log.Error("Error marshalling!", "reason", err) return } - err = client.Conn.WriteMessage(websocket.TextMessage, messageBytes) + err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes) if err != nil { log.Error("Write error!", "reason", err) return } } -func SendCmd(client ChronoTomato.Client, pwd string, cmd string) { +func (c Client) SendCmd(pwd string, cmd string) { message := GoTomato.ClientCommand{ Command: cmd, Password: pwd, } - sendClientCommand(client, message) + c.sendClientCommand(message) } -func Send_updateSettings(client ChronoTomato.Client, pwd string, settings GoTomato.PomodoroConfig) { +func (c Client) SendSettingsUpdate(pwd string, settings GoTomato.PomodoroConfig) { message := GoTomato.ClientCommand{ Command: "updateSettings", Password: pwd, Settings: settings, } - sendClientCommand(client, message) + c.sendClientCommand(message) } diff --git a/pkg/models/client.go b/pkg/models/client.go new file mode 100644 index 0000000..3cd5007 --- /dev/null +++ b/pkg/models/client.go @@ -0,0 +1,8 @@ +package models + +import "github.com/gorilla/websocket" + +// Represents a websocket client +type PomodoroClient struct { + Conn *websocket.Conn // The websocket connection of the client +} From b374ee8aff809e95e70ee670d46fb15fc6fe521e Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 30 Oct 2024 09:00:29 +0100 Subject: [PATCH 3/4] feat: add Password field to `PomodoroClient` struct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit remove unused password parameters from Client-Methods 🤖 --- cmd/client/main.go | 1 + internal/frontend/keyhandler.go | 6 +++--- internal/websocket/send.go | 8 ++++---- pkg/models/client.go | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 392ec5b..d2e920f 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -36,6 +36,7 @@ func Start() { } client := websocket.Connect(config.URL) + client.Password = config.Password channel := make(chan GoTomato.ServerMessage) go websocket.ProcessServerMessages(client, channel) diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index bd0805b..85a2e4c 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -24,12 +24,12 @@ func keyhandler(key keyboard.KeyEvent, client websocket.Client, config ChronoTom switch key.Rune { case 0: // space cmd := start_pause_resume(message) - client.SendCmd(config.Password, cmd) + client.SendCmd(cmd) case 115: // s - client.SendCmd(config.Password, "stop") + client.SendCmd("stop") case 114: // r if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) { - client.SendSettingsUpdate(config.Password, config.PomodoroConfig) + client.SendSettingsUpdate(config.PomodoroConfig) } case 113: // q return false diff --git a/internal/websocket/send.go b/internal/websocket/send.go index fa736dd..650e24a 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -25,19 +25,19 @@ func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { } } -func (c Client) SendCmd(pwd string, cmd string) { +func (c Client) SendCmd(cmd string) { message := GoTomato.ClientCommand{ Command: cmd, - Password: pwd, + Password: c.Password, } c.sendClientCommand(message) } -func (c Client) SendSettingsUpdate(pwd string, settings GoTomato.PomodoroConfig) { +func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) { message := GoTomato.ClientCommand{ Command: "updateSettings", - Password: pwd, + Password: c.Password, Settings: settings, } diff --git a/pkg/models/client.go b/pkg/models/client.go index 3cd5007..cefc095 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -4,5 +4,6 @@ import "github.com/gorilla/websocket" // Represents a websocket client type PomodoroClient struct { - Conn *websocket.Conn // The websocket connection of the client + Conn *websocket.Conn // The websocket connection of the client + Password string // Pomodoro password } From b06fac60d547805d90bbf235f7c85d7df7089bb0 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 30 Oct 2024 09:03:14 +0100 Subject: [PATCH 4/4] fix: rename model `PomodoroClient` to `GoTomatoClient` --- internal/websocket/send.go | 2 +- pkg/models/client.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/websocket/send.go b/internal/websocket/send.go index 650e24a..2d891ba 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -9,7 +9,7 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -type Client ChronoTomato.PomodoroClient // New websocket client +type Client ChronoTomato.GoTomatoClient // New websocket client func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) diff --git a/pkg/models/client.go b/pkg/models/client.go index cefc095..9cf8436 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -3,7 +3,7 @@ package models import "github.com/gorilla/websocket" // Represents a websocket client -type PomodoroClient struct { +type GoTomatoClient struct { Conn *websocket.Conn // The websocket connection of the client Password string // Pomodoro password }