bool LockSerializationKey(string initiationSource)
        {
            try
            {
                string   key     = GenerateKey(initiationSource);
                BoundKey binding = null;

                while (true)
                {
                    try
                    {
                        binding = _Keys.Values.FirstOrDefault(i => i.InitiationSource != null && i.InitiationSource.ToUpper() == initiationSource.ToUpper());
                        if (binding != null)
                        {
                            lock (binding)
                            {
                                if (key != null)
                                {
                                    if (!binding.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        CoordinatedKeyManager.Unlock(binding.Key, binding);
                                    }
                                }
                                else if (!CoordinatedKeyManager.Locked(binding.InitiationSource))
                                {
                                    CoordinatedKeyManager.Unlock(binding.Key, binding);
                                    return(false);
                                }
                            }
                        }

                        break;
                    }
                    catch { }
                }

                if (key == null)
                {
                    return(true);
                }

                lock (_Keys)
                {
                    key = key.ToUpper();

                    binding = null;

                    if (_Keys.ContainsKey(key))
                    {
                        binding = _Keys[key];
                    }

                    if (binding != null)
                    {
                        lock (binding)
                        {
                            if (binding.Assigned)
                            {
                                if (binding.InitiationSource != null)
                                {
                                    if (!CoordinatedKeyManager.Locked(binding.InitiationSource))
                                    {
                                        binding.InitiationSource = null;
                                        binding.Assigned         = false;
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                                else
                                {
                                    binding.Assigned = false;
                                }

                                if (binding.Assigned)
                                {
                                    return(false);
                                }
                            }

                            if (CoordinatedKeyManager.CoordinatedMachineHasLock(key, CoordinateWith) == CoordinatedKeyManager.RemoteLockExists.True)
                            {
                                CoordinatedKeyManager.Unlock(key, binding);
                                STEM.Sys.EventLog.WriteEntry("SerialDeploymentController.KeyedBranchIP", "A previously bound key has been found to be owned by a different DeploymentService.", STEM.Sys.EventLog.EventLogEntryType.Error);
                                return(false);
                            }

                            _Keys[key].InitiationSource = initiationSource;
                            _Keys[key].Assigned         = true;
                        }
                    }
                    else
                    {
                        binding = new BoundKey(this, initiationSource, key, true);

                        if (UseSubnetCoordination)
                        {
                            if (!CoordinatedKeyManager.Lock(key, binding, CoordinateWith))
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            if (!CoordinatedKeyManager.Lock(key, binding))
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                STEM.Sys.EventLog.WriteEntry("SerialDeploymentController.LockSerializationKey", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        string LockSerializationKey(string initiationSource, string branchIP)
        {
            try
            {
                string   key     = GenerateKey(initiationSource);
                BoundKey binding = null;

                while (true)
                {
                    try
                    {
                        binding = _Keys.Values.FirstOrDefault(i => i.InitiationSource != null && i.InitiationSource.ToUpper() == initiationSource.ToUpper());
                        if (binding != null)
                        {
                            lock (binding)
                            {
                                if (key != null)
                                {
                                    if (!binding.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        CoordinatedKeyManager.Unlock(binding.Key, binding);
                                    }
                                }
                                else if (!CoordinatedKeyManager.Locked(binding.InitiationSource))
                                {
                                    CoordinatedKeyManager.Unlock(binding.Key, binding);
                                    return(null);
                                }
                            }
                        }

                        break;
                    }
                    catch { }
                }

                if (key == null)
                {
                    return(branchIP);
                }

                lock (_Keys)
                {
                    key = key.ToUpper();

                    binding = null;

                    if (_Keys.ContainsKey(key))
                    {
                        binding = _Keys[key];
                    }

                    if (binding != null)
                    {
                        lock (binding)
                        {
                            if (binding.Assigned)
                            {
                                if (binding.InitiationSource != null)
                                {
                                    if (!CoordinatedKeyManager.Locked(binding.InitiationSource))
                                    {
                                        binding.InitiationSource = null;
                                        binding.Assigned         = false;
                                    }
                                    else
                                    {
                                        return(null);
                                    }
                                }
                                else
                                {
                                    binding.Assigned = false;
                                }

                                if (binding.Assigned)
                                {
                                    return(null);
                                }
                            }

                            if (CoordinatedKeyManager.CoordinatedMachineHasLock(key, CoordinateWith) == CoordinatedKeyManager.RemoteLockExists.True)
                            {
                                CoordinatedKeyManager.Unlock(key, binding);
                                STEM.Sys.EventLog.WriteEntry("KeyedDeploymentController.LockSerializationKey", "A previously bound key has been found to be owned by a different DeploymentService.", STEM.Sys.EventLog.EventLogEntryType.Error);
                                return(null);
                            }

                            if (FileExists(initiationSource))
                            {
                                _Keys[key].FileSize = new FileInfo(initiationSource).Length;
                            }

                            _Keys[key].InitiationSource = initiationSource;
                            _Keys[key].Assigned         = true;
                            return(_Keys[key].BranchIP);
                        }
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(branchIP) || branchIP == STEM.Sys.IO.Net.EmptyAddress)
                        {
                            return(null);
                        }

                        if (!_BranchBoundWeight.ContainsKey(branchIP))
                        {
                            return(null);
                        }

                        branchIP = BestBranchByWeight(branchIP);

                        if (branchIP == null)
                        {
                            return(null);
                        }

                        long fileLen = 0;

                        if (FileExists(initiationSource))
                        {
                            fileLen = GetFileInfo(initiationSource).Size;
                        }

                        binding = new BoundKey(this, initiationSource, key, branchIP, fileLen, true);

                        if (!CoordinatedKeyManager.Lock(key, binding, CoordinateWith))
                        {
                            return(null);
                        }

                        return(binding.BranchIP);
                    }
                }
            }
            catch (Exception ex)
            {
                STEM.Sys.EventLog.WriteEntry("KeyedDeploymentController.LockSerializationKey", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
            }

            return(null);
        }