Пример #1
0
        /// <summary>
        /// Removes wires between server and client components (as defined in correlation set).
        /// </summary>
        /// <param name="type">Type of the server component</param>
        /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param>
        /// <param name="delegateCorrelationSet">Correlation set with wiring information</param>
        /// <param name="wiringList">List with known wirings</param>
        private void RemoveClientServerWires(Type type, EventStub eventStub, List <DelegateCorrelationInfo> delegateCorrelationSet, Dictionary <Guid, Delegate> wiringList)
        {
            if (delegateCorrelationSet == null)
            {
                return;
            }

            foreach (var correlationInfo in delegateCorrelationSet)
            {
                if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                {
                    var dynamicWireDelegate = wiringList[correlationInfo.CorrelationID];
                    eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicWireDelegate);
                    wiringList.Remove(correlationInfo.CorrelationID);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Removes wires between server and client components (as defined in correlation set).
        /// </summary>
        /// <param name="type">Type of the server component</param>
        /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param>
        /// <param name="delegateCorrelationSet">Correlation set with wiring information</param>
        /// <param name="wiringList">List with known wirings</param>
        private void RemoveClientServerWires(Type type, EventStub eventStub, IEnumerable <DelegateCorrelationInfo> delegateCorrelationSet, Dictionary <Guid, Delegate> wiringList)
        {
            if (delegateCorrelationSet == null)
            {
                return;
            }

            var currentSession = ServerSession.CurrentSession;

            foreach (var correlationInfo in delegateCorrelationSet)
            {
                if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                {
                    lock (wiringList)
                    {
                        if (!wiringList.ContainsKey(correlationInfo.CorrelationID))
                        {
                            continue;
                        }

                        var dynamicWireDelegate = wiringList[correlationInfo.CorrelationID];
                        eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicWireDelegate);
                        wiringList.Remove(correlationInfo.CorrelationID);

                        var dynamicWire = DynamicWireFactory.GetDynamicWire(dynamicWireDelegate);
                        if (dynamicWire != null)
                        {
                            dynamicWire.Dispose();
                        }
                    }

                    _host.OnSubscriptionRemoved(new SubscriptionEventArgs
                    {
                        ComponentType      = type,
                        DelegateMemberName = correlationInfo.DelegateMemberName,
                        CorrelationID      = correlationInfo.CorrelationID,
                    });
                }
            }

            if (currentSession != null)
            {
                currentSession.UntrackRemoteSubscriptions(delegateCorrelationSet);
            }
        }
Пример #3
0
        /// <summary>
        /// Creates wires between client component and server component.
        /// </summary>
        /// <param name="type">Implementation type of the server component.</param>
        /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param>
        /// <param name="delegateCorrelationSet">Correlation set (say how to wire)</param>
        /// <param name="wiringList">Collection of built wires</param>
        private void CreateClientServerWires(Type type, EventStub eventStub, List <DelegateCorrelationInfo> delegateCorrelationSet, Dictionary <Guid, Delegate> wiringList)
        {
            if (delegateCorrelationSet == null)
            {
                return;
            }

            foreach (var correlationInfo in delegateCorrelationSet)
            {
                if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                {
                    continue;
                }

                if (ServerSession.CurrentSession == null)
                {
                    throw new InvalidSessionException(string.Format(LanguageResource.InvalidSessionException_SessionIDInvalid, "(null)"));
                }

                var dynamicWire = DynamicWireFactory.CreateDynamicWire(type, correlationInfo.DelegateMemberName, correlationInfo.IsEvent);
                dynamicWire.Interceptor = correlationInfo.ClientDelegateInterceptor;

                if (correlationInfo.IsEvent)
                {
                    var dynamicEventWire = (DynamicEventWireBase)dynamicWire;
                    dynamicEventWire.EventFilter = correlationInfo.EventFilter;

                    // add session validation handler and unsubscription callback
                    var sessionId      = ServerSession.CurrentSession.SessionID;
                    var sessionManager = _host.SessionManager;
                    dynamicEventWire.ValidateSession    = () => sessionManager.ExistSession(sessionId);
                    dynamicEventWire.CancelSubscription = () => eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate);

                    eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate);
                    wiringList.Add(correlationInfo.CorrelationID, dynamicEventWire.InDelegate);
                }
                else
                {
                    eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicWire.InDelegate);
                    wiringList.Add(correlationInfo.CorrelationID, dynamicWire.InDelegate);
                }
            }
        }
Пример #4
0
        public void EventStub_FuncDelegateTests()
        {
            // add the first handler
            var eventStub = new EventStub(typeof(ISampleInterface));
            var fired     = false;

            eventStub.AddHandler("FuncDelegate", new Func <int, string>(a => { fired = true; return(a.ToString()); }));

            // check if it is called
            var handler = (Func <int, string>)eventStub["FuncDelegate"];
            var result  = handler(123);

            Assert.IsTrue(fired);
            Assert.AreEqual("123", result);

            // add the second handler
            fired = false;
            var firedAgain  = false;
            var tempHandler = new Func <int, string>(a => { firedAgain = true; return(a.ToString()); });

            eventStub.AddHandler("FuncDelegate", tempHandler);

            // check if it is called
            result = handler(321);
            Assert.IsTrue(fired);
            Assert.IsTrue(firedAgain);
            Assert.AreEqual("321", result);

            // remove the second handler
            fired      = false;
            firedAgain = false;
            eventStub.RemoveHandler("FuncDelegate", tempHandler);

            // check if it is not called
            result = handler(0);
            Assert.IsTrue(fired);
            Assert.IsFalse(firedAgain);
            Assert.AreEqual("0", result);
        }
Пример #5
0
        public void EventStub_ActionDelegateTests()
        {
            // add the first handler
            var eventStub = new EventStub(typeof(ISampleInterface));
            var fired     = false;

            eventStub.AddHandler("ActionDelegate", new Action(() => fired = true));

            // check if it is called
            var handler = (Action)eventStub["ActionDelegate"];

            handler();
            Assert.IsTrue(fired);

            // add the second handler
            fired = false;
            var firedAgain  = false;
            var tempHandler = new Action(() => firedAgain = true);

            eventStub.AddHandler("ActionDelegate", tempHandler);

            // check if it is called
            handler();
            Assert.IsTrue(fired);
            Assert.IsTrue(firedAgain);

            // remove the second handler
            fired      = false;
            firedAgain = false;
            eventStub.RemoveHandler("ActionDelegate", tempHandler);

            // check if it is not called
            handler();
            Assert.IsTrue(fired);
            Assert.IsFalse(firedAgain);
        }
Пример #6
0
        public void EventStub_CancelEventTests()
        {
            // add the first handler
            var eventStub = new EventStub(typeof(ISampleInterface));
            var fired     = false;

            eventStub.AddHandler("CancelEvent", new EventHandler <CancelEventArgs>((sender, args) => fired = true));

            // check if it is called
            var handler = (EventHandler <CancelEventArgs>)eventStub["CancelEvent"];

            handler(this, new CancelEventArgs());
            Assert.IsTrue(fired);

            // add the second handler
            fired = false;
            var firedAgain  = false;
            var tempHandler = new EventHandler <CancelEventArgs>((sender, args) => firedAgain = true);

            eventStub.AddHandler("CancelEvent", tempHandler);

            // check if it is called
            handler(this, new CancelEventArgs());
            Assert.IsTrue(fired);
            Assert.IsTrue(firedAgain);

            // remove the second handler
            fired      = false;
            firedAgain = false;
            eventStub.RemoveHandler("CancelEvent", tempHandler);

            // check if it is not called
            handler(this, new CancelEventArgs());
            Assert.IsTrue(fired);
            Assert.IsFalse(firedAgain);
        }
Пример #7
0
        /// <summary>
        /// Creates wires between client component and server component.
        /// </summary>
        /// <param name="type">Implementation type of the server component.</param>
        /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param>
        /// <param name="delegateCorrelationSet">Correlation set (say how to wire)</param>
        /// <param name="wiringList">Collection of built wires</param>
        private void CreateClientServerWires(Type type, EventStub eventStub, IEnumerable <DelegateCorrelationInfo> delegateCorrelationSet, Dictionary <Guid, Delegate> wiringList)
        {
            if (delegateCorrelationSet == null)
            {
                return;
            }

            var currentSession = ServerSession.CurrentSession;

            if (currentSession == null)
            {
                throw new InvalidSessionException(string.Format(LanguageResource.InvalidSessionException_SessionIDInvalid, "(null)"));
            }

            foreach (var correlationInfo in delegateCorrelationSet)
            {
                if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                {
                    continue;
                }

                var dynamicWire = DynamicWireFactory.CreateDynamicWire(type, correlationInfo.DelegateMemberName, correlationInfo.IsEvent);
                dynamicWire.Interceptor = correlationInfo.ClientDelegateInterceptor;

                lock (wiringList)
                {
                    if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                    {
                        continue;
                    }

                    if (correlationInfo.IsEvent)
                    {
                        var dynamicEventWire = (DynamicEventWireBase)dynamicWire;
                        dynamicEventWire.EventFilter = correlationInfo.EventFilter;

                        // add session validation handler and unsubscription callback
                        var sessionId      = currentSession.SessionID;
                        var sessionManager = _host.SessionManager;
                        dynamicEventWire.ValidateSession    = () => sessionManager.ExistSession(sessionId);
                        dynamicEventWire.CancelSubscription = ex =>
                        {
                            eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate);
                            wiringList.Remove(correlationInfo.CorrelationID);
                            currentSession.DecrementRemoteSubscriptionCounter();
                            _host.OnSubscriptionCanceled(new SubscriptionEventArgs
                            {
                                ComponentType      = type,
                                DelegateMemberName = correlationInfo.DelegateMemberName,
                                CorrelationID      = correlationInfo.CorrelationID,
                                Exception          = ex
                            });
                        };

                        eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate);
                        wiringList.Add(correlationInfo.CorrelationID, dynamicEventWire.InDelegate);
                    }
                    else
                    {
                        eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicWire.InDelegate);
                        wiringList.Add(correlationInfo.CorrelationID, dynamicWire.InDelegate);
                    }
                }

                currentSession.IncrementRemoteSubscriptionCounter();
                _host.OnSubscriptionAdded(new SubscriptionEventArgs
                {
                    ComponentType      = type,
                    DelegateMemberName = correlationInfo.DelegateMemberName,
                    CorrelationID      = correlationInfo.CorrelationID,
                });
            }
        }
Пример #8
0
        /// <summary>
        /// Removes wires between server and client components (as defined in correlation set).
        /// </summary>
        /// <param name="type">Type of the server component</param>
        /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param>
        /// <param name="delegateCorrelationSet">Correlation set with wiring information</param>
        /// <param name="wiringList">List with known wirings</param>
        private void RemoveClientServerWires(Type type, EventStub eventStub, List<DelegateCorrelationInfo> delegateCorrelationSet, Dictionary<Guid, Delegate> wiringList)
        {
            if (delegateCorrelationSet == null)
                return;

            foreach (var correlationInfo in delegateCorrelationSet)
            {
                if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                {
                    var dynamicWireDelegate = wiringList[correlationInfo.CorrelationID];
                    eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicWireDelegate);
                    wiringList.Remove(correlationInfo.CorrelationID);
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Creates wires between client component and server component.
        /// </summary>
        /// <param name="type">Implementation type of the server component.</param>
        /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param>
        /// <param name="delegateCorrelationSet">Correlation set (say how to wire)</param>
        /// <param name="wiringList">Collection of built wires</param>
        private void CreateClientServerWires(Type type, EventStub eventStub, List<DelegateCorrelationInfo> delegateCorrelationSet, Dictionary<Guid, Delegate> wiringList)
        {
            if (delegateCorrelationSet == null)
                return;

            foreach (var correlationInfo in delegateCorrelationSet)
            {
                if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                    continue;

                if (ServerSession.CurrentSession == null)
                    throw new InvalidSessionException(string.Format(LanguageResource.InvalidSessionException_SessionIDInvalid, "(null)"));

                var dynamicWire = DynamicWireFactory.CreateDynamicWire(type, correlationInfo.DelegateMemberName, correlationInfo.IsEvent);
                dynamicWire.Interceptor = correlationInfo.ClientDelegateInterceptor;

                if (correlationInfo.IsEvent)
                {
                    var dynamicEventWire = (DynamicEventWireBase)dynamicWire;
                    dynamicEventWire.EventFilter = correlationInfo.EventFilter;

                    // add session validation handler and unsubscription callback
                    var sessionId = ServerSession.CurrentSession.SessionID;
                    var sessionManager = _host.SessionManager;
                    dynamicEventWire.ValidateSession = () => sessionManager.ExistSession(sessionId);
                    dynamicEventWire.CancelSubscription = () => eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate);

                    eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate);
                    wiringList.Add(correlationInfo.CorrelationID, dynamicEventWire.InDelegate);
                }
                else
                {
                    eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicWire.InDelegate);
                    wiringList.Add(correlationInfo.CorrelationID, dynamicWire.InDelegate);
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Removes wires between server and client components (as defined in correlation set).
        /// </summary>
        /// <param name="type">Type of the server component</param>
        /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param>
        /// <param name="delegateCorrelationSet">Correlation set with wiring information</param>
        /// <param name="wiringList">List with known wirings</param>
        private void RemoveClientServerWires(Type type, EventStub eventStub, IEnumerable<DelegateCorrelationInfo> delegateCorrelationSet, Dictionary<Guid, Delegate> wiringList)
        {
            if (delegateCorrelationSet == null)
                return;

            var currentSession = ServerSession.CurrentSession;
            foreach (var correlationInfo in delegateCorrelationSet)
            {
                if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                {
                    lock (wiringList)
                    {
                        if (!wiringList.ContainsKey(correlationInfo.CorrelationID))
                            continue;

                        var dynamicWireDelegate = wiringList[correlationInfo.CorrelationID];
                        eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicWireDelegate);
                        wiringList.Remove(correlationInfo.CorrelationID);
                        if (currentSession != null)
                        {
                            currentSession.DecrementRemoteSubscriptionCounter();
                        }
                    }

                    _host.OnSubscriptionRemoved(new SubscriptionEventArgs
                    {
                        ComponentType = type,
                        DelegateMemberName = correlationInfo.DelegateMemberName,
                        CorrelationID = correlationInfo.CorrelationID,
                    });
                }
            }
        }
Пример #11
0
        /// <summary>
        /// Creates wires between client component and server component.
        /// </summary>
        /// <param name="type">Implementation type of the server component.</param>
        /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param>
        /// <param name="delegateCorrelationSet">Correlation set (say how to wire)</param>
        /// <param name="wiringList">Collection of built wires</param>
        private void CreateClientServerWires(Type type, EventStub eventStub, IEnumerable<DelegateCorrelationInfo> delegateCorrelationSet, Dictionary<Guid, Delegate> wiringList)
        {
            if (delegateCorrelationSet == null)
                return;

            var currentSession = ServerSession.CurrentSession;
            if (currentSession == null)
                throw new InvalidSessionException(string.Format(LanguageResource.InvalidSessionException_SessionIDInvalid, "(null)"));

            foreach (var correlationInfo in delegateCorrelationSet)
            {
                if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                    continue;

                var dynamicWire = DynamicWireFactory.CreateDynamicWire(type, correlationInfo.DelegateMemberName, correlationInfo.IsEvent);
                dynamicWire.Interceptor = correlationInfo.ClientDelegateInterceptor;

                lock (wiringList)
                {
                    if (wiringList.ContainsKey(correlationInfo.CorrelationID))
                        continue;

                    if (correlationInfo.IsEvent)
                    {
                        var dynamicEventWire = (DynamicEventWireBase)dynamicWire;
                        dynamicEventWire.EventFilter = correlationInfo.EventFilter;

                        // add session validation handler and unsubscription callback
                        var sessionId = currentSession.SessionID;
                        var sessionManager = _host.SessionManager;
                        dynamicEventWire.ValidateSession = () => sessionManager.ExistSession(sessionId);
                        dynamicEventWire.CancelSubscription = ex =>
                        {
                            eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate);
                            wiringList.Remove(correlationInfo.CorrelationID);
                            currentSession.DecrementRemoteSubscriptionCounter();
                            _host.OnSubscriptionCanceled(new SubscriptionEventArgs
                            {
                                ComponentType = type,
                                DelegateMemberName = correlationInfo.DelegateMemberName,
                                CorrelationID = correlationInfo.CorrelationID,
                                Exception = ex
                            });
                        };

                        eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate);
                        wiringList.Add(correlationInfo.CorrelationID, dynamicEventWire.InDelegate);
                    }
                    else
                    {
                        eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicWire.InDelegate);
                        wiringList.Add(correlationInfo.CorrelationID, dynamicWire.InDelegate);
                    }
                }

                currentSession.IncrementRemoteSubscriptionCounter();
                _host.OnSubscriptionAdded(new SubscriptionEventArgs
                {
                    ComponentType = type,
                    DelegateMemberName = correlationInfo.DelegateMemberName,
                    CorrelationID = correlationInfo.CorrelationID,
                });
            }
        }