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