Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#

The Super Pool Framework

Rate me:
Please Sign up or sign in to vote.
4.87/5 (53 votes)
31 Aug 2010CPOL26 min read 101.4K   1.5K   178  
The Super Pool is a framework for decoupled communication and management of components. The Super Pool introduces a natural asynchronous communication environment into your solution that can be fluently spread over different components, threads, processes, or even computers or networks.
// -----
// Copyright 2010 Deyan Timnev
// This file is part of the Matrix Platform (www.matrixplatform.com).
// The Matrix Platform is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, 
// either version 3 of the License, or (at your option) any later version. The Matrix Platform is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
// without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License along with the Matrix Platform. If not, see http://www.gnu.org/licenses/lgpl.html
// -----
using System;
using Matrix.Framework.MessageBus.Clients.ExecutionStrategies;
using Matrix.Framework.MessageBus.Core;
using Matrix.Common.Core.Identification;

namespace Matrix.Framework.MessageBus.Clients
{
    /// <summary>
    /// Default stub standalone implementation of an Message bus Slim client.
    /// </summary>
    public class ActiveClient : MessageBusClient
    {
        volatile IMessageBus _messageBus = null;
        /// <summary>
        /// The instance of the message bus this client belongs to.
        /// </summary>
        public override IMessageBus MessageBus
        {
            get { return _messageBus; }
        }

        ExecutionStrategy _executionStrategy;
        /// <summary>
        /// Execution strategy defines how threads are managed on executing the tasks in the client.
        /// </summary>
        public override ExecutionStrategy ExecutionStrategy
        {
            get { return _executionStrategy; }
        }

        ClientId _id;
        /// <summary>
        /// The Id of this client as provided by the currently assigned
        /// message bus.
        /// </summary>
        public override ClientId Id
        {
            get { return _id; }
        }

        public override Type OptionalSourceType
        {
            get { return null; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ActiveClient(ClientId id)
        {
            _id = id;

            _executionStrategy = null;
            _messageBus = null;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ActiveClient(string name)
            : this(new ClientId(name))
        {
        }

        public override void Dispose()
        {
            _executionStrategy = null;
            _messageBus = null;

            base.Dispose();
        }

        /// <summary>
        /// Setup execution strategy for the 
        /// execution of class on this client.
        /// </summary>
        public override bool SetupExecutionStrategy(ExecutionStrategy executionStrategy)
        {
            lock (this)
            {
                if (_executionStrategy != null)
                {
                    _executionStrategy.Dispose();
                    _executionStrategy = null;
                }

                _executionStrategy = executionStrategy;
            }

            if (executionStrategy != null)
            {
                return executionStrategy.Initialize(this);
            }

            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        internal override bool AssignMessageBus(IMessageBus messageBus, int indexId)
        {
            if (_messageBus != null)
            {
                return false;
            }

            _id.MessageBus = messageBus;
            _id.LocalMessageBusIndex = indexId;

            //lock (this)
            {// Lock needed to assure operations go together.
                _messageBus = messageBus;
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        internal override void ReleaseMessageBus()
        {
            lock (this)
            {// Lock needed to assure operations go together.
                _messageBus = null;
            }
        }

        /// <summary>
        /// Receive an envelope for executing.
        /// </summary>
        internal override bool Receive(Envelope envelope)
        {
            ExecutionStrategy executionStrategy = _executionStrategy;
            if (executionStrategy != null)
            {
                executionStrategy.Execute(envelope);
            }

            RaiseEnvelopeReceivedEvent(envelope);
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void PerformExecution(Envelope envelope)
        {
            RaiseEnvelopeExecutingEvent(envelope);

            OnPerformExecution(envelope);

            RaiseEnvelopeExecutedEvent(envelope);
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void OnPerformExecution(Envelope envelope)
        {

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Ingenious Ltd, Bulgaria
Bulgaria Bulgaria
I worked for a few years as a C++/Win32 developer and software architect, and then moved on to the .NET environment where I was able to discover the beauty of managed programming.

I am currently involved in the development and management of Open Forex Platform (www.openforexplatform.com) and the Matrix Platform (www.matrixplatform.com).

Comments and Discussions