Repository navigation
Expand file tree
/
Copy pathapp.go
More file actions
258 lines (234 loc) · 8.41 KB
/
Copy pathapp.go
File metadata and controls
258 lines (234 loc) · 8.41 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"HRPMLW/internal/database"
"HRPMLW/internal/interop"
"HRPMLW/internal/repository"
"HRPMLW/internal/services"
"HRPMLW/internal/updater"
"context"
"errors"
"fmt"
"path/filepath"
"sync"
"time"
)
// App struct
//
// ADR-0004: the frontend reaches the backend exclusively through the Wails
// binding methods in bindings.go. There is no frontend-facing HTTP listener;
// the previous embedded Gin server, its routes/middleware, the HTTP client,
// and the SERVER_PORT configuration have all been retired.
//
// The match handoff listener (INTEROP-SPEC.md) is a separate, inter-process
// boundary: a loopback-only HTTP server authenticated by a per-process token
// in a 0600 discovery file. It never serves the frontend and never carries
// credentials.
type App struct {
ctx context.Context
authService *services.AuthService
microsoftService *services.MicrosoftService
bmclService *services.BMCLAPIService
launcherService *services.LauncherService
javaRepo *repository.JavaRepository
tokenStore services.TokenStore
// Install cancellation (PENDING-DECISIONS #10): one cancellable context per
// in-flight download/install, keyed by version id, so CancelInstall can
// abort it. Guarded by installMu.
installMu sync.Mutex
installCancels map[string]*installCancel
updateMu sync.Mutex
updateStatus UpdateStatus
updateCandidate *updater.Update
updateCheckCancel context.CancelFunc
updateInstallCancel context.CancelFunc
updateCheckID uint64
updateInstalling bool
// Match handoff state (INTEROP-SPEC §6): single pending slot plus the
// loopback server publishing it.
interopServer *interop.Server
handoffMu sync.Mutex
pendingHandoff *interop.HandoffPayload
started bool
deepLink *interop.DeepLink
}
// installCancel pairs a running install's cancel func with an identity the
// cleanup path can compare, so a finished install never deletes a newer
// install registered under the same version id.
type installCancel struct {
cancel context.CancelFunc
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
authService: services.NewAuthService(),
microsoftService: services.NewMicrosoftService(),
bmclService: services.NewBMCLAPIService(),
launcherService: services.NewLauncherService(),
javaRepo: repository.NewJavaRepository(),
tokenStore: services.NewFallbackTokenStore(
services.NewKeychainTokenStore(),
services.NewFileTokenStore(filepath.Join(database.DataDir(), "tokens.json")),
),
installCancels: map[string]*installCancel{},
updateStatus: UpdateStatus{State: "idle"},
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Initialize database. Data lives next to the executable (ADR-0010);
// adopt any pre-portability ~/.hrpml data before opening the new path.
database.AdoptLegacyData(database.DataDir())
if err := database.Initialize(database.DefaultConfig()); err != nil {
fmt.Printf("Failed to initialize database: %v\n", err)
}
a.startInterop()
// A deep link received before startup completed (argv of this process)
// is acted on now that the listener exists.
a.handoffMu.Lock()
link := a.deepLink
a.handoffMu.Unlock()
if link != nil {
a.followDeepLink(link)
}
a.startUpdateCheck()
}
// startInterop brings up the loopback handoff listener and publishes the
// discovery file. Every failure is logged and non-fatal: the launcher works
// without the handoff feature.
func (a *App) startInterop() {
if err := interop.RegisterDeepLinkHandler(); err != nil {
fmt.Printf("[interop] deep-link registration: %v\n", err)
}
srv, err := interop.NewServer(interop.AppReceiver, a.acceptHandoff, func(format string, args ...interface{}) {
fmt.Printf("[interop] "+format+"\n", args...)
})
if err != nil {
fmt.Printf("[interop] create listener: %v\n", err)
return
}
if err := srv.Start(); err != nil {
fmt.Printf("[interop] start listener: %v\n", err)
return
}
a.interopServer = srv
if _, err := interop.WriteDiscovery(interop.AppReceiver, srv.Port(), srv.Token()); err != nil {
fmt.Printf("[interop] write discovery file: %v\n", err)
} else {
fmt.Printf("[interop] handoff listener on 127.0.0.1:%d\n", srv.Port())
}
a.handoffMu.Lock()
a.started = true
a.handoffMu.Unlock()
}
// shutdown is called when the app is closing
func (a *App) shutdown(ctx context.Context) {
if a.interopServer != nil {
a.interopServer.Stop()
}
// Close database connection
if err := database.Close(); err != nil {
fmt.Printf("Failed to close database: %v\n", err)
}
}
// HandleDeepLinkArg processes one matchcore:// URL arriving from argv (this
// process or a second instance redirected through the single-instance lock).
// The deep link is an invocation hint only; the real parameters are pulled
// from the sender over the loopback channel (INTEROP-SPEC §7 flow B).
func (a *App) HandleDeepLinkArg(raw string) {
if raw == "" {
return
}
link, err := interop.ParseDeepLink(raw)
if err != nil {
fmt.Printf("[interop] ignoring invalid deep link: %v\n", err)
return
}
a.handoffMu.Lock()
if link.Version != interop.ProtocolVersion {
a.handoffMu.Unlock()
a.emit("interop:handoff", map[string]interface{}{
"status": "version_mismatch",
"message": fmt.Sprintf("Match handoff protocol version %d is not supported by this SamuelClient (supported: %d). Please update the out-of-date app.", link.Version, interop.ProtocolVersion),
})
return
}
if a.started {
a.handoffMu.Unlock()
a.followDeepLink(link)
return
}
a.deepLink = link
a.handoffMu.Unlock()
}
// followDeepLink pulls the pending handoff from the sender for up to 30 s
// (the sender may still be publishing its discovery file). Outcomes are
// pushed to the frontend as interop:handoff events; nothing is dropped
// silently.
func (a *App) followDeepLink(link *interop.DeepLink) {
go func() {
deadline := time.Now().Add(30 * time.Second)
for time.Now().Before(deadline) {
doc, _ := interop.LoadLiveSenderDiscovery()
if doc != nil {
if doc.ProtocolVersion != interop.ProtocolVersion {
a.emit("interop:handoff", map[string]interface{}{
"status": "version_mismatch",
"message": fmt.Sprintf("The matchmaking client speaks protocol version %d, this SamuelClient speaks %d. Please update the out-of-date app.", doc.ProtocolVersion, interop.ProtocolVersion),
})
return
}
payload, err := interop.FetchPendingHandoff(doc, link.HandoffID, 3*time.Second)
if err == nil {
if verr := payload.Validate(time.Now()); verr != nil {
fmt.Printf("[interop] pulled handoff failed validation: %v\n", verr)
a.emit("interop:handoff", map[string]interface{}{
"status": "invalid",
"message": "The match handoff from the matchmaking client is malformed and was discarded.",
})
return
}
fmt.Printf("[interop] handoff pulled from sender: %s match=%s target=%s:%d\n",
payload.HandoffID, payload.Match.MatchID, payload.Match.JoinTarget.Host, payload.Match.JoinTarget.Port)
if aerr := a.acceptHandoff(payload); aerr != nil {
// A push arrived first (slot busy) — that copy wins.
fmt.Printf("[interop] pull skipped: %v\n", aerr)
}
return
}
if errors.Is(err, interop.ErrHandoffGone) {
a.emit("interop:handoff", map[string]interface{}{
"status": "gone",
"message": "The matchmaking client no longer holds this match handoff (already joined or expired).",
})
return
}
fmt.Printf("[interop] pull attempt failed: %v\n", err)
}
time.Sleep(2 * time.Second)
}
a.emit("interop:handoff", map[string]interface{}{
"status": "pull_timeout",
"handoff_id": link.HandoffID,
"message": "Could not fetch the match details from the matchmaking client. Return to it and retry, or keep this launcher open and try again.",
})
}()
}
// acceptHandoff is the interop.Server accept hook: it fills the single
// pending slot and notifies the frontend. The payload carries no credentials
// by protocol design, so it is forwarded to the UI as-is.
func (a *App) acceptHandoff(payload *interop.HandoffPayload) error {
a.handoffMu.Lock()
if a.pendingHandoff != nil {
a.handoffMu.Unlock()
return interop.ErrSlotBusy
}
a.pendingHandoff = payload
a.handoffMu.Unlock()
a.emit("interop:handoff", map[string]interface{}{
"status": "pending",
"handoff": payload,
})
return nil
}