Compare commits
2 commits
09c1591db9
...
93376705dc
Author | SHA1 | Date | |
---|---|---|---|
93376705dc | |||
cb15068109 |
9 changed files with 225 additions and 149 deletions
150
GoTomato.go
150
GoTomato.go
|
@ -1,155 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"git.smsvc.net/pomodoro/GoTomato/cmd/server"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
workDuration = 15 * 60
|
|
||||||
shortBreakDuration = 5 * 60
|
|
||||||
longBreakDuration = 10 * 60
|
|
||||||
sessions = 4
|
|
||||||
)
|
|
||||||
|
|
||||||
type BroadcastMessage struct {
|
|
||||||
Mode string `json:"mode"`
|
|
||||||
Session int `json:"session"`
|
|
||||||
MaxSession int `json:"max_session"`
|
|
||||||
TimeLeft int `json:"time_left"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ClientCommand struct {
|
|
||||||
Command string `json:"command"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var clients = make(map[*websocket.Conn]bool)
|
|
||||||
var timerRunning bool
|
|
||||||
var timerStopChannel = make(chan bool, 1)
|
|
||||||
var mu sync.Mutex // to synchronize access to shared state
|
|
||||||
|
|
||||||
// broadcastMessage sends the remaining time to all connected clients.
|
|
||||||
func broadcastMessage(message BroadcastMessage) {
|
|
||||||
jsonMessage, _ := json.Marshal(message)
|
|
||||||
for client := range clients {
|
|
||||||
err := client.WriteMessage(websocket.TextMessage, jsonMessage)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error broadcasting to client: %v", err)
|
|
||||||
client.Close()
|
|
||||||
delete(clients, client)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// startTimer runs the countdown and broadcasts every second.
|
|
||||||
func startTimer(remainingSeconds int, mode string, session int) bool {
|
|
||||||
for remainingSeconds > 0 {
|
|
||||||
select {
|
|
||||||
case <-timerStopChannel:
|
|
||||||
return false // Stop the timer if a stop command is received
|
|
||||||
default:
|
|
||||||
broadcastMessage(BroadcastMessage{
|
|
||||||
Mode: mode,
|
|
||||||
Session: session,
|
|
||||||
MaxSession: sessions,
|
|
||||||
TimeLeft: remainingSeconds,
|
|
||||||
})
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
remainingSeconds--
|
|
||||||
}
|
|
||||||
}
|
|
||||||
broadcastMessage(BroadcastMessage{
|
|
||||||
Mode: mode,
|
|
||||||
Session: session,
|
|
||||||
MaxSession: sessions,
|
|
||||||
TimeLeft: 0,
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// runPomodoroTimer iterates the Pomodoro work/break sessions.
|
|
||||||
func runPomodoroTimer() {
|
|
||||||
mu.Lock()
|
|
||||||
timerRunning = true
|
|
||||||
|
|
||||||
for session := 1; session <= sessions; session++ {
|
|
||||||
if !startTimer(workDuration, "Work", session) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if session == sessions {
|
|
||||||
if !startTimer(longBreakDuration, "LongBreak", session) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if !startTimer(shortBreakDuration, "ShortBreak", session) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
timerRunning = false
|
|
||||||
mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// upgrade HTTP requests to WebSocket connections.
|
|
||||||
var upgrader = websocket.Upgrader{
|
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleConnections(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Upgrade initial GET request to a WebSocket
|
|
||||||
ws, err := upgrader.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("WebSocket upgrade error: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer ws.Close()
|
|
||||||
|
|
||||||
// Register the new client
|
|
||||||
clients[ws] = true
|
|
||||||
|
|
||||||
// Listen for commands from this client
|
|
||||||
for {
|
|
||||||
_, message, err := ws.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Client disconnected: %v", err)
|
|
||||||
delete(clients, ws)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle incoming commands
|
|
||||||
var command ClientCommand
|
|
||||||
err = json.Unmarshal(message, &command)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error unmarshalling command: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process the commands
|
|
||||||
switch command.Command {
|
|
||||||
case "start":
|
|
||||||
if !timerRunning {
|
|
||||||
go runPomodoroTimer()
|
|
||||||
}
|
|
||||||
case "stop":
|
|
||||||
if timerRunning {
|
|
||||||
timerStopChannel <- true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/ws", handleConnections)
|
server.Start()
|
||||||
|
|
||||||
log.Println("Pomodoro WebSocket server started on :8080")
|
|
||||||
err := http.ListenAndServe(":8080", nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error starting server: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
17
cmd/server/main.go
Normal file
17
cmd/server/main.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/internal/websocket"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Start() {
|
||||||
|
http.HandleFunc("/ws", websocket.HandleConnections)
|
||||||
|
|
||||||
|
log.Println("Pomodoro WebSocket server started on :8080")
|
||||||
|
err := http.ListenAndServe(":8080", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error starting server: %v", err)
|
||||||
|
}
|
||||||
|
}
|
2
go.mod
2
go.mod
|
@ -2,4 +2,4 @@ module git.smsvc.net/pomodoro/GoTomato
|
||||||
|
|
||||||
go 1.23.1
|
go 1.23.1
|
||||||
|
|
||||||
require github.com/gorilla/websocket v1.5.3 // indirect
|
require github.com/gorilla/websocket v1.5.3
|
||||||
|
|
28
internal/broadcast/broadcast.go
Normal file
28
internal/broadcast/broadcast.go
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package broadcast
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BroadcastMessage sends a message to all connected WebSocket clients.
|
||||||
|
func BroadcastMessage(clients map[*websocket.Conn]bool, message models.BroadcastMessage) {
|
||||||
|
// Marshal the message into JSON format
|
||||||
|
jsonMessage, err := json.Marshal(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 {
|
||||||
|
err := client.WriteMessage(websocket.TextMessage, jsonMessage)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error broadcasting to client: %v", err)
|
||||||
|
client.Close()
|
||||||
|
delete(clients, client) // Remove the client if an error occurs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
39
internal/pomodoro/pomodoro.go
Normal file
39
internal/pomodoro/pomodoro.go
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
package pomodoro
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
workDuration = 15 * 60
|
||||||
|
shortBreakDuration = 5 * 60
|
||||||
|
longBreakDuration = 10 * 60
|
||||||
|
sessions = 4
|
||||||
|
)
|
||||||
|
|
||||||
|
var mu sync.Mutex // to synchronize access to shared state
|
||||||
|
|
||||||
|
// RunPomodoroTimer iterates the Pomodoro work/break sessions.
|
||||||
|
func RunPomodoroTimer(clients map[*websocket.Conn]bool) {
|
||||||
|
mu.Lock()
|
||||||
|
timerRunning = true
|
||||||
|
|
||||||
|
for session := 1; session <= sessions; session++ {
|
||||||
|
if !startTimer(clients, workDuration, "Work", session) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if session == sessions {
|
||||||
|
if !startTimer(clients, longBreakDuration, "LongBreak", session) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !startTimer(clients, shortBreakDuration, "ShortBreak", session) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
timerRunning = false
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
51
internal/pomodoro/timer.go
Normal file
51
internal/pomodoro/timer.go
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package pomodoro
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var timerRunning bool
|
||||||
|
var timerStopChannel = make(chan bool, 1)
|
||||||
|
|
||||||
|
// startTimer runs the countdown and broadcasts every second.
|
||||||
|
func startTimer(clients map[*websocket.Conn]bool, remainingSeconds int, mode string, session int) bool {
|
||||||
|
for remainingSeconds > 0 {
|
||||||
|
select {
|
||||||
|
case <-timerStopChannel:
|
||||||
|
return false // Stop the timer if a stop command is received
|
||||||
|
default:
|
||||||
|
// Broadcast the current state to all clients
|
||||||
|
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
|
||||||
|
Mode: mode,
|
||||||
|
Session: session,
|
||||||
|
MaxSession: sessions,
|
||||||
|
TimeLeft: remainingSeconds,
|
||||||
|
})
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
remainingSeconds--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final broadcast when time reaches zero
|
||||||
|
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
|
||||||
|
Mode: mode,
|
||||||
|
Session: session,
|
||||||
|
MaxSession: sessions,
|
||||||
|
TimeLeft: 0,
|
||||||
|
})
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopTimer sends a signal to stop the running Pomodoro timer.
|
||||||
|
func StopTimer() {
|
||||||
|
timerStopChannel <- true
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsTimerRunning returns the status of the timer.
|
||||||
|
func IsTimerRunning() bool {
|
||||||
|
return timerRunning
|
||||||
|
}
|
41
internal/websocket/client_commands.go
Normal file
41
internal/websocket/client_commands.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// handleClientCommands listens for commands from WebSocket clients and dispatches to the timer.
|
||||||
|
func handleClientCommands(ws *websocket.Conn) {
|
||||||
|
for {
|
||||||
|
_, message, err := ws.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Client disconnected: %v", err)
|
||||||
|
delete(Clients, ws)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle incoming commands
|
||||||
|
var command models.ClientCommand
|
||||||
|
err = json.Unmarshal(message, &command)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error unmarshalling command: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the command
|
||||||
|
switch command.Command {
|
||||||
|
case "start":
|
||||||
|
if !pomodoro.IsTimerRunning() {
|
||||||
|
go pomodoro.RunPomodoroTimer(Clients) // Start the timer with the list of clients
|
||||||
|
}
|
||||||
|
case "stop":
|
||||||
|
if pomodoro.IsTimerRunning() {
|
||||||
|
pomodoro.StopTimer() // Stop the timer in the Pomodoro package
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
internal/websocket/handle_connections.go
Normal file
32
internal/websocket/handle_connections.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Map to track connected clients
|
||||||
|
var Clients = make(map[*websocket.Conn]bool)
|
||||||
|
|
||||||
|
// Upgrader to upgrade HTTP requests to WebSocket connections
|
||||||
|
var upgrader = websocket.Upgrader{
|
||||||
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleConnections upgrades HTTP requests to WebSocket connections and manages the client lifecycle.
|
||||||
|
func HandleConnections(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Upgrade initial GET request to a WebSocket
|
||||||
|
ws, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("WebSocket upgrade error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer ws.Close()
|
||||||
|
|
||||||
|
// Register the new client
|
||||||
|
Clients[ws] = true
|
||||||
|
|
||||||
|
// Listen for commands from the connected client
|
||||||
|
handleClientCommands(ws)
|
||||||
|
}
|
14
pkg/models/types.go
Normal file
14
pkg/models/types.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
// BroadcastMessage represents the data sent to the client via WebSocket.
|
||||||
|
type BroadcastMessage struct {
|
||||||
|
Mode string `json:"mode"` // "Work", "ShortBreak", or "LongBreak"
|
||||||
|
Session int `json:"session"` // Current session number
|
||||||
|
MaxSession int `json:"max_session"` // Total number of sessions
|
||||||
|
TimeLeft int `json:"time_left"` // Remaining time in seconds
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClientCommand represents a command from the client (start/stop).
|
||||||
|
type ClientCommand struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
}
|
Loading…
Reference in a new issue