Exemplo n.º 1
0
        public virtual void Register <T>(Action <T> action) where T : class
        {
            var  handlesType = HandleValueTypeMessage(typeof(T));
            bool decorateActorWithMailbox = LocalConcurrencyType.OneThreadPerActor == this.LocalConcurrencyType;
            bool isManyThreads            = LocalConcurrencyType.OneActorPerThread == this.LocalConcurrencyType;

            IActor anonActor = new AnonActor <T>(action);

            if (decorateActorWithMailbox)
            {
                anonActor = new MailBoxActor(anonActor);
            }
            //else
            //   anonActor = new AnonActor<T>(action);

            // For invocation return the anonActor we just created always.
            ActorInvocationBase invoker = new ActorInvocation <IActor>(() => anonActor);

            //NOTE: for anonymouse Actions the actor type is the same as the message type
            if (!_actorTypeToInstatiation.TryAdd(handlesType, invoker))
            {
                throw new FailedToRegisterActorInvocationForActionException(handlesType.ToString());
            }

            if (!_messageTypeToActor.TryAdd(handlesType, anonActor))
            {
                throw new FailedToRegisterActionForTypeException(handlesType.ToString());
            }

            uint iterationCount = (isManyThreads) ? this.Concurrency : 1;

            try { _actorRegistered.Set(); }
            catch (SemaphoreFullException) { }
        }
Exemplo n.º 2
0
        public virtual void Register <T, K>(System.Linq.Expressions.Expression <Func <K> > actorCreationFunction)
        //where T : IActorMessage
            where K : IActor
        {
            //var handlesType = typeof(T);
            //var actor = func.Invoke();
            bool decorateActorWithMailbox = LocalConcurrencyType.OneThreadPerActor == this.LocalConcurrencyType;
            var  actorType   = typeof(K);
            var  handlesType = HandleValueTypeMessage(typeof(T));

            IActor actor = null;
            ActorInvocationBase invoker = null;

            if (_actorTypeToInstatiation.ContainsKey(actorType))
            {
                if (!_actorTypeToInstatiation.TryGetValue(actorType, out invoker))
                {
                    invoker = null;
                }
                else
                {
                    actor = invoker.Invoke(decorateActorWithMailbox);
                }
            }

            if (null == invoker)
            {
                var func = actorCreationFunction.Compile();
                invoker = new ActorInvocation <K>(func);
                actor   = invoker.Invoke(decorateActorWithMailbox);
                if (!_actorTypeToInstatiation.TryAdd(actorType, invoker))
                {
                    throw new FailedToRegisterActorInvocationForTypeException(actorType.ToString());
                }
            }

            if (!_messageTypeToActor.TryAdd(handlesType, actor))
            {
                throw new FailedToRegisterActorForTypeException(handlesType.ToString());
            }

            try { _actorRegistered.Set(); }
            catch (SemaphoreFullException) { }
        }