100 lines
3.4 KiB
C#
100 lines
3.4 KiB
C#
using Microsoft.Owin.Hosting;
|
|
using Step.UI;
|
|
using System.Threading;
|
|
using TeamDev.SDK.MVVM;
|
|
using Step.Config;
|
|
using static Step.Config.ServerConfig;
|
|
using static Step.Utils.StepLogger;
|
|
using static Step.Model.Constants;
|
|
using Step.Database;
|
|
using Step.Core;
|
|
using Step.Listeners;
|
|
using System;
|
|
using Step.Utils;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
|
|
namespace Step
|
|
{
|
|
public class Application
|
|
{
|
|
|
|
public static readonly ManualResetEvent StopRequest = new ManualResetEvent(false);
|
|
//Set the Mutef of GUID
|
|
private static Mutex CmsStepMutex = new Mutex(true, "{689e76ce-b845-49f0-919c-c26491079fca}");
|
|
|
|
public static int MessageService { get; private set; }
|
|
|
|
public static void Main()
|
|
{
|
|
LogInfo("Application started");
|
|
|
|
//Check if is already running an instance of this application
|
|
if (!CmsStepMutex.WaitOne(TimeSpan.Zero, true))
|
|
ExceptionManager.Manage(ERROR_LEVEL.FATAL, "Only one istance of Step-Server can be executed!");
|
|
|
|
// Start WinForm
|
|
ServerControlWindow.Start();
|
|
|
|
// Read config
|
|
ServerConfigController.ReadStartupConfig();
|
|
|
|
DatabaseContext.SetUpDbConnectionAndDbConfig();
|
|
|
|
// Register listener to "close application" messages
|
|
MessageServices.Current.Subscribe(SEND_STOP_SERVER, (a, b) =>
|
|
{
|
|
StopRequest.Set();
|
|
if (NcConfig.NcVendor.ToUpper() == NC_VENDOR.SIEMENS)
|
|
ThreadSiemensHmi.StopWindow();
|
|
});
|
|
|
|
// Start server services
|
|
if(!ValidateAddress(ServerStartupConfig.ServerAddress))
|
|
ExceptionManager.Manage(ERROR_LEVEL.FATAL, "IP Address not valid (must be one configured in Windows-OS)!");
|
|
|
|
StartOptions opt = new StartOptions();
|
|
opt.Urls.Add("http://localhost:" + ServerStartupConfig.ServerPort.ToString());
|
|
opt.Urls.Add("http://127.0.0.1:" + ServerStartupConfig.ServerPort.ToString());
|
|
|
|
if (!String.IsNullOrWhiteSpace(ServerStartupConfig.ServerAddress.ToString()))
|
|
opt.Urls.Add("http://" + ServerStartupConfig.ServerAddress.ToString() + ":" + ServerStartupConfig.ServerPort.ToString());
|
|
|
|
using (WebApp.Start<Step.App_Start.Startup>(opt))
|
|
{
|
|
|
|
// Start Threads
|
|
ThreadsHandler.Start();
|
|
// Start listeners
|
|
ListenersHandler.Start();
|
|
|
|
// Wait interrupt from client
|
|
StopRequest.WaitOne();
|
|
LogInfo("Application closed");
|
|
}
|
|
// Stop Threads
|
|
ThreadsHandler.Close();
|
|
// Stop messageservice listeners
|
|
ListenersHandler.Stop();
|
|
// Close WinForm
|
|
ServerControlWindow.Stop();
|
|
}
|
|
|
|
private static Boolean ValidateAddress(String Addr) {
|
|
//If is an asterisk is OK
|
|
if (String.IsNullOrWhiteSpace(Addr) || Addr == "*")
|
|
return true;
|
|
|
|
//Find an IP Address
|
|
foreach (IPAddress ipAddr in Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList, a => a.AddressFamily == AddressFamily.InterNetwork))
|
|
{
|
|
if (ipAddr.ToString() == Addr)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|