Exemple #1
0
        /// <summary>
        /// Gets the availability of a specified cell.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <returns>
        /// The availability of the specified cell.
        /// </returns>
        public static Availability GetAvailability(
            this IAvailabilityCheckCell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }

            var availabilityCheckStatus = cell.GetAvailabilityCheckStatus();

            switch (availabilityCheckStatus)
            {
            case AvailabilityCheckStatus.AvailabilityCheckMissing:
            case AvailabilityCheckStatus.Unchecked:
                return(cell.DefaultAvailability);

            case AvailabilityCheckStatus.DeterminedSubjectIsEnabled:
                return(Availability.Enabled);

            case AvailabilityCheckStatus.DeterminedSubjectIsDisabled:
                return(Availability.Disabled);

            default:
                return(Availability.Unknown);
            }
        }
Exemple #2
0
        private async Task CheckAvailabilityOfCellIfNecessaryAsync(
            IAvailabilityCheckCell cell)
        {
            // NOTE: THIS CODE IS A NEAR DUPLICATE OF THE SYNC METHOD ABOVE; NO GOOD WAY TO D.R.Y. IT OUT
            var availabilityCheckStatus = cell.GetAvailabilityCheckStatus();

            if (availabilityCheckStatus == AvailabilityCheckStatus.AvailabilityCheckMissing)
            {
                // no-op
            }
            else if (availabilityCheckStatus == AvailabilityCheckStatus.Unchecked)
            {
                CellAvailabilityCheckEventBase availabilityCheckEvent;

                try
                {
                    var availabilityCheck = cell.AvailabilityCheck;

                    var availabilityCheckResult = await this.protocolFactory.GetProtocolAndExecuteViaReflectionAsync <AvailabilityCheckResult>(availabilityCheck.Operation);

                    string message = null;

                    if (availabilityCheckResult.MessageOp != null)
                    {
                        message = await this.protocolFactory.GetProtocolAndExecuteViaReflectionAsync <string>(availabilityCheckResult.MessageOp);
                    }

                    var availability = await this.protocolFactory.GetProtocolAndExecuteViaReflectionAsync <Availability>(availabilityCheckResult.AvailabilityOp);

                    if (availability == Availability.Disabled)
                    {
                        availabilityCheckEvent = new CellAvailabilityCheckDeterminedCellDisabledEvent(this.timestampUtc, null, message);
                    }
                    else if (availability == Availability.Enabled)
                    {
                        availabilityCheckEvent = new CellAvailabilityCheckDeterminedCellEnabledEvent(this.timestampUtc, null, message);
                    }
                    else
                    {
                        throw new NotSupportedException(Invariant($"This {nameof(Availability)} is not supported: {availability}."));
                    }
                }
                catch (Exception ex)
                {
                    // The "proper" exception for a protocol to throw is an OpExecutionFailedExceptionBase.
                    // Protocol authors might not comply.
                    availabilityCheckEvent = new CellAvailabilityCheckFailedEvent(this.timestampUtc, ex.ToString());
                }

                cell.Record(availabilityCheckEvent);
            }
            else if (cell.AvailabilityCheckEvents.Last().TimestampUtc != this.timestampUtc)
            {
                throw new InvalidOperationException("Something went wrong.  The cell was checked for availability, but the recorded timestamp doesn't match this timestamp.");
            }
        }