General cleanup

This commit is contained in:
szporowolik
2019-09-13 15:13:12 +02:00
parent 238f6e29ae
commit 9d42ccc1d4
6 changed files with 165 additions and 111 deletions

View File

@@ -16,9 +16,9 @@ public class DatabaseController
string strUDPFrameType = strUDPFrame.type;
string strUDPFrameTimestamp = strUDPFrame.timestamp;
string strUDPFrameInstance = strUDPFrame.instance;
string strUDPFramePayload = "";
string strUDPFramePayload_Perun = "";
string strSQLQueryTxt = "";
string strUDPFramePayload;
string strUDPFramePayload_Perun;
string strSQLQueryTxt;
// Some frames may come without timestamp, use database currrent timestampe then
@@ -86,48 +86,58 @@ public class DatabaseController
}
// Connect to mysql and execute sql
connMySQL = new MySqlConnection(strMySQLConnectionString);
try
{
Console.WriteLine("Sending data to MySQL - Begin");
connMySQL.Open();
bStatus = true;
MySqlCommand cmdMySQL = new MySqlCommand(strSQLQueryTxt, connMySQL);
MySqlDataReader rdrMySQL = cmdMySQL.ExecuteReader();
connMySQL = new MySqlConnection(strMySQLConnectionString);
while (rdrMySQL.Read())
try
{
Console.WriteLine(rdrMySQL[0] + " -- " + rdrMySQL[1]);
Console.WriteLine("Sending data to MySQL - Begin");
connMySQL.Open();
bStatus = true;
MySqlCommand cmdMySQL = new MySqlCommand(strSQLQueryTxt, connMySQL);
MySqlDataReader rdrMySQL = cmdMySQL.ExecuteReader();
while (rdrMySQL.Read())
{
Console.WriteLine(rdrMySQL[0] + " -- " + rdrMySQL[1]);
}
rdrMySQL.Close();
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > MySQL updated, package type: " + strUDPFrameType);
}
rdrMySQL.Close();
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#"+strUDPFrameInstance + " > MySQL updated, package type: " + strUDPFrameType);
}
catch (ArgumentException a_ex)
{
// General exception found
Console.WriteLine(a_ex.ToString());
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
bStatus = false;
}
catch (MySqlException m_ex)
{
// MySQL exception found
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
switch (m_ex.Number)
catch (ArgumentException a_ex)
{
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - unable to connect");
break;
case 0: // Access denied (Check DB name,username,password)
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - access denied");
break;
default:
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - " + m_ex.Number);
break;
// General exception found
Console.WriteLine(a_ex.ToString());
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
bStatus = false;
}
bStatus = false;
catch (MySqlException m_ex)
{
// MySQL exception found
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
switch (m_ex.Number)
{
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - unable to connect");
break;
case 0: // Access denied (Check DB name,username,password)
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - access denied");
break;
default:
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - " + m_ex.Number);
break;
}
bStatus = false;
}
connMySQL.Close();
Console.WriteLine("Sending data to MySQL - Done");
}
connMySQL.Close();
Console.WriteLine("Sending data to MySQL - Done");
catch (ArgumentException)
{
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - wrong connection parameters");
}
}
}

View File

@@ -17,7 +17,6 @@ namespace Perun_v1
static void Main()
{
// Only instance
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

View File

@@ -7,67 +7,63 @@ using System.Text;
using System.Threading;
using System.Windows.Forms;
internal class TCPController
public class TCPController
{
// Main class for TCP listener
public static int intListenPort; // Port to connect to
public static bool boolDone; // Helper to exit main loop without killing thread
public static TcpListener tcpServer; // Listener object
public static string[] arrLogHistory; // Log history for GUI
public static string[] arrSendBuffer; // Mysql send buffer
public static Thread thrTCPListener; // Seperate thread for UDP
public int intListenPort; // Port to connect to
public bool bDone; // Helper to exit main loop without killing thread
public string[] arrLogHistory; // Log history for GUI
public string[] arrSendBuffer; // Mysql send buffer
public Thread thrTCPListener; // Seperate thread for TCP
public bool bStatus; // TCP connecion status
public static void Create(int intListenPort, ref string[] arrLogHistory, ref string[] arrSendBuffer)
public void Create(int par_intListenPort, ref string[] par_arrLogHistory, ref string[] par_arrSendBuffer)
{
// Create class
TCPController.intListenPort = intListenPort;
TCPController.arrLogHistory = arrLogHistory;
TCPController.arrSendBuffer = arrSendBuffer;
intListenPort = par_intListenPort;
arrLogHistory = par_arrLogHistory;
arrSendBuffer = par_arrSendBuffer;
bDone = false;
bStatus = false;
}
public static void StopListen()
public void StopListen()
{
// FInish listening
TCPController.boolDone = true;
TCPController.tcpServer.Stop();
TCPController.tcpServer = null;
// Finish listening
bDone = true;
bStatus = false;
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
{
arrSendBuffer[i] = null;
}
}
public static void StartListen()
public void StartListen()
{
Console.WriteLine("TCP Listen start");
while (!TCPController.boolDone)
while (!bDone)
{
Console.WriteLine("TCP Listen start");
TcpListener tcpServer = new TcpListener(IPAddress.Any, intListenPort); ; // Listener object
try
{
// Start listening to TCP
tcpServer = new TcpListener(IPAddress.Any, intListenPort);
string strReceivedData;
// Start the main loop
TCPController.boolDone = false;
tcpServer.Start();
while (!TCPController.boolDone)
bStatus = true;
while (!bDone)
{
// Start listening
Console.WriteLine("TCP: Waiting for packet");
TcpClient tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it
NetworkStream ns = tcpClient.GetStream(); //networkstream is used to send/receive messages
//tcpClient.ReceiveBufferSize = 65534;
while (tcpClient.Connected && !TCPController.boolDone) //while the client is connected, we look for incoming messages
while (tcpClient.Connected && !bDone) //while the client is connected, we look for incoming messages
{
//arrReceiveByteArray = new byte[tcpClient.ReceiveBufferSize]; //the messages arrive as byte array
//ns.Read(arrReceiveByteArray, 0, arrReceiveByteArray.Length); //the same networkstream reads the message sent by the client
//strReceivedData = Encoding.ASCII.GetString(arrReceiveByteArray, 0, arrReceiveByteArray.Length);
StringBuilder CompleteMessage = new StringBuilder();
if (ns.CanRead)
@@ -87,24 +83,33 @@ internal class TCPController
strReceivedData = CompleteMessage.ToString();
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawTCPFrameType = dynamicRawTCPFrame.type;
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP packet received, type: " + strRawTCPFrameType);
// Add to mySQL send buffer (find first empty slot)
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
try
{
if (arrSendBuffer[i] == null)
dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawTCPFrameType = dynamicRawTCPFrame.type;
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP packet received, type: " + strRawTCPFrameType);
// Add to mySQL send buffer (find first empty slot)
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
{
arrSendBuffer[i] = strReceivedData;
break;
if (arrSendBuffer[i] == null)
{
arrSendBuffer[i] = strReceivedData;
break;
}
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP ERROR incorrect JSON");
}
}
}
tcpServer.Stop();
bStatus = false;
}
catch (Exception e)
{
@@ -114,8 +119,14 @@ internal class TCPController
Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP error - connection closed or port in use");
}
}
//Console.WriteLine("TCP listen stop");
if (!(tcpServer is null))
{
tcpServer.Stop();
}
bStatus = false;
Console.WriteLine("TCP listen stop");
}
}
}