Exemplo n.º 1
0
 private void JournalEntriesOnNewMessageReceived(object sender, JournalEntry journalEntry)
 {
     ReceiveJournalEntry(journalEntry);
 }
Exemplo n.º 2
0
        public void WaitAny(TimeSpan?timeout = null)
        {
            timeout = timeout ?? defaultTimeout?.Invoke();

            JournalEntry beforeWaitAny     = null;
            long?        lastWaitEntryId   = null;
            long?        lastActionEntryId = null;


            if (journalSource != null)
            {
                lastActionEntryId = journalSource.LastActionJournalEntryId;
                trace?.Log($"WaitAny {lastWaitEntryId}, {lastActionEntryId}");

                journalSource.NewMessageReceived += JournalEntriesOnNewMessageReceived;
                beforeWaitAny = journalSource.LastOrDefault();
            }

            if (journal != null)
            {
                trace?.Log($"WaitAny - preprocessing");

                lastWaitEntryId = journal.LastWaitEntryId;
                foreach (var entry in journal.AfterLastAction())
                {
                    var pair =
                        whenActions.FirstOrDefault(x =>
                                                   x.Key.Any(k => entry.Message.IndexOf(k, StringComparison.OrdinalIgnoreCase) >= 0));
                    if (pair.Value != null)
                    {
                        journal.NotifyWait();
                        pair.Value(entry);
                        return;
                    }
                }
            }

            trace?.Log($"WaitAny - starting awaiting");

            try
            {
                var totalWaitingMillieseconds = 0;
                while (!entryReceivedEvent.WaitOne(100))
                {
                    totalWaitingMillieseconds += 100;
                    if (timeout.HasValue && timeout.Value.TotalMilliseconds < totalWaitingMillieseconds)
                    {
                        if (timeoutAction != null)
                        {
                            timeoutAction.Invoke();
                        }
                        else
                        {
                            throw new TimeoutException(message: BuildTimeoutDiagnosticInfo(beforeWaitAny, timeout,
                                                                                           lastWaitEntryId, lastActionEntryId));
                        }

                        return;
                    }

                    cancellation?.Check();
                }

                var action = receivedAction;
                receivedAction = null;

                var entry = receivedJournalEntry;
                receivedJournalEntry = null;

                entryReceivedEvent.Reset();

                action(entry);
            }
            finally
            {
                if (journalSource != null)
                {
                    journalSource.NewMessageReceived -= JournalEntriesOnNewMessageReceived;
                }
                journal?.NotifyWait();
            }
        }