diff --git a/internal/broadcast/broadcast.go b/internal/broadcast/broadcast.go index bd93d06..ac23e7e 100644 --- a/internal/broadcast/broadcast.go +++ b/internal/broadcast/broadcast.go @@ -2,34 +2,24 @@ package broadcast import ( "encoding/json" - "git.smsvc.net/pomodoro/GoTomato/pkg/models" + "git.smsvc.net/pomodoro/GoTomato/internal/shared" + ws "git.smsvc.net/pomodoro/GoTomato/internal/websocket" "github.com/gorilla/websocket" "log" "time" ) -// Clients is a map of connected WebSocket clients, where each client is represented by the Client struct -var Clients = make(map[*websocket.Conn]*models.Client) -var Message = models.ServerMessage{ - Mode: "none", - Session: 0, - TotalSession: 0, - TimeLeft: 0, - Ongoing: false, - Paused: false, -} - // BroadcastMessage sends a message to all connected WebSocket clients. func SendPermanentBroadCastMessage() { for { // Marshal the message into JSON format - jsonMessage, err := json.Marshal(Message) + jsonMessage, err := json.Marshal(shared.Message) if err != nil { log.Printf("Error marshalling message: %v", err) return } // Iterate over all connected clients and broadcast the message - for _, client := range Clients { + for _, client := range ws.Clients { err := client.SendMessage(websocket.TextMessage, jsonMessage) if err != nil { log.Printf("Error broadcasting to client: %v", err) diff --git a/internal/pomodoro/pomodoro.go b/internal/pomodoro/pomodoro.go index a72319c..b748d0a 100644 --- a/internal/pomodoro/pomodoro.go +++ b/internal/pomodoro/pomodoro.go @@ -1,7 +1,7 @@ package pomodoro import ( - "git.smsvc.net/pomodoro/GoTomato/internal/broadcast" + "git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/pkg/models" "sync" ) @@ -15,14 +15,14 @@ var mu sync.Mutex // to synchronize access to shared state // RunPomodoro iterates the Pomodoro work/break sessions. func RunPomodoro(config models.GoTomatoPomodoroConfig) { mu.Lock() - broadcast.Message.Ongoing = true - broadcast.Message.Paused = false + shared.Message.Ongoing = true + shared.Message.Paused = false mu.Unlock() - broadcast.Message.TotalSession = config.Sessions + shared.Message.TotalSession = config.Sessions for session := 1; session <= config.Sessions; session++ { - broadcast.Message.Session = session + shared.Message.Session = session if !startTimer(config.Work, "Work") { break } @@ -38,7 +38,7 @@ func RunPomodoro(config models.GoTomatoPomodoroConfig) { } mu.Lock() - broadcast.Message.Ongoing = false + shared.Message.Ongoing = false mu.Unlock() } @@ -48,20 +48,20 @@ func ResetPomodoro() { pomodoroResetChannel <- true mu.Lock() - broadcast.Message.Ongoing = false - broadcast.Message.Paused = false + shared.Message.Ongoing = false + shared.Message.Paused = false mu.Unlock() // Reset message - broadcast.Message.Mode = "none" - broadcast.Message.Session = 0 - broadcast.Message.TotalSession = 0 - broadcast.Message.TimeLeft = 0 + shared.Message.Mode = "none" + shared.Message.Session = 0 + shared.Message.TotalSession = 0 + shared.Message.TimeLeft = 0 } func PausePomodoro() { mu.Lock() - broadcast.Message.Paused = true + shared.Message.Paused = true mu.Unlock() pomodoroPauseChannel <- true @@ -69,7 +69,7 @@ func PausePomodoro() { func ResumePomodoro() { mu.Lock() - broadcast.Message.Paused = false + shared.Message.Paused = false mu.Unlock() pomodoroResumeChannel <- true } @@ -77,11 +77,11 @@ func ResumePomodoro() { func IsPomodoroOngoing() bool { mu.Lock() defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done - return broadcast.Message.Ongoing + return shared.Message.Ongoing } func IsPomodoroPaused() bool { mu.Lock() defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done - return broadcast.Message.Paused + return shared.Message.Paused } diff --git a/internal/pomodoro/timer.go b/internal/pomodoro/timer.go index 30466c6..1c1f360 100644 --- a/internal/pomodoro/timer.go +++ b/internal/pomodoro/timer.go @@ -1,11 +1,11 @@ package pomodoro import ( - "git.smsvc.net/pomodoro/GoTomato/internal/broadcast" + "git.smsvc.net/pomodoro/GoTomato/internal/shared" "time" ) -// startTimer runs the countdown and broadcasts every second. +// startTimer runs the countdown and shared. every second. func startTimer(remainingSeconds int, mode string) bool { for remainingSeconds > 0 { select { @@ -18,16 +18,16 @@ func startTimer(remainingSeconds int, mode string) bool { default: // Broadcast the current state to all clients if !IsPomodoroPaused() { - broadcast.Message.Mode = mode - broadcast.Message.TimeLeft = remainingSeconds + shared.Message.Mode = mode + shared.Message.TimeLeft = remainingSeconds time.Sleep(time.Second) remainingSeconds-- } } } - // Final broadcast when time reaches zero - broadcast.Message.TimeLeft = remainingSeconds + // Final shared.when time reaches zero + shared.Message.TimeLeft = remainingSeconds return true } diff --git a/internal/shared/state.go b/internal/shared/state.go new file mode 100644 index 0000000..26ff2d6 --- /dev/null +++ b/internal/shared/state.go @@ -0,0 +1,14 @@ +package shared + +import ( + "git.smsvc.net/pomodoro/GoTomato/pkg/models" +) + +var Message = models.ServerMessage{ + Mode: "none", + Session: 0, + TotalSession: 0, + TimeLeft: 0, + Ongoing: false, + Paused: false, +} diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index 8952fd0..df4fdc3 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -2,7 +2,6 @@ package websocket import ( "encoding/json" - "git.smsvc.net/pomodoro/GoTomato/internal/broadcast" "git.smsvc.net/pomodoro/GoTomato/internal/pomodoro" "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/gorilla/websocket" @@ -25,7 +24,7 @@ func handleClientCommands(ws *websocket.Conn) { _, message, err := ws.ReadMessage() if err != nil { log.Printf("Client disconnected: %v", err) - delete(broadcast.Clients, ws) + delete(Clients, ws) break } diff --git a/internal/websocket/handle_connections.go b/internal/websocket/handle_connections.go index cbf0f04..be49918 100644 --- a/internal/websocket/handle_connections.go +++ b/internal/websocket/handle_connections.go @@ -1,7 +1,6 @@ package websocket import ( - "git.smsvc.net/pomodoro/GoTomato/internal/broadcast" "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/gorilla/websocket" "log" @@ -9,6 +8,8 @@ import ( "sync" ) +// Clients is a map of connected WebSocket clients, where each client is represented by the Client struct +var Clients = make(map[*websocket.Conn]*models.Client) var mu sync.Mutex // Mutex to protect access to the Clients map // Upgrader to upgrade HTTP requests to WebSocket connections @@ -28,7 +29,7 @@ func HandleConnections(w http.ResponseWriter, r *http.Request) { // Register the new client mu.Lock() - broadcast.Clients[ws] = &models.Client{ + Clients[ws] = &models.Client{ Conn: ws, // Store the WebSocket connection } mu.Unlock()