Click here to Skip to main content
11,411,494 members (66,536 online)
Click here to Skip to main content
Add your own
alternative version

The Super Pool Framework

, 31 Aug 2010 CPOL
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.
Matrix_SuperPool_Standalone.zip
Lib
PowerCollections.dll
Matrix.Framework.SuperPool.Standalone
bin
Debug
Release
Common.Sockets
Common
Core
Matrix.Common.Core
Collections
Identification
Results
Serialization
Matrix.Common.Extended
FastSerialization
Operationals
ThreadPools
Matrix.Common.Sockets
Common
Core
Matrix.Framework.MessageBus
Client
Clients
ExecutionStrategies
Core
Net
Messages
Matrix.Framework.SuperPool
Call
Clients
Core
DynamicProxy
Subscription
Matrix.Framework.SuperPool.Standalone.csproj.user
Properties
Tests
Lib
log4net.dll
Matrix.Common.Core.dll
Matrix.Common.Diagnostics.dll
Matrix.Common.Diagnostics.FrontEnd.dll
Matrix.Common.Extended.dll
Matrix.Common.FrontEnd.dll
Matrix.Common.Sockets.dll
Matrix.Framework.TestFramework.dll
nunit.framework.dll
Matrix.Framework.SuperPool.Demonstration
bin
Debug
Release
FormServer.cs.bak
Properties
Matrix.Framework.SuperPool.Test
bin
Debug
Release
Matrix.Framework.SuperPool.Test.vshost.exe.manifest
MultiThreadTests
Properties
Settings.settings
SpeedTests
Matrix.Framework.SuperPool.UnitTest
bin
Debug
Release
Matrix.Framework.SuperPool.UnitTest.csproj.user
Properties
// -----
// 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 System.Runtime.Serialization;
using Matrix.Common.Core.Serialization;
using Matrix.Common.Extended.FastSerialization;

#if Matrix_Diagnostics
using Matrix.Common.Diagnostics;
#endif


namespace Matrix.Framework.MessageBus.Core
{
    /// <summary>
    /// Envelope stores a message.
    /// </summary>
    [Serializable]
    public class Envelope
    {
        /// <summary>
        /// The type of duplication (or cloning), required to be done.
        /// </summary>
        public enum DuplicationModeEnum : int
        {
            /// <summary>
            /// Duplicate nothing.
            /// </summary>
            None,
            /// <summary>
            /// Duplicate the message instance only.
            /// </summary>
            DuplicateMessage,
            /// <summary>
            /// Duplicate the envelope instance.
            /// </summary>
            DuplicateEnvelope,
            /// <summary>
            /// Duplicate the envelope instance and the message instance.
            /// </summary>
            DuplicateBoth
        }

        /// <summary>
        /// Determine the type of execution to perform on the client.
        /// </summary>
        public enum ExecutionModelEnum
        {
            Default, // Execute the item by the default execution engine on the receiver.
            Direct // Execute the item directly on the receiver, bypassing the execution engine.
        }

        DuplicationModeEnum _duplicationMode = DuplicationModeEnum.None;
        /// <summary>
        /// Determines how (if any) duplication will be performed on the envelope and its data.
        /// By default value is None, meaning everything is transported using references.
        /// Duplication is mandatory (DuplicateBoth) when transporting to a remote location trough TCP.IP.
        /// </summary>
        public DuplicationModeEnum DuplicationMode
        {
            get { return _duplicationMode; }
            set { _duplicationMode = value; }
        }

        volatile object _message = null;
        /// <summary>
        /// The actual message.
        /// </summary>
        public object Message
        {
            get { return _message; }
            set { _message = value; }
        }

        ExecutionModelEnum _executionModel = ExecutionModelEnum.Default;
        /// <summary>
        /// Instructions on how to execute the item.
        /// </summary>
        public ExecutionModelEnum ExecutionModel
        {
            get { return _executionModel; }
            set { _executionModel = value; }
        }

        volatile EnvelopeTransportation _transportHistory = new EnvelopeTransportation();
        /// <summary>
        /// A history of the locations the item has visited.
        /// </summary>
        public EnvelopeTransportation History
        {
            get { return _transportHistory; }
        }

        volatile EnvelopeTransportation _transportTargetAddress = null;
        /// <summary>
        /// Information regarding transporting the item to somewhere.
        /// </summary>
        public EnvelopeTransportation Address
        {
            get { return _transportTargetAddress; }
            set { _transportTargetAddress = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public Envelope()
        {
        }

        /// <summary>
        /// Detailed constructor.
        /// </summary>
        public Envelope(object message)
        {
            _message = message;
        }

        /// <summary>
        /// Implementing the ISerializable to provide a faster, more optimized
        /// serialization for the class using the fast serialization elements.
        /// </summary>
        public Envelope(SerializationInfo info, StreamingContext context)
        {
            // Get from the info.
            SerializationReader reader = new SerializationReader((byte[])info.GetValue("data", typeof(byte[])));

            _duplicationMode = (DuplicationModeEnum)reader.ReadInt32();
            _executionModel = (ExecutionModelEnum)reader.ReadInt32();
            _message = reader.ReadObject();
            _transportHistory = (EnvelopeTransportation)reader.ReadObject();
            _transportTargetAddress = (EnvelopeTransportation)reader.ReadObject();
        }

        /// <summary>
        /// Implementing the ISerializable to provide a faster, more optimized
        /// serialization for the class.
        /// </summary>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            SerializationWriter writer = new SerializationWriter();

            writer.Write((int)_duplicationMode);
            writer.Write((int)_executionModel);
            writer.WriteObject(_message);
            writer.WriteObject(_transportHistory);
            writer.WriteObject(_transportTargetAddress);

            // Put to the info.
            info.AddValue("data", writer.ToArray());
        }

        /// <summary>
        /// This may actually *not* duplicate the object, since it follows the DuplicationMode.
        /// </summary>
        /// <returns></returns>
        public virtual Envelope Duplicate(ISerializer serializer)
        {
            Envelope newEnvelope = this;

            if (_duplicationMode == DuplicationModeEnum.DuplicateEnvelope
                || _duplicationMode == DuplicationModeEnum.DuplicateBoth)
            {
                //newObject = (Envelope)this.MemberwiseClone();
                //newObject = (Envelope)serializer.Duplicate(this);

                newEnvelope = new Envelope() { _duplicationMode = this._duplicationMode };

                EnvelopeTransportation transportHistory = _transportHistory;
                EnvelopeTransportation transportTargetAddress = _transportTargetAddress;

                if (transportHistory != null)
                {
                    newEnvelope._transportHistory = transportHistory.Duplicate();
                }

                if (transportTargetAddress != null)
                {
                    newEnvelope._transportTargetAddress = transportTargetAddress.Duplicate();
                }
            }
            
            if (_message != null &&
                (_duplicationMode == DuplicationModeEnum.DuplicateMessage
                 || _duplicationMode == DuplicationModeEnum.DuplicateBoth))
            {
                if (_message is ICloneable)
                {
                    newEnvelope._message = ((ICloneable)_message).Clone();
                }
                else if (_message.GetType().IsClass)
                {// We need to use the slow cloning mechanism.
                    newEnvelope._message = serializer.Duplicate(_message);
#if Matrix_Diagnostics
                    SystemMonitor.OperationErrorIf(newEnvelope._message == null, "Failed to serialize message [" + _message.GetType().Name + "].");
#endif
                }

                // Value type items are supposed to be copied by referencing.
            }

            return newEnvelope;
        }

    }
}

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)

Share

About the Author

Deyan Timnev
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).

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 31 Aug 2010
Article Copyright 2010 by Deyan Timnev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid