Exemple #1
0
        /// <summary>
        /// Deletes the specified state set.
        /// </summary>
        /// <param name="identity">The identity of the caller.</param>
        /// <param name="name">The name of the state set.</param>
        /// <param name="cancellationToken">The cancellation token for the request.</param>
        /// <returns>
        /// A flag that indicates if the state set was deleted.
        /// </returns>
        /// <remarks>
        /// Implementations should return <see langword="false"/> only if the specified
        /// state set <paramref name="name"/> does not exist.  Delete operations that fail due to
        /// authorization issues should throw a <see cref="System.Security.SecurityException"/>.
        /// </remarks>
        protected override async Task <bool> DeleteStateSet(ClaimsPrincipal identity, string name, CancellationToken cancellationToken)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (!_stateSets.TryRemove(name, out var removed))
            {
                return(false);
            }

            await RedisStateSet.Delete(this, removed.Name, cancellationToken).ConfigureAwait(false);

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Creates a new state set.
        /// </summary>
        /// <param name="identity">The identity of the caller.</param>
        /// <param name="settings">The state set settings.</param>
        /// <param name="cancellationToken">The cancellation token for the request.</param>
        /// <returns>
        /// A new <see cref="StateSet"/>.
        /// </returns>
        protected override async Task <StateSet> CreateStateSet(ClaimsPrincipal identity, StateSetSettings settings, CancellationToken cancellationToken)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var stateSet = new StateSet(settings.Name, settings.Description, settings.States);

            if (!_stateSets.TryAdd(settings.Name, stateSet))
            {
                throw new ArgumentException(Resources.Error_StateSetAlreadyExists, nameof(settings));
            }

            await RedisStateSet.Save(this, stateSet, true, cancellationToken).ConfigureAwait(false);

            return(stateSet);
        }
Exemple #3
0
        /// <summary>
        /// Initializes the <see cref="RedisHistorian"/>.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel initialization.</param>
        /// <returns>
        /// A task that will initialize the historian.
        /// </returns>
        protected override async Task Init(CancellationToken cancellationToken)
        {
            void onConnected(object sender, ConnectionFailedEventArgs args)
            {
                _logger?.LogInformation($"Redis connection established: {args.EndPoint} (Connection Type = {args.ConnectionType})");
                Properties[Resources.Properties_Connected] = Connection.IsConnected;
            };

            void onConnectionFailed(object sender, ConnectionFailedEventArgs args)
            {
                _logger?.LogError($"Redis connection failed: {args.EndPoint} (Connection Type = {args.ConnectionType}, Failure Type = {args.FailureType}).", args.Exception);
                Properties[Resources.Properties_Connected] = Connection.IsConnected;
            };

            try {
                _isInitializing = true;
                var configurationOptions = ConfigurationOptions.Parse(_redisSettings);
                configurationOptions.ClientName = "Aika";

                Connection = await ConnectionMultiplexer.ConnectAsync(configurationOptions).ConfigureAwait(false);

                Connection.ConnectionRestored += onConnected;
                Connection.ConnectionFailed   += onConnectionFailed;

                Properties[Resources.Properties_Connected] = Connection.IsConnected;

                // Load state sets first so that they are already loaded when we start loading tags.
                await RedisStateSet.LoadAll(this, stateSet => _stateSets[stateSet.Name] = stateSet, cancellationToken).ConfigureAwait(false);

                await RedisTagDefinition.LoadAll(this, tag => _tags[tag.Id] = tag, cancellationToken).ConfigureAwait(false);
            }
            catch {
                if (Connection != null)
                {
                    Connection.ConnectionRestored -= onConnected;
                    Connection.ConnectionFailed   -= onConnectionFailed;
                    Connection.Dispose();
                }
                throw;
            }
            finally {
                _isInitializing = false;
            }
        }
Exemple #4
0
        /// <summary>
        /// Updates an existing state set.
        /// </summary>
        /// <param name="identity">The identity of the caller.</param>
        /// <param name="name">The name of the state set.</param>
        /// <param name="settings">The state set settings.</param>
        /// <param name="cancellationToken">The cancellation token for the request.</param>
        /// <returns>
        /// The updated <see cref="StateSet"/>.
        /// </returns>
        protected override async Task <StateSet> UpdateStateSet(ClaimsPrincipal identity, string name, StateSetSettings settings, CancellationToken cancellationToken)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var stateSet = new StateSet(name, settings.Description, settings.States);

            if (!_stateSets.ContainsKey(name))
            {
                throw new ArgumentException(Resources.Error_StateSetDoesNotExist, nameof(name));
            }

            _stateSets[name] = stateSet;

            await RedisStateSet.Save(this, stateSet, false, cancellationToken).ConfigureAwait(false);

            return(stateSet);
        }