Ejemplo n.º 1
0
        private static void Main(string[] args)
        {
            CommandLineArguments sargs = new CommandLineArguments();

#if GUI
            if (!Parser.ParseArguments(args, sargs))
            {
                string usage = Parser.ArgumentsUsage(typeof(CommandLineArguments), 120);
                MessageBox.Show(usage);
                Environment.Exit(-1);
                return;
            }
            switch (sargs.RunMode)
            {
            case RunMode.Server:
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                using (IPresenter presenter = ServiceFactory.CreatePresenter())
                {
                    IView view = ServiceFactory.CreateFormsView();
                    presenter.InitializeView(view, sargs);
                    Application.Run((Form)view);
                }
                break;

            case RunMode.Hostsfile:
                SetHostsFile(sargs);
                break;
            }
#endif
#if CONSOLE
            if (!Parser.ParseArgumentsWithUsage(args, sargs))
            {
                Environment.Exit(-1);
            }
            else
            {
                switch (sargs.RunMode)
                {
                case RunMode.Server:
                    using (IPresenter presenter = ServiceFactory.CreatePresenter())
                    {
                        try
                        {
                            if (string.IsNullOrEmpty(sargs.ApplicationPath))
                            {
                                throw new CassiniException(
                                          "ApplicationPath is null.\r\n" +
                                          Parser.ArgumentsUsage(typeof(CommandLineArguments)), ErrorField.ApplicationPath);
                            }



                            #region Listen to presenter events
                            if (!sargs.Quiet)
                            {
                                // since we don't need to unhook it just use anonymous delegate
                                presenter.RequestComplete +=
                                    (s, e) => Console.WriteLine("RequestComplete:" + e.Request.ToString(true));
                                presenter.RequestBegin +=
                                    (s, e) => Console.WriteLine("RequestBegin:" + e.Request.ToString(true));
                            }

                            presenter.ServerStarted += ((s, e) =>
                            {
                                Console.WriteLine("started: {0}", e.RootUrl);
                                Console.WriteLine("Press Enter key to exit....");
                            });

                            bool stopped = false;
                            presenter.ServerStopped += ((s, e) =>
                            {
                                stopped = true;
                            });

                            #endregion


                            presenter.Start(sargs);


                            //TODO: refine this - sleep is not optimal
                            if (sargs.Headless)
                            {
                                while (!stopped)
                                {
                                    Thread.Sleep(10);
                                }
                            }
                            else
                            {
                                while (!stopped && !Console.KeyAvailable)
                                {
                                    Thread.Sleep(10);
                                }

                                if (!stopped)
                                {
                                    Console.ReadKey();
                                    presenter.Stop();
                                }
                            }

                            Console.WriteLine("stopped:");
                        }
                        catch (CassiniException ex)
                        {
                            Console.WriteLine("error:{0} {1}",
                                              ex.Field == ErrorField.None
                                                      ? ex.GetType().Name
                                                      : ex.Field.ToString(), ex.Message);
                        }
                        catch (Exception ex2)
                        {
                            Console.WriteLine("error:{0}", ex2.Message);
                            Console.WriteLine(Parser.ArgumentsUsage(typeof(CommandLineArguments)));
                        }
                    }
                    break;

                case RunMode.Hostsfile:
                    SetHostsFile(sargs);
                    break;
                }
            }
#endif
        }