205 lines
12 KiB
C#
205 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
using System.Threading;
|
|
using SafeMobileLib;
|
|
using System.Net;
|
|
using RegLibDLL;
|
|
|
|
namespace AppServer
|
|
{
|
|
class RegistrationThread
|
|
{
|
|
int port;
|
|
private Socket socket;
|
|
DBregistrationManager regManager;
|
|
RegLib regLib = new RegLib();
|
|
string databaseIP = "127.0.0.1";
|
|
public RegistrationThread(int registrationPort, string p_server, string p_dbname, string p_user, string p_password, string p_port)
|
|
{
|
|
port = registrationPort;
|
|
regManager = new DBregistrationManager(p_server, p_dbname, p_user, p_password, p_port);
|
|
databaseIP = Program.cfg.DB_IP == "127.0.0.1" || Program.cfg.DB_IP == "localhost" ? UdpMulticast.getPreferedIPAdress().ToString() : Program.cfg.DB_IP;
|
|
}
|
|
|
|
public void handleConnection()
|
|
{
|
|
Console.WriteLine("Registration Thread - Initialized on Port " + port);
|
|
TcpListener myListener = new TcpListener(IPAddress.Any, port);
|
|
try
|
|
{
|
|
myListener.Start();
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
MainForm.isRunning = false;
|
|
Utils.WriteLine("AppServer Registration Thread Exception: " + ex.ToString(), ConsoleColor.Red);
|
|
FeedbackRadMessageBox.ShowError("The Registration Server was unable to be started so the AppServer needs to be restarted! Try berifying that the port " + Program.cfg.regPort
|
|
+ " is free or try to change it from configuration file. The following exception was caught: " + System.Environment.NewLine + ex.ToString(), "Unable to Start Registration Server");
|
|
}
|
|
while (!MainForm.error && MainForm.isRunning)
|
|
{
|
|
try
|
|
{
|
|
socket = myListener.AcceptSocket();
|
|
String clientIP = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
|
|
Console.WriteLine("IP " + clientIP + " connected");
|
|
byte[] data = new byte[128];
|
|
int receive = socket.Receive(data);
|
|
String stringData = System.Text.Encoding.ASCII.GetString(data, 0, data.Length);
|
|
String usefulData = stringData.Trim();
|
|
Console.WriteLine("Data received:" + usefulData);
|
|
String[] command = usefulData.Split("#".ToCharArray());
|
|
SDRegistration sdReg = null;
|
|
String header = "";
|
|
int digCommand;
|
|
if (Int32.TryParse(command[0], out digCommand))
|
|
{
|
|
String version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
String audibleAlerts = regLib.audibleAlertsCount().ToString();
|
|
|
|
String responseString;
|
|
Byte[] sendBytes;
|
|
int i;
|
|
switch (digCommand)
|
|
{
|
|
case 100: //SafeDispatch registration request
|
|
Console.WriteLine("ClinetIP:" + clientIP);
|
|
sdReg = regManager.getSafeDispatchFeatures(clientIP);
|
|
if (sdReg.ip.Equals("invalid"))
|
|
{
|
|
header = "invalid";
|
|
}
|
|
else
|
|
{
|
|
header = "valid-" + version;
|
|
}
|
|
responseString = "101" + ";" + header + ";" + sdReg.gps.ToString() + ";" + sdReg.sms.ToString() + ";"
|
|
+ sdReg.ticketing.ToString() + ";" + sdReg.reports.ToString() + ";" + sdReg.voice.ToString() + ";"
|
|
+ sdReg.recordings.ToString() + ";"
|
|
+ sdReg.zones.ToString() + ";" + sdReg.telemetry.ToString() + ";" + sdReg.map_type.ToString() + ";"
|
|
+ Program.cfg.msgBusIP.ToString() + ";" + Program.cfg.msgBusPort + ";" + databaseIP + ";"
|
|
+ Program.cfg.DB_schema + ";" + Program.cfg.DB_user + ";" + Program.cfg.DB_passwd
|
|
+ ";" + Program.cfg.DB_port + ";" + Program.cfg.VoicePort + ";"
|
|
+ audibleAlerts.ToString() + ";" + MainForm.dbAccess.ToString() + ";";
|
|
sendBytes = Encoding.ASCII.GetBytes(responseString);
|
|
//byte[] encryptedSendBytes = Encryption.Encrypt(sendBytes);
|
|
i = socket.Send(sendBytes);
|
|
Console.WriteLine("Sent registration to SafeDispatch with ip " + clientIP);
|
|
break;
|
|
case 1000:
|
|
Console.WriteLine("Clinet UniqueID:224.0.0." + command[1]);
|
|
sdReg = regManager.getSafeDispatchFeatures("224.0.0." + command[1].Trim());
|
|
if (sdReg.ip.Equals("invalid"))
|
|
{
|
|
header = "invalid";
|
|
}
|
|
else
|
|
{
|
|
header = "valid-" + version;
|
|
}
|
|
responseString = "101" + ";" + header + ";" + sdReg.gps.ToString() + ";" + sdReg.sms.ToString() + ";"
|
|
+ sdReg.ticketing.ToString() + ";" + sdReg.reports.ToString() + ";" + sdReg.voice.ToString() + ";"
|
|
+ sdReg.recordings.ToString() + ";"
|
|
+ sdReg.zones.ToString() + ";" + sdReg.telemetry.ToString() + ";" + sdReg.map_type.ToString() + ";"
|
|
+ Program.cfg.msgBusIP.ToString() + ";" + Program.cfg.msgBusPort + ";" + databaseIP + ";"
|
|
+ Program.cfg.DB_schema + ";" + Program.cfg.DB_user + ";" + Program.cfg.DB_passwd
|
|
+ ";" + Program.cfg.DB_port + ";" + Program.cfg.VoicePort + ";"
|
|
+ audibleAlerts.ToString() + ";" + MainForm.dbAccess.ToString() + ";";
|
|
MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0#156#" + command[1].Trim() + "#"), Utils.Convert_text_For_multicast("#0.0#156#"
|
|
+ command[1].Trim() + "#").Length);
|
|
|
|
MainForm.RegistrationAnswer = false;
|
|
Int32 countWait = 0; //maxim 30 = 3 seconds
|
|
while ((!MainForm.RegistrationAnswer) && (countWait < 30) && MainForm.isRunning)
|
|
{
|
|
countWait++;
|
|
Console.WriteLine("Registration answer:" + MainForm.RegistrationAnswer);
|
|
Thread.Sleep(100);
|
|
}
|
|
if (MainForm.RegistrationAnswer)
|
|
responseString = "101;invalid;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;10;";
|
|
sendBytes = Encoding.ASCII.GetBytes(responseString);
|
|
i = socket.Send(sendBytes);
|
|
Console.WriteLine("Sent registration to SafeDispatch with " + "224.0.0." + command[1]);
|
|
break;
|
|
case 200: //Gateway registration request
|
|
if (regManager.getGateway(clientIP))
|
|
{
|
|
|
|
responseString = "201;valid-" + version + ";" + Program.cfg.msgBusIP.ToString() + ";" + Program.cfg.msgBusPort + ";" + databaseIP + ";"
|
|
+ Program.cfg.DB_schema + ";" + Program.cfg.DB_user + ";" + Program.cfg.DB_passwd
|
|
+ ";" + Program.cfg.DB_port + ";" + Program.cfg.VoicePort + ";";
|
|
}
|
|
else
|
|
{
|
|
responseString = "201;invalid";
|
|
}
|
|
|
|
sendBytes = Encoding.ASCII.GetBytes(responseString);
|
|
//encryptedSendBytes = Encryption.Encrypt(sendBytes);
|
|
i = socket.Send(sendBytes);
|
|
Console.WriteLine("Sent registration to gateway with ip " + clientIP);
|
|
break;
|
|
case 300: //Administrative module request
|
|
int totalGateways = regLib.gatewayCount();
|
|
|
|
int totalGPS = regLib.gpsCount();
|
|
int totalSMS = regLib.smsCount();
|
|
int totalTicketing = regLib.jobTicketingCount();
|
|
int totalReports = regLib.reportsCount();
|
|
int totalVoice = regLib.voiceCount();
|
|
int totalRecordings = regLib.recordingsCount();
|
|
int totalZones = regLib.zonesCount();
|
|
int totalTelemetry = regLib.telemetryCount();
|
|
int mapGoogle = regLib.mapType1Count();
|
|
int mapUS = regLib.mapType2Count();
|
|
int mapEUROPE = regLib.mapType3Count();
|
|
int mapArcgis = regLib.mapType4Count();
|
|
int totalUnits = regLib.getNumberOfUnits();
|
|
responseString = "301;" + totalGateways.ToString() + ";" + totalGPS.ToString() + ";"
|
|
+ totalSMS.ToString() + ";" + totalTicketing.ToString() + ";" + totalReports.ToString() + ";"
|
|
+ totalVoice.ToString() + ";" + totalRecordings.ToString() + ";" + totalZones.ToString() + ";" + totalTelemetry.ToString() + ";"
|
|
+ mapGoogle.ToString() + ";" + mapUS.ToString() + ";" + mapEUROPE.ToString() + ";" + mapArcgis.ToString() + ";"
|
|
+ totalUnits.ToString() + ";" + Program.cfg.msgBusIP.ToString() + ";" + Program.cfg.msgBusPort + ";" + databaseIP + ";"
|
|
+ Program.cfg.DB_schema + ";" + Program.cfg.DB_user + ";" + Program.cfg.DB_passwd
|
|
+ ";" + Program.cfg.DB_port + ";"
|
|
+ version;
|
|
sendBytes = Encoding.ASCII.GetBytes(responseString);
|
|
i = socket.Send(sendBytes);
|
|
Console.WriteLine("Sent registration to SUM ");
|
|
break;
|
|
case 400: //Gateway registration request
|
|
if (regLib.SDMobileCount() > 0)
|
|
{
|
|
responseString = "401;valid";
|
|
}
|
|
else
|
|
{
|
|
responseString = "401;invalid";
|
|
}
|
|
sendBytes = Encoding.ASCII.GetBytes(responseString);
|
|
//encryptedSendBytes = Encryption.Encrypt(sendBytes);
|
|
i = socket.Send(sendBytes);
|
|
Console.WriteLine("Sent registration to AppServer Mobile");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine("Exception in appserver:Registration thread: " + e.ToString());
|
|
}
|
|
Thread.Sleep(100);
|
|
}
|
|
|
|
myListener?.Stop();
|
|
}
|
|
|
|
}
|
|
}
|