Exemplo n.º 1
0
        private void SaveChanges()
        {
            if (chkUseSpecificIPAddress.Checked == true && IsIpAddressValid() == false)
            {
                MessageBox.Show("IP address specified is not a legal IP address.");
                return;
            }

            //if (VerifyFieldValues() == false)
            //{
            //    MessageBox.Show("IP address specified is not a legal IP address.");
            //    return;
            //}

            if (AreIpAndTcpAndUdpPortUniqueForInstance() == false)
            {
                MessageBox.Show("You must specify a unique TCP/IP IP address and port combination.", "IP address or port duplication error.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (IsInstanceNameUnique() == false)
            {
                MessageBox.Show("Instance name is not unique.");
                return;
            }

            Cursor = Cursors.WaitCursor;
            SetStatusLabel("Saving.");

            //ImpersonateUser impersonateUser = null;

            try
            {
                //if (_server.UseImpersonation == true)
                //{
                //    SetStatusLabel("Saving: Setting up impersonation.");
                //    impersonateUser = new ImpersonateUser(_server.UserName, Encryption.Decrypt(_server.Password));
                //}

                //
                //string networkPath = null;
                //try
                //{
                //    networkPath = "\\\\" + _server.ServerName + "\\c$";
                //    Directory.GetDirectories(networkPath);
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show("Couldn't establish connection on network path: " + networkPath + "\n\n" + ex.Message);
                //    return;
                //}

                bool isNewInstance = false;

                try
                {
                    if (_instance == null)
                    {
                        string nextInstanceServiceName = GetNextMemcachedInstanceName();

                        _instance = new MemCacheDManager.Business.Instance();

                        _instance.ServiceName   = nextInstanceServiceName;
                        _instance.ImageBasePath = _server.BinaryPath;

                        _server.Instances.Add(_instance);

                        isNewInstance = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Couldn't get a list of instances on this server. Please check server connectivity and user credentials and try again.\n" + ex.Message, "Error.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // TODO: Add rest of fields here.
                _instance.DisplayName = txtInstanceName.Text;

                decimal chunkSizeGrowthFactor = 0;
                string  chunkSizeString       = ConvertDecimalUserInputToLocalizedString(txtChunkSizeGrowthFactor.Text);
                if (Decimal.TryParse(chunkSizeString, out chunkSizeGrowthFactor) == true)
                {
                    _instance.ChunkSizeGrowthFactor = chunkSizeGrowthFactor;
                }

                int defaultKeySize = 0;
                if (Int32.TryParse(txtDefaultKeySize.Text, out defaultKeySize) == true)
                {
                    _instance.DefaultKeySize = defaultKeySize;
                }


                _instance.MaximizeCoreFile = chkMaximizeCoreFile.Checked;

                int maximumConnections = 0;
                if (Int32.TryParse(txtMaximumConnections.Text, out maximumConnections) == true)
                {
                    _instance.MaximumConnections = maximumConnections;
                }

                int memoryLimit = 0;
                if (Int32.TryParse(txtMemoryLimit.Text, out memoryLimit) == true)
                {
                    _instance.MemoryLimit = memoryLimit;
                }

                _instance.UseManagedInstance = chkUseManagedInstance.Checked;

                if (chkUseSpecificIPAddress.Checked == true)
                {
                    _instance.IpAddress = txtIpAddress.Text;
                }
                else
                {
                    _instance.IpAddress = null;
                }

                if (chkUseUDP.Checked == true)
                {
                    int udpPort = 0;
                    if (Int32.TryParse(txtUdpPort.Text, out udpPort) == true)
                    {
                        _instance.UdpPort = udpPort;
                    }
                }
                else
                {
                    _instance.UdpPort = 0;
                }

                int tcpPort;
                Int32.TryParse(txtTcpPort.Text, out tcpPort);
                _instance.TcpPort = tcpPort;

                _instance.UpdateInstanceOnServer(_server, this.SetStatusLabel);

                if (isNewInstance == true)
                {
                    if (MessageBox.Show("Would you like to start the new instance now?", "Start Service?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        string userName = null;
                        string password = null;

                        if (_server.UseImpersonation == true)
                        {
                            userName = _server.UserName;
                            password = Encryption.Decrypt(_server.Password);
                        }

                        SetStatusLabel("Starting service.");
                        Cursor.Current = Cursors.WaitCursor;
                        try
                        {
                            WmiService.Start(_server.ServerName, userName, password, _instance.ServiceName);
                        }
                        finally
                        {
                            SetStatusLabel("Service started.");
                            Cursor.Current = Cursors.Default;
                        }
                    }
                }

                btnApply.Enabled = false;

                SetStatusLabel("Updating cofiguration file.");

                ServerConfiguration.Save(Configuration.Default.LastConfigFile);

                SetStatusLabel("Refreshing UI.");

                if (Save != null)
                {
                    Save(this, EventArgs.Empty);
                }
            }
            finally
            {
                //if (impersonateUser != null && impersonateUser.IsImpersonating == true)
                //    impersonateUser.Undo();

                Cursor = Cursors.Default;
                SetStatusLabel("Ready.");
            }
        }