using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.Transport.Routing
{
///
/// Represents a simple transport router.
///
///
public class SimpleTransportRouter : ITransportRouter
{
///
/// Gets or sets the name of the transport component.
///
public String ComponentName { get; set; }
///
/// Occurs when component state changes.
///
public event EventHandler StateChanged;
///
/// Gets the collection of channels.
///
public ObservableCollection Channels { get; private set; }
private TransportComponentState _state;
///
/// Gets the component state.
///
public TransportComponentState State
{
get { return _state; }
set { _state = value; StateChanged?.Invoke(this, State); }
}
///
/// Initializes a new instance of the class.
///
public SimpleTransportRouter()
{
Channels = new ObservableCollection();
Channels.CollectionChanged += OnChannelsCollectionChanged;
ComponentName = "Router";
}
///
/// Called when the collection has changed.
///
/// The sender.
/// The instance containing the event data.
protected virtual void OnChannelsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
//TODO: Do something here ?
}
///
/// Connects the transport component.
///
///
public Task Connect()
{
return Task.WhenAll(Channels.Select(x => x.Connect()));
}
///
/// Disconnects the transport component.
///
///
public Task Disconnect()
{
return Task.WhenAll(Channels.Select(x => x.Disconnect()));
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Channels.ToList().ForEach(x => x.Dispose());
}
}
}