/
hub.go
182 lines (156 loc) · 4.99 KB
/
hub.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
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
package pkg
import (
"log"
"github.com/gorilla/websocket"
)
// HubError is an enum of errors that can cause the hub to stop managing.
// Unexpected errors will come with no HubError attached.
type HubError int
const (
ErrConnectionGoingAway HubError = 1
ErrCanceled HubError = 2
)
// Error implements the error interface for HubError
func (e HubError) Error() string {
switch e {
case ErrConnectionGoingAway:
return "connection going away"
case ErrCanceled:
return "canceled"
default:
return "unknown error"
}
}
// GameConstructor describes something which can initialize games from
// a sendQueue, where the sendQueue is a channel that client packets can
// be sent to be forwarded to the server.
type GameConstructor func(sendQueue chan interface{}) Game
// Hub manages a lobby socket connection in order to detect and handle
// game notifications by connecting to the server and then initializing
// a game with a given GameConstructor, then managing the connection
// to use that Game.
type Hub struct {
lobbySocketConn *Conn
lobbySocketRecvQueue chan ReceivedMessage
lobbySocketClosedChan chan string
gameConstructor GameConstructor
gameHubsByUID map[string]*GameHub
gameFinishedQueue chan string
cancelChan chan struct{}
welcomeMsg map[string]interface{}
}
// NewHub initializes a hub that will take over the given lobby socket
// connection. Upon a receiving a notification about a new game this
// will handle connecting to the server and managing the websocket in
// order to run the Game produced by the given GameConstructor. The
// welcomeMsg should be the first packet received on the lobby connection
// and is used exclusively for debugging.
func NewHub(lobbyConn *websocket.Conn, welcomeMsg map[string]interface{}, gameConstructor GameConstructor) *Hub {
recvQueue := make(chan ReceivedMessage, 64)
closedChan := make(chan string, 1)
return &Hub{
lobbySocketConn: NewConn(lobbyConn, "ls", recvQueue, closedChan),
lobbySocketRecvQueue: recvQueue,
lobbySocketClosedChan: closedChan,
gameConstructor: gameConstructor,
gameHubsByUID: make(map[string]*GameHub),
gameFinishedQueue: make(chan string, 16),
cancelChan: make(chan struct{}, 1),
welcomeMsg: welcomeMsg,
}
}
// Manage the hub forever or until we are disconnected from the lobby or
// Cancel'd. This cannot be run in multiple routines simultaneously.
func (h *Hub) Manage() error {
var manageEndReason error
log.Printf("Hub manage loop started..")
manageLoop:
for {
select {
case msg := <-h.lobbySocketRecvQueue:
log.Printf("Notification from lobby-socket server: %v", msg.Message)
typeRaw, found := msg.Message["type"]
if !found {
log.Printf("Ignoring notification (missing type)")
break
}
typeStr, ok := typeRaw.(string)
if !ok {
log.Printf("Ignoring notification (type not a string)")
break
}
switch typeStr {
case "match-available":
manageEndReason = h.handleMatchAvailable(msg)
if manageEndReason != nil {
break manageLoop
}
default:
log.Printf("Ignoring notification (unknown type: %s)", typeStr)
}
case gameUID := <-h.gameFinishedQueue:
log.Printf("Game finished: %s", gameUID)
delete(h.gameHubsByUID, gameUID)
case <-h.lobbySocketClosedChan:
manageEndReason = ErrConnectionGoingAway
break manageLoop
case <-h.cancelChan:
manageEndReason = ErrCanceled
break manageLoop
}
}
for _, gh := range h.gameHubsByUID {
gh.Close()
}
// this avoids the game finished channel filling up and gives a chance
// for the game hubs to actually finish
for len(h.gameHubsByUID) > 0 {
gameUID := <-h.gameFinishedQueue
log.Printf("Game finished: %s", gameUID)
delete(h.gameHubsByUID, gameUID)
}
return manageEndReason
}
func (h *Hub) handleMatchAvailable(msg ReceivedMessage) error {
urlRaw, found := msg.Message["url"]
if !found {
log.Printf("Ignoring notification (missing URL)")
return nil
}
url, ok := urlRaw.(string)
if !ok {
log.Printf("Ignoring notification (url not a string)")
return nil
}
var jwtRaw interface{}
jwtRaw, found = msg.Message["jwt"]
if !found {
log.Printf("Ignoring notification (missing jwt)")
return nil
}
var jwt string
jwt, ok = jwtRaw.(string)
if !ok {
log.Printf("Ignoring notification (jwt not a string)")
return nil
}
gconn, err := ConnectGame(url, jwt)
if err != nil {
log.Printf("Failed to connect to %s: %v", url, err)
return nil
}
uid := generateSecureToken(23)
gh := NewGameHub(gconn, uid, h.gameFinishedQueue, h.gameConstructor)
h.gameHubsByUID[uid] = gh
go gh.Manage()
log.Printf("Assigned match the uid %s", uid)
return nil
}
// Cancels the hub. This may be called from any goroutine and, if Manage
// is being run, it will stop and return ErrCanceled
func (h *Hub) Cancel() {
select {
case h.cancelChan <- struct{}{}:
default:
}
}