示例#1
0
        public void Cleanup()
        {
            _service.Stop();
            _service.Dispose();

            // Clear the PID file if it exists.
            File.Delete(Constants.PID_FILE_PATH);
        }
示例#2
0
文件: Program.cs 项目: kf6kjg/f-stop
        public static int Main(string[] args)
        {
            // First line, hook the appdomain to the crash reporter
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            var createdNew = true;
            var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "70a9f94f-59e8-4073-93ab-00aaacc26111", out createdNew);

            if (!createdNew)
            {
                LOG.Error("Server process already started, please stop that server first.");
                return(2);
            }

            // Add the arguments supplied when running the application to the configuration
            var configSource = new ArgvConfigSource(args);

            // Commandline switches
            configSource.AddSwitch("Startup", "inifile");
            configSource.AddSwitch("Startup", "logconfig");
            configSource.AddSwitch("Startup", "pidfile");

            var startupConfig = configSource.Configs["Startup"];

            var pidFileManager = new PIDFileManager(startupConfig.GetString("pidfile", string.Empty));

            // Configure Log4Net
            var logConfigFile = startupConfig.GetString("logconfig", string.Empty);

            if (string.IsNullOrEmpty(logConfigFile))
            {
                XmlConfigurator.Configure();
                LogBootMessage();
                LOG.Info("Configured log4net using ./WHIP_LRU.exe.config as the default.");
            }
            else
            {
                XmlConfigurator.Configure(new FileInfo(logConfigFile));
                LogBootMessage();
                LOG.Info($"Configured log4net using \"{logConfigFile}\" as configuration file.");
            }

            // Configure nIni aliases and locale
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US", true);

            configSource.Alias.AddAlias("On", true);
            configSource.Alias.AddAlias("Off", false);
            configSource.Alias.AddAlias("True", true);
            configSource.Alias.AddAlias("False", false);
            configSource.Alias.AddAlias("Yes", true);
            configSource.Alias.AddAlias("No", false);

            var isRunning = true;

            F_Stop f_stop = null;

            // Handlers for signals.
            Console.CancelKeyPress += (sender, cargs) => {
                LOG.Debug("CTRL-C pressed, terminating.");
                isRunning = false;
                f_stop?.Stop();

                cargs.Cancel = true;
                waitHandle.Set();
            };

            // TODO: incorporate UNIX signals for reloading etc, once the crossplatform kinks have been worked out in WHIP-LRU.

            while (isRunning)
            {
                // Read in the ini file
                ReadConfigurationFromINI(configSource);

                var configRead = configSource.Configs["AssetsRead"];

                var serversRead = GetServers(configSource, configRead, _assetServersByName);

                var chattelConfigRead = GetConfig(configRead, serversRead);

                var chattelReader = new ChattelReader(chattelConfigRead);

                var serverConfig = configSource.Configs["Server"];

                var address = serverConfig?.GetString("Address", F_Stop.DEFAULT_ADDRESS) ?? F_Stop.DEFAULT_ADDRESS;
                if (address == "*")
                {
                    address = "localhost";
                }
                var port            = (uint?)serverConfig?.GetInt("Port", (int)F_Stop.DEFAULT_PORT) ?? F_Stop.DEFAULT_PORT;
                var useSSL          = serverConfig?.GetBoolean("UseSSL", F_Stop.DEFAULT_USE_SSL) ?? F_Stop.DEFAULT_USE_SSL;
                var adminToken      = serverConfig?.GetString("AdminToken", F_Stop.DEFAULT_ADMIN_TOKEN) ?? F_Stop.DEFAULT_ADMIN_TOKEN;
                var validAssetTypes = serverConfig?.GetString("AllowedAssetTypes", F_Stop.DEFAULT_VALID_ASSET_TYPES) ?? F_Stop.DEFAULT_VALID_ASSET_TYPES;

                var cacheConfig = configSource.Configs["Cache"];

                var negativeCacheItemLifetime = TimeSpan.FromSeconds((uint?)cacheConfig?.GetInt("NegativeCacheItemLifetimeSeconds", (int)F_Stop.DEFAULT_NC_LIFETIME_SECONDS) ?? F_Stop.DEFAULT_NC_LIFETIME_SECONDS);

                var protocol = useSSL ? "https" : "http";

                var uri = new Uri($"{protocol}://{address}:{port}");
                f_stop = new F_Stop(
                    uri,
                    adminToken,
                    negativeCacheItemLifetime,
                    chattelReader,
                    validAssetTypes.Split(',').Select(type => sbyte.Parse(type))
                    );

                f_stop.Start();

                waitHandle.WaitOne();
            }

            return(0);
        }