2018-04-18 16:46:53 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using RestEase;
|
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.API.Master
|
|
|
|
|
{
|
2018-04-28 01:22:18 -04:00
|
|
|
|
public class HeartbeatState
|
2018-04-18 16:46:53 -04:00
|
|
|
|
{
|
2018-04-28 01:22:18 -04:00
|
|
|
|
public bool Connected { get; set; }
|
|
|
|
|
}
|
2018-04-18 16:46:53 -04:00
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
public class Heartbeat
|
|
|
|
|
{
|
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,
|
|
|
|
|
Version = (float)Program.Version,
|
|
|
|
|
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,
|
2018-11-25 21:00:36 -05:00
|
|
|
|
Port = (short)s.GetPort(),
|
|
|
|
|
IPAddress = s.IP
|
2018-04-18 16:46:53 -04:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (firstHeartbeat)
|
|
|
|
|
{
|
2018-04-19 01:48:14 -04:00
|
|
|
|
var message = await api.AddInstance(instance);
|
2018-04-18 16:46:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-04-19 01:48:14 -04:00
|
|
|
|
var message = await api.UpdateInstance(instance.Id, instance);
|
2018-04-18 16:46:53 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|