public void FilteredCustomHandlerUsingCombinedFilter_FiltersEventsRemotely() { // prepare event handler var firstArgument = 0; var secondArgument = string.Empty; var handled = false; var handler = new CustomEventType((first, second) => { firstArgument = first; secondArgument = second; handled = true; }); // initialize event filter handler = FilteredEventHandler.Create(handler, new CustomEventFilter("2.718", "1.618"), false); handler = FilteredEventHandler.Create(handler, new CustomEventFilter("3.14", "2.718"), false); // attach client-side event filter var proxy = ZyanConnection.CreateProxy <ISampleServer>(); proxy.CustomEvent += handler; // raise events, check results proxy.RaiseCustomEvent(3, ".14"); // filtered out Assert.IsFalse(handled); proxy.RaiseCustomEvent(2, ".718"); Assert.IsTrue(handled); Assert.AreEqual(2, firstArgument); Assert.AreEqual(".718", secondArgument); handled = false; proxy.RaiseCustomEvent(1, ".618"); // filtered out Assert.IsFalse(handled); }
public void FilteredEventHandlerOfCustomType_FiltersEventsLocally() { // prepare event handler var firstArgument = 0; var secondArgument = string.Empty; var handled = false; var handler = new CustomEventType((first, second) => { firstArgument = first; secondArgument = second; handled = true; }); // attach client-side event filter var sample = new SampleServer(); sample.CustomEvent += FilteredEventHandler.Create(handler, new CustomEventFilter("3.14")); // raise events, check results sample.RaiseCustomEvent(1, string.Empty); // filtered out Assert.IsFalse(handled); sample.RaiseCustomEvent(3, ".14"); Assert.IsTrue(handled); Assert.AreEqual(3, firstArgument); Assert.AreEqual(".14", secondArgument); handled = false; sample.RaiseCustomEvent(); // filtered out Assert.IsFalse(handled); }
public void FilteredEventHandlerOfCustomType_FiltersEventsRemotely() { // prepare event handler var firstArgument = 0; var secondArgument = string.Empty; var handled = false; var handler = new CustomEventType((first, second) => { firstArgument = first; secondArgument = second; handled = true; }); // attach server-side event filter var proxy = ZyanConnection.CreateProxy <ISampleServer>(); proxy.CustomEvent += FilteredEventHandler.Create(handler, new CustomEventFilter("2.71828"), false); // raise events, check results proxy.RaiseCustomEvent(1, string.Empty); // filtered out Assert.IsFalse(handled); proxy.RaiseCustomEvent(2, ".71828"); Assert.IsTrue(handled); Assert.AreEqual(2, firstArgument); Assert.AreEqual(".71828", secondArgument); handled = false; proxy.RaiseCustomEvent(); // filtered out Assert.IsFalse(handled); }
public void EventsWithArgumentsDerivedFromSessionBoundEvents_AreBoundToSessionsAndCanBeFiltered() { // start a new session using (var conn = new ZyanConnection(ZyanConnection.ServerUrl, new NullClientProtocolSetup())) { var proxy1 = ZyanConnection.CreateProxy <ISampleServer>(); var proxy2 = conn.CreateProxy <ISampleServer>(); var handled1 = 0; var handled2 = 0; proxy1.CustomSessionBoundEvent += FilteredEventHandler.Create((object s, CustomEventArgs args) => handled1 = args.Value, new CustomSessionBoundEventFilter(123, 321)); proxy2.CustomSessionBoundEvent += (s, args) => handled2 = args.Value; proxy1.RaiseCustomSessionBoundEvent(123); Assert.AreEqual(123, handled1); Assert.AreEqual(0, handled2); proxy2.RaiseCustomSessionBoundEvent(321); Assert.AreEqual(123, handled1); Assert.AreEqual(321, handled2); proxy1.RaiseCustomSessionBoundEvent(111); // filtered out Assert.AreEqual(123, handled1); Assert.AreEqual(321, handled2); } }
/* Syntax checks */ private void SyntaxChecks() { var proxy = ZyanConnection.CreateProxy <ISampleServer>(); // Create for EventHandler type — no generic parameters proxy.TestEvent += FilteredEventHandler.Create(TestEventHandler, new TestEventFilter()); // Create for EventHandler<TEventArgs> type — single generic parameter for EventArgs type proxy.SampleEvent += FilteredEventHandler.Create <SampleEventArgs>(SampleEventHandler, new SampleEventFilter()); // Create for custom event type — single generic parameter for delegate type proxy.CustomEvent += FilteredEventHandler.Create <CustomEventType>(CustomEventHandler, new CustomEventFilter()); // AddFilter for EventHandler type — no generic parameters var testEventHandler = new EventHandler(TestEventHandler); testEventHandler = testEventHandler.AddFilter(new TestEventFilter()); // AddFilter for EventHandler<TEventArgs> type — no generic parameters var sampleEventHandler = new EventHandler <SampleEventArgs>(SampleEventHandler); sampleEventHandler = sampleEventHandler.AddFilter(new SampleEventFilter()); // AddFilter for EventHandler<TEventArgs> using Linq expression — no generic parameters sampleEventHandler = sampleEventHandler.AddFilter((sender, args) => args.Value != 1); }
public void FilteredEventHandlerUsingFactorySyntax_FiltersEventsLocally() { // prepare event handler, attach event filter var handledValue = 0; var sample = new SampleServer(); sample.SampleEvent += FilteredEventHandler.Create((object sender, SampleEventArgs args) => handledValue = args.Value, new SampleEventFilter(321)); // raise events, check results sample.RaiseSampleEvent(123); // filtered out Assert.AreEqual(0, handledValue); sample.RaiseSampleEvent(321); Assert.AreEqual(321, handledValue); handledValue = 111; sample.RaiseSampleEvent(456); // filtered out Assert.AreEqual(111, handledValue); }
public void FilteredEventHandlerUsingFactorySyntax_FiltersEventsRemotely() { // prepare event handler var handledValue = 0; var handler = new EventHandler <SampleEventArgs>((sender, args) => handledValue = args.Value); // attach server-side event filter var proxy = ZyanConnection.CreateProxy <ISampleServer>(); proxy.SampleEvent += FilteredEventHandler.Create(handler, new SampleEventFilter(123), false); // raise events proxy.RaiseSampleEvent(111); // filtered out Assert.AreEqual(0, handledValue); proxy.RaiseSampleEvent(123); Assert.AreEqual(123, handledValue); handledValue = 222; proxy.RaiseSampleEvent(456); // filtered out Assert.AreEqual(222, handledValue); }
public void EventsWithArgumentsDerivedFromSessionBoundEvents_CanListenToOtherSessions() { var nullProtocol = new NullClientProtocolSetup(); // start two new sessions using (var conn2 = new ZyanConnection(ZyanConnection.ServerUrl, nullProtocol)) using (var conn3 = new ZyanConnection(ZyanConnection.ServerUrl, nullProtocol)) { var proxy1 = ZyanConnection.CreateProxy <ISampleServer>(); var proxy2 = conn2.CreateProxy <ISampleServer>(); var proxy3 = conn3.CreateProxy <ISampleServer>(); var sessions13 = new[] { ZyanConnection.SessionID, conn3.SessionID }; // session2 is not included var handled1 = 0; var handled2 = 0; var handled3 = 0; proxy1.CustomSessionBoundEvent += (s, args) => handled1 = args.Value; proxy2.CustomSessionBoundEvent += FilteredEventHandler.Create <CustomEventArgs>((s, args) => handled2 = args.Value, new SessionEventFilter(sessions13)); proxy3.CustomSessionBoundEvent += (s, args) => handled3 = args.Value; proxy1.RaiseCustomSessionBoundEvent(123); Assert.AreEqual(123, handled1); Assert.AreEqual(123, handled2); // proxy2 receives event from session1 Assert.AreEqual(0, handled3); proxy2.RaiseCustomSessionBoundEvent(321); Assert.AreEqual(123, handled1); Assert.AreEqual(123, handled2); // proxy2 doesn't receive events from session2 Assert.AreEqual(0, handled3); proxy3.RaiseCustomSessionBoundEvent(111); Assert.AreEqual(123, handled1); Assert.AreEqual(111, handled2); // proxy2 receives event from session3 Assert.AreEqual(111, handled3); } }