2020-01-06 20:57:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/websocket"
|
2023-10-15 18:04:22 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
websocketBufferSize = 1024
|
|
|
|
websocketUpdatePoolSize = 10
|
2020-01-06 20:57:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type position struct {
|
|
|
|
Lat float64 `json:"lat"`
|
|
|
|
Lon float64 `json:"lon"`
|
|
|
|
Retained bool `json:"retained"`
|
2020-01-06 23:18:48 +00:00
|
|
|
SenderID string `json:"sender_id"`
|
2020-01-06 20:57:17 +00:00
|
|
|
Time time.Time `json:"time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
reqDistributors = map[string]map[string]chan position{}
|
|
|
|
reqDistributorsLock = new(sync.RWMutex)
|
|
|
|
reqRetainer = map[string]position{}
|
|
|
|
reqRetainerLock = new(sync.RWMutex)
|
|
|
|
|
|
|
|
upgrader = websocket.Upgrader{
|
2023-10-15 18:04:22 +00:00
|
|
|
ReadBufferSize: websocketBufferSize,
|
|
|
|
WriteBufferSize: websocketBufferSize,
|
2020-01-06 20:57:17 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleRedirectRandom(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, uuid.Must(uuid.NewV4()).String(), http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMapFrontend(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "frontend/index.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMapSocket(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
vars = mux.Vars(r)
|
|
|
|
mapID = vars["mapID"]
|
|
|
|
sockID = uuid.Must(uuid.NewV4()).String()
|
2023-10-15 18:04:22 +00:00
|
|
|
updates = make(chan position, websocketUpdatePoolSize)
|
2020-01-06 20:57:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register update channel
|
|
|
|
reqDistributorsLock.Lock()
|
|
|
|
if _, ok := reqDistributors[mapID]; !ok {
|
|
|
|
reqDistributors[mapID] = make(map[string]chan position)
|
|
|
|
}
|
|
|
|
reqDistributors[mapID][sockID] = updates
|
|
|
|
reqDistributorsLock.Unlock()
|
|
|
|
|
|
|
|
// In case a retained position is available queue it
|
|
|
|
reqRetainerLock.RLock()
|
2020-01-06 23:49:40 +00:00
|
|
|
if p, ok := reqRetainer[mapID]; ok && time.Since(p.Time) < cfg.StateTimeout {
|
2020-01-06 20:57:17 +00:00
|
|
|
updates <- p
|
|
|
|
}
|
|
|
|
reqRetainerLock.RUnlock()
|
|
|
|
|
|
|
|
// Queue deregistration
|
|
|
|
defer func() {
|
|
|
|
reqDistributorsLock.Lock()
|
|
|
|
defer reqDistributorsLock.Unlock()
|
|
|
|
|
|
|
|
delete(reqDistributors[mapID], sockID)
|
|
|
|
close(updates)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Open socket
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2023-10-15 18:04:22 +00:00
|
|
|
logrus.WithError(err).Debug("opening websocket")
|
2020-01-06 20:57:17 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-15 18:04:22 +00:00
|
|
|
defer func() {
|
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing socket connection (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2020-01-06 20:57:17 +00:00
|
|
|
|
|
|
|
// Send updates
|
|
|
|
for pos := range updates {
|
|
|
|
if err = conn.WriteJSON(pos); err != nil {
|
2023-10-15 18:04:22 +00:00
|
|
|
logrus.WithError(err).Debug("sending position")
|
2020-01-06 20:57:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMapSubmit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
pos position
|
|
|
|
vars = mux.Vars(r)
|
|
|
|
mapID = vars["mapID"]
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&pos); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-06 23:18:48 +00:00
|
|
|
pos.Time = time.Now()
|
|
|
|
|
2020-01-06 23:20:51 +00:00
|
|
|
reqRetainerLock.Lock()
|
2020-01-06 20:57:17 +00:00
|
|
|
if pos.Retained {
|
|
|
|
reqRetainer[mapID] = pos
|
2020-01-06 23:20:51 +00:00
|
|
|
} else {
|
|
|
|
delete(reqRetainer, mapID)
|
2020-01-06 20:57:17 +00:00
|
|
|
}
|
2020-01-06 23:20:51 +00:00
|
|
|
reqRetainerLock.Unlock()
|
2020-01-06 20:57:17 +00:00
|
|
|
|
2020-01-06 23:49:40 +00:00
|
|
|
go func() {
|
|
|
|
if err := retainState(); err != nil {
|
2023-10-15 18:04:22 +00:00
|
|
|
logrus.WithError(err).Error("retaining state to disk")
|
2020-01-06 23:49:40 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-01-06 20:57:17 +00:00
|
|
|
reqDistributorsLock.RLock()
|
|
|
|
defer reqDistributorsLock.RUnlock()
|
|
|
|
|
|
|
|
distributors, ok := reqDistributors[mapID]
|
|
|
|
if !ok || len(distributors) == 0 {
|
|
|
|
// No subscribers at all
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range distributors {
|
|
|
|
c <- pos
|
|
|
|
}
|
|
|
|
}
|