123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Management;
- using System.Reflection;
- using System.ServiceProcess;
- using System.Threading;
- using System.Threading.Tasks;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.Logging;
- using Timer = System.Timers.Timer;
- namespace PRSServer
- {
- public class PRSService : ServiceBase
- {
- private IEngine? _engine;
- private string _servicename;
- private Thread? _thread;
- //private CancellationTokenSource Source = new();
- // TODO: Is this necessary?
- private App app;
- public PRSService(string serviceName, App app)
- {
- this.app = app;
- CanPauseAndContinue = false;
- CanShutdown = true;
- if (string.IsNullOrWhiteSpace(serviceName))
- _servicename = GetServiceName();
- else
- _servicename = serviceName;
- ServiceName = _servicename;
- }
- private string GetServiceName()
- {
- if (string.IsNullOrWhiteSpace(_servicename))
- {
- var processId = Process.GetCurrentProcess().Id;
- var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service where ProcessId = " + processId);
- var collection = searcher.Get();
- var services = collection.Cast<ManagementBaseObject>();
- _servicename = services.Any() ? (string)services.First()["Name"] : "";
- Logger.Send(LogType.Information, "", string.Format("Service Name is [{0}]", _servicename));
- }
- return _servicename;
- }
- public static LocalConfiguration<ServerSettings> GetConfiguration(string section = "")
- {
- // TODO : Remove the fallback location
- var configuration = new LocalConfiguration<ServerSettings>(CoreUtils.GetCommonAppData(), section);
- if (!File.Exists(configuration.GetFileName()))
- {
- var oldsettings = new LocalConfiguration<ServerSettings>(
- Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location),
- ""
- );
- if (File.Exists(oldsettings.GetFileName()))
- {
- // Transition old data to new location
- var settings = oldsettings.LoadAll();
- configuration.SaveAll(settings);
- File.Delete(oldsettings.GetFileName());
- }
- }
- return configuration;
- }
- public void Run(string servicename)
- {
- _servicename = servicename;
- OnStart(new string[] { });
- }
- public void Halt()
- {
- OnStop();
- }
- protected override void OnStart(string[] args)
- {
- Logger.OnLog += MainLogger.Send;
- //InABox.Logging.NamedPipeLogger.Start(CoreUtils.GetPath());
- var svcname = GetServiceName();
- var settings = GetConfiguration(svcname).Load();
- if (settings != null)
- {
- var server = settings.CreateServer(svcname);
- if (settings.Type == ServerType.Database)
- _engine = new DatabaseEngine();
- else if (settings.Type == ServerType.GPS)
- _engine = new GPSEngine();
- else if (settings.Type == ServerType.AutoDiscovery)
- _engine = new AutoDiscoveryEngine();
- else if (settings.Type == ServerType.Schedule)
- _engine = new ScheduleEngine();
- else if (settings.Type == ServerType.Web)
- _engine = new WebEngine();
- else if (settings.Type == ServerType.Certificate)
- _engine = new CertificateEngine();
- if (_engine != null)
- {
- _engine.ServiceName = svcname;
- _engine.Version = CoreUtils.GetVersion();
- _engine.Configure(server);
- /*_task = Task.Run(() =>
- {
- _engine.Run();
- }, Source.Token);*/
- _thread = new Thread(() => { _engine.Run(); });
- _thread.IsBackground = true;
- _thread.Start();
- }
- }
- }
- protected override void OnStop()
- {
- Logger.Send(LogType.Information, "", "Shutting down Service: " + _servicename);
- _engine?.Stop();
- /*var timer = new Timer { Interval = 1000 };
- timer.Elapsed += (o, e) =>
- {
- try
- {
- Logger.Send(LogType.Information, "", "Terminating Process..");
- MainLogger.Stop();
- Environment.Exit(0);
- }
- catch (Exception err)
- {
- Logger.Send(LogType.Error, "", "Exception while killing process: " + err.Message);
- }
- };
- timer.AutoReset = false;
- timer.Enabled = true;*/
- Logger.Send(LogType.Information, "", "Terminating Process..");
- MainLogger.Stop();
- }
- }
- }
|