mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 05:36:45 -04:00
Various improvements to display configuration.
This commit is contained in:
@@ -58,6 +58,7 @@ type Display struct {
|
||||
Nickname string
|
||||
Type DisplayType
|
||||
Configuration map[string]string
|
||||
IpAddress string
|
||||
ConnectionCount int
|
||||
}
|
||||
|
||||
@@ -140,12 +141,18 @@ func (arena *Arena) RegisterDisplay(display *Display) {
|
||||
defer displayRegistryMutex.Unlock()
|
||||
|
||||
existingDisplay, ok := arena.Displays[display.Id]
|
||||
if ok {
|
||||
display.ConnectionCount = existingDisplay.ConnectionCount + 1
|
||||
if ok && display.Type == PlaceholderDisplay && existingDisplay.Type != PlaceholderDisplay {
|
||||
// Don't rewrite the registered configuration if the new one is a placeholder -- if it is reconnecting after a
|
||||
// restart, it should adopt the existing configuration.
|
||||
arena.Displays[display.Id].ConnectionCount++
|
||||
} else {
|
||||
display.ConnectionCount = 1
|
||||
if ok {
|
||||
display.ConnectionCount = existingDisplay.ConnectionCount + 1
|
||||
} else {
|
||||
display.ConnectionCount = 1
|
||||
}
|
||||
arena.Displays[display.Id] = display
|
||||
}
|
||||
arena.Displays[display.Id] = display
|
||||
arena.DisplayConfigurationNotifier.Notify()
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th># Connected</th>
|
||||
<th>IP Address</th>
|
||||
<th>Nickname</th>
|
||||
<th>Type</th>
|
||||
<th>Configuration</th>
|
||||
@@ -32,6 +33,7 @@
|
||||
<tr>
|
||||
<td>{{"{{Id}}"}}</td>
|
||||
<td>{{"{{ConnectionCount}}"}}</td>
|
||||
<td>{{"{{IpAddress}}"}}</td>
|
||||
<td><input type="text" id="displayNickname{{"{{Id}}"}}" size="30" /></td>
|
||||
<td>
|
||||
<select id="displayType{{"{{Id}}"}}">
|
||||
|
||||
@@ -21,15 +21,16 @@ server {
|
||||
proxy_pass http://localhost:9080/api/;
|
||||
}
|
||||
|
||||
location ~ ^/display.*/websocket$ {
|
||||
location ~ ^(/setup)?/display.*/websocket$ {
|
||||
proxy_set_header Host $host;
|
||||
proxy_pass http://localhost:9080$request_uri;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
|
||||
location ~ ^/display {
|
||||
location ~ ^(/setup)?/display {
|
||||
proxy_pass http://localhost:9080$request_uri;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/Team254/cheesy-arena/field"
|
||||
"github.com/Team254/cheesy-arena/game"
|
||||
"github.com/Team254/cheesy-arena/model"
|
||||
"github.com/Team254/cheesy-arena/websocket"
|
||||
@@ -46,12 +45,11 @@ func (web *Web) allianceStationDisplayWebsocketHandler(w http.ResponseWriter, r
|
||||
return
|
||||
}
|
||||
|
||||
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
|
||||
display, err := web.registerDisplay(r)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
web.arena.RegisterDisplay(display)
|
||||
defer web.arena.MarkDisplayDisconnected(display)
|
||||
|
||||
ws, err := websocket.NewWebsocket(w, r)
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/Team254/cheesy-arena/field"
|
||||
"github.com/Team254/cheesy-arena/game"
|
||||
"github.com/Team254/cheesy-arena/model"
|
||||
"github.com/Team254/cheesy-arena/websocket"
|
||||
@@ -46,12 +45,11 @@ func (web *Web) announcerDisplayWebsocketHandler(w http.ResponseWriter, r *http.
|
||||
return
|
||||
}
|
||||
|
||||
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
|
||||
display, err := web.registerDisplay(r)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
web.arena.RegisterDisplay(display)
|
||||
defer web.arena.MarkDisplayDisconnected(display)
|
||||
|
||||
ws, err := websocket.NewWebsocket(w, r)
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/Team254/cheesy-arena/field"
|
||||
"github.com/Team254/cheesy-arena/game"
|
||||
"github.com/Team254/cheesy-arena/model"
|
||||
"github.com/Team254/cheesy-arena/websocket"
|
||||
@@ -46,12 +45,11 @@ func (web *Web) audienceDisplayWebsocketHandler(w http.ResponseWriter, r *http.R
|
||||
return
|
||||
}
|
||||
|
||||
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
|
||||
display, err := web.registerDisplay(r)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
web.arena.RegisterDisplay(display)
|
||||
defer web.arena.MarkDisplayDisconnected(display)
|
||||
|
||||
ws, err := websocket.NewWebsocket(w, r)
|
||||
|
||||
@@ -7,8 +7,10 @@ package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Team254/cheesy-arena/field"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -47,3 +49,21 @@ func (web *Web) enforceDisplayConfiguration(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
return allPresent
|
||||
}
|
||||
|
||||
// Constructs, registers, and returns the display object for the given incoming websocket request.
|
||||
func (web *Web) registerDisplay(r *http.Request) (*field.Display, error) {
|
||||
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Extract the source IP address of the request and store it in the display object.
|
||||
if ipAddress := r.Header.Get("X-Real-IP"); ipAddress != "" {
|
||||
display.IpAddress = ipAddress
|
||||
} else {
|
||||
display.IpAddress = regexp.MustCompile("(.*):\\d+$").FindStringSubmatch(r.RemoteAddr)[1]
|
||||
}
|
||||
|
||||
web.arena.RegisterDisplay(display)
|
||||
return display, nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/Team254/cheesy-arena/field"
|
||||
"github.com/Team254/cheesy-arena/model"
|
||||
"github.com/Team254/cheesy-arena/websocket"
|
||||
"net/http"
|
||||
@@ -43,12 +42,11 @@ func (web *Web) ftaDisplayWebsocketHandler(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
|
||||
display, err := web.registerDisplay(r)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
web.arena.RegisterDisplay(display)
|
||||
defer web.arena.MarkDisplayDisconnected(display)
|
||||
|
||||
ws, err := websocket.NewWebsocket(w, r)
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/Team254/cheesy-arena/field"
|
||||
"github.com/Team254/cheesy-arena/model"
|
||||
"github.com/Team254/cheesy-arena/websocket"
|
||||
"net/http"
|
||||
@@ -43,12 +42,11 @@ func (web *Web) pitDisplayWebsocketHandler(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
|
||||
display, err := web.registerDisplay(r)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
web.arena.RegisterDisplay(display)
|
||||
defer web.arena.MarkDisplayDisconnected(display)
|
||||
|
||||
ws, err := websocket.NewWebsocket(w, r)
|
||||
|
||||
@@ -6,8 +6,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Team254/cheesy-arena/field"
|
||||
"github.com/Team254/cheesy-arena/model"
|
||||
"github.com/Team254/cheesy-arena/websocket"
|
||||
"net/http"
|
||||
@@ -19,10 +17,7 @@ func (web *Web) placeholderDisplayHandler(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
|
||||
// Generate a display ID and redirect if the client doesn't already have one.
|
||||
displayId := r.URL.Query().Get("displayId")
|
||||
if displayId == "" {
|
||||
http.Redirect(w, r, fmt.Sprintf(r.URL.Path+"?displayId=%s", web.arena.NextDisplayId()), 302)
|
||||
if !web.enforceDisplayConfiguration(w, r, nil) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -47,12 +42,11 @@ func (web *Web) placeholderDisplayWebsocketHandler(w http.ResponseWriter, r *htt
|
||||
return
|
||||
}
|
||||
|
||||
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
|
||||
display, err := web.registerDisplay(r)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
web.arena.RegisterDisplay(display)
|
||||
defer web.arena.MarkDisplayDisconnected(display)
|
||||
|
||||
ws, err := websocket.NewWebsocket(w, r)
|
||||
|
||||
@@ -43,9 +43,9 @@ func TestSetupDisplaysWebsocket(t *testing.T) {
|
||||
"/displays/alliance_station/websocket?displayId=2&station=R2", nil)
|
||||
defer displayConn2.Close()
|
||||
expectedDisplay1 := &field.Display{Id: "1", Type: field.PlaceholderDisplay, Configuration: map[string]string{},
|
||||
ConnectionCount: 1}
|
||||
ConnectionCount: 1, IpAddress: "127.0.0.1"}
|
||||
expectedDisplay2 := &field.Display{Id: "2", Type: field.AllianceStationDisplay,
|
||||
Configuration: map[string]string{"station": "R2"}, ConnectionCount: 1}
|
||||
Configuration: map[string]string{"station": "R2"}, ConnectionCount: 1, IpAddress: "127.0.0.1"}
|
||||
message = readDisplayConfiguration(t, ws)
|
||||
if assert.Equal(t, 2, len(message.Displays)) {
|
||||
assert.Equal(t, expectedDisplay1, message.Displays["1"])
|
||||
|
||||
Reference in New Issue
Block a user