217 lines
8.0 KiB
C#
217 lines
8.0 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Windows.Forms;
|
|||
|
using System.Diagnostics;
|
|||
|
using SafeMobileLib;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Net;
|
|||
|
using Telerik.WinControls;
|
|||
|
using System.Threading;
|
|||
|
|
|||
|
namespace AppServerMobile
|
|||
|
{
|
|||
|
public partial class MainForm : Form
|
|||
|
{
|
|||
|
private TcpClient client;
|
|||
|
private BackgroundWorker bgWorkerRegistration;
|
|||
|
private WaitingForm wf;
|
|||
|
private SDRegistration sdReg;
|
|||
|
public MainForm()
|
|||
|
{
|
|||
|
testIfAlreadyRunning();
|
|||
|
InitializeComponent();
|
|||
|
|
|||
|
|
|||
|
Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|||
|
Console.WriteLine("Version: " + v.ToString());
|
|||
|
Console.WriteLine("--------AppServer MOBILE!!! " + v.ToString() + "---------");
|
|||
|
Console.WriteLine("--------www.safemobile.com---------------");
|
|||
|
this.Text += " " + v.ToString();
|
|||
|
|
|||
|
Program.CFG = new Config();
|
|||
|
AndroidHandler aHandler = new AndroidHandler();
|
|||
|
aHandler.Start();
|
|||
|
MessageBussHandler mHandler = new MessageBussHandler(Program.CFG.msgBusIP, Program.CFG.msgBusPort);
|
|||
|
|
|||
|
this.WindowState = FormWindowState.Minimized;
|
|||
|
this.Visible = false;
|
|||
|
notifyIcon1.Visible = true;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
Utils.WriteEventLog("Safemobile", "AppServerMobile ON", EventLogEntryType.Information, 3162);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
SM.Debug("Unable to write log Event ex:" + ex.ToString());
|
|||
|
}
|
|||
|
|
|||
|
bgWorkerRegistration = new BackgroundWorker();
|
|||
|
bgWorkerRegistration.DoWork += new DoWorkEventHandler(bgWorkerRegistration_DoWork);
|
|||
|
bgWorkerRegistration.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorkerRegistration_RunWorkerCompleted);
|
|||
|
bgWorkerRegistration.RunWorkerAsync();
|
|||
|
|
|||
|
wf = new WaitingForm();
|
|||
|
wf.Show();
|
|||
|
}
|
|||
|
|
|||
|
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
|
|||
|
{
|
|||
|
this.Visible = true;
|
|||
|
this.WindowState = FormWindowState.Normal;
|
|||
|
notifyIcon1.Visible = false;
|
|||
|
}
|
|||
|
|
|||
|
private void MainForm_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
this.Visible = false;
|
|||
|
}
|
|||
|
|
|||
|
private void MainForm_Resize(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (this.WindowState == FormWindowState.Minimized)
|
|||
|
{
|
|||
|
this.Visible = false;
|
|||
|
notifyIcon1.Visible = true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
Utils.WriteEventLog("Safemobile", "AppServerMobile OFF", EventLogEntryType.Information, 3162);
|
|||
|
System.Environment.Exit(0);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
SM.Debug("Unable to write log Event ex:" + ex.ToString());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region bg worker registration events
|
|||
|
void bgWorkerRegistration_DoWork(object sender, DoWorkEventArgs e)
|
|||
|
{
|
|||
|
sdReg = getRegistration();
|
|||
|
}
|
|||
|
|
|||
|
void bgWorkerRegistration_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|||
|
{
|
|||
|
if (wf != null)
|
|||
|
if (wf.IsInitialized)
|
|||
|
if (wf.IsLoaded)
|
|||
|
{
|
|||
|
SM.Debug("Shuting down waiting form");
|
|||
|
wf.Close();
|
|||
|
}
|
|||
|
if (appServerMissing)
|
|||
|
{
|
|||
|
MessageBox.Show("Could not reach Application Server. Please check the connection and try again!!!", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
System.Environment.Exit(0);
|
|||
|
}
|
|||
|
ContinueLogin();
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
private bool appServerMissing = false;
|
|||
|
private SDRegistration getRegistration()
|
|||
|
{
|
|||
|
SDRegistration toReturn = new SDRegistration();
|
|||
|
toReturn.ip = "valid";
|
|||
|
/* try
|
|||
|
{
|
|||
|
client = new TcpClient();
|
|||
|
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(Program.CFG.DB_IP), Program.CFG.regPort);
|
|||
|
client.Connect(serverEndPoint);
|
|||
|
NetworkStream clientStream = client.GetStream();
|
|||
|
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
|
|||
|
byte[] buffer = encoding.GetBytes("400");
|
|||
|
//encryption doesn't work, to be added when working
|
|||
|
//byte[] encodedBuffer = Encryption.Encrypt(buffer);
|
|||
|
clientStream.Write(buffer, 0, buffer.Length);
|
|||
|
SM.Debug("Sent registration request to server, waiting...");
|
|||
|
byte[] message = new byte[128];
|
|||
|
//byte[] decryptedMessage = Encryption.Decrypt(message);
|
|||
|
clientStream.ReadTimeout = 10000;
|
|||
|
|
|||
|
int result = clientStream.Read(message, 0, message.Length);
|
|||
|
|
|||
|
SM.Debug("Received registration from server");
|
|||
|
String decodedString = encoding.GetString(message);
|
|||
|
String[] options = decodedString.Split(';');
|
|||
|
if (options[0].Equals("401"))
|
|||
|
{
|
|||
|
if (options[1].StartsWith("inv"))
|
|||
|
{
|
|||
|
toReturn.ip = "invalid";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
toReturn.ip = "valid";
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
SM.Debug("Dongle registration:" + ex.ToString());
|
|||
|
appServerMissing = true;
|
|||
|
toReturn.ip = "invalid";
|
|||
|
}*/
|
|||
|
return toReturn;
|
|||
|
}
|
|||
|
|
|||
|
private void ContinueLogin()
|
|||
|
{
|
|||
|
SM.Debug("Continuing login form after registration info aquired from app server IP:" + sdReg.ip);
|
|||
|
if (sdReg.ip.Equals("invalid"))
|
|||
|
{
|
|||
|
RadMessageBox.Show("Your dongle don't have license for Aplication Server Mobile", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
|
|||
|
System.Environment.Exit(1);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void testIfAlreadyRunning()
|
|||
|
{
|
|||
|
Process[] processlist = Process.GetProcesses();
|
|||
|
Process curentP = Process.GetCurrentProcess();
|
|||
|
int count = 0;
|
|||
|
foreach (Process theprocess in processlist)
|
|||
|
{
|
|||
|
if (theprocess.ProcessName == curentP.ProcessName)
|
|||
|
{
|
|||
|
count++;
|
|||
|
}
|
|||
|
}
|
|||
|
if (count > 1)
|
|||
|
{
|
|||
|
SM.Debug("AppMobile already running!!!");
|
|||
|
DialogResult dr = MessageBox.Show("Aplication already running!!!Restart application?", "error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
|
|||
|
if (dr == DialogResult.No)
|
|||
|
{
|
|||
|
System.Environment.Exit(0);
|
|||
|
}
|
|||
|
|
|||
|
foreach (Process theprocess in processlist)
|
|||
|
{
|
|||
|
if (theprocess.ProcessName == curentP.ProcessName)
|
|||
|
{
|
|||
|
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
|
|||
|
Console.WriteLine("Curent Process: {0} ID: {1}", curentP.ProcessName, curentP.Id);
|
|||
|
if (theprocess.Id != curentP.Id)
|
|||
|
{
|
|||
|
Console.WriteLine("Killing Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
|
|||
|
theprocess.Kill();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
Thread.Sleep(1000);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|