Exemplo n.º 1
0
        public async Task <bool> SendAsync(IConfirmable order)
        {
            var errorState = ErrorState.Unchecked().Valid();

            try
            {
                if (!IsReady)
                {
                    errorState = errorState.DeviceDisconnected();
                }
                else if (!await _deviceHandler.SendAsync(_commandTranslator.TranslateToCommand(order)).ConfigureAwait(false))
                {
                    errorState = errorState.SendFailure();
                }
            }
            catch (DeviceDisconnectedException)
            {
                errorState = errorState.DeviceDisconnected();
            }

            if (errorState != ErrorState.Unchecked().Valid())
            {
                _awaiterDispatch.AddResponse(_confirmationFactory.Create(order, errorState));
            }

            return(true);
        }
Exemplo n.º 2
0
        public async Task <bool> Confirm(IConfirmable message, ErrorState errors = null)
        {
            var state        = errors ?? ErrorState.Unchecked().Valid();
            var confirmation = _confirmationFactory.Create(message, state);

            return(await SendAsync(confirmation).ConfigureAwait(false));
        }
Exemplo n.º 3
0
        private ChildAwaiter CreateNormalAwaiterWithBufferedToken(IConfirmable message)
        {
            var cancellationTokenSource = new CancellationTokenSource();

            if (_tokenBuffer.TryAdd(message.ID, cancellationTokenSource))
            {
                return(new ChildAwaiter(_timeout, cancellationTokenSource.Token));
            }

            throw new Exception($"Could not create new ChildAwaiter of type for \"{message.ID}\" - Cancellation token for that ID in this instance is already used in buffer");
        }
Exemplo n.º 4
0
        public void Put(User user, IConfirmable command)
        {
            IConfirmable confirmable;

            if (confirm.TryGetValue(user, out confirmable))
            {
                confirmable.Invalidate();
                confirm.Remove(user);
            }

            confirm.Add(user, command);
        }
Exemplo n.º 5
0
        public ICommand TranslateToCommand(IConfirmable message)
        {
            switch (message)
            {
            case Order order:
                return(TranslateToCommand(order));

            case Reply reply:
                return(TranslateToCommand(reply));

            default:
                throw new Exception($"Unknown base type of {nameof(IConfirmable)} passed.");
            }
        }
Exemplo n.º 6
0
        internal IChildAwaiter GetAwaiterFor(IConfirmable message)
        {
            if (ReplyExists(message))
            {
                return(new AlreadyKnownChildAwaiter(!IsTimeoutExceeded(message)));
            }

            if (IsTimeoutExceeded(message))
            {
                return(new AlreadyKnownChildAwaiter(false));
            }

            return(CreateNormalAwaiterWithBufferedToken(message));
        }
Exemplo n.º 7
0
 public Confirmation Create(IConfirmable message, ErrorState errors)
 {
     return(Create(message.ID, errors, Timestamp.Now));
 }
Exemplo n.º 8
0
 public static void ConfirmActivation(this IConfirmable confirmable)
 {
     confirmable.ConfirmationToken  = null;
     confirmable.ConfirmationSentAt = null;
     confirmable.ConfirmedAt        = DateTime.Now;
 }
Exemplo n.º 9
0
 public Confirmation ConfirmationOf(IConfirmable message) =>
 _responseAwaiters[ResponseType.Confirmation].GetResponse(message) as Confirmation;
Exemplo n.º 10
0
 public Task <bool> WasConfirmedInTimeAsync(IConfirmable message)
 {
     return(_responseAwaiters[ResponseType.Confirmation].GetAwaiterFor(message).HasRespondedInTimeAsync());
 }
Exemplo n.º 11
0
 internal IResponseMessage GetResponse(IConfirmable order)
 {
     _responseBuffer.TryRemove(order.ID, out var response);
     return(response);
 }
Exemplo n.º 12
0
 private bool IsTimeoutExceeded(IConfirmable order) => _timeout.IsExceeded(order.Timestamp);
Exemplo n.º 13
0
 private bool ReplyExists(IConfirmable message) => _responseBuffer.TryGetValue(message.ID, out _);
Exemplo n.º 14
0
 public static void Confirm(this IConfirmable confirmable, IActorRef target)
 {
     target.Tell(confirmable.GetConfirmation());
 }
Exemplo n.º 15
0
        public virtual async Task <Confirmation> GetConfirmationOfAsync(IConfirmable message)
        {
            await _handler.Order().WasConfirmedInTimeAsync(message).ConfigureAwait(false);

            return(_handler.Retrieve().ConfirmationOf(message));
        }