public Notify WaitForNotify(Action action, int timeout, WaitHandle stop) { Notify notify = null; Events_.NotificationConsumer consumer = new Events_.NotificationConsumer(GetNotificationUri()); NotifyReceived.Reset(); ErrorReceived.Reset(); consumer.OnError += new Action <Exception>( (err) => { _rawData = consumer.RawData; RaiseDataReceived(consumer.RawData); OnNotifyError(err); } ); consumer.OnNotify += OnNotify; try { consumer.Start(); if (action != null) { action(); } RaiseWaitStarted(); int res = WaitHandle.WaitAny(new WaitHandle[] { NotifyReceived, ErrorReceived, stop }, timeout); if (res == 0) { notify = _notify; RaiseDataReceived(_rawData); RaiseWaitFinished(); } else if (res == 1) { consumer.Stop(); throw _error; } else if (res == 2) { // stop event throw new StopEventException(); } else { RaiseTimeout(); } } finally { consumer.Stop(); } return(notify); }
public void StartNotify() { _consumer = new Events_.NotificationConsumer(GetNotificationUri()); NotifyReceived.Reset(); ErrorReceived.Reset(); _consumer.OnError += new Action <Exception>( (err) => { _rawData = _consumer.RawData; RaiseDataReceived(_consumer.RawData); OnNotifyError(err); } ); _consumer.OnNotify += OnNotify; _consumer.Start(); }
public void StartCollecting(WaitHandle stop) { lock (_notificationsLock) { _notifications = new Dictionary <Notify, byte[]>(); } _consumer = new Events.NotificationConsumer(GetNotificationUri()); _consumer.OnError += new Action <Exception>( (err) => { RaiseDataReceived(_consumer.RawData); OnNotifyError(err); } ); _consumer.OnNotify += new Action <SoapMessage <Notify>, byte[]>((n, raw) => AddMessage(n, raw)); NotifyReceived.Reset(); ErrorReceived.Reset(); try { _consumer.Start(); RaiseWaitStarted(); } finally { } if (_error != null) { throw _error; } }
/// <summary> /// Blocks calling thread and collects notifications. Notifications collecting will continue until /// - count of messages achieves <paramref name="limit"/>. If <paramref name="messageCheck"/> parameter /// is specified, messages will be checked before adding to the list. /// </summary> /// <param name="action">Action which should trigger notification sending</param> /// <param name="timeout">Timeout for waiting</param> /// <param name="limit">Limit of message to receive</param> /// <param name="messageCheck">Filter procedure</param> /// <param name="stop">Semaphore object</param> /// <returns></returns> public Dictionary <Notify, byte[]> CollectNotifications(Action action, int timeout, /* Milliseconds! */ int limit, Func <NotificationMessageHolderType, bool> messageCheck, WaitHandle startProcessing, WaitHandle stop) { Dictionary <Notify, byte[]> notifications = new Dictionary <Notify, byte[]>(); _notifications = new Queue <NotificationInfo>(); Events_.NotificationConsumer consumer = new Events_.NotificationConsumer(GetNotificationUri()); Action <Exception> errorHandling = new Action <Exception>( (err) => { RaiseDataReceived(consumer.RawData); OnNotifyError(err); } ); consumer.OnError += errorHandling; consumer.OnNotify += OnNotify; NotifyReceived.Reset(); ErrorReceived.Reset(); bool exitByStop = false; try { consumer.Start(); if (action != null) { action(); } RaiseWaitStarted(); int hndl = WaitHandle.WaitAny(new WaitHandle[] { startProcessing }, timeout); if (hndl == WaitHandle.WaitTimeout) { return(notifications); } int total = 0; while (true) { int res = WaitHandle.WaitAny(new WaitHandle[] { NotifyReceived, ErrorReceived, stop }, timeout); if (res == 0) { while (_notifications.Count > 0) { NotificationInfo info = _notifications.Dequeue(); Notify notify = info.Notify; System.Diagnostics.Debug.WriteLine("Process Notify: " + notify.GetHashCode()); RaiseDataReceived(info.RawData); bool add = false; if (notify.NotificationMessage != null) { if (messageCheck == null) { add = true; total += notify.NotificationMessage.Length; } else { foreach (NotificationMessageHolderType message in notify.NotificationMessage) { System.Diagnostics.Debug.WriteLine("Check if message passes the test"); if (messageCheck(message)) { System.Diagnostics.Debug.WriteLine("PASSED!"); add = true; total += 1; } else { System.Diagnostics.Debug.WriteLine("Message filtered out!"); } } } } if (add) { try { System.Diagnostics.Debug.WriteLine("Add Notify object: " + notify.GetHashCode()); notifications.Add(notify, info.RawData); } catch (Exception exc) { System.Diagnostics.Debug.WriteLine("Error when adding notify [" + notify.GetHashCode() + "]"); throw exc; } } if (total >= limit) { System.Diagnostics.Debug.WriteLine(string.Format("All {0} messages received", limit)); break; } } // while notifications.Count > 0 if (total >= limit) { break; // exit from the second cycle } } else if (res == 1) { // error - stop consumer.Stop(); break; } else if (res == 2) { // stop event exitByStop = true; break; } else { break; } } } finally { consumer.OnError -= errorHandling; consumer.OnNotify -= OnNotify; consumer.Stop(); } if (exitByStop) { throw new TestTool.Tests.Definitions.Exceptions.StopEventException(); } return(notifications); }