Click here to Skip to main content
11,577,429 members (36,655 online)
Click here to Skip to main content
Add your own
alternative version

The Super Pool Framework

, 31 Aug 2010 CPOL 52.1K 1.2K 169
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.Collections.Generic;
using System.Text;
using System.Reflection;
using Matrix.Common.Core.Collections;

namespace Matrix.Framework.SuperPool.DynamicProxy
{
    /// <summary>
    /// Class manages the operation of dynamically built proxy type instances and types.
    /// Operation is separated as follows:
    /// - proxy class, implemetns a proxy instance of a client instance.
    /// </summary>
    public class ProxyTypeManager : IDisposable
    {
        private ProxyTypeBuilder _builder = new ProxyTypeBuilder("ProxyTypeManager.Proxies");

        Dictionary<Type, object> _proxyObjects = new Dictionary<Type, object>();

        /// <summary>
        /// The builder we used to create the proxy types.
        /// </summary>
        public ProxyTypeBuilder Builder
        {
            get { return _builder; }
        }

        /// <summary>
        /// All reference types are automatically defaulted to null, so this only handles the value types.
        /// </summary>
        static HotSwapDictionary<Type, object> _defaultReturnValues = new HotSwapDictionary<Type, object>();

        /// <summary>
        /// Static constructor.
        /// </summary>
        static ProxyTypeManager()
        {
            _defaultReturnValues.AddRange(
                new Type[] { typeof(Int16), typeof(Int32), typeof(Int64), typeof(uint), typeof(double), typeof(string) },
                new object[] { 0, 0, 0, 0, 0, string.Empty });
        }

        IProxyTypeSink _sink;

        public IProxyTypeSink Sink
        {
            get { return _sink; }
            set { _sink = value; }
        }

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

        /// <summary>
        /// 
        /// </summary>
        public virtual void Dispose()
        {
        }

        /// <summary>
        /// Helper.
        /// </summary>
        protected void Abort(string message)
        {
            throw new Exception(message);
        }

        #region Public 

        ///// <summary>
        ///// Obtain a proxy method delegate for a dynamic method.
        ///// Obtain only once, when we attach it to the event.
        ///// </summary>
        //public GeneratedDynamicMethodInfo ObtainDynamicMethodProxyDelegate(Type delegateType)
        //{
        //    // Establish delegate parameters.
        //    MethodInfo delegateMethodInfo = delegateType.GetMethod("Invoke");

        //    // Generate a new dynamic method; we can *NOT REUSE THEM*, since they work on handling events
        //    // and each event handler must be traceable to the instance that we subscribed it for.
        //    return _builder.GenerateDynamicProxyMethod(ProxyTypeBuilder.GetMethodParametersTypes(delegateMethodInfo), 
        //        delegateMethodInfo.ReturnType);

        //    //lock (_dynamicMethods)
        //    //{
        //    //    if (_dynamicMethods.ContainsKey(delegateMethodInfo))
        //    //    {
        //    //        return _dynamicMethods[delegateMethodInfo];
        //    //    }

        //    //    Delegate delegateInstance = dynamicMethod.Method.CreateDelegate(delegateType, _dynamicMethodSink);
                
        //    //    //object result = delegateInstance2.DynamicInvoke(0);
        //    //    //delegateStatic.DynamicInvoke(_dynamicMethodSink, 0);
        //    //    //delegateInstance.DynamicInvoke(null, EventArgs.Empty);

        //    //    //return delegateInstance;

        //    //    //// Lock it, since this way we shall only create what we actually use.
        //    //    //object proxy = Activator.CreateInstance(dynamicMethod.GeneratedType, _sink);

        //    //    //Delegate delegateInstance = Delegate.CreateDelegate(delegateType, proxy,
        //    //    //    dynamicMethod.GeneratedType.GetMethod(delegateMethodInfo.Name));

        //    //    //_dynamicMethods[delegateMethodInfo] = delegateInstance;

        //    //    //return delegateInstance;
        //    //    return delegateInstance;
        //    //}

        //}

        /// <summary>
        /// Invoked on each call.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public object ObtainInterfaceProxy(Type type)
        {
            lock (_proxyObjects)
            {
                if (_proxyObjects.ContainsKey(type))
                {
                    return _proxyObjects[type];
                }
            }

            if (type.IsInterface == false)
            {
                Abort("Type [" + type.Name + "] not an interface.");
            }

            Type proxyType = _builder.GenerateInterfaceProxyImplementation(type);
            if (proxyType == null)
            {// Failed to create proxy type for this interface.
                return null;
            }

            ConstructorInfo info = proxyType.GetConstructor(new Type[] { typeof(IProxyTypeSink) });
            object proxyInstance = info.Invoke(new object[] { _sink });

            lock (_proxyObjects)
            {
                if (_proxyObjects.ContainsKey(type) == false)
                {
                    _proxyObjects.Add(type, proxyInstance);
                }
            }

            return proxyInstance;
        }

        #endregion

        /// <summary>
        /// Implementation, separate in order to allow calls from child classes.
        /// </summary>
        public static object GetTypeDefaultValue(Type returnType)
        {
            if (returnType == null || returnType.IsByRef
                || returnType.IsClass)
            {
                return null;
            }

            object result;
            if (_defaultReturnValues.TryGetValue(returnType, out result))
            {
                return result;
            }

            result = Activator.CreateInstance(returnType);
            _defaultReturnValues.Add(returnType, result);

            return result;
        }

    }
}

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

You may also be interested in...

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