Refactor WIP

This commit is contained in:
giongto35 2019-05-01 15:36:21 +08:00
parent 7b2b9d034b
commit 142b9d7bb4
6 changed files with 83 additions and 64 deletions

View file

@ -82,11 +82,13 @@ func main() {
log.Println("Running as overlord ")
initilizeOverlord()
IsOverlord = true
initilizeOverlord()
} else {
if strings.HasPrefix(*config.OverlordHost, "ws") && !strings.HasSuffix(*config.OverlordHost, "wso") {
log.Fatal("Overlord connection is invalid. Should have the form `ws://.../wso`")
}
log.Println("Running as slave ")
IsOverlord = false
initializeServer()
}
}

View file

@ -25,17 +25,13 @@ func initOverlord() *httptest.Server {
return overlord
}
func initServer(t *testing.T, oclient *handler.OverlordClient) *httptest.Server {
conn := connectTestOverlordServer()
handler, err := handler.NewHandler(oclient)
if err != nil {
t.Fatalf("%v", err)
}
func initServer(t *testing.T, oconn *websocket.Conn) *httptest.Server {
handler := handler.NewHandler(oconn)
server := httptest.NewServer(http.HandlerFunc(handler.WS))
return server
}
func connectTestOverlordServer(t *testing.T, overlordURL string) *handler.OverlordClient {
func connectTestOverlordServer(t *testing.T, overlordURL string) *websocket.Conn {
if overlordURL == "" {
return nil
} else {
@ -47,7 +43,6 @@ func connectTestOverlordServer(t *testing.T, overlordURL string) *handler.Overlo
if err != nil {
t.Fatalf("%v", err)
}
defer oconn.Close()
return oconn
}
@ -142,7 +137,7 @@ func TestSingleServerOneOverlord(t *testing.T) {
o := initOverlord()
defer o.Close()
oconn := connectOverlord(t, o.URL)
oconn := connectTestOverlordServer(t, o.URL)
// Init slave server
s := initServer(t, oconn)
defer s.Close()

View file

@ -6,20 +6,17 @@ import (
"github.com/giongto35/cloud-game/config"
"github.com/giongto35/cloud-game/cws"
"github.com/giongto35/cloud-game/handler/gamelist"
"github.com/giongto35/cloud-game/webrtc"
"github.com/gorilla/websocket"
pionRTC "github.com/pion/webrtc"
uuid "github.com/satori/go.uuid"
)
type BrowserClient struct {
*cws.Client
session *Session
oclient *OverlordClient
gameName string
roomID string
playerIndex int
//session *Session
//oclient *OverlordClient
//gameName string
//roomID string
//playerIndex int
}
func (s *Session) RegisterBrowserClient() {
@ -39,7 +36,7 @@ func (s *Session) RegisterBrowserClient() {
return cws.WSPacket{
ID: "sdp",
Data: localSession,
SessionID: s.SessionID,
SessionID: s.ID,
}
})
@ -80,35 +77,34 @@ func (s *Session) RegisterBrowserClient() {
//})
browserClient.Receive("start", func(resp cws.WSPacket) (req cws.WSPacket) {
gameName = resp.Data
roomID = resp.RoomID
playerIndex = resp.PlayerIndex
s.GameName = resp.Data
s.RoomID = resp.RoomID
s.PlayerIndex = resp.PlayerIndex
isNewRoom := false
log.Println("Starting game")
// If we are connecting to overlord, request serverID from roomID
if browserClient.oclient != nil {
session := browserClient.session
roomServerID := getServerIDOfRoom(session.OverlordClient, roomID)
log.Println("Server of RoomID ", roomID, " is ", roomServerID)
if roomServerID != "" && wssession.ServerID != roomServerID {
if s.OverlordClient != nil {
roomServerID := getServerIDOfRoom(s.OverlordClient, s.RoomID)
log.Println("Server of RoomID ", s.RoomID, " is ", s.RoomID)
if roomServerID != "" && s.ServerID != roomServerID {
// TODO: Re -register
go bridgeConnection(wssession, roomServerID, gameName, roomID, playerIndex)
go s.bridgeConnection(roomServerID, s.GameName, s.RoomID, s.PlayerIndex)
return
}
}
roomID, isNewRoom = startSession(wssession.peerconnection, gameName, roomID, playerIndex)
s.RoomID, isNewRoom = startSession(s.peerconnection, s.GameName, s.RoomID, s.PlayerIndex)
// Register room to overlord if we are connecting to overlord
if isNewRoom && browserClient.session.OverlordClient != nil {
browserClient.session.OverlordClient.Send(cws.WSPacket{
if isNewRoom && s.OverlordClient != nil {
s.OverlordClient.Send(cws.WSPacket{
ID: "registerRoom",
Data: roomID,
Data: s.RoomID,
}, nil)
}
req.ID = "start"
req.RoomID = roomID
req.SessionID = sessionID
req.RoomID = s.RoomID
req.SessionID = s.ID
return req
})
@ -130,31 +126,27 @@ func (s *Session) RegisterBrowserClient() {
}
// NewOverlordClient returns a client connecting to browser. This connection exchanges information between clients and server
func NewBrowserClient(c *websocket.Conn, overlordClient *OverlordClient) *BrowserClient {
roomID := ""
gameName := ""
playerIndex := 0
func NewBrowserClient(c *websocket.Conn) *BrowserClient {
//roomID := ""
//gameName := ""
//playerIndex := 0
// Create connection to overlord
browserClient := &BrowserClient{
Client: cws.NewClient(c),
gameName: "",
roomID: "",
playerIndex: 0,
Client: cws.NewClient(c),
//gameName: "",
//roomID: "",
//playerIndex: 0,
}
//sessionID := strconv.Itoa(rand.Int())
sessionID := uuid.Must(uuid.NewV4()).String()
//sessionID := uuid.Must(uuid.NewV4()).String()
wssession := &Session{
BrowserClient: browserClient,
OverlordClient: overlordClient,
peerconnection: webrtc.NewWebRTC(),
// The server session is maintaining
}
//wssession := &Session{
//BrowserClient: browserClient,
//OverlordClient: overlordClient,
//peerconnection: webrtc.NewWebRTC(),
//// The server session is maintaining
//}
browserClient.Send(cws.WSPacket{
ID: "gamelist",
Data: gamelist.GetEncodedGameList(),
}, nil)
return browserClient
}

View file

@ -1,6 +1,7 @@
package handler
import (
"fmt"
"io/ioutil"
"log"
"net/http"
@ -9,6 +10,7 @@ import (
"github.com/giongto35/cloud-game/config"
"github.com/giongto35/cloud-game/webrtc"
"github.com/gorilla/websocket"
uuid "github.com/satori/go.uuid"
)
const (
@ -18,7 +20,7 @@ const (
debugIndex = "./static/index_ws.html"
)
var indexFN = gameboyIndex
var indexFN = debugIndex
// Time allowed to write a message to the peer.
var readWait = 30 * time.Second
@ -71,6 +73,27 @@ func (h *Handler) WS(w http.ResponseWriter, r *http.Request) {
log.Print("[!] WS upgrade:", err)
return
}
client := NewBrowserClient(c)
//client := NewClient(c)
////sessionID := strconv.Itoa(rand.Int())
sessionID := uuid.Must(uuid.NewV4()).String()
wssession := &Session{
ID: sessionID,
BrowserClient: client,
OverlordClient: h.oClient,
peerconnection: webrtc.NewWebRTC(),
}
wssession.RegisterBrowserClient()
fmt.Println("oclient : ", h.oClient)
if wssession.OverlordClient != nil {
wssession.RegisterOverlordClient()
go wssession.OverlordClient.Heartbeat()
go wssession.OverlordClient.Listen()
}
wssession.BrowserClient.Listen()
defer c.Close()
//var gameName string
//var roomID string
@ -191,8 +214,7 @@ func (h *Handler) WS(w http.ResponseWriter, r *http.Request) {
//return req
//})
client := NewBrowserClient(c, oclient)
client.Listen()
//client.Listen()
}
func createOverlordConnection() (*websocket.Conn, error) {

View file

@ -11,17 +11,23 @@ import (
type OverlordClient struct {
*cws.Client
peerconnections map[string]*webrtc.WebRTC
}
// NewOverlordClient returns a client connecting to overlord for coordiation between different server
func NewOverlordClient(oc *websocket.Conn) *OverlordClient {
if oc == nil {
return nil
}
oclient := &OverlordClient{
Client: cws.NewClient(oc),
}
return oclient
}
func (h *Handler) RegisterOverlordClient(oclient *OverlordClient) {
func (s *Session) RegisterOverlordClient() {
oclient := s.OverlordClient
// Received from overlord the serverID
oclient.Receive(
@ -29,7 +35,7 @@ func (h *Handler) RegisterOverlordClient(oclient *OverlordClient) {
func(response cws.WSPacket) (request cws.WSPacket) {
// Stick session with serverID got from overlord
log.Println("Received serverID ", response.Data)
h.serverID = response.Data
s.ServerID = response.Data
return cws.EmptyPacket
},
@ -45,7 +51,7 @@ func (h *Handler) RegisterOverlordClient(oclient *OverlordClient) {
peerconnection := webrtc.NewWebRTC()
// init new peerconnection from sessionID
localSession, err := peerconnection.StartClient(resp.Data, config.Width, config.Height)
h.peerconnections[resp.SessionID] = peerconnection
oclient.peerconnections[resp.SessionID] = peerconnection
if err != nil {
log.Fatalln(err)
@ -66,7 +72,7 @@ func (h *Handler) RegisterOverlordClient(oclient *OverlordClient) {
log.Println("Received a start request from overlord")
log.Println("Add the connection to current room on the host")
peerconnection := h.peerconnections[resp.SessionID]
peerconnection := oclient.peerconnections[resp.SessionID]
log.Println("start session")
roomID, isNewRoom := startSession(peerconnection, resp.Data, resp.RoomID, resp.PlayerIndex)
log.Println("Done, sending back")
@ -82,9 +88,6 @@ func (h *Handler) RegisterOverlordClient(oclient *OverlordClient) {
},
)
// heartbeat to keep pinging overlord. We not ping from server to browser, so we don't call heartbeat in browserClient
go oclient.Heartbeat()
go oclient.Listen()
}
func getServerIDOfRoom(oc *OverlordClient, roomID string) string {
@ -100,7 +103,7 @@ func getServerIDOfRoom(oc *OverlordClient, roomID string) string {
return packet.Data
}
func bridgeConnection(session *Session, serverID string, gameName string, roomID string, playerIndex int) {
func (session *Session) bridgeConnection(serverID string, gameName string, roomID string, playerIndex int) {
log.Println("Bridging connection to other Host ", serverID)
client := session.BrowserClient
// Ask client to init

View file

@ -8,10 +8,15 @@ import (
// It involves one connection to browser and one connection to the overlord
// Peerconnection can be from other server to ensure better latency
type Session struct {
ID string
BrowserClient *BrowserClient
OverlordClient *OverlordClient
peerconnection *webrtc.WebRTC
ServerID string
ServerID string
GameName string
RoomID string
PlayerIndex int
}
// startSession handles one session call