Exemple #1
0
        public CommandRunnerMongo(IConfiguration config, IMediaCache cache, MongoDb.CaveContext db, bool isCommandLine = false)
        {
            _config = config ?? throw new ArgumentNullException(nameof(config));
            _cache  = cache ?? throw new ArgumentNullException(nameof(config));
            _db     = db ?? throw new ArgumentNullException(nameof(db));

            this._isCommandLine = isCommandLine;

            Console.WriteLine($"Database at: {_db.ConnectionString}");

            _rng = RandomNumberGenerator.Create();

            _commands = new Dictionary <Type, Func <object, object> >();

            foreach (var method in this.GetType().GetMethods())
            {
                var pars = method.GetParameters();

                if (pars.Length == 1 &&
                    pars[0].ParameterType.IsSubclassOf(typeof(API.Request)) &&
                    method.ReturnType.IsSubclassOf(typeof(API.Response)))
                {
                    Type requestType = pars[0].ParameterType;
                    _commands[requestType] = (request) =>
                    {
                        var response = method.Invoke(this, new object[] { request });
                        return(response);
                    };
                    Console.WriteLine("Added command for {0}", requestType.Name);
                }
            }

            BootStrap();
        }
Exemple #2
0
        private void ProgMain(string[] args)
        {
            _config     = new ConfigurationReader();
            _db         = new MongoDb.CaveContext(_config.ConnectionString);
            _mediaCache = new MediaCache(_config);
            //var loggerFactory = db.GetService<ILoggerFactory>();
            //loggerFactory.AddProvider(new ConsoleLoggerProvider());

            try
            {
                if (args.Length == 0 || args[0] == "-i")
                {
                    // initialize the database

                    CommandRunnerMongo cmd = new CommandRunnerMongo(_config, _mediaCache, _db, true);

                    if (args.Length > 0 && args[0] == "-i")
                    {
                        Console.WriteLine("New Cave Survey Server {0} Interaction Mode", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
                        Console.Write("> ");
                        var line = Console.ReadLine();
                        while (line != "quit")
                        {
                            string[] cmdArgs = SplitLine(line);
                            RunCommand(cmdArgs, cmd);
                            Console.Write("> ");
                            line = Console.ReadLine();
                        }
                    }

                    RunCommand(args, cmd);
                }
                else if (args[0] == "-runhttpapi")
                {
                    using (var api = new CaveCacheHttp(_config, _mediaCache, _db))
                    {
                        while (true)
                        {
                            System.Threading.Thread.Sleep(250);
                        }
                    }
                }
            }
            finally
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Console.ReadKey();
                }
            }
        }