SafeDispatch/MotoRepeaterCore/SMSConfirm.cs
2024-02-22 18:43:59 +02:00

139 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Timers;
using SafeMobileLib;
namespace MotoRepeater
{
class SMSConfirm
{
private static Boolean debug = false;
private static Boolean listFilled = false;
private static Boolean timerInitialized = false;
private static Dictionary<int, SeqAndTime> toBeConfirmed = new Dictionary<int, SeqAndTime>();
private static List<int> availableIDs = new List<int>();
private static int recycleInterval = 60; //seconds
private static int noACKindex = 1;
static Timer recycleTimer;
private class SeqAndTime
{
public String seq_id;
public double timeStamp;
public SeqAndTime(String seq, double tStamp)
{
seq_id = seq;
timeStamp = tStamp;
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void initializeTimer()
{
if (!timerInitialized)
{
recycleTimer = new Timer(recycleInterval * 1000);
recycleTimer.Elapsed += new ElapsedEventHandler(recycleTimer_Elapsed);
recycleTimer.Enabled = true;
timerInitialized = true;
}
}
private static void recycleTimer_Elapsed(object sender, ElapsedEventArgs e)
{
recycleOldConfirmations();
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void fillList()
{
if (!listFilled)
{
for (int i = 1; i < 128; i++)
{
availableIDs.Add(i);
}
listFilled = true;
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void recycleOldConfirmations()
{
if (debug)
{
Utils.WriteLine("Recycling confirmations at " + DateTime.Now.ToString());
}
Dictionary<int, SeqAndTime> tempToBeConfirmed = new Dictionary<int, SeqAndTime>();
double currentTime = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
foreach (int index in toBeConfirmed.Keys)
{
if (toBeConfirmed[index].timeStamp < currentTime - recycleInterval)
{
availableIDs.Add(index);
if (debug)
{
Utils.WriteLine("Removed " + toBeConfirmed[index].seq_id + " from the confirmation requests list");
}
}
else
{
tempToBeConfirmed.Add(index, toBeConfirmed[index]);
}
}
toBeConfirmed = tempToBeConfirmed;
if (debug)
{
Utils.WriteLine("There are " + toBeConfirmed.Keys.Count + " messages waiting for confirmation,");
Utils.WriteLine("and " + availableIDs.Count + " available confirmations left");
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static int addToConfirmationQueue(String seq_idFromSD)
{
if (availableIDs.Count > 0)
{
double timeStamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
SeqAndTime sT = new SeqAndTime(seq_idFromSD, timeStamp);
int index = (int)availableIDs[0];
availableIDs.RemoveAt(0);
if (debug)
{
Utils.WriteLine("Removed from availableIDs, remaining " + availableIDs.Count);
}
toBeConfirmed.Add(index, sT);
return index;
}
else
{
return -1;
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static String getFromConfirmationQueue(int index)
{
String toReturn = "";
if (toBeConfirmed.ContainsKey(index))
{
toReturn = toBeConfirmed[index].seq_id;
toBeConfirmed.Remove(index);
availableIDs.Add(index);
if (debug)
{
Utils.WriteLine("Confirmed message with key " + index + ", removed from hashtable and added to availableIs");
Utils.WriteLine("There are " + availableIDs.Count + " available confirmations left");
}
return toReturn;
}
else
{
return "0.0";
}
}
}
}