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); }
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)); }
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"); }
public void Put(User user, IConfirmable command) { IConfirmable confirmable; if (confirm.TryGetValue(user, out confirmable)) { confirmable.Invalidate(); confirm.Remove(user); } confirm.Add(user, command); }
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."); } }
internal IChildAwaiter GetAwaiterFor(IConfirmable message) { if (ReplyExists(message)) { return(new AlreadyKnownChildAwaiter(!IsTimeoutExceeded(message))); } if (IsTimeoutExceeded(message)) { return(new AlreadyKnownChildAwaiter(false)); } return(CreateNormalAwaiterWithBufferedToken(message)); }
public Confirmation Create(IConfirmable message, ErrorState errors) { return(Create(message.ID, errors, Timestamp.Now)); }
public static void ConfirmActivation(this IConfirmable confirmable) { confirmable.ConfirmationToken = null; confirmable.ConfirmationSentAt = null; confirmable.ConfirmedAt = DateTime.Now; }
public Confirmation ConfirmationOf(IConfirmable message) => _responseAwaiters[ResponseType.Confirmation].GetResponse(message) as Confirmation;
public Task <bool> WasConfirmedInTimeAsync(IConfirmable message) { return(_responseAwaiters[ResponseType.Confirmation].GetAwaiterFor(message).HasRespondedInTimeAsync()); }
internal IResponseMessage GetResponse(IConfirmable order) { _responseBuffer.TryRemove(order.ID, out var response); return(response); }
private bool IsTimeoutExceeded(IConfirmable order) => _timeout.IsExceeded(order.Timestamp);
private bool ReplyExists(IConfirmable message) => _responseBuffer.TryGetValue(message.ID, out _);
public static void Confirm(this IConfirmable confirmable, IActorRef target) { target.Tell(confirmable.GetConfirmation()); }
public virtual async Task <Confirmation> GetConfirmationOfAsync(IConfirmable message) { await _handler.Order().WasConfirmedInTimeAsync(message).ConfigureAwait(false); return(_handler.Retrieve().ConfirmationOf(message)); }