Ejemplo n.º 1
0
        public IFABChannelPipeline AddLast(IEventExecutorGroup group, string name, IFABChannelHandler handler)
        {
            Contract.Requires(handler != null);

            FABHandlerContext newCtx;

            lock (this)
            {
                CheckMultiplicity(handler);

                newCtx = this.NewContext(group, this.FilterName(name, handler), handler);
                IEventExecutor executor = this.ExecutorSafe(newCtx.executor);

                this.AddLast0(newCtx);

                // If the executor is null it means that the channel was not registered on an eventloop yet.
                // In this case we add the context to the pipeline and add a task that will call
                // ChannelHandler.handlerAdded(...) once the channel is registered.
                if (executor == null)
                {
                    this.CallHandlerCallbackLater(newCtx, true);
                    return(this);
                }

                if (!executor.InEventLoop)
                {
                    executor.Execute(CallHandlerAddedAction, this, newCtx);
                    return(this);
                }
            }
            this.CallHandlerAdded0(newCtx);
            return(this);
        }
Ejemplo n.º 2
0
        IFABChannelHandler Replace(FABHandlerContext ctx, string newName, IFABChannelHandler newHandler)
        {
            Contract.Requires(newHandler != null);
            Contract.Assert(ctx != this.head && ctx != this.tail);

            FABHandlerContext newCtx;

            lock (this)
            {
                CheckMultiplicity(newHandler);
                if (newName == null)
                {
                    newName = this.GenerateName(newHandler);
                }
                else
                {
                    bool sameName = ctx.Name.Equals(newName, StringComparison.Ordinal);
                    if (!sameName)
                    {
                        this.CheckDuplicateName(newName);
                    }
                }

                newCtx = this.NewContext(ctx.executor, newName, newHandler);
                IEventExecutor executor = this.ExecutorSafe(ctx.executor);

                Replace0(ctx, newCtx);

                // If the executor is null it means that the channel was not registered on an event loop yet.
                // In this case we replace the context in the pipeline
                // and add a task that will signal handler it was added or removed
                // once the channel is registered.
                if (executor == null)
                {
                    this.CallHandlerCallbackLater(newCtx, true);
                    this.CallHandlerCallbackLater(ctx, false);
                    return(ctx.Handler);
                }

                if (!executor.InEventLoop)
                {
                    executor.Execute(() =>
                    {
                        // Indicate new handler was added first (i.e. before old handler removed)
                        // because "removed" will trigger ChannelRead() or Flush() on newHandler and
                        // those event handlers must be called after handler was signaled "added".
                        this.CallHandlerAdded0(newCtx);
                        this.CallHandlerRemoved0(ctx);
                    });
                    return(ctx.Handler);
                }
            }
            // Indicate new handler was added first (i.e. before old handler removed)
            // because "removed" will trigger ChannelRead() or Flush() on newHandler and
            // those event handlers must be called after handler was signaled "added".
            this.CallHandlerAdded0(newCtx);
            this.CallHandlerRemoved0(ctx);
            return(ctx.Handler);
        }
Ejemplo n.º 3
0
        protected static SkipFlags GetSkipPropagationFlags(IFABChannelHandler handler)
        {
            Tuple <SkipFlags> skipDirection = SkipTable.GetValue(
                handler.GetType(),
                handlerType => Tuple.Create(CalculateSkipPropagationFlags(handlerType)));

            return(skipDirection == null ? 0 : skipDirection.Item1);
        }
Ejemplo n.º 4
0
        public SealedFABHandlerContext(
            FABChannelPipeline pipeline, IEventExecutor executor, string name, IFABChannelHandler handler)
            : base(pipeline, executor, name, GetSkipPropagationFlags(handler))
        {
            Contract.Requires(handler != null);

            this.handler = handler;
        }
Ejemplo n.º 5
0
 string FilterName(string name, IFABChannelHandler handler)
 {
     if (name == null)
     {
         return(this.GenerateName(handler));
     }
     this.CheckDuplicateName(name);
     return(name);
 }
Ejemplo n.º 6
0
        FABHandlerContext GetContextOrThrow(IFABChannelHandler handler)
        {
            var ctx = (FABHandlerContext)this.Context(handler);

            if (ctx == null)
            {
                throw new FABChannelPipelineException("Handler of type `{0}` could not be found in the pipeline.", typeof(IFABChannelHandler).Name);
            }

            return(ctx);
        }
Ejemplo n.º 7
0
        public IFABChannelPipeline AddFirst(IEventExecutorGroup group, params IFABChannelHandler[] handlers)
        {
            Contract.Requires(handlers != null);

            for (int i = handlers.Length - 1; i >= 0; i--)
            {
                IFABChannelHandler h = handlers[i];
                this.AddFirst(group, (string)null, h);
            }

            return(this);
        }
Ejemplo n.º 8
0
        static void CheckMultiplicity(IFABChannelHandler handler)
        {
            var adapter = handler as FABChannelAdapter;

            if (adapter != null)
            {
                FABChannelAdapter h = adapter;
                if (!h.IsSharable && h.Added)
                {
                    throw new FABChannelPipelineException(
                              h.GetType().Name + " is not a @Sharable handler, so can't be added or removed multiple times.");
                }
                h.Added = true;
            }
        }
Ejemplo n.º 9
0
        public IFABChannelHandlerContext Context(IFABChannelHandler handler)
        {
            Contract.Requires(handler != null);

            FABHandlerContext ctx = this.head.Next;

            while (true)
            {
                if (ctx == null)
                {
                    return(null);
                }

                if (ctx.Handler == handler)
                {
                    return(ctx);
                }

                ctx = ctx.Next;
            }
        }
Ejemplo n.º 10
0
        string GenerateName(IFABChannelHandler handler)
        {
            ConditionalWeakTable <Type, string> cache = NameCaches.Value;
            Type   handlerType = handler.GetType();
            string name        = cache.GetValue(handlerType, t => GenerateName0(t));

            // It's not very likely for a user to put more than one handler of the same type, but make sure to avoid
            // any name conflicts.  Note that we don't cache the names generated here.
            if (this.Context0(name) != null)
            {
                string baseName = name.Substring(0, name.Length - 1); // Strip the trailing '0'.
                for (int i = 1; ; i++)
                {
                    string newName = baseName + i;
                    if (this.Context0(newName) == null)
                    {
                        name = newName;
                        break;
                    }
                }
            }
            return(name);
        }
Ejemplo n.º 11
0
 public IFABChannelHandler Replace(string oldName, string newName, IFABChannelHandler newHandler)
 {
     return(this.Replace(this.GetContextOrThrow(oldName), newName, newHandler));
 }
Ejemplo n.º 12
0
 public IFABChannelPipeline AddLast(string name, IFABChannelHandler handler)
 {
     return(this.AddLast(null, name, handler));
 }
Ejemplo n.º 13
0
 FABHandlerContext NewContext(IEventExecutor executor, string name, IFABChannelHandler handler)
 {
     return(new SealedFABHandlerContext(this, executor, name, handler));
 }
Ejemplo n.º 14
0
 FABHandlerContext NewContext(IEventExecutorGroup group, string name, IFABChannelHandler handler)
 {
     return(new SealedFABHandlerContext(this, this.GetChildExecutor(group), name, handler));
 }
Ejemplo n.º 15
0
 public IFABChannelPipeline AddAfter(string baseName, string name, IFABChannelHandler handler)
 {
     return(this.AddAfter(null, baseName, name, handler));
 }
Ejemplo n.º 16
0
 public T Replace <T>(string newName, IFABChannelHandler newHandler)
     where T : class, IFABChannelHandler
 {
     return((T)this.Replace(this.GetContextOrThrow <T>(), newName, newHandler));
 }
Ejemplo n.º 17
0
 public IFABChannelPipeline Replace(IFABChannelHandler oldHandler, string newName, IFABChannelHandler newHandler)
 {
     this.Replace(this.GetContextOrThrow(oldHandler), newName, newHandler);
     return(this);
 }
Ejemplo n.º 18
0
 public IFABChannelPipeline Remove(IFABChannelHandler handler)
 {
     this.Remove(this.GetContextOrThrow(handler));
     return(this);
 }