SafeDispatch/MotoTrbo_GW/RouteManager.cs

201 lines
8.2 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SafeMobileLib;
using Telerik.WinControls;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using MotoTRBO_GW;
namespace MotoTrbo_GW
{
class RouteManager
{
private bool DEBUG = true;
private List<RadioGateway> radioGwList;
private DBgatewaysManager DBgateways;
private Thread threadWorker;
public static Hashtable gwRadio_status;
public static Hashtable NetworkIDs_Hash;
public static Hashtable RadioGW_IDs;
public RouteManager()
{
try
{
DBgateways = new DBgatewaysManager(Main.DBServer, Main.DBSchema, Main.DBUser, Main.DBPass, Main.DBPort);
radioGwList = DBgateways.gelAllRadioGateways(Main.GWID, (int)GatewayType.Tier2Radio);
gwRadio_status = new Hashtable();
RadioGW_IDs = new Hashtable();
for (int i = 0; i < radioGwList.Count; i++)
{
//adding 1 to the IP of the radio to get the interface IP
string[] arrStr = (radioGwList[i].Ip).Split('.');
int lastPartOfIP = Convert.ToInt32(arrStr[3]);
arrStr[3] = (lastPartOfIP + 1).ToString();
string interfaceIP = arrStr[0] + "." + arrStr[1] + "." + arrStr[2] + "." + arrStr[3];
radioGwList[i].InterfaceIP = interfaceIP;
if (!gwRadio_status.Contains(interfaceIP))
{
gwRadio_status.Add(interfaceIP, false);
RadioGW_IDs.Add(radioGwList[i].Ip, radioGwList[i].Id);
}
}
NetworkIDs_Hash = new Hashtable();
}
catch (Exception ex)
{
string err = "Fatal Error:" + ex.ToString();
SafeMobileLib.Utils.WriteLine(err);
RadMessageBox.Show(err, "Fatal ERROR", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
//System.Environment.Exit(0);
System.Windows.Forms.Application.Exit();
}
}
public void Start()
{
if(!Main.capacityPlus)
for (int i = 0; i < radioGwList.Count; i++)
{
//SafeMobileLib.Utils.WriteLine("Starting ping thread for:" + radioGwList[i].InterfaceIP+ "interval:"+Main.pingInterval);
PingThread threadWorker = new PingThread(radioGwList[i].InterfaceIP, Main.pingInterval);
threadWorker.OnNewPingComplete += new PingThread.NewPingCompleteDEl(RouteManager_OnNewPingComplete);
Thread PingThreadObj = new Thread(new ThreadStart(threadWorker.Worker));
PingThreadObj.IsBackground = true;
PingThreadObj.Start();
}
else
{
for (int i = 0; i < radioGwList.Count; i++)
{
if (radioGwList[i].Ip.ToString().Equals(Main.masterRadioIP))
{
SafeMobileLib.Utils.WriteLine("Starting ping thread for:" + radioGwList[i].InterfaceIP+ "interval:"+Main.pingInterval, ConsoleColor.Green);
PingThread threadWorker = new PingThread(radioGwList[i].InterfaceIP, Main.pingInterval);
threadWorker.OnNewPingComplete += new PingThread.NewPingCompleteDEl(RouteManager_OnNewPingComplete);
Thread PingThreadObj = new Thread(new ThreadStart(threadWorker.Worker));
PingThreadObj.IsBackground = true;
PingThreadObj.Start();
}
}
}
}
void restoreRoutes(string ip_temp)
{
try
{
SafeMobileLib.Utils.WriteLine("Starting Route restore!!! for :" + ip_temp);
ArrayList imeiList = null;
bool tryToGet = true;
byte retryNr = 3;
while (tryToGet)
{
try
{
imeiList = DBgateways.GetAllUnits4GW_RGW(Main.GWID, ip_temp);
tryToGet = false;
}
catch (Exception e)
{
Thread.Sleep(1000);
DBgateways = new DBgatewaysManager(Main.DBServer, Main.DBSchema, Main.DBUser, Main.DBPass, Main.DBPort);
retryNr--;
if (retryNr == 0)
return;
}
}
if (imeiList != null)
{
if (imeiList.Count > 0)
{
foreach (string imei in imeiList)
{
//register route
if (!Main.capacityPlus)
{
if (RouteManager.NetworkIDs_Hash.ContainsKey(ip_temp))
{
RoutingUtils.addRoute(imei, ip_temp, NetworkIDs_Hash[ip_temp].ToString());
}
else
{
SafeMobileLib.Utils.WriteLine("RouteManager.NetworkIDs_Hash does not contain a key for " + ip_temp);
}
}
}
}
}
}
catch (Exception e)
{
SafeMobileLib.Utils.WriteLine("**************\nCould not restore routes\n" + e.ToString());
DBgateways = new DBgatewaysManager(Main.DBServer, Main.DBSchema, Main.DBUser, Main.DBPass, Main.DBPort);
}
}
void RouteManager_OnNewPingComplete(bool status, string ip)
{
SafeMobileLib.Utils.WriteLine("****** OnNewPingComplete ** " + status + " *** " + ip);
string ip_temp = RadioStuff.GetReverse_Interface(ip);
SafeMobileLib.Utils.WriteLine("Interface IP:" + ip_temp);
bool value = (bool)gwRadio_status[ip];
if (status) //radio going from OFF->ON
{
try
{
gwRadio_status[ip_temp] = true;
if (NetworkIDs_Hash != null)
{
if (NetworkIDs_Hash.ContainsKey(ip_temp))
NetworkIDs_Hash.Remove(ip_temp);
int id = Convert.ToInt32(RadioStuff.GetInterfaceID(ip_temp));
if (!NetworkIDs_Hash.Contains(ip_temp))
NetworkIDs_Hash.Add(ip_temp, id);
}
//restoreRoutes(ip_temp);
if (this.OnRadioStatusUpdate != null)
{
this.OnRadioStatusUpdate(status, ip_temp);
}
}
catch (Exception ex)
{
SafeMobileLib.Utils.WriteLine("!!!!! Could not turn radio ON:" + ex.ToString());
}
}
else //going from ON->OFF
{
try
{
if (DEBUG) SafeMobileLib.Utils.WriteLine("GW:" + ip_temp + " was turned OFF!!!!");
//remove from hash
if (NetworkIDs_Hash.ContainsKey(ip_temp))
NetworkIDs_Hash.Remove(ip_temp);
gwRadio_status[ip] = false;
if (this.OnRadioStatusUpdate != null)
{
this.OnRadioStatusUpdate(status, ip_temp);
}
}
catch (Exception ex)
{
SafeMobileLib.Utils.WriteLine("!!!!!!! Could not turn radio OFF:" + ex.ToString());
}
}
}
public delegate void RadioStatusUpdateDEl(bool status, string ip);
public event RadioStatusUpdateDEl OnRadioStatusUpdate;
}
}