/// <summary> /// Perform the actual call. /// </summary> protected TType DoCall <TType>(ComponentId receiverId, TimeSpan?requestConfirmTimeout, TimeSpan?timeout, AsyncCallResultDelegate asyncResultDelegate, object asyncResultState, TimeSpan?asyncResultTimeout, SuperPoolProxyCall.ModeEnum callMode, CallOutcome outcome) where TType : class { Matrix.Framework.SuperPool.Core.SuperPool pool = _superPool; if (pool == null) { return(null); } SuperPoolProxyCall proxyCall; TType result; if (pool.Call <TType>(this, receiverId, out result, out proxyCall) == false) { // Call failed. return(null); } proxyCall.AsyncResultDelegate = asyncResultDelegate; proxyCall.AsyncResultState = asyncResultState; proxyCall.AsyncResultTimeout = asyncResultTimeout; proxyCall.RequestConfirmTimeout = requestConfirmTimeout; proxyCall.Timeout = timeout; proxyCall.Mode = callMode; proxyCall.Outcome = outcome; return(result); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.Text += " - " + this.ClientName; // The steps here are exactly the same, as in server side, only difference is the ClientMessageBus // instead of ServerMessageBus. Since this is the only difference, all the remaining source code // is completely independent of whether its a server or a client side. //// Assign the default tracer, to provide system wide tracing functionality. //SystemMonitor.AssignTracer(new Tracer()); //this.tracerControl1.Tracer = SystemMonitor.Tracer; IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, ServerMessageBus.DefaultPort); // Create the underlying (client) message bus, that takes care of transporting the // actual communication messages; the message bus TCP communication is created // at default port, at localhost. ClientMessageBus messageBus = new ClientMessageBus(endPoint, this.ClientName, null); // Initialize the super pool with this message bus. _pool = new Matrix.Framework.SuperPool.Core.SuperPool(messageBus); // Create the client that will server as a connection between this // class and the super pool and add the client to the pool. _poolClient = new SuperPoolClient("Client." + this.ClientName, this); _pool.AddClient(_poolClient); // Use this to assign a specific execution strategy to a given client. // _poolClient.SetupExecutionStrategy(new FrameworkThreadPoolExecutionStrategy()); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); //// [Optional] Assign the default tracer, to provide system wide tracing functionality. //SystemMonitor.AssignTracer(new Tracer()); //this.tracerControl1.Tracer = SystemMonitor.Tracer; // Create the underlying (server) message bus and put the pool on it. ServerMessageBus messageBus = new ServerMessageBus("Server", null, null); _pool = new Matrix.Framework.SuperPool.Core.SuperPool(messageBus); // Create the client that will server as a connection between this // class and the super pool and add the client to the pool. _poolClient = new SuperPoolClient("Server", this); _pool.AddClient(_poolClient); // Finally subscribe to the event of having a client added to the bus/pool. _pool.MessageBus.ClientAddedEvent += new MessageBus.Core.MessageBusClientUpdateDelegate(MessageBus_ClientAddedEvent); messageBus.ClientRemovedEvent += (bus, id, remove) => { this.Invoke(new GeneralHelper.GenericDelegate <string>(Report), "Client removed " + id.ToString()); }; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); //// [Optional] Assign the default tracer, to provide system wide tracing functionality. //SystemMonitor.AssignTracer(new Tracer()); //this.tracerControl1.Tracer = SystemMonitor.Tracer; // Create the underlying (server) message bus and put the pool on it. ServerMessageBus messageBus = new ServerMessageBus("Server", null, null); _pool = new Matrix.Framework.SuperPool.Core.SuperPool(messageBus); // Create the client that will server as a connection between this // class and the super pool and add the client to the pool. _poolClient = new SuperPoolClient("Server", this); _pool.AddClient(_poolClient); // Finally subscribe to the event of having a client added to the bus/pool. _pool.MessageBus.ClientAddedEvent += new MessageBus.Core.MessageBusClientUpdateDelegate(MessageBus_ClientAddedEvent); messageBus.ClientRemovedEvent += (bus, id, remove) => { this.Invoke(new GeneralHelper.GenericDelegate<string>(Report), "Client removed " + id.ToString()); }; }
/// <summary> /// Creates a new client, assigns it with a custom execution strategy and adds it to the super pool. /// </summary> /// <param name="superPool"></param> public void Demonstrate(Matrix.Framework.SuperPool.Core.SuperPool superPool) { SuperPoolClient client = new SuperPoolClient("Client", this); client.SetupExecutionStrategy(new CustomExecutionStrategy()); superPool.AddClient(client); }
/// <summary> /// Constructor. /// </summary> public DirectCallSpeedTest() : base(true) { pool = new Matrix.Framework.SuperPool.Core.SuperPool(); client1 = new SuperPoolClient("c1", this); client2 = new SuperPoolClient("c2", this); bool result = pool.AddClient(client1); result = pool.AddClient(client2); }
public List <ClientId> ResolveAll(Type interfaceType) { List <ClientId> result = new List <ClientId>(); Matrix.Framework.SuperPool.Core.SuperPool pool = _superPool; if (pool != null) { result.AddRange(pool.GetInterfaceImplementors(interfaceType)); } return(result); }
public ClientId Resolve(Type interfaceType) { Matrix.Framework.SuperPool.Core.SuperPool pool = _superPool; if (pool != null) { foreach (ClientId id in pool.GetInterfaceImplementors(interfaceType)) { return(id); } } return(null); }
/// <summary> /// Release the contained instance of super pool, executed when removing client from pool. /// </summary> internal void ReleaseSuperPool() { lock (_syncRoot) { #if Matrix_Diagnostics SystemMonitor.ErrorIf(_superPool == null, "Super pool not assigned to client [" + this.ToString() + "] so no need to release."); #endif _superPool = null; } SuperPoolClientUpdateDelegate del = SuperPoolReleasedEvent; if (del != null) { del(this); } }
/// <summary> /// Perform an event subscription. /// All subscribes are expected to be asynchronous, /// and executed agains the actual pool only. /// </summary> public TType Subscribe <TType>(EventSubscriptionRequest subscription) where TType : class { Matrix.Framework.SuperPool.Core.SuperPool pool = _superPool; if (pool == null) { return(null); } TType result; if (pool.Subscribe <TType>(this, subscription, out result)) { return(result); } return(null); }
/// <summary> /// *Throws* Assign the contained instance of super pool, executed when adding client from pool. /// </summary> internal void AssignSuperPool(Matrix.Framework.SuperPool.Core.SuperPool superPool) { lock (_syncRoot) { if (_superPool != null && _superPool != superPool) { throw new Exception("Client already assigned to another super pool."); } _superPool = superPool; } SuperPoolClientUpdateDelegate del = SuperPoolAssignedEvent; if (del != null) { del(this); } }
public void ShowMe() { // Create the pool. Matrix.Framework.SuperPool.Core.SuperPool pool = new Matrix.Framework.SuperPool.Core.SuperPool("MyPool"); // Create component 1 and 2. MyComponent component1 = new MyComponent(); MyComponent component2 = new MyComponent(); // Add them both to the pool. pool.AddClient(component1.Client); pool.AddClient(component2.Client); // Request some work: component1.RequestSomeWork(component2.Client.Id); // Or we can also do it directly like this (although the previous is often a more suitable aproach) component1.Client.Call<ISomeInterface>(component2.Client.Id).DoSomeWork(); }
public void ShowMe() { // Create the pool. Matrix.Framework.SuperPool.Core.SuperPool pool = new Matrix.Framework.SuperPool.Core.SuperPool("MyPool"); // Create component 1 and 2. MyComponent component1 = new MyComponent(); MyComponent component2 = new MyComponent(); // Add them both to the pool. pool.AddClient(component1.Client); pool.AddClient(component2.Client); // Request some work: component1.RequestSomeWork(component2.Client.Id); // Or we can also do it directly like this (although the previous is often a more suitable aproach) component1.Client.Call <ISomeInterface>(component2.Client.Id).DoSomeWork(); }
/// <summary> /// Perform the actual call. /// </summary> /// <param name="receivers">The list of recipients ids.</param> /// <param name="timeOut">The time out for sync calls, or null for async.</param> protected TType DoCallMany <TType>(IEnumerable <ComponentId> receivers, TimeSpan?timeOut) where TType : class { Matrix.Framework.SuperPool.Core.SuperPool pool = _superPool; if (pool == null) { return(null); } SuperPoolProxyCall call; TType result; if (pool.Call <TType>(this, receivers, out result, out call)) { call.Timeout = timeOut; return(result); } // Call failed. return(null); }
/// <summary> /// An envelope has arrived from the messaging infrastructure. /// </summary> protected override void OnPerformExecution(Envelope envelope) { object messageConsumer = Source; if (messageConsumer == null || envelope.Message.GetType() != typeof(SuperPoolCall)) {// This is a not a super pool call, or no message consumer. base.OnPerformExecution(envelope); return; } try { SuperPoolCall call = envelope.Message as SuperPoolCall; if (call.State == SuperPoolCall.StateEnum.Responding) {// Response. object response = call.Parameters.Length > 0 ? call.Parameters[0] : null; Exception exception = call.Parameters.Length > 1 ? call.Parameters[1] as Exception : null; long callId = call.Id; SyncCallInfo syncCallInfo = null; lock (_syncCalls) { if (_syncCalls.TryGetValue(callId, out syncCallInfo)) { if (syncCallInfo.IsMultiResponse == false) {// Only remove single response ones, since we have 1 for sure. _syncCalls.Remove(callId); } } else { syncCallInfo = null; } } if (syncCallInfo != null) { syncCallInfo.AcceptResponse(this, response, exception); if (syncCallInfo.IsMultiResponse && syncCallInfo.IsMultiResponseComplete) { lock (_syncCalls) { _syncCalls.Remove(callId); } } } } else if (call.State == SuperPoolCall.StateEnum.Requesting) {// Call (Request). if (_consumerInterfacesHotSwap.Contains(call.MethodInfoLocal.ReflectedType)) { object result = null; Exception exception = null; result = PerformCall(call, messageConsumer, out exception); if (call.RequestResponse) { call.State = SuperPoolCall.StateEnum.Responding; if (exception == null) { call.Parameters = new object[] { result }; } else {// Also transport the exception. call.Parameters = new object[] { result, exception }; } Matrix.Framework.SuperPool.Core.SuperPool pool = _superPool; if (pool == null) { #if Matrix_Diagnostics SystemMonitor.Error(this.GetType().Name + " has failed to find super pool instance, execution failed."); #endif return; } IMessageBus messageBus = pool.MessageBus; if (messageBus == null) { #if Matrix_Diagnostics SystemMonitor.Error(this.GetType().Name + " has failed to find super pool's message bus instance, execution failed."); #endif return; } messageBus.Respond(envelope, new Envelope(call) { DuplicationMode = EnvelopeDuplicationMode }); } else { call.State = SuperPoolCall.StateEnum.Finished; } } else { if (call.MethodInfoLocal == null) { #if Matrix_Diagnostics SystemMonitor.OperationError(string.Format("Call with no method info assigned ignored.")); #endif } else { #if Matrix_Diagnostics SystemMonitor.OperationError(string.Format("Call to [{0}] not recognized.", call.MethodInfoLocal.ToString())); #endif } } } else if (call.State == SuperPoolCall.StateEnum.EventRaise) { Exception exception; object result = PerformCall(call, messageConsumer, out exception); call.State = SuperPoolCall.StateEnum.Finished; } } catch (Exception ex) {// It is possible we encounter some invocation error (for ex. source type changed while call travelling) // so gracefully handle these here. #if Matrix_Diagnostics SystemMonitor.OperationError("Execution failed", ex); #endif } }