2018-04-18 16:46:53 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using RestEase;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.API.Master
|
|
|
|
|
{
|
2020-01-11 21:32:27 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines the heartbeat functionality for IW4MAdmin
|
|
|
|
|
/// </summary>
|
2018-04-28 01:22:18 -04:00
|
|
|
|
public class Heartbeat
|
|
|
|
|
{
|
2020-01-11 21:32:27 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends heartbeat to master server
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="mgr"></param>
|
|
|
|
|
/// <param name="firstHeartbeat"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-04-18 16:46:53 -04:00
|
|
|
|
public static async Task Send(ApplicationManager mgr, bool firstHeartbeat = false)
|
|
|
|
|
{
|
2018-04-19 01:48:14 -04:00
|
|
|
|
var api = Endpoint.Get();
|
2018-04-18 16:46:53 -04:00
|
|
|
|
|
|
|
|
|
if (firstHeartbeat)
|
|
|
|
|
{
|
|
|
|
|
var token = await api.Authenticate(new AuthenticationId()
|
|
|
|
|
{
|
|
|
|
|
Id = mgr.GetApplicationSettings().Configuration().Id
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
api.AuthorizationToken = $"Bearer {token.AccessToken}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var instance = new ApiInstance()
|
|
|
|
|
{
|
|
|
|
|
Id = mgr.GetApplicationSettings().Configuration().Id,
|
|
|
|
|
Uptime = (int)(DateTime.UtcNow - mgr.StartTime).TotalSeconds,
|
2020-01-11 21:32:27 -05:00
|
|
|
|
Version = Program.Version,
|
2018-04-18 16:46:53 -04:00
|
|
|
|
Servers = mgr.Servers.Select(s =>
|
|
|
|
|
new ApiServer()
|
|
|
|
|
{
|
|
|
|
|
ClientNum = s.ClientNum,
|
|
|
|
|
Game = s.GameName.ToString(),
|
2018-12-16 22:16:56 -05:00
|
|
|
|
Version = s.Version,
|
2018-04-18 16:46:53 -04:00
|
|
|
|
Gametype = s.Gametype,
|
|
|
|
|
Hostname = s.Hostname,
|
|
|
|
|
Map = s.CurrentMap.Name,
|
|
|
|
|
MaxClientNum = s.MaxClients,
|
2018-11-27 19:31:48 -05:00
|
|
|
|
Id = s.EndPoint,
|
2019-05-31 11:17:01 -04:00
|
|
|
|
Port = (short)s.Port,
|
2018-11-25 21:00:36 -05:00
|
|
|
|
IPAddress = s.IP
|
2018-04-18 16:46:53 -04:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-11 21:32:27 -05:00
|
|
|
|
Response<ResultMessage> response = null;
|
|
|
|
|
|
2018-04-18 16:46:53 -04:00
|
|
|
|
if (firstHeartbeat)
|
|
|
|
|
{
|
2020-01-11 21:32:27 -05:00
|
|
|
|
response = await api.AddInstance(instance);
|
2018-04-18 16:46:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-11 21:32:27 -05:00
|
|
|
|
response = await api.UpdateInstance(instance.Id, instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response.ResponseMessage.StatusCode != System.Net.HttpStatusCode.OK)
|
|
|
|
|
{
|
|
|
|
|
mgr.Logger.WriteWarning($"Response code from master is {response.ResponseMessage.StatusCode}, message is {response.StringContent}");
|
2018-04-18 16:46:53 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|