Compare commits
No commits in common. "93376705dcf8d368f3fb099c620808735071de73" and "09c1591db946f7ea6c2e0ed5e9fe13ee7ce9008a" have entirely different histories.
93376705dc
...
09c1591db9
9 changed files with 150 additions and 226 deletions
152
GoTomato.go
152
GoTomato.go
|
@ -1,9 +1,155 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.smsvc.net/pomodoro/GoTomato/cmd/server"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
const (
|
||||||
server.Start()
|
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() {
|
||||||
|
http.HandleFunc("/ws", handleConnections)
|
||||||
|
|
||||||
|
log.Println("Pomodoro WebSocket server started on :8080")
|
||||||
|
err := http.ListenAndServe(":8080", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error starting server: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
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
|
require github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
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()
|
|
||||||
}
|
|
|
@ -1,51 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
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)
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
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