finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
@@ -8,10 +8,11 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/actions"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/requests"
|
||||
json_models "gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/temp_models/json"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models/db"
|
||||
json_models "gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models/json"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/transport"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/types"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -24,7 +25,7 @@ var (
|
||||
|
||||
type FoundryApi struct {
|
||||
//TODO: make check of admin's authentication
|
||||
transport *transport.FoundryTransport
|
||||
Transport *transport.FoundryTransport
|
||||
IsAvailable bool
|
||||
|
||||
// Logger *slog.Logger
|
||||
@@ -37,16 +38,16 @@ func NewFoundry() *FoundryApi {
|
||||
}
|
||||
|
||||
func (foundry *FoundryApi) SetTransport(tr *transport.FoundryTransport) {
|
||||
foundry.transport = tr
|
||||
foundry.Transport = tr
|
||||
}
|
||||
|
||||
func (foundry *FoundryApi) ListenAndServeWS() error {
|
||||
var err error
|
||||
|
||||
foundry.transport.ReadChan = *types.InitWsChannels()
|
||||
defer foundry.transport.ReadChan.Close()
|
||||
foundry.Transport.ReadChan = *types.InitWsChannels()
|
||||
defer foundry.Transport.ReadChan.Close()
|
||||
|
||||
foundry.background(foundry.transport.ListenWebSocket)
|
||||
foundry.background(foundry.Transport.ListenWebSocket)
|
||||
|
||||
err = foundry.ServeWebSocket()
|
||||
if err != nil {
|
||||
@@ -57,21 +58,21 @@ func (foundry *FoundryApi) ListenAndServeWS() error {
|
||||
|
||||
func (foundry *FoundryApi) ServeWebSocket() error {
|
||||
var err error
|
||||
wsChannels := &foundry.transport.ReadChan
|
||||
wsChannels := &foundry.Transport.ReadChan
|
||||
|
||||
for {
|
||||
select {
|
||||
case message, ok := <-wsChannels.Msg():
|
||||
if !ok {
|
||||
foundry.transport.Logger.Debug("Read channel is closed", "type", types.WebSocketCode)
|
||||
foundry.transport.Http.SessionID = nil
|
||||
foundry.Transport.Logger.Debug("Read channel is closed", "type", types.WebSocketCode)
|
||||
foundry.Transport.Http.SessionID = nil
|
||||
return ChannelIsClosed
|
||||
}
|
||||
foundry.transport.Logger.Debug("Data has been received\n", "msg", message.Code, "type", types.WebSocketCode) //, "msg", message.MsgJson)
|
||||
foundry.Transport.Logger.Debug("Data has been received\n", "msg", message.Code, "type", types.WebSocketCode) //, "msg", message.MsgJson)
|
||||
|
||||
switch message.Code {
|
||||
case types.RespPingCode, types.RespSessionDataCode:
|
||||
err := foundry.transport.SendOnlyCodeRequest(message.Code)
|
||||
err := foundry.Transport.SendOnlyCodeRequest(message.Code)
|
||||
if err != nil {
|
||||
wsChannels.Err() <- &types.FoundryError{Direction: types.WriterCode, Err: err, Type: types.WebSocketCode}
|
||||
continue
|
||||
@@ -84,69 +85,69 @@ func (foundry *FoundryApi) ServeWebSocket() error {
|
||||
continue
|
||||
}
|
||||
|
||||
foundry.transport.Logger.Debug("RespServerChangeCode", "data", data)
|
||||
foundry.Transport.Logger.Debug("RespServerChangeCode", "data", data)
|
||||
if data == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
err = data.Action(foundry.transport, &foundry.Status)
|
||||
err = data.Action(foundry.Transport, &foundry.Status)
|
||||
if err != nil {
|
||||
wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
||||
continue
|
||||
}
|
||||
case types.RespDataCode:
|
||||
foundry.transport.ExchangeChan.Msgs[message.Id] = make(chan []byte, 1)
|
||||
foundry.transport.ExchangeChan.Msgs[message.Id] <- []byte(message.MsgJson)
|
||||
go foundry.transport.CloseMsgChannel(message.Id, 5*time.Second)
|
||||
foundry.Transport.ExchangeChan.Msgs[message.Id] = make(chan []byte, 1)
|
||||
foundry.Transport.ExchangeChan.Msgs[message.Id] <- []byte(message.MsgJson)
|
||||
go foundry.Transport.CloseMsgChannel(message.Id, 5*time.Second)
|
||||
default:
|
||||
}
|
||||
case err = <-wsChannels.Err():
|
||||
var foundryErr *types.FoundryError
|
||||
if errors.As(err, &foundryErr) {
|
||||
if foundryErr.IsFatal {
|
||||
foundry.transport.CloseWebSocketConn()
|
||||
foundry.transport.Http.SessionID = nil
|
||||
foundry.Transport.CloseWebSocketConn()
|
||||
foundry.Transport.Http.SessionID = nil
|
||||
return foundryErr
|
||||
}
|
||||
foundry.transport.Logger.Warn("Got error when listening or served", "err", err.Error(), "type", foundryErr.Type, "direction", foundryErr.Direction)
|
||||
foundry.Transport.Logger.Warn("Got error when listening or served", "err", err.Error(), "type", foundryErr.Type, "direction", foundryErr.Direction)
|
||||
}
|
||||
case <-wsChannels.Done():
|
||||
foundry.transport.CloseWebSocketConn()
|
||||
foundry.transport.Http.SessionID = nil
|
||||
foundry.Transport.CloseWebSocketConn()
|
||||
foundry.Transport.Http.SessionID = nil
|
||||
return ListenIsDone
|
||||
case <-wsChannels.Reconnect():
|
||||
foundry.transport.CloseWebSocketConn()
|
||||
foundry.Transport.CloseWebSocketConn()
|
||||
return ReconnectToWebSocket
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (foundry *FoundryApi) HandleWSRequest(msgType string) ([]byte, error) {
|
||||
msg := types.NewWsMessageByPage(msgType, foundry.transport.CurrWsId)
|
||||
msg := types.NewWsMessageByPage(msgType, foundry.Transport.CurrWsId)
|
||||
|
||||
return foundry.transport.HandleWebsocketRequest(msg)
|
||||
return foundry.Transport.HandleWebsocketRequest(msg)
|
||||
}
|
||||
|
||||
func (foundry *FoundryApi) Shutdown() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
|
||||
foundry.transport.Logger.Info("Completing foundry background tasks")
|
||||
foundry.Transport.Logger.Info("Completing foundry background tasks")
|
||||
|
||||
foundryClosed := make(chan struct{})
|
||||
go func() {
|
||||
types.CloseChannel(foundry.transport.ReadChan.Done())
|
||||
types.CloseChannel(foundry.Transport.ReadChan.Done())
|
||||
|
||||
foundry.wg.Wait()
|
||||
foundry.transport.ExchangeChan.Close()
|
||||
foundry.transport.ReadChan.Close()
|
||||
foundry.Transport.ExchangeChan.Close()
|
||||
foundry.Transport.ReadChan.Close()
|
||||
|
||||
types.CloseChannel(foundryClosed)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-foundryClosed:
|
||||
foundry.transport.Logger.Info("Stopped foundry server")
|
||||
foundry.Transport.Logger.Info("Stopped foundry server")
|
||||
case <-ctx.Done():
|
||||
return CloseTimeoutExceed
|
||||
}
|
||||
@@ -156,24 +157,24 @@ func (foundry *FoundryApi) Shutdown() error {
|
||||
|
||||
func (foundry *FoundryApi) ConnectToWebSocket() (bool, error) {
|
||||
var err error
|
||||
if !foundry.transport.HasSessionId() {
|
||||
err = foundry.transport.InitSessionId()
|
||||
if !foundry.Transport.HasSessionId() {
|
||||
err = foundry.Transport.InitSessionId()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
err = foundry.transport.ConnectToFoundry()
|
||||
err = foundry.Transport.ConnectToFoundry()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
err = foundry.transport.InitWebSocketConnection()
|
||||
err = foundry.Transport.InitWebSocketConnection()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
status, err := foundry.transport.Http.GetStatus()
|
||||
status, err := foundry.Transport.Http.GetStatus()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -195,21 +196,21 @@ func (foundry *FoundryApi) StartListenFoundry() {
|
||||
ok, err := foundry.ConnectToWebSocket()
|
||||
if err != nil {
|
||||
if errors.Is(err, ReconnectToWebSocket) {
|
||||
if foundry.transport.ReconnectNum >= foundry.transport.ReconnectNumMax {
|
||||
if foundry.Transport.ReconnectNum >= foundry.Transport.ReconnectNumMax {
|
||||
return
|
||||
}
|
||||
time.Sleep(foundry.transport.ReconnectTimeout)
|
||||
foundry.transport.ReconnectNum++
|
||||
foundry.transport.Logger.Info("Reconnecting to WebSocket", "times", foundry.transport.ReconnectNum)
|
||||
time.Sleep(foundry.Transport.ReconnectTimeout)
|
||||
foundry.Transport.ReconnectNum++
|
||||
foundry.Transport.Logger.Info("Reconnecting to WebSocket", "times", foundry.Transport.ReconnectNum)
|
||||
continue
|
||||
}
|
||||
|
||||
foundry.transport.Logger.Error("Error raised", "err", err.Error())
|
||||
foundry.Transport.Logger.Error("Error raised", "err", err.Error())
|
||||
|
||||
if ok {
|
||||
timeInterval = 1 * time.Second
|
||||
}
|
||||
foundry.transport.Logger.Info("Trying to reconnect", "timer", timeInterval.String())
|
||||
foundry.Transport.Logger.Info("Trying to reconnect", "timer", timeInterval.String())
|
||||
|
||||
time.Sleep(timeInterval)
|
||||
timeInterval = min(timeInterval*2, 15*time.Second)
|
||||
@@ -220,11 +221,28 @@ func (foundry *FoundryApi) StartListenFoundry() {
|
||||
})
|
||||
}
|
||||
|
||||
func (foundry *FoundryApi) Test() (*json_models.Game, error) {
|
||||
wsMsg := types.NewWsMessage("world", foundry.transport.CurrWsId)
|
||||
foundry.transport.CurrWsId++
|
||||
func (foundry *FoundryApi) PrepareDB() error {
|
||||
dbConn := foundry.Transport.DB
|
||||
|
||||
answer, err := foundry.transport.HandleWebsocketRequest(wsMsg)
|
||||
group, _ := errgroup.WithContext(context.Background())
|
||||
|
||||
group.Go(func() error { return db.DeleteSetupAll(dbConn) })
|
||||
group.Go(func() error { return db.DeleteGameAll(dbConn) })
|
||||
group.Go(func() error { return db.DeleteSeqAll(dbConn) })
|
||||
|
||||
err := group.Wait()
|
||||
if err != nil && !errors.Is(err, db.ErrorRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (foundry *FoundryApi) Test() (*json_models.Game, error) {
|
||||
wsMsg := types.NewWsMessage("world", foundry.Transport.CurrWsId)
|
||||
foundry.Transport.CurrWsId++
|
||||
|
||||
answer, err := foundry.Transport.HandleWebsocketRequest(wsMsg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -238,11 +256,6 @@ func (foundry *FoundryApi) Test() (*json_models.Game, error) {
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
|
||||
foundry.transport.Logger.Info("World data successfully received and parsed", "parseTime", elapsed)
|
||||
foundry.Transport.Logger.Info("World data successfully received and parsed", "parseTime", elapsed)
|
||||
return &game[0], nil
|
||||
}
|
||||
|
||||
// TODO: remove
|
||||
func (foundry *FoundryApi) GetHTTP() *requests.FoundryHttpRequest {
|
||||
return foundry.transport.Http
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user