-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (98 loc) · 3.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2018 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Wrapper script for running a Cheesy Arena remote display that could be either on the field network or remote.
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
)
const (
displayIdFilePath = "/boot/display_id"
localServerUrl = "http://10.0.100.5:8080/display?displayId="
remoteServerUrl = "https://cheesyarena.com/display?displayId="
httpTimeout = 5 * time.Second
pollPeriod = 5 * time.Second
)
// Main entry point for the application.
func main() {
// Log both to file and to stdout.
logFile, err := os.OpenFile("cheesy-arena-rpi.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
log.Fatalln(err)
}
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
var displayId, serverUrl string
for {
// Loop until either the local or remote path to the Cheesy Arena server works.
if displayId = tryGetDisplayId(localServerUrl); displayId != "" {
serverUrl = localServerUrl
break
}
if displayId = tryGetDisplayId(remoteServerUrl); displayId != "" {
serverUrl = remoteServerUrl
break
}
log.Printf("Unsuccessful at connecting; waiting %v before trying again.", pollPeriod)
time.Sleep(pollPeriod)
}
// Try to read the stored display ID if it exists.
if displayIdBytes, _ := ioutil.ReadFile(displayIdFilePath); len(displayIdBytes) > 0 {
displayId = strings.TrimSpace(string(displayIdBytes))
log.Printf("Using existing stored display ID '%s'.", displayId)
} else {
log.Printf("Using new display ID '%s'.", displayId)
}
var browserCommand *exec.Cmd
switch runtime.GOOS {
case "darwin":
browserCommand = exec.Command("open", "-a", "Google Chrome", "--args", "--start-fullscreen",
fmt.Sprintf("--app=%s%s", serverUrl, displayId))
case "linux":
browserCommand = exec.Command("chromium-browser", "--start-fullscreen",
fmt.Sprintf("--app=%s%s", serverUrl, displayId))
default:
log.Fatalf("Don't know how to launch browser for unsupported operating system '%s'.", runtime.GOOS)
}
// Shell out to launch a browser window for the display.
if err := browserCommand.Run(); err != nil {
log.Fatalln(err)
}
}
// Attempts to connect to the given Cheesy Arena server endpoint. Returns the suggested new display ID from the redirect
// response if successful, or the empty string on failure.
func tryGetDisplayId(url string) string {
log.Printf("Checking %s for a connection to the Cheesy Arena server.", url)
httpClient := &http.Client{
Timeout: httpTimeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return ""
}
response, err := httpClient.Do(request)
if err != nil {
return ""
}
// Check for the expected redirect response and extract the suggested display ID.
if response.StatusCode == 302 {
displayIdRe := regexp.MustCompile("displayId=(\\d+)")
if matches := displayIdRe.FindStringSubmatch(response.Header.Get("Location")); len(matches) > 0 {
log.Printf("Successfully connected to %s with suggested display ID '%s'.", url, matches[1])
return matches[1]
}
}
return ""
}