예제 #1
0
        //		public static int Main (string [] args)
        public static int initXSP(string [] args, out ApplicationServer appServer)
        {
            bool nonstop = false;
            bool verbose = false;

            appServer = null;

            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
            string apps          = ConfigurationSettings.AppSettings ["MonoApplications"];
            string appConfigDir  = ConfigurationSettings.AppSettings ["MonoApplicationsConfigDir"];
            string appConfigFile = ConfigurationSettings.AppSettings ["MonoApplicationsConfigFile"];
            string rootDir       = ConfigurationSettings.AppSettings ["MonoServerRootDir"];
            object oport;
            string ip = ConfigurationSettings.AppSettings ["MonoServerAddress"];

#if MODMONO_SERVER
            string filename = ConfigurationSettings.AppSettings ["MonoUnixSocket"];
#endif
            if (ip == "" || ip == null)
            {
                ip = "0.0.0.0";
            }

            oport = ConfigurationSettings.AppSettings ["MonoServerPort"];
            if (oport == null)
            {
                oport = 8080;
            }

            Options options = 0;
            int     hash    = 0;
            for (int i = 0; i < args.Length; i++)
            {
                string a = args [i];
                hash ^= args [i].GetHashCode() + i;

                switch (a)
                {
#if MODMONO_SERVER
                case "--filename":
                    CheckAndSetOptions(a, Options.FileName, ref options);
                    filename = args [++i];
                    break;

                case "--terminate":
                    CheckAndSetOptions(a, Options.Terminate, ref options);
                    break;
#endif
                case "--port":
                    CheckAndSetOptions(a, Options.Port, ref options);
                    oport = args [++i];
                    break;

                case "--address":
                    CheckAndSetOptions(a, Options.Address, ref options);
                    ip = args [++i];
                    break;

                case "--root":
                    CheckAndSetOptions(a, Options.Root, ref options);
                    rootDir = args [++i];
                    break;

                case "--applications":
                    CheckAndSetOptions(a, Options.Applications, ref options);
                    apps = args [++i];
                    break;

                case "--appconfigfile":
                    CheckAndSetOptions(a, Options.AppConfigFile, ref options);
                    appConfigFile = args [++i];
                    break;

                case "--appconfigdir":
                    CheckAndSetOptions(a, Options.AppConfigDir, ref options);
                    appConfigDir = args [++i];
                    break;

                case "--nonstop":
                    nonstop = true;
                    break;

                case "--help":
                    ShowHelp();
                    return(0);

                case "--version":
                    ShowVersion();
                    return(0);

                case "--verbose":
                    verbose = true;
                    break;

                default:
                    Console.WriteLine("Unknown argument: {0}", a);
                    ShowHelp();
                    return(1);
                }
            }

#if MODMONO_SERVER
            if (hash < 0)
            {
                hash = -hash;
            }

            string lockfile;
            bool   useTCP = ((options & Options.Port) != 0);
            if (!useTCP)
            {
                if (filename == null || filename == "")
                {
                    filename = "/tmp/mod_mono_server";
                }

                if ((options & Options.Address) != 0)
                {
                    ShowHelp();
                    Console.WriteLine();
                    Console.WriteLine("ERROR: --address without --port");
                    Environment.Exit(1);
                }
                lockfile = Path.Combine(Path.GetTempPath(), Path.GetFileName(filename));
                lockfile = String.Format("{0}_{1}", lockfile, hash);
            }
            else
            {
                lockfile = Path.Combine(Path.GetTempPath(), "mod_mono_TCP_");
                lockfile = String.Format("{0}_{1}", lockfile, hash);
            }
#endif
            IPAddress ipaddr = null;
            ushort    port;
            try {
                port = Convert.ToUInt16(oport);
            } catch (Exception) {
                Console.WriteLine("The value given for the listen port is not valid: " + oport);
                return(1);
            }

            try {
                ipaddr = IPAddress.Parse(ip);
            } catch (Exception) {
                Console.WriteLine("The value given for the address is not valid: " + ip);
                return(1);
            }

            if (rootDir != null && rootDir != "")
            {
                try {
                    Environment.CurrentDirectory = rootDir;
                } catch (Exception e) {
                    Console.WriteLine("Error: {0}", e.Message);
                    return(1);
                }
            }

            rootDir = Directory.GetCurrentDirectory();

            IWebSource webSource;
#if MODMONO_SERVER
            if (useTCP)
            {
                webSource = new ModMonoTCPWebSource(ipaddr, port, lockfile);
            }
            else
            {
                webSource = new ModMonoWebSource(filename, lockfile);
            }

            if ((options & Options.Terminate) != 0)
            {
                if (verbose)
                {
                    Console.WriteLine("Shutting down running mod-mono-server...");
                }

                bool res = ((ModMonoWebSource)webSource).GracefulShutdown();
                if (verbose)
                {
                    Console.WriteLine(res ? "Done." : "Failed");
                }

                return((res) ? 0 : 1);
            }

            ApplicationServer server = new ApplicationServer(webSource);
#else
            webSource = new XSPWebSource(ipaddr, port);
            ApplicationServer server = new ApplicationServer(webSource);
#endif
            server.Verbose = verbose;

            Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name);
            if (apps != null)
            {
                server.AddApplicationsFromCommandLine(apps);
            }

            if (appConfigFile != null)
            {
                server.AddApplicationsFromConfigFile(appConfigFile);
            }

            if (appConfigDir != null)
            {
                server.AddApplicationsFromConfigDirectory(appConfigDir);
            }

            if (apps == null && appConfigDir == null && appConfigFile == null)
            {
                server.AddApplicationsFromCommandLine("/:.");
            }
#if MODMONO_SERVER
            if (!useTCP)
            {
                Console.WriteLine("Listening on: {0}", filename);
            }
            else
#endif
            {
                Console.WriteLine("Listening on port: {0}", port);
                Console.WriteLine("Listening on address: {0}", ip);
            }

            Console.WriteLine("Root directory: {0}", rootDir);

            //KNV: return appServer reference to beagle to control it
            appServer = server;

            try {
                if (server.Start(!nonstop) == false)
                {
                    return(2);
                }

                if (!nonstop)
                {
                    Console.WriteLine("Hit Return to stop the server.");
                    Console.ReadLine();
                    server.Stop();
                }
            } catch (Exception e) {
                Console.WriteLine("Error: {0}", e.Message);
                return(1);
            }

            try {       //test beagle/search.asmx is accessible:
                string          wsurl = "http://localhost:" + oport + "/beagle/search.asmx";
                HttpWebRequest  w     = (HttpWebRequest)WebRequest.Create(wsurl);
                HttpWebResponse r     = (HttpWebResponse)w.GetResponse();
                r.Close();
            }
            catch (Exception e) {
                //Console.WriteLine ("Error: {0} in starting bgXsp", e.Message);
                //return 3;
            }

            return(0);
        }
예제 #2
0
		//		public static int Main (string [] args)
		public static int initXSP(string [] args, out ApplicationServer appServer)
		{
			bool nonstop = false;
			bool verbose = false;
			appServer = null;
						
			Trace.Listeners.Add (new TextWriterTraceListener (Console.Out));
			string apps = ConfigurationSettings.AppSettings ["MonoApplications"];
			string appConfigDir = ConfigurationSettings.AppSettings ["MonoApplicationsConfigDir"];
			string appConfigFile = ConfigurationSettings.AppSettings ["MonoApplicationsConfigFile"];
			string rootDir = ConfigurationSettings.AppSettings ["MonoServerRootDir"];
			object oport;
			string ip = ConfigurationSettings.AppSettings ["MonoServerAddress"];
#if MODMONO_SERVER
			string filename = ConfigurationSettings.AppSettings ["MonoUnixSocket"];
#endif
			if (ip == "" || ip == null)
				ip = "0.0.0.0";

			oport = ConfigurationSettings.AppSettings ["MonoServerPort"];
			if (oport == null)
				oport = 8080;

			Options options = 0;
			int hash = 0;
			for (int i = 0; i < args.Length; i++){
				string a = args [i];
				hash ^= args [i].GetHashCode () + i;
				
				switch (a){
#if MODMONO_SERVER
				case "--filename":
					CheckAndSetOptions (a, Options.FileName, ref options);
					filename = args [++i];
					break;
				case "--terminate":
					CheckAndSetOptions (a, Options.Terminate, ref options);
					break;
#endif
				case "--port":
					CheckAndSetOptions (a, Options.Port, ref options);
					oport = args [++i];
					break;
				case "--address":
					CheckAndSetOptions (a, Options.Address, ref options);
					ip = args [++i];
					break;
				case "--root":
					CheckAndSetOptions (a, Options.Root, ref options);
					rootDir = args [++i];
					break;
				case "--applications":
					CheckAndSetOptions (a, Options.Applications, ref options);
					apps = args [++i];
					break;
				case "--appconfigfile":
					CheckAndSetOptions (a, Options.AppConfigFile, ref options);
					appConfigFile = args [++i];
					break;
				case "--appconfigdir":
					CheckAndSetOptions (a, Options.AppConfigDir, ref options);
					appConfigDir = args [++i];
					break;
				case "--nonstop":
					nonstop = true;
					break;
				case "--help":
					ShowHelp ();
					return 0;
				case "--version":
					ShowVersion ();
					return 0;
				case "--verbose":
					verbose = true;
					break;
				default:
					Console.WriteLine ("Unknown argument: {0}", a);
					ShowHelp ();
					return 1;
				}
			}

#if MODMONO_SERVER
			if (hash < 0)
				hash = -hash;

			string lockfile;
			bool useTCP = ((options & Options.Port) != 0);
			if (!useTCP) {
				if (filename == null || filename == "")
					filename = "/tmp/mod_mono_server";

				if ((options & Options.Address) != 0) {
					ShowHelp ();
					Console.WriteLine ();
					Console.WriteLine ("ERROR: --address without --port");
					Environment.Exit (1);
				}
				lockfile = Path.Combine (Path.GetTempPath (), Path.GetFileName (filename));
				lockfile = String.Format ("{0}_{1}", lockfile, hash);
			} else {
				lockfile = Path.Combine (Path.GetTempPath (), "mod_mono_TCP_");
				lockfile = String.Format ("{0}_{1}", lockfile, hash);
			}
#endif
			IPAddress ipaddr = null;
			ushort port;
			try {
				port = Convert.ToUInt16 (oport);
			} catch (Exception) {
				Console.WriteLine ("The value given for the listen port is not valid: " + oport);
				return 1;
			}

			try {
				ipaddr = IPAddress.Parse (ip);
			} catch (Exception) {
				Console.WriteLine ("The value given for the address is not valid: " + ip);
				return 1;
			}

			if (rootDir != null && rootDir != "") {
				try {
					Environment.CurrentDirectory = rootDir;
				} catch (Exception e) {
					Console.WriteLine ("Error: {0}", e.Message);
					return 1;
				}
			}

			rootDir = Directory.GetCurrentDirectory ();
			
			IWebSource webSource;
#if MODMONO_SERVER
			if (useTCP) {
				webSource = new ModMonoTCPWebSource (ipaddr, port, lockfile);
			} else {
				webSource = new ModMonoWebSource (filename, lockfile);
			}

			if ((options & Options.Terminate) != 0) {
				if (verbose)
					Console.WriteLine ("Shutting down running mod-mono-server...");
				
				bool res = ((ModMonoWebSource) webSource).GracefulShutdown ();
				if (verbose)
					Console.WriteLine (res ? "Done." : "Failed");

				return (res) ? 0 : 1;
			}

			ApplicationServer server = new ApplicationServer (webSource);
#else
			webSource = new XSPWebSource (ipaddr, port);
			ApplicationServer server = new ApplicationServer (webSource);
#endif
			server.Verbose = verbose;

			Console.WriteLine (Assembly.GetExecutingAssembly ().GetName ().Name);
			if (apps != null)
				server.AddApplicationsFromCommandLine (apps);

			if (appConfigFile != null)
				server.AddApplicationsFromConfigFile (appConfigFile);

			if (appConfigDir != null)
				server.AddApplicationsFromConfigDirectory (appConfigDir);

			if (apps == null && appConfigDir == null && appConfigFile == null)
				server.AddApplicationsFromCommandLine ("/:.");
#if MODMONO_SERVER
			if (!useTCP) {
				Console.WriteLine ("Listening on: {0}", filename);
			} else
#endif
			{
				Console.WriteLine ("Listening on port: {0}", port);
				Console.WriteLine ("Listening on address: {0}", ip);
			}
			
			Console.WriteLine ("Root directory: {0}", rootDir);

			//KNV: return appServer reference to beagle to control it
			appServer = server;
			
			try {
				if (server.Start (!nonstop) == false)
					return 2;

				if (!nonstop) {
					Console.WriteLine ("Hit Return to stop the server.");
					Console.ReadLine ();
					server.Stop ();
				}
			} catch (Exception e) {
				Console.WriteLine ("Error: {0}", e.Message);
				return 1;
			}

		try {	//test beagle/search.asmx is accessible:
			string wsurl = "http://localhost:"+ oport + "/beagle/search.asmx";
			HttpWebRequest w = (HttpWebRequest)WebRequest.Create(wsurl);
			HttpWebResponse r = (HttpWebResponse)w.GetResponse();
			r.Close();
		} 
		catch (Exception e) {
			//Console.WriteLine ("Error: {0} in starting bgXsp", e.Message);
			//return 3;
		}

		return 0;
		}