Ejemplo n.º 1
0
        public static int Main(string[] args)
        {
            // Load the configuration file stored in the
            // executable's resources.
            configmanager = new ConfigurationManager(
                typeof(MainClass).Assembly,
                "ConfigurationManager.xml");

            configmanager.LoadCommandLineArgs(args);

            // Show the help and exit.
            if ((bool)configmanager ["help"] || (bool)configmanager ["?"])
            {
                ShowHelp();
                return(0);
            }

            // Show the version and exit.
            if ((bool)configmanager ["version"])
            {
                ShowVersion();
                return(0);
            }

            string config = (string)configmanager ["config"];

            if (config == null)
            {
                Console.WriteLine("You must pass /config=<filename> option. See 'help' for more info");
                return(1);
            }


            try {
                string config_file = (string)configmanager ["configfile"];
                if (config_file != null)
                {
                    configmanager.LoadXmlConfig(config_file);
                }
            } catch (ApplicationException e) {
                Console.WriteLine(e.Message);
                return(1);
            } catch (System.Xml.XmlException e) {
                Console.WriteLine("Error reading XML configuration: {0}", e.Message);
                return(1);
            }

            try {
                string log_level = (string)configmanager ["loglevels"];

                if (log_level != null)
                {
                    Logger.Level = (LogLevel)Enum.Parse(typeof(LogLevel), log_level);
                }
            } catch {
                Console.WriteLine("Failed to parse log levels.");
                Console.WriteLine("Using default levels: {0}", Logger.Level);
            }

            // Enable console logging during Main ().
            Logger.WriteToConsole = true;

            try {
                string log_file = (string)configmanager ["logfile"];

                if (log_file != null)
                {
                    Logger.Open(log_file);
                }
            } catch (Exception e) {
                Logger.Write(LogLevel.Error, "Error opening log file: {0}", e.Message);
                Logger.Write(LogLevel.Error, "Events will not be logged.");
            }

            Logger.Write(LogLevel.Debug,
                         Assembly.GetExecutingAssembly().GetName().Name);

            bool auto_map = false;             //(bool) configmanager ["automappaths"];

            string applications = (string)configmanager ["applications"];
            string app_config_file;
            string app_config_dir;
            List <WebAppConfig> webapps = new List <WebAppConfig> ();

            try {
                app_config_file = (string)configmanager ["appconfigfile"];
                app_config_dir  = (string)configmanager ["appconfigdir"];
            } catch (ApplicationException e) {
                Logger.Write(LogLevel.Error, e.Message);
                return(1);
            }

            if (config != null)
            {
                webapps.AddRange(ConfigUtils.GetApplicationsFromConfigFile(config));
            }

            if (applications != null)
            {
                webapps.AddRange(ConfigUtils.GetApplicationsFromCommandLine(applications));
            }

            if (app_config_file != null)
            {
                webapps.AddRange(ConfigUtils.GetApplicationsFromConfigFile(app_config_file));
            }

            if (app_config_dir != null)
            {
                webapps.AddRange(ConfigUtils.GetApplicationsFromConfigDirectory(app_config_dir));
            }

            if (webapps.Count == 0 && !auto_map)
            {
                Logger.Write(LogLevel.Error,
                             "There are no applications defined, and path mapping is disabled.");
                Logger.Write(LogLevel.Error,
                             "Define an application using /applications, /appconfigfile, /appconfigdir");

                /*
                 * Logger.Write (LogLevel.Error,
                 *      "or by enabling application mapping with /automappaths=True.");
                 */
                return(1);
            }

//			server.MaxConnections = (ushort)
//			                        configmanager ["maxconns"];
//			server.MaxRequests = (ushort)
//			                     configmanager ["maxreqs"];
//			server.MultiplexConnections = (bool)
//			                              configmanager ["multiplex"];

//			Logger.Write (LogLevel.Debug, "Max connections: {0}",
//				server.MaxConnections);
//			Logger.Write (LogLevel.Debug, "Max requests: {0}",
//				server.MaxRequests);
//			Logger.Write (LogLevel.Debug, "Multiplex connections: {0}",
//				server.MultiplexConnections);

            bool stopable = (bool)configmanager ["stopable"];

            Logger.WriteToConsole = (bool)configmanager ["printlog"];

            List <ConfigInfo> serverConfigs = ConfigUtils.GetConfigsFromFile(config, "server", typeof(AppServerConfig));

            if (serverConfigs.Count != 1)
            {
                if (serverConfigs.Count == 0)
                {
                    Console.WriteLine("Could not find <server> node in file '{0}'", config);
                }
                else
                {
                    Console.WriteLine("Only one server is supported currently. Please remove redudant <server> node from file '{0}'", config);
                }
                return(1);
            }
            IApplicationServer srv = (IApplicationServer)Activator.CreateInstance(serverConfigs [0].Type);

            srv.Configure(serverConfigs [0].Config);

            List <ConfigInfo> listenerConfigs = ConfigUtils.GetConfigsFromFile(config, "listener", typeof(ListenerConfig));

            if (listenerConfigs.Count != 1)
            {
                if (listenerConfigs.Count == 0)
                {
                    Console.WriteLine("Could not find <listener> node in file '{0}'", config);
                }
                else
                {
                    Console.WriteLine("Only one listener is supported currently. Please remove redudant <listener> node from file '{0}'", config);
                }
                return(1);
            }
            List <ConfigInfo> hostConfigs = ConfigUtils.GetConfigsFromFile(config, "apphost", typeof(AppHostConfig));

            if (hostConfigs.Count == 0)
            {
                Console.WriteLine("Can't find <apphost> node in file '{0}'", config);
                return(1);
            }

            IWebListener listener = (IWebListener)Activator.CreateInstance(listenerConfigs[0].Type);

            listener.Configure(listenerConfigs[0].Config, srv,
                               listenerConfigs[0].ListenerTransport != null? listenerConfigs[0].ListenerTransport.Type: null,
                               listenerConfigs[0].ListenerTransport != null? listenerConfigs[0].ListenerTransport.Config: null,
                               listenerConfigs[0].AppHostTransport != null? listenerConfigs[0].AppHostTransport.Type: null,
                               listenerConfigs[0].AppHostTransport != null? listenerConfigs[0].AppHostTransport.Config: null
                               );

            foreach (WebAppConfig appConfig in webapps)
            {
                srv.CreateApplicationHost(
                    hostConfigs[0].Type, hostConfigs[0].Config,
                    appConfig,
                    listener.Transport, listener.AppHostTransportType,
                    listenerConfigs[0].AppHostTransport != null ? listenerConfigs[0].AppHostTransport.Config: null);
            }
            if (listener.Listen() != 0)
            {
                Logger.Write(LogLevel.Error, "Could not start server");
                return(1);
            }

            configmanager = null;

            if (stopable)
            {
                Console.WriteLine("Hit Return to stop the server.");
                Console.ReadLine();
            }
            else
            {
                UnixSignal[] signals = new UnixSignal[] {
                    new UnixSignal(Signum.SIGINT),
                    new UnixSignal(Signum.SIGTERM),
                };

                // Wait for a unix signal
                for (bool exit = false; !exit;)
                {
                    int id = UnixSignal.WaitAny(signals);

                    if (id >= 0 && id < signals.Length)
                    {
                        if (signals [id].IsSet)
                        {
                            exit = true;
                        }
                    }
                }
            }
            listener.Shutdown();

            return(0);
        }
		public FastCgiNetworkConnector (Socket client, IWebListener listener) : this (client)
		{
			this.Transport = listener.Transport;
		}
		public virtual void Configure (IWebListener listener, object config)
		{
			this.listener = listener;
		}
 public FastCgiNetworkConnector(Socket client, IWebListener listener) : this(client)
 {
     this.Transport = listener.Transport;
 }
 public virtual void Configure(IWebListener listener, object config)
 {
     this.listener = listener;
 }