コード例 #1
0
        public void InitializeView(IView view, CommandLineArguments args)
        {
            _view = view;
            _view.Presenter = this;
            _view.RunState = RunState.Idle;

            _view.AddHost = args.AddHost;
            if (!string.IsNullOrEmpty(args.ApplicationPath))
            {
                args.ApplicationPath = args.ApplicationPath.Trim('\"').TrimEnd('\\');
            }

            _view.ApplicationPath = args.ApplicationPath;
            if (!string.IsNullOrEmpty(args.VirtualPath))
            {
                args.VirtualPath = args.VirtualPath.Trim('\"');
            }
            _view.VirtualPath = args.VirtualPath;
            _view.HostName = args.HostName;
            _view.IPAddress = args.IPAddress;
            _view.IPMode = args.IPMode;
            _view.IPv6 = args.IPv6;
            _view.Port = args.Port;
            _view.PortMode = args.PortMode;
            _view.PortRangeEnd = args.PortRangeEnd;
            _view.PortRangeStart = args.PortRangeStart;
            _view.RootUrl = string.Empty;
            _view.WaitForPort = args.WaitForPort;
            _view.TimeOut = args.TimeOut;

            try
            {
                ServiceFactory.Rules.ValidateArgs(args);
                // if an app path was passed, user wanted to start server
                if (!string.IsNullOrEmpty(args.ApplicationPath))
                {
                    Start(args);
                }
            }
            catch (CassiniException ex)
            {
                _view.SetError(ex.Field, ex.Message);
            }
        }
コード例 #2
0
        private static void Main(string[] cmdLine)
        {
            CommandLineArguments args = new CommandLineArguments();

            if (!CommandLineParser.ParseArgumentsWithUsage(cmdLine, args))
            {
                Environment.Exit(-1);
            }
            else
            {
                switch (args.RunMode)
                {
                    case RunMode.Server:
                        IPAddress ip=IPAddress.Loopback;
                        try
                        {
                            args.Validate();

                            ip = CommandLineArguments.ParseIP(args.IPMode, args.IPv6, args.IPAddress);
                            int port = args.PortMode == PortMode.FirstAvailable ?
                                CassiniNetworkUtils.GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true) :
                                args.Port;

                            if(args.AddHost)
                            {
                                HostsFile.AddHostEntry(ip.ToString(), args.HostName);
                            }

                            using (var server =
                                new Server(port, args.VirtualPath, args.ApplicationPath,
                                    ip, args.HostName, args.TimeOut))
                            {
                                server.Start();
                                Console.WriteLine("started: {0}\r\nPress Enter key to exit....", server.RootUrl);
                                Console.ReadLine();
                                server.ShutDown();
                            }
                        }
                        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(CommandLineParser.ArgumentsUsage(typeof(CommandLineArguments)));
                        }
                        finally
                        {
                            if (args.AddHost)
                            {
                                HostsFile.RemoveHostEntry(ip.ToString(), args.HostName);
                            }

                        }
                        break;
                    case RunMode.Hostsfile:
                        SetHostsFile(args);
                        break;
                }
            }
        }
コード例 #3
0
 private static void SetHostsFile(CommandLineArguments sargs)
 {
     try
     {
         if (sargs.AddHost)
         {
             HostsFile.AddHostEntry(sargs.IPAddress, sargs.HostName);
         }
         else
         {
             HostsFile.RemoveHostEntry(sargs.IPAddress, sargs.HostName);
         }
     }
     catch (UnauthorizedAccessException)
     {
         Environment.Exit(-1);
     }
     catch
     {
         Environment.Exit(-2);
     }
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: MagnusVortex/OCTGN
        public static int Main(string[] cmdLine)
        {
            Server server = null;

            if (cmdLine != null && cmdLine.Length > 0)
            {
                bool isVS = Assembly.GetExecutingAssembly()
                    .GetName().Name.StartsWith("WEBDEV.WEBSERVER", StringComparison.OrdinalIgnoreCase);

                CommandLineArguments args = new CommandLineArguments();

                if (!CommandLineParser.ParseArgumentsWithUsage(cmdLine, args))
                {
                    if (isVS)
                    {
                        // will display vs usage and return a code that VS understands
                        return ValidateForVS(cmdLine);
                    }

                    string usage = CommandLineParser.ArgumentsUsage(typeof(CommandLineArguments), 120);
                    ShowMessage(usage, MessageBoxIcon.Asterisk);
                    return 0;
                }

                if (args.RunMode == RunMode.Hostsfile)
                {
                    SetHostsFile(args);
                    return 0;
                }

                // now we validate for us.
                int returnValue = -1;
                string message = null;

                try
                {
                    args.VisualStudio = isVS;
                    args.Validate();
                }
                catch (CassiniException ex)
                {
                    switch (ex.Message)
                    {
                        case SR.ErrNoAvailablePortFound:
                        case SR.ErrApplicationPathIsNull:
                            message = ex.Message;
                            break;
                        case SR.ErrInvalidIPMode:
                            message = SR.GetString(ex.Message, args.IPMode);
                            break;
                        case SR.ErrInvalidPortMode:
                            message = SR.GetString(ex.Message, args.PortMode);
                            break;
                        case SR.ErrPortIsInUse:
                            message = SR.GetString(ex.Message, args.Port);
                            break;
                        case SR.ErrPortRangeEndMustBeEqualOrGreaterThanPortRangeSta:
                            message = SR.GetString(ex.Message, args.PortRangeStart, args.PortRangeEnd);
                            break;
                        case SR.ErrInvalidPortRangeValue:
                            message = SR.GetString(ex.Message,
                                                   ex.Field == ErrorField.PortRangeStart
                                                       ? "start " + args.PortRangeStart
                                                       : "end " + args.PortRangeEnd);
                            break;
                        case SR.ErrInvalidHostname:
                            message = SR.GetString(ex.Message, args.HostName);
                            break;
                        case SR.WebdevDirNotExist:
                            message = SR.GetString(ex.Message, args.ApplicationPath);
                            returnValue = -2;
                            break;
                        case SR.ErrPortOutOfRange:
                            message = SR.GetString(ex.Message, args.Port);
                            break;
                    }

                    if (!args.Silent)
                    {
                        ShowMessage(message, MessageBoxIcon.Asterisk);
                    }
                    return returnValue;
                }
                catch (Exception exception)
                {
                    if (!args.Silent)
                    {
                        ShowMessage(SR.GetString(SR.ErrFailedToStartCassiniDevServerOnPortError, args.Port,
                                                 exception.Message, exception.HelpLink), MessageBoxIcon.Error);
                    }
                    return -1;
                }

                server = new Server(args.Port, args.VirtualPath, args.ApplicationPath, args.Ntlm, args.Nodirlist);

                if (args.AddHost)
                {
                    HostsFile.AddHostEntry(server.IPAddress.ToString(), server.HostName);
                }

                try
                {
                    server.Start();
                }
                catch (Exception exception)
                {
                    if (!args.Silent)
                    {
                        ShowMessage(SR.GetString(SR.ErrFailedToStartCassiniDevServerOnPortError, args.Port,
                                                 exception.Message, exception.HelpLink), MessageBoxIcon.Error);
                    }
                    return -4;
                }

            }

            using (FormView form = new FormView(server))
            {
                Application.Run(form);
            }

            return 0;
        }
コード例 #5
0
ファイル: FormView.cs プロジェクト: nothingmn/NzbDrone
 private CommandLineArguments GetArgs()
 {
     CommandLineArguments args = new CommandLineArguments
                 {
                     AddHost = AddHost,
                     ApplicationPath = ApplicationPath,
                     HostName = HostName,
                     IPAddress = IPAddress,
                     IPMode = IPMode,
                     IPv6 = V6,
                     Port = Port,
                     PortMode = PortMode,
                     PortRangeEnd = PortRangeEnd,
                     PortRangeStart = PortRangeStart,
                     VirtualPath = VirtualPath,
                     TimeOut = TimeOut,
                     WaitForPort = 0,
                     Ntlm = NtmlAuthenticationRequired,
                     Nodirlist = NoDirList
                 };
     return args;
 }
コード例 #6
0
ファイル: Program.cs プロジェクト: mesh42/awmanaged
        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.", ErrorField.ApplicationPath);
                            }
                            IView view = ServiceFactory.CreateConsoleView();
                            presenter.InitializeView(view, sargs);
                            Console.WriteLine("started: {0}\r\nPress Enter key to exit....", presenter.Server.RootUrl);
                            Console.ReadLine();
                            view.Stop();
                        }
                        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
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: maikebing/CassiniDev
        public static int Main(string[] cmdLine)
        {
            Server server = null;

            if (cmdLine != null && cmdLine.Length > 0)
            {
                bool isVS = Assembly.GetExecutingAssembly()
                            .GetName().Name.StartsWith("WEBDEV.WEBSERVER", StringComparison.OrdinalIgnoreCase);

                CommandLineArguments args = new CommandLineArguments();

                if (!CommandLineParser.ParseArgumentsWithUsage(cmdLine, args))
                {
                    if (isVS)
                    {
                        // will display vs usage and return a code that VS understands
                        return(ValidateForVS(cmdLine));
                    }

                    string usage = CommandLineParser.ArgumentsUsage(typeof(CommandLineArguments), 120);
                    ShowMessage(usage, MessageBoxIcon.Asterisk);
                    return(0);
                }


                if (args.RunMode == RunMode.Hostsfile)
                {
                    SetHostsFile(args);
                    return(0);
                }


                // now we validate for us.
                int    returnValue = -1;
                string message     = null;

                try
                {
                    args.VisualStudio = isVS;
                    args.Validate();
                }
                catch (CassiniException ex)
                {
                    switch (ex.Message)
                    {
                    case SR.ErrNoAvailablePortFound:
                    case SR.ErrApplicationPathIsNull:
                        message = ex.Message;
                        break;

                    case SR.ErrInvalidIPMode:
                        message = SR.GetString(ex.Message, args.IPMode);
                        break;

                    case SR.ErrInvalidPortMode:
                        message = SR.GetString(ex.Message, args.PortMode);
                        break;

                    case SR.ErrPortIsInUse:
                        message = SR.GetString(ex.Message, args.Port);
                        break;

                    case SR.ErrPortRangeEndMustBeEqualOrGreaterThanPortRangeSta:
                        message = SR.GetString(ex.Message, args.PortRangeStart, args.PortRangeEnd);
                        break;

                    case SR.ErrInvalidPortRangeValue:
                        message = SR.GetString(ex.Message,
                                               ex.Field == ErrorField.PortRangeStart
                                                       ? "start " + args.PortRangeStart
                                                       : "end " + args.PortRangeEnd);
                        break;

                    case SR.ErrInvalidHostname:
                        message = SR.GetString(ex.Message, args.HostName);
                        break;

                    case SR.WebdevDirNotExist:
                        message     = SR.GetString(ex.Message, args.ApplicationPath);
                        returnValue = -2;
                        break;

                    case SR.ErrPortOutOfRange:
                        message = SR.GetString(ex.Message, args.Port);
                        break;
                    }

                    if (!args.Silent)
                    {
                        ShowMessage(message, MessageBoxIcon.Asterisk);
                    }
                    return(returnValue);
                }
                catch (Exception exception)
                {
                    if (!args.Silent)
                    {
                        ShowMessage(SR.GetString(SR.ErrFailedToStartCassiniDevServerOnPortError, args.Port,
                                                 exception.Message, exception.HelpLink), MessageBoxIcon.Error);
                    }
                    return(-1);
                }


                server = new Server(args.Port, args.VirtualPath, args.ApplicationPath, IPAddress.Parse(args.IPAddress), args.HostName, args.Ntlm, args.Nodirlist);


                if (args.AddHost)
                {
                    HostsFile.AddHostEntry(server.IPAddress.ToString(), server.HostName);
                }

                try
                {
                    server.Start();
                }
                catch (Exception exception)
                {
                    if (!args.Silent)
                    {
                        ShowMessage(SR.GetString(SR.ErrFailedToStartCassiniDevServerOnPortError, args.Port,
                                                 exception.Message, exception.HelpLink), MessageBoxIcon.Error);
                    }
                    return(-4);
                }
            }

            using (FormView form = new FormView(server))
            {
                Application.Run(form);
            }

            return(0);
        }
コード例 #8
0
ファイル: Presenter.cs プロジェクト: maikebing/CassiniDev
        public void Start(CommandLineArguments args)
        {
            _args = null;
            if (_view != null)
            {
                _view.ClearError();
            }

            try
            {
                ServiceFactory.Rules.ValidateArgs(args);
            }
            catch (CassiniException ex)
            {
                if (_view != null)
                {
                    _view.SetError(ex.Field, ex.Message);
                    return;
                }
                throw;
            }

            if (string.IsNullOrEmpty(args.ApplicationPath) || !Directory.Exists(args.ApplicationPath))
            {
                if (_view != null)
                {
                    _view.SetError(ErrorField.ApplicationPath, "Invalid Application Path");
                    return;
                }
                throw new CassiniException("Invalid Application Path", ErrorField.ApplicationPath);
            }

            // prepare arguments
            IPAddress ip = ServiceFactory.Rules.ParseIP(args.IPMode, args.IPv6, args.IPAddress);

            if (_view != null)
            {
                _view.IPAddress = ip.ToString();
            }

            ushort port = args.Port;

            if (args.PortMode == PortMode.FirstAvailable)
            {
                port = ServiceFactory.Rules.GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true);
            }

            if (_view != null)
            {
                _view.Port = port;
            }


            if (_view != null)
            {
                _view.HostName = args.HostName;
            }

            _server = ServiceFactory.CreateServer(new ServerArguments
            {
                Port            = port,
                VirtualPath     = args.VirtualPath,
                ApplicationPath = args.ApplicationPath,
                IPAddress       = ip,
                Hostname        = args.HostName,
                TimeOut         = args.TimeOut
            });

            WireServerEvents();

            if (args.AddHost)
            {
                ServiceFactory.Rules.AddHostEntry(_server.IPAddress.ToString(), _server.HostName);
            }

            try
            {
                _server.Start();
                _args = args;

                if (_view != null)
                {
                    _view.RootUrl  = _server.RootUrl;
                    _view.RunState = RunState.Running;
                }
            }
            catch (Exception ex)
            {
                if (_view != null)
                {
                    _view.SetError(ErrorField.None, ex.Message);
                }

                Stop();
            }
        }
コード例 #9
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
        }
コード例 #10
0
ファイル: Rules.cs プロジェクト: mesh42/awmanaged
        /// <summary>
        /// Validates all but application path
        /// </summary>
        /// <param name="args"></param>
        /// <exception cref="CassiniException">If vpath is null or does not begin with '/'</exception>
        /// <exception cref="CassiniException">If an invalid hostname is specified</exception>
        /// <exception cref="CassiniException">If AddHost is true and a null or invalid hostname is specified</exception>
        /// <exception cref="CassiniException">If either port range is less than 1</exception>
        /// <exception cref="CassiniException">If PortRangeStart is greater than PortRangeEnd</exception>
        /// <exception cref="CassiniException">If no available port within specified range is found.</exception>
        /// <exception cref="CassiniException">If specified port is in use.</exception>
        /// <exception cref="CassiniException">If PortMode is invalid</exception>
        /// <exception cref="CassiniException">If PortMode is invalid</exception>
        /// <exception cref="CassiniException">If IPMode is 'Specific' and IPAddress is invalid</exception>
        public void ValidateArgs(CommandLineArguments args)
        {
            if (string.IsNullOrEmpty(args.VirtualPath) || !args.VirtualPath.StartsWith("/"))
            {
                throw new CassiniException("Invalid VPath", ErrorField.VirtualPath);
            }

            if (!string.IsNullOrEmpty(args.HostName) && !ValidateHostName(args.HostName))
            {
                throw new CassiniException("Invalid Hostname", ErrorField.HostName);
            }

            if (args.AddHost && (string.IsNullOrEmpty(args.HostName) || !ValidateHostName(args.HostName)))
            {
                throw new CassiniException("Invalid Hostname", ErrorField.HostName);
            }

            IPAddress ip = ParseIP(args.IPMode, args.IPv6, args.IPAddress);

            switch (args.PortMode)
            {
            case PortMode.FirstAvailable:

                if (args.PortRangeStart < 1)
                {
                    throw new CassiniException("Invalid port.", ErrorField.PortRangeStart);
                }

                if (args.PortRangeEnd < 1)
                {
                    throw new CassiniException("Invalid port.", ErrorField.PortRangeEnd);
                }

                if (args.PortRangeStart > args.PortRangeEnd)
                {
                    throw new CassiniException("Port range end must be equal or greater than port range start.",
                                               ErrorField.PortRange);
                }
                if (GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true) == 0)
                {
                    throw new CassiniException("No available port found.", ErrorField.PortRange);
                }
                break;

            case PortMode.Specific:
                // start waiting....
                //TODO: design this hack away.... why am I waiting in a validation method?
                int now = Environment.TickCount;

                // wait until either 1) the specified port is available or 2) the specified amount of time has passed
                while (Environment.TickCount < now + args.WaitForPort &&
                       GetAvailablePort(args.Port, args.Port, ip, true) != args.Port)
                {
                    Thread.Sleep(100);
                }

                // is the port available?
                if (GetAvailablePort(args.Port, args.Port, ip, true) != args.Port)
                {
                    throw new CassiniException("Port is in use.", ErrorField.Port);
                }
                break;

            default:
                throw new CassiniException("Invalid PortMode", ErrorField.None);
            }
        }
コード例 #11
0
        public void Start(CommandLineArguments args)
        {
            _args = null;
            if (_view != null)
            {
                _view.ClearError();
            }

            try
            {
                ServiceFactory.Rules.ValidateArgs(args);
            }
            catch (CassiniException ex)
            {
                if (_view != null)
                {
                    _view.SetError(ex.Field, ex.Message);
                    return;
                }
                throw;
            }

            if (string.IsNullOrEmpty(args.ApplicationPath) || !Directory.Exists(args.ApplicationPath))
            {
                if (_view != null)
                {
                    _view.SetError(ErrorField.ApplicationPath, "Invalid Application Path");
                    return;
                }
                throw new CassiniException("Invalid Application Path", ErrorField.ApplicationPath);
            }

            // prepare arguments
            IPAddress ip = ServiceFactory.Rules.ParseIP(args.IPMode, args.IPv6, args.IPAddress);

            if (_view != null)
            {
                _view.IPAddress = ip.ToString();
            }

            ushort port = args.Port;
            if (args.PortMode == PortMode.FirstAvailable)
            {
                port = ServiceFactory.Rules.GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true);
            }

            if (_view != null)
            {
                _view.Port = port;
            }

            if (_view != null)
            {
                _view.HostName = args.HostName;
            }

            _server = ServiceFactory.CreateServer(new ServerArguments
                                                      {
                                                          Port = port,
                                                          VirtualPath = args.VirtualPath,
                                                          ApplicationPath = args.ApplicationPath,
                                                          IPAddress = ip,
                                                          Hostname = args.HostName,
                                                          TimeOut = args.TimeOut
                                                      });

            WireServerEvents();

            if (args.AddHost)
            {
                ServiceFactory.Rules.AddHostEntry(_server.IPAddress.ToString(), _server.HostName);
            }

            try
            {

                _server.Start();
                _args = args;

                if (_view != null)
                {
                    _view.RootUrl = _server.RootUrl;
                    _view.RunState = RunState.Running;
                }
            }
            catch (Exception ex)
            {
                if (_view != null)
                {
                    _view.SetError(ErrorField.None, ex.Message);
                }

                Stop();
            }
        }
コード例 #12
0
        /// <summary>
        /// Validates all but application path
        /// </summary>
        /// <param name="args"></param>
        /// <exception cref="CassiniException">If vpath is null or does not begin with '/'</exception>
        /// <exception cref="CassiniException">If an invalid hostname is specified</exception>
        /// <exception cref="CassiniException">If AddHost is true and a null or invalid hostname is specified</exception>
        /// <exception cref="CassiniException">If either port range is less than 1</exception>
        /// <exception cref="CassiniException">If PortRangeStart is greater than PortRangeEnd</exception>
        /// <exception cref="CassiniException">If no available port within specified range is found.</exception>
        /// <exception cref="CassiniException">If specified port is in use.</exception>
        /// <exception cref="CassiniException">If PortMode is invalid</exception>
        /// <exception cref="CassiniException">If PortMode is invalid</exception>
        /// <exception cref="CassiniException">If IPMode is 'Specific' and IPAddress is invalid</exception>
        public void ValidateArgs(CommandLineArguments args)
        {
            if (string.IsNullOrEmpty(args.VirtualPath) || !args.VirtualPath.StartsWith("/"))
            {
                throw new CassiniException("Invalid VPath", ErrorField.VirtualPath);
            }

            if (args.AddHost && (string.IsNullOrEmpty(args.HostName) || !ValidateHostName(args.HostName)))
            {
                throw new CassiniException("Invalid Hostname", ErrorField.HostName);
            }

            IPAddress ip = ParseIP(args.IPMode, args.IPv6, args.IPAddress);

            switch (args.PortMode)
            {
                case PortMode.FirstAvailable:

                    if (args.PortRangeStart < 1)
                    {
                        throw new CassiniException("Invalid port.", ErrorField.PortRangeStart);
                    }

                    if (args.PortRangeEnd < 1)
                    {
                        throw new CassiniException("Invalid port.", ErrorField.PortRangeEnd);
                    }

                    if (args.PortRangeStart > args.PortRangeEnd)
                    {
                        throw new CassiniException("Port range end must be equal or greater than port range start.",
                                                   ErrorField.PortRange);
                    }
                    if (GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true) == 0)
                    {
                        throw new CassiniException("No available port found.", ErrorField.PortRange);
                    }
                    break;
                case PortMode.Specific:
                    // start waiting....
                    //TODO: design this hack away.... why am I waiting in a validation method?
                    int now = Environment.TickCount;

                    // wait until either 1) the specified port is available or 2) the specified amount of time has passed
                    while (Environment.TickCount < now + args.WaitForPort &&
                           GetAvailablePort(args.Port, args.Port, ip, true) != args.Port)
                    {
                        Thread.Sleep(100);
                    }

                    // is the port available?
                    if (GetAvailablePort(args.Port, args.Port, ip, true) != args.Port)
                    {
                        throw new CassiniException("Port is in use.", ErrorField.Port);
                    }
                    break;
                default:
                    throw new CassiniException("Invalid PortMode", ErrorField.None);
            }
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: maikebing/CassiniDev
        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
        }
コード例 #14
0
        private static void Main(string[] cmdLine)
        {
            CommandLineArguments args = new CommandLineArguments();


            if (!CommandLineParser.ParseArgumentsWithUsage(cmdLine, args))
            {
                Environment.Exit(-1);
            }
            else
            {
                switch (args.RunMode)
                {
                case RunMode.Server:
                    IPAddress ip = IPAddress.Loopback;
                    try
                    {
                        args.Validate();

                        ip = CommandLineArguments.ParseIP(args.IPMode, args.IPv6, args.IPAddress);
                        int port = args.PortMode == PortMode.FirstAvailable ?
                                   CassiniNetworkUtils.GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true) :
                                   args.Port;

                        if (args.AddHost)
                        {
                            HostsFile.AddHostEntry(ip.ToString(), args.HostName);
                        }

                        using (var server =
                                   new Server(port, args.VirtualPath, args.ApplicationPath,
                                              ip, args.HostName, args.TimeOut))
                        {
                            server.Start();
                            if (!string.IsNullOrEmpty(args.Browse))
                            {
                                try
                                {
                                    string openTo = server.RootUrl + args.Browse;
                                    Console.WriteLine("Opening default browser to " + openTo);
                                    System.Diagnostics.Process.Start(openTo);
                                }
                                catch (System.ComponentModel.Win32Exception noBrowser)
                                {
                                    if (noBrowser.ErrorCode == -2147467259)
                                    {
                                        Console.WriteLine(noBrowser.Message);
                                    }
                                }
                                catch (System.Exception other)
                                {
                                    Console.WriteLine(other.Message);
                                }
                            }
                            Console.WriteLine("started: {0}\r\nPress Enter key to exit....", server.RootUrl);
                            Console.ReadLine();
                            server.ShutDown();
                        }
                    }
                    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(CommandLineParser.ArgumentsUsage(typeof(CommandLineArguments)));
                    }
                    finally
                    {
                        if (args.AddHost)
                        {
                            HostsFile.RemoveHostEntry(ip.ToString(), args.HostName);
                        }
                    }
                    break;

                case RunMode.Hostsfile:
                    SetHostsFile(args);
                    break;
                }
            }
        }