/// <summary>
/// Adds a server's posted snapshot into the Snapshot Processing Queue, which
/// will process the snapshot as soon as possible. This method is Non-Blocking.
/// </summary>
/// <remarks>
/// Any errors that occur during the actual import of the data will be
/// logged inside the StatsDebug log
/// </remarks>
/// <param name="Data">The snapshot data provided by the server.</param>
/// <param name="ServerAddress">The IP address of the server.</param>
/// <exception cref="UnauthorizedAccessException">
/// Thrown if the Server IP is not authorized to post game data to this server
/// </exception>
/// <exception cref="InvalidDataException">
/// Thrown if the provided Snapshot data is not valid, and cannot be processed
/// </exception>
public static void QueueServerSnapshot(string Data, IPAddress ServerAddress)
{
// Make sure the server is authorized
if (!IsAuthorizedGameServer(ServerAddress))
throw new UnauthorizedAccessException("Un-Authorised Gameserver (Ip: " + ServerAddress + ")");
// Create the Snapshot Object
Snapshot Snap = new Snapshot(Data, ServerAddress);
// Update this server in the Database
using (StatsDatabase Database = new StatsDatabase())
{
// Try and grab the ID of this server
int id = Database.ExecuteScalar<int>(
"SELECT COALESCE(id, -1), COUNT(id) FROM servers WHERE ip=@P0 AND port=@P1",
ServerAddress, Snap.ServerPort
);
// New server?
if (id < 0)
{
InsertQueryBuilder builder = new InsertQueryBuilder(Database);
builder.SetTable("servers");
builder.SetField("ip", ServerAddress);
builder.SetField("port", Snap.ServerPort);
builder.SetField("prefix", Snap.ServerPrefix);
builder.SetField("name", Snap.ServerName);
builder.SetField("queryport", Snap.QueryPort);
builder.SetField("lastupdate", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
builder.Execute();
}
else // existing
{
UpdateQueryBuilder builder = new UpdateQueryBuilder(Database);
builder.SetTable("servers");
builder.SetField("prefix", Snap.ServerPrefix);
builder.SetField("name", Snap.ServerName);
builder.SetField("queryport", Snap.QueryPort);
builder.SetField("lastupdate", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
builder.AddWhere("id", Comparison.Equals, id);
builder.Execute();
}
}
// Add snapshot to Queue
SnapshotQueue.Enqueue(Snap);
}