0% found this document useful (0 votes)
12 views6 pages

Main Code

The document is a C# Windows Forms application code for a program named LISA, which manages connections and analyzes data from devices based on their MAC address. It includes functionalities for checking the MAC address, running analyzers in parallel, handling port configurations, and managing socket connections. The application also has user interface elements for minimizing, exiting, and reloading the application, along with error handling mechanisms for various operations.

Uploaded by

ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Main Code

The document is a C# Windows Forms application code for a program named LISA, which manages connections and analyzes data from devices based on their MAC address. It includes functionalities for checking the MAC address, running analyzers in parallel, handling port configurations, and managing socket connections. The application also has user interface elements for minimizing, exiting, and reloading the application, along with error handling mechanisms for various operations.

Uploaded by

ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

using System;

using System.Data;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.IO;
using System.IO.Ports;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;

namespace LISA
{
public partial class MainForm : Form
{
private Connection objCONNECTION;
private StreamWriter sw;
public string mac_address;

public MainForm()
{
InitializeComponent();

mac_address = check_macaddress();

//List<string> macAddresses = new List<string> { "C025A5879313",


"04BF1B9651D9", "A83B767D695D" };
// if (!macAddresses.Contains(mac_address))
if (1 == 2)
{
MessageBox.Show("NOT A VALID USER", "LISA V1.0",
MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Environment.Exit(1);
}

private void run_analyzers_parallel()


{
try
{
string[] ports = SerialPort.GetPortNames();
DataTable dt = new DataTable();
objCONNECTION = new Connection();

dt = objCONNECTION.GetDataTable("select(select device_name from


device d where d.id = deviceid)device_name, c.* from comportsetting c where
macaddress = '" + mac_address + "' and active = 'Y' order by comportid");

if (dt != null && dt.Rows.Count > 0)


{
AnalyzerDetails.Controls.Clear();
// Use HashSet for faster lookup of available ports.
HashSet<string> availablePorts = new HashSet<string>(ports);

foreach (DataRow row in dt.Rows)


{
bool chk_comport_present = false;
if (row["protocol"].ToString() == "SERIAL")
{
// Check if the port number exists in the available
ports.
chk_comport_present =
availablePorts.Contains(row["portnumber"].ToString());
}
else
{
chk_comport_present = true; // Non-serial protocols are
assumed present.
}

if (chk_comport_present == true)
{
UserControlAnalyzer ucl = new UserControlAnalyzer(row);
AnalyzerDetails.Controls.Add(ucl);
}
}
}
else
{
MessageBox.Show("No Port Configuration Available", "LISA V1.0",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}
catch (Exception ex)
{
sw = File.AppendText(Application.StartupPath + "\\
run_analyzer_parallel.log");
sw.WriteLine(DateTime.Now.ToString() + ex.Message);
sw.Close();
}
}

private string check_macaddress()


{
string text = string.Empty;
NetworkInterface[] allNetworkInterfaces =
NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in allNetworkInterfaces)
{
if (networkInterface.OperationalStatus == OperationalStatus.Up)
{
text += networkInterface.GetPhysicalAddress().ToString();
break;
}
}
return text;
}

private void button_exit_Click(object sender, EventArgs e)


{
if (MessageBox.Show("Do You Want to Close LISA", "Close!",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Environment.Exit(Environment.ExitCode);
}
}

private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)


{
}

private void Form_Main_Load(object sender, EventArgs e)


{
try
{
run_analyzers_parallel();
}
catch (Exception ex)
{
run_analyzers_parallel();
}
}

private void button_minimize_Click(object sender, EventArgs e)


{
base.WindowState = FormWindowState.Minimized;
Hide();
notifyIconLISA.Visible = true;
notifyIconLISA.ShowBalloonTip(1000, "LISA", " Minimized",
ToolTipIcon.Info);
}

private void lISASETUPToolStripMenuItem_Click(object sender, EventArgs e)


{

private void ANALYZERCONFIGURATIONToolStripMenuItem_Click(object sender,


EventArgs e)
{
try
{
PortConfiguration objportconfiguration = new PortConfiguration();
objportconfiguration.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

/*try
{
PortConfiguration objportConfiguration = new PortConfiguration();
objportConfiguration.Show();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}*/

private void notifyIconLISA_Click(object sender, EventArgs e)


{

Show();
base.WindowState = FormWindowState.Normal;
notifyIconLISA.Visible = false;
}

private void button_reload_Click(object sender, EventArgs e)


{
try
{

System.Diagnostics.Process.Start(Application.StartupPath + "\\
LISA.exe");
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
catch (Exception ex)
{

}
}
private List<Socket> activeSocketList = new List<Socket>();
private List<TcpClient> activeTcpList = new List<TcpClient>();
private List<NetworkStream> activeStreamList = new List<NetworkStream>();
private bool isClosing = false;

// This method registers all connections you create


public void RegisterConnection(Socket socket, TcpClient client,
NetworkStream stream)
{
if (socket != null) activeSocketList.Add(socket);
if (client != null) activeTcpList.Add(client);
if (stream != null) activeStreamList.Add(stream);
}
private void portConfigurationToolStripMenuItem_Click(object sender,
EventArgs e)
{
MessageBox.Show("LISA", "LISA V1.0", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)


{

if (MessageBox.Show("Do You Want to Close LISA", "Close!",


MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
isClosing = true;

// First try to gracefully close each device using its protocol


handler
// For example, call yourDeviceInstance.CloseConnection(stream) for
each device

// Wait a brief moment for messages to be sent


Thread.Sleep(200);

// Then forcefully close all streams


foreach (NetworkStream stream in activeStreamList)
{
try
{
if (stream != null)
{
stream.Flush();
stream.Close();
stream.Dispose();
}
}
catch (Exception) { /* Ignore */ }
}

// Then close all TcpClients


foreach (TcpClient client in activeTcpList)
{
try
{
if (client != null)
{
if (client.Connected) client.Close();
client.Dispose();
}
}
catch (Exception) { /* Ignore */ }
}

// Finally close all raw sockets


foreach (Socket socket in activeSocketList)
{
try
{
if (socket != null && socket.Connected)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close(1000);
socket.Dispose();
}
}
catch (Exception) { /* Ignore */ }
}

// Clear all collections


activeSocketList.Clear();
activeTcpList.Clear();
activeStreamList.Clear();
Environment.Exit(Environment.ExitCode);
}
else
{
// User clicked No, cancel the close operation.
e.Cancel = true;
}
}

private void menuStrip1_ItemClicked(object sender,


ToolStripItemClickedEventArgs e)
{
}

private void menuStrip1_Click(object sender, EventArgs e)


{
PortConfiguration objportconfiguration = new PortConfiguration();
objportconfiguration.Show();
}
}
}

You might also like