2018-09-06 14:25:58 -04:00
|
|
|
|
using IW4MAdmin.Application.API.GameLogServer;
|
|
|
|
|
using RestEase;
|
|
|
|
|
using SharedLibraryCore;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-05-13 11:36:11 -04:00
|
|
|
|
using System.Linq;
|
2018-10-25 09:14:39 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.IO
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-05-04 17:50:02 -04:00
|
|
|
|
/// provides capability of reading log files over HTTP
|
2018-08-28 17:32:59 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
class GameLogReaderHttp : IGameLogReader
|
|
|
|
|
{
|
2020-04-01 15:11:56 -04:00
|
|
|
|
private readonly IEventParser _eventParser;
|
|
|
|
|
private readonly IGameLogServer _logServerApi;
|
2020-05-04 17:50:02 -04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly string _safeLogPath;
|
2019-07-24 20:15:07 -04:00
|
|
|
|
private string lastKey = "next";
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
public GameLogReaderHttp(Uri[] gameLogServerUris, IEventParser parser, ILogger logger)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2020-04-01 15:11:56 -04:00
|
|
|
|
_eventParser = parser;
|
2020-05-04 17:50:02 -04:00
|
|
|
|
_logServerApi = RestClient.For<IGameLogServer>(gameLogServerUris[0].ToString());
|
|
|
|
|
_safeLogPath = gameLogServerUris[1].LocalPath.ToBase64UrlSafeString();
|
|
|
|
|
_logger = logger;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:25:58 -04:00
|
|
|
|
public long Length => -1;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
public int UpdateInterval => 500;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
public async Task<IEnumerable<GameEvent>> ReadEventsFromLog(long fileSizeDiff, long startPosition)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2018-09-06 14:25:58 -04:00
|
|
|
|
var events = new List<GameEvent>();
|
2020-05-04 17:50:02 -04:00
|
|
|
|
var response = await _logServerApi.Log(_safeLogPath, lastKey);
|
2019-07-24 20:15:07 -04:00
|
|
|
|
lastKey = response.NextKey;
|
2018-09-06 14:25:58 -04:00
|
|
|
|
|
2019-07-24 20:15:07 -04:00
|
|
|
|
if (!response.Success && string.IsNullOrEmpty(lastKey))
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
_logger.WriteError($"Could not get log server info of {_safeLogPath}");
|
2018-10-25 09:14:39 -04:00
|
|
|
|
return events;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 11:22:02 -04:00
|
|
|
|
else if (!string.IsNullOrWhiteSpace(response.Data))
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2019-07-24 20:15:07 -04:00
|
|
|
|
// parse each line
|
2020-04-01 15:11:56 -04:00
|
|
|
|
var lines = response.Data
|
|
|
|
|
.Split(Environment.NewLine)
|
|
|
|
|
.Where(_line => _line.Length > 0);
|
|
|
|
|
|
|
|
|
|
foreach (string eventLine in lines)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2019-10-18 14:39:21 -04:00
|
|
|
|
try
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2020-04-01 15:11:56 -04:00
|
|
|
|
// this trim end should hopefully fix the nasty runaway regex
|
|
|
|
|
var gameEvent = _eventParser.GenerateGameEvent(eventLine.TrimEnd('\r'));
|
2019-10-18 14:39:21 -04:00
|
|
|
|
events.Add(gameEvent);
|
|
|
|
|
}
|
2019-05-13 11:36:11 -04:00
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
_logger.WriteError("Could not properly parse event line from http");
|
|
|
|
|
_logger.WriteDebug(e.Message);
|
|
|
|
|
_logger.WriteDebug(eventLine);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return events;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|