Exemple #1
0
        static Result SubMain(ILogger logger)
        {
            var configuration = Configuration.Load("configuration.xml");

            _database = new Database(configuration);

            logger.Write(LogType.Notice, "Testing database connection ...");

            try
            {
                _database.TestConnection();
            }
            catch (Exception ex)
            {
                _database = null;
                logger.Write(LogType.Error, "Failed to connect to database.");
                logger.Write(LogType.Error, $"({ex.GetType().Name}): {ex.Message}");
                return(Result.DatabaseConnectionFailed);
            }

            logger.Write(LogType.Normal, "DB OK");

            var minecraftRoot = GetEnvironmentVariable("MCHOST_MINECRAFT");

            if (minecraftRoot == null ||
                !Directory.Exists(minecraftRoot = Path.GetFullPath(minecraftRoot)))
            {
                if (minecraftRoot == null)
                {
                    logger.Write(LogType.Error, "The MCHOST_MINECRAFT environment variable was not provided.");
                }
                else
                {
                    logger.Write(LogType.Error, "The MCHOST_MINECRAFT path is not pointing to a valid directory.");
                }
                return(Result.HostVariableNotFound);
            }

            MinecraftRoot = minecraftRoot;

            var serviceBindingMatch = Regex.Match(configuration["ServiceBinding"], @"^([0-9.]+):(\d+)$", RegexOptions.IgnoreCase);

            if (!serviceBindingMatch.Success)
            {
                logger.Write(LogType.Error, "The ServiceBinding is invalid in the configuration file. The format is ip:port");
                logger.Write(LogType.Error, "The IP 0.0.0.0 can be used to bind on all network interfaces.");
                return(Result.ServiceBindingInvalid);
            }

            var ip   = serviceBindingMatch.Groups[1].Value;
            var port = int.Parse(serviceBindingMatch.Groups[2].Value);

            var maxConcurrentInstances = int.Parse(configuration["MaxConcurrentInstances"]);

            using (var webSocketService = new WebSocketService(logger, _database, configuration))
                using (var instanceManager = new InstanceManager(logger, configuration, maxConcurrentInstances))
                {
                    webSocketService.SetInstanceManager(instanceManager); // sets up callbacks

                    try
                    {
                        webSocketService.Start(new IPEndPoint[] { new IPEndPoint(IPAddress.Parse(ip), port) });
                    }
                    catch (Exception ex)
                    {
                        logger.Write(LogType.Error, $"Failed to bind on interface {ip}:{port}");
                        logger.Write(LogType.Error, $"({ex.GetType().Name}): {ex.Message}");
                        return(Result.FailedToBindInterface);
                    }

                    _database.AddLog("Started minecraft service host.");
                    logger.Write(LogType.Notice, "Started minecraft service host.");

                    var nextInstanceProcess = DateTime.UtcNow.AddSeconds(1);

                    while (true)
                    {
                        var now = DateTime.UtcNow;

                        if (Console.KeyAvailable)
                        {
                            var key = Console.ReadKey(true);

                            /*if (key.Key == ConsoleKey.F1)
                             * {
                             *  var package = new Package()
                             *  {
                             *      Name = "MCInstance",
                             *      Description = "",
                             *      Filename = @"packages\test.zip"
                             *  };
                             *  instanceManager.CreateInstance(package, true);
                             * }*/

                            if (key.Key == ConsoleKey.Escape)
                            {
                                break;
                            }
                        }

                        webSocketService.Process();
                        webSocketService.ProcessDelegates();

                        if (now >= nextInstanceProcess)
                        {
                            instanceManager.RemoveDeadInstances();
                            nextInstanceProcess = now.AddSeconds(1);
                        }

                        Thread.Sleep(50);
                    }

                    _database.AddLog("Stopped minecraft service host.");
                    logger.Write(LogType.Notice, "Stopped minecraft service host.");
                }

            return(0);
        }