refactor: move broadcast.Message to shared.Message
- create a new shared state file to manage message state
- replace broadcast.Message with shared.Message in pomodoro logic
- update websocket client handling to use shared.Clients
- remove unnecessary broadcast imports from various files
- ensure consistent message handling across the application
🤖
This commit is contained in:
parent
234f3c17dc
commit
aab6896c7d
6 changed files with 44 additions and 40 deletions
|
@ -2,34 +2,24 @@ package broadcast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"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"
|
"github.com/gorilla/websocket"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"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.
|
// BroadcastMessage sends a message to all connected WebSocket clients.
|
||||||
func SendPermanentBroadCastMessage() {
|
func SendPermanentBroadCastMessage() {
|
||||||
for {
|
for {
|
||||||
// Marshal the message into JSON format
|
// Marshal the message into JSON format
|
||||||
jsonMessage, err := json.Marshal(Message)
|
jsonMessage, err := json.Marshal(shared.Message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error marshalling message: %v", err)
|
log.Printf("Error marshalling message: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Iterate over all connected clients and broadcast the message
|
// Iterate over all connected clients and broadcast the message
|
||||||
for _, client := range Clients {
|
for _, client := range ws.Clients {
|
||||||
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error broadcasting to client: %v", err)
|
log.Printf("Error broadcasting to client: %v", err)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package pomodoro
|
package pomodoro
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -15,14 +15,14 @@ var mu sync.Mutex // to synchronize access to shared state
|
||||||
// RunPomodoro iterates the Pomodoro work/break sessions.
|
// RunPomodoro iterates the Pomodoro work/break sessions.
|
||||||
func RunPomodoro(config models.GoTomatoPomodoroConfig) {
|
func RunPomodoro(config models.GoTomatoPomodoroConfig) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
broadcast.Message.Ongoing = true
|
shared.Message.Ongoing = true
|
||||||
broadcast.Message.Paused = false
|
shared.Message.Paused = false
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
broadcast.Message.TotalSession = config.Sessions
|
shared.Message.TotalSession = config.Sessions
|
||||||
|
|
||||||
for session := 1; session <= config.Sessions; session++ {
|
for session := 1; session <= config.Sessions; session++ {
|
||||||
broadcast.Message.Session = session
|
shared.Message.Session = session
|
||||||
if !startTimer(config.Work, "Work") {
|
if !startTimer(config.Work, "Work") {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ func RunPomodoro(config models.GoTomatoPomodoroConfig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
broadcast.Message.Ongoing = false
|
shared.Message.Ongoing = false
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,20 +48,20 @@ func ResetPomodoro() {
|
||||||
pomodoroResetChannel <- true
|
pomodoroResetChannel <- true
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
broadcast.Message.Ongoing = false
|
shared.Message.Ongoing = false
|
||||||
broadcast.Message.Paused = false
|
shared.Message.Paused = false
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
// Reset message
|
// Reset message
|
||||||
broadcast.Message.Mode = "none"
|
shared.Message.Mode = "none"
|
||||||
broadcast.Message.Session = 0
|
shared.Message.Session = 0
|
||||||
broadcast.Message.TotalSession = 0
|
shared.Message.TotalSession = 0
|
||||||
broadcast.Message.TimeLeft = 0
|
shared.Message.TimeLeft = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func PausePomodoro() {
|
func PausePomodoro() {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
broadcast.Message.Paused = true
|
shared.Message.Paused = true
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
pomodoroPauseChannel <- true
|
pomodoroPauseChannel <- true
|
||||||
|
@ -69,7 +69,7 @@ func PausePomodoro() {
|
||||||
|
|
||||||
func ResumePomodoro() {
|
func ResumePomodoro() {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
broadcast.Message.Paused = false
|
shared.Message.Paused = false
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
pomodoroResumeChannel <- true
|
pomodoroResumeChannel <- true
|
||||||
}
|
}
|
||||||
|
@ -77,11 +77,11 @@ func ResumePomodoro() {
|
||||||
func IsPomodoroOngoing() bool {
|
func IsPomodoroOngoing() bool {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
|
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 {
|
func IsPomodoroPaused() bool {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
|
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
|
||||||
return broadcast.Message.Paused
|
return shared.Message.Paused
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package pomodoro
|
package pomodoro
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// startTimer runs the countdown and broadcasts every second.
|
// startTimer runs the countdown and shared. every second.
|
||||||
func startTimer(remainingSeconds int, mode string) bool {
|
func startTimer(remainingSeconds int, mode string) bool {
|
||||||
for remainingSeconds > 0 {
|
for remainingSeconds > 0 {
|
||||||
select {
|
select {
|
||||||
|
@ -18,16 +18,16 @@ func startTimer(remainingSeconds int, mode string) bool {
|
||||||
default:
|
default:
|
||||||
// Broadcast the current state to all clients
|
// Broadcast the current state to all clients
|
||||||
if !IsPomodoroPaused() {
|
if !IsPomodoroPaused() {
|
||||||
broadcast.Message.Mode = mode
|
shared.Message.Mode = mode
|
||||||
broadcast.Message.TimeLeft = remainingSeconds
|
shared.Message.TimeLeft = remainingSeconds
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
remainingSeconds--
|
remainingSeconds--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Final broadcast when time reaches zero
|
// Final shared.when time reaches zero
|
||||||
broadcast.Message.TimeLeft = remainingSeconds
|
shared.Message.TimeLeft = remainingSeconds
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
14
internal/shared/state.go
Normal file
14
internal/shared/state.go
Normal file
|
@ -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,
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
|
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
@ -25,7 +24,7 @@ func handleClientCommands(ws *websocket.Conn) {
|
||||||
_, message, err := ws.ReadMessage()
|
_, message, err := ws.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Client disconnected: %v", err)
|
log.Printf("Client disconnected: %v", err)
|
||||||
delete(broadcast.Clients, ws)
|
delete(Clients, ws)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"log"
|
"log"
|
||||||
|
@ -9,6 +8,8 @@ import (
|
||||||
"sync"
|
"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
|
var mu sync.Mutex // Mutex to protect access to the Clients map
|
||||||
|
|
||||||
// Upgrader to upgrade HTTP requests to WebSocket connections
|
// Upgrader to upgrade HTTP requests to WebSocket connections
|
||||||
|
@ -28,7 +29,7 @@ func HandleConnections(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// Register the new client
|
// Register the new client
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
broadcast.Clients[ws] = &models.Client{
|
Clients[ws] = &models.Client{
|
||||||
Conn: ws, // Store the WebSocket connection
|
Conn: ws, // Store the WebSocket connection
|
||||||
}
|
}
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
Loading…
Reference in a new issue