Esempio n. 1
0
        static void RunServer()
        {
            var config = ConfigLoadingManager.GetInstance().GetConfig();

            SortedDictionary <string, RouteHandler> routeHandlers = new SortedDictionary <string, RouteHandler>();

            routeHandlers.Add("/article/latest", ArticleHandler.GetInstance());
            routeHandlers.Add("/article", ArticleHandler.GetInstance());
            routeHandlers.Add("/interpreter", InterpreterHandler.GetInstance());

            SortedList <string, RouteHandler> regexRouteHandlers = new SortedList <string, RouteHandler>();

            regexRouteHandlers.Add(@"\/article\/\d+$", ArticleHandler.GetInstance());

            LogManager exceptionLogger = new LogManager(ConfigLoadingManager.GetInstance().GetConfig().ExceptionLogFile);
            LogManager accessLogger    = new LogManager(ConfigLoadingManager.GetInstance().GetConfig().AccessLogFile);

            ExecuteRouteHandler executeRouteHandler = new ExecuteRouteHandler {
                RouteHandlers = routeHandlers, RegexRouteHandlers = regexRouteHandlers, ExceptionLogger = exceptionLogger, AccessLogger = accessLogger
            };

            HttpRequestDispatcher httpDispatcher  = null;
            HttpRequestDispatcher httpsDispatcher = null;

            if (config.HttpListenAddress.IsAvailable())
            {
                httpDispatcher = new HttpRequestDispatcher();
                httpDispatcher.Start(config.HttpListenAddress.IP, config.HttpListenAddress.Port,
                                     config.SessionReadBufferSize, config.SessionNoActionTimeout,
                                     executeRouteHandler.HttpRequestHandler, executeRouteHandler.InternalServerError,
                                     exceptionLogger);
                Console.WriteLine("Http Server - " + Environment.NewLine + config.HttpListenAddress.IP + ":" + config.HttpListenAddress.Port);
            }
            if (config.HttpsListenAddress.IsAvailable())
            {
                httpsDispatcher = new HttpRequestDispatcher();
                httpsDispatcher.Start(config.HttpsListenAddress.IP, config.HttpsListenAddress.Port,
                                      config.SessionReadBufferSize, config.SessionNoActionTimeout,
                                      new X509Certificate2(config.HttpsPfxCertificate, config.HttpsPfxCertificatePassword),
                                      executeRouteHandler.HttpRequestHandler, executeRouteHandler.InternalServerError,
                                      exceptionLogger);
                Console.WriteLine("Https Server - " + Environment.NewLine + config.HttpsListenAddress.IP + ":" + config.HttpsListenAddress.Port);
            }
            if (httpDispatcher == null && httpsDispatcher == null)
            {
                return;
            }

            exceptionLogger.LogAsync("startup successfully");

            bool   stopFlag = false;
            object mut      = new object();

            new Thread(() =>
            {
                const int second = 5;
                Thread.Sleep(second * 1000);
                while (true)
                {
                    Console.Clear();
                    Console.Write("Active Session: " + (httpDispatcher.SessionCount + httpsDispatcher.SessionCount).ToString());
                    lock (mut)
                    {
                        for (int i = 0; i < second; i++)
                        {
                            if (stopFlag)
                            {
                                return;
                            }
                            Console.Write('.');
                            Monitor.Wait(mut, 1000);
                            if (stopFlag)
                            {
                                return;
                            }
                        }
                    }
                }
            }).Start();

            Console.WriteLine("press any key to shut down...");
            Console.ReadKey();

            lock (mut)
            {
                stopFlag = true;
                Monitor.Pulse(mut);
            }

            //stop dispatcher
            if (httpDispatcher != null)
            {
                httpDispatcher.Stop();
            }
            if (httpsDispatcher != null)
            {
                httpsDispatcher.Stop();
            }

            //stop logic
            ArticleHandler.GetInstance().Stop();

            exceptionLogger.LogAsync("stopped");

            //stop logger
            exceptionLogger.Stop();
            accessLogger.Stop();
        }