|
using System;
using System.Threading;
using System.Collections;
namespace DistributedCommand.Framework
{
/// <summary>
/// Command Bus is the message bus which takes command and executes
/// via command executors.
/// </summary>
public class CommandBus : ICommandExecutorListener
{
/// <summary>
/// Pipeline contains the distributed executors which get invoked
/// on every command execution
/// </summary>
private ArrayList _Pipeline = new ArrayList();
/// <summary>
/// Broadcast mode, set to true when any incoming command is also
/// send to the pipeline in order to distribute to all distributed
/// executors.
/// </summary>
private bool _BroadcastReceive = false;
/// <summary>
/// Initialize the command bus.
///
/// Command bus can either work as just transmitter or can work
/// as command broadcast server.
/// </summary>
/// <param name="broadcastReceive">True for broadcast servers</param>
public CommandBus( bool broadcastReceive )
{
this._BroadcastReceive = broadcastReceive;
}
/// <summary>
/// Add a command executor in the bus pipeline
/// </summary>
/// <param name="executor">The executor which executes command</param>
public void AddExecutor( ICommandExecutor executor )
{
this._Pipeline.Add( executor );
executor.AddListener( this );
}
/// <summary>
/// Remove the executor
/// </summary>
/// <param name="executor">The executor to remove</param>
public void RemoveExecutor( ICommandExecutor executor )
{
this._Pipeline.Remove( executor );
}
/// <summary>
/// Execute the specified command via all command executors
/// in the pipeline
/// </summary>
/// <param name="command">The command to execute</param>
public void Execute( ICommand command )
{
foreach( ICommandExecutor executor in this._Pipeline )
executor.Execute( command );
}
/// <summary>
/// A command was received from any of the distributed executors.
/// Excute the command on the pipeline
/// </summary>
/// <param name="command">Received command from a executor</param>
void ICommandExecutorListener.Receive(ICommand command)
{
this.Execute( command );
}
/// <summary>
/// Remove the executor from the pipeline.
/// </summary>
/// <param name="executor"></param>
void ICommandExecutorListener.Remove( ICommandExecutor executor )
{
this._Pipeline.Remove( executor );
}
}
}
|
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.