Skip to content

Commit 6540d79

Browse files
committed
Various improvements to display configuration.
1 parent c5955fb commit 6540d79

11 files changed

+45
-31
lines changed

field/display.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Display struct {
5858
Nickname string
5959
Type DisplayType
6060
Configuration map[string]string
61+
IpAddress string
6162
ConnectionCount int
6263
}
6364

@@ -140,12 +141,18 @@ func (arena *Arena) RegisterDisplay(display *Display) {
140141
defer displayRegistryMutex.Unlock()
141142

142143
existingDisplay, ok := arena.Displays[display.Id]
143-
if ok {
144-
display.ConnectionCount = existingDisplay.ConnectionCount + 1
144+
if ok && display.Type == PlaceholderDisplay && existingDisplay.Type != PlaceholderDisplay {
145+
// Don't rewrite the registered configuration if the new one is a placeholder -- if it is reconnecting after a
146+
// restart, it should adopt the existing configuration.
147+
arena.Displays[display.Id].ConnectionCount++
145148
} else {
146-
display.ConnectionCount = 1
149+
if ok {
150+
display.ConnectionCount = existingDisplay.ConnectionCount + 1
151+
} else {
152+
display.ConnectionCount = 1
153+
}
154+
arena.Displays[display.Id] = display
147155
}
148-
arena.Displays[display.Id] = display
149156
arena.DisplayConfigurationNotifier.Notify()
150157
}
151158

templates/setup_displays.html

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<tr>
1515
<th>ID</th>
1616
<th># Connected</th>
17+
<th>IP Address</th>
1718
<th>Nickname</th>
1819
<th>Type</th>
1920
<th>Configuration</th>
@@ -32,6 +33,7 @@
3233
<tr>
3334
<td>{{"{{Id}}"}}</td>
3435
<td>{{"{{ConnectionCount}}"}}</td>
36+
<td>{{"{{IpAddress}}"}}</td>
3537
<td><input type="text" id="displayNickname{{"{{Id}}"}}" size="30" /></td>
3638
<td>
3739
<select id="displayType{{"{{Id}}"}}">

tunnel_nginx_config

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ server {
2121
proxy_pass http://localhost:9080/api/;
2222
}
2323

24-
location ~ ^/display.*/websocket$ {
24+
location ~ ^(/setup)?/display.*/websocket$ {
2525
proxy_set_header Host $host;
2626
proxy_pass http://localhost:9080$request_uri;
2727
proxy_http_version 1.1;
2828
proxy_set_header Upgrade $http_upgrade;
2929
proxy_set_header Connection "upgrade";
30+
proxy_set_header X-Real-IP $remote_addr;
3031
}
3132

32-
location ~ ^/display {
33+
location ~ ^(/setup)?/display {
3334
proxy_pass http://localhost:9080$request_uri;
3435
}
3536

web/alliance_station_display.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package web
77

88
import (
9-
"github.com/Team254/cheesy-arena/field"
109
"github.com/Team254/cheesy-arena/game"
1110
"github.com/Team254/cheesy-arena/model"
1211
"github.com/Team254/cheesy-arena/websocket"
@@ -46,12 +45,11 @@ func (web *Web) allianceStationDisplayWebsocketHandler(w http.ResponseWriter, r
4645
return
4746
}
4847

49-
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
48+
display, err := web.registerDisplay(r)
5049
if err != nil {
5150
handleWebErr(w, err)
5251
return
5352
}
54-
web.arena.RegisterDisplay(display)
5553
defer web.arena.MarkDisplayDisconnected(display)
5654

5755
ws, err := websocket.NewWebsocket(w, r)

web/announcer_display.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package web
77

88
import (
9-
"github.com/Team254/cheesy-arena/field"
109
"github.com/Team254/cheesy-arena/game"
1110
"github.com/Team254/cheesy-arena/model"
1211
"github.com/Team254/cheesy-arena/websocket"
@@ -46,12 +45,11 @@ func (web *Web) announcerDisplayWebsocketHandler(w http.ResponseWriter, r *http.
4645
return
4746
}
4847

49-
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
48+
display, err := web.registerDisplay(r)
5049
if err != nil {
5150
handleWebErr(w, err)
5251
return
5352
}
54-
web.arena.RegisterDisplay(display)
5553
defer web.arena.MarkDisplayDisconnected(display)
5654

5755
ws, err := websocket.NewWebsocket(w, r)

web/audience_display.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package web
77

88
import (
9-
"github.com/Team254/cheesy-arena/field"
109
"github.com/Team254/cheesy-arena/game"
1110
"github.com/Team254/cheesy-arena/model"
1211
"github.com/Team254/cheesy-arena/websocket"
@@ -46,12 +45,11 @@ func (web *Web) audienceDisplayWebsocketHandler(w http.ResponseWriter, r *http.R
4645
return
4746
}
4847

49-
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
48+
display, err := web.registerDisplay(r)
5049
if err != nil {
5150
handleWebErr(w, err)
5251
return
5352
}
54-
web.arena.RegisterDisplay(display)
5553
defer web.arena.MarkDisplayDisconnected(display)
5654

5755
ws, err := websocket.NewWebsocket(w, r)

web/display_utils.go

+20
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ package web
77

88
import (
99
"fmt"
10+
"github.com/Team254/cheesy-arena/field"
1011
"net/http"
1112
"net/url"
13+
"regexp"
1214
"strings"
1315
)
1416

@@ -47,3 +49,21 @@ func (web *Web) enforceDisplayConfiguration(w http.ResponseWriter, r *http.Reque
4749
}
4850
return allPresent
4951
}
52+
53+
// Constructs, registers, and returns the display object for the given incoming websocket request.
54+
func (web *Web) registerDisplay(r *http.Request) (*field.Display, error) {
55+
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
// Extract the source IP address of the request and store it in the display object.
61+
if ipAddress := r.Header.Get("X-Real-IP"); ipAddress != "" {
62+
display.IpAddress = ipAddress
63+
} else {
64+
display.IpAddress = regexp.MustCompile("(.*):\\d+$").FindStringSubmatch(r.RemoteAddr)[1]
65+
}
66+
67+
web.arena.RegisterDisplay(display)
68+
return display, nil
69+
}

web/fta_display.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package web
77

88
import (
9-
"github.com/Team254/cheesy-arena/field"
109
"github.com/Team254/cheesy-arena/model"
1110
"github.com/Team254/cheesy-arena/websocket"
1211
"net/http"
@@ -43,12 +42,11 @@ func (web *Web) ftaDisplayWebsocketHandler(w http.ResponseWriter, r *http.Reques
4342
return
4443
}
4544

46-
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
45+
display, err := web.registerDisplay(r)
4746
if err != nil {
4847
handleWebErr(w, err)
4948
return
5049
}
51-
web.arena.RegisterDisplay(display)
5250
defer web.arena.MarkDisplayDisconnected(display)
5351

5452
ws, err := websocket.NewWebsocket(w, r)

web/pit_display.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package web
77

88
import (
9-
"github.com/Team254/cheesy-arena/field"
109
"github.com/Team254/cheesy-arena/model"
1110
"github.com/Team254/cheesy-arena/websocket"
1211
"net/http"
@@ -43,12 +42,11 @@ func (web *Web) pitDisplayWebsocketHandler(w http.ResponseWriter, r *http.Reques
4342
return
4443
}
4544

46-
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
45+
display, err := web.registerDisplay(r)
4746
if err != nil {
4847
handleWebErr(w, err)
4948
return
5049
}
51-
web.arena.RegisterDisplay(display)
5250
defer web.arena.MarkDisplayDisconnected(display)
5351

5452
ws, err := websocket.NewWebsocket(w, r)

web/placeholder_display.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package web
77

88
import (
9-
"fmt"
10-
"github.com/Team254/cheesy-arena/field"
119
"github.com/Team254/cheesy-arena/model"
1210
"github.com/Team254/cheesy-arena/websocket"
1311
"net/http"
@@ -19,10 +17,7 @@ func (web *Web) placeholderDisplayHandler(w http.ResponseWriter, r *http.Request
1917
return
2018
}
2119

22-
// Generate a display ID and redirect if the client doesn't already have one.
23-
displayId := r.URL.Query().Get("displayId")
24-
if displayId == "" {
25-
http.Redirect(w, r, fmt.Sprintf(r.URL.Path+"?displayId=%s", web.arena.NextDisplayId()), 302)
20+
if !web.enforceDisplayConfiguration(w, r, nil) {
2621
return
2722
}
2823

@@ -47,12 +42,11 @@ func (web *Web) placeholderDisplayWebsocketHandler(w http.ResponseWriter, r *htt
4742
return
4843
}
4944

50-
display, err := field.DisplayFromUrl(r.URL.Path, r.URL.Query())
45+
display, err := web.registerDisplay(r)
5146
if err != nil {
5247
handleWebErr(w, err)
5348
return
5449
}
55-
web.arena.RegisterDisplay(display)
5650
defer web.arena.MarkDisplayDisconnected(display)
5751

5852
ws, err := websocket.NewWebsocket(w, r)

web/setup_displays_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func TestSetupDisplaysWebsocket(t *testing.T) {
4343
"/displays/alliance_station/websocket?displayId=2&station=R2", nil)
4444
defer displayConn2.Close()
4545
expectedDisplay1 := &field.Display{Id: "1", Type: field.PlaceholderDisplay, Configuration: map[string]string{},
46-
ConnectionCount: 1}
46+
ConnectionCount: 1, IpAddress: "127.0.0.1"}
4747
expectedDisplay2 := &field.Display{Id: "2", Type: field.AllianceStationDisplay,
48-
Configuration: map[string]string{"station": "R2"}, ConnectionCount: 1}
48+
Configuration: map[string]string{"station": "R2"}, ConnectionCount: 1, IpAddress: "127.0.0.1"}
4949
message = readDisplayConfiguration(t, ws)
5050
if assert.Equal(t, 2, len(message.Displays)) {
5151
assert.Equal(t, expectedDisplay1, message.Displays["1"])

0 commit comments

Comments
 (0)