aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Transport/Routing/TransportRoutingChannel.cs
blob: 01b957975b59dd28e5b8df461e98b5b0277ca5e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highl
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Transport.Routing
{
    /// <summary>
    /// Represents a transport router capable of exchanging data between two <see cref="ITransportAdapter"/> of different types.
    /// </summary>
    /// <seealso cref="IDisposable" />
    public class TransportRoutingChannel : ITransportComponent
    {
        /// <summary>
        /// Gets or sets the name of the transport component.
        /// </summary>
        public String ComponentName { get; set; }

        /// <summary>
        /// Occurs when component state changes.
        /// </summary>
        public event EventHandler<TransportComponentState> StateChanged;

        /// <summary>
        /// Gets or sets the first adapter.
        /// </summary>
        public ITransportAdapter Adapter1 { get; private set; }

        /// <summary>
        /// Gets or sets the second adapter.
        /// </summary>
        public ITransportAdapter Adapter2 { get; private set; }

        private TransportComponentState _state;
        /// <summary>
        /// Gets the component state.
        /// </summary>
        public TransportComponentState State
        {
            get { return _state; }
            private set { _state = value; StateChanged?.Invoke(this, State); }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="TransportRoutingChannel"/> class.
        /// </summary>
        /// <param name="adapter1">The first adapter.</param>
        /// <param name="adapter2">The second adapter.</param>
        public TransportRoutingChannel(ITransportAdapter adapter1, ITransportAdapter adapter2)
        {
            ComponentName = "Routing Channel";

            Adapter1 = adapter1;
            Adapter2 = adapter2;

            Adapter1.DataAvailable += OnAdapter1DataAvailable;
            Adapter2.DataAvailable += OnAdapter2DataAvailable;

            Adapter1.StateChanged += OnAdapter1StateChanged;
            Adapter2.StateChanged += OnAdapter2StateChanged;
        }

        /// <summary>
        /// Called when the first adapter state has changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="state">The state.</param>
        protected virtual void OnAdapter1StateChanged(object sender, TransportComponentState state)
        {
            if (state == TransportComponentState.Failed)
            {
                State = TransportComponentState.Failed;
            }
        }

        /// <summary>
        /// Called when the second adapter state has changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="state">The state.</param>
        protected virtual void OnAdapter2StateChanged(object sender, TransportComponentState state)
        {
            if (state == TransportComponentState.Failed)
            {
                State = TransportComponentState.Failed;
            }
        }

        /// <summary>
        /// Called when the first adapter has available data.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="data">The data.</param>
        protected virtual void OnAdapter1DataAvailable(object sender, byte[] data)
        {
            Adapter2.Write(data);
        }

        /// <summary>
        /// Called when the second adapter has available data.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="data">The data.</param>
        protected virtual void OnAdapter2DataAvailable(object sender, byte[] data)
        {
            Adapter1.Write(data);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (Adapter1 != null) Adapter1.Dispose();
            if (Adapter2 != null) Adapter2.Dispose();
        }

        /// <summary>
        /// Connects the transport component.
        /// </summary>
        /// <returns></returns>
        public Task Connect()
        {
            var result = Task.WhenAll(Adapter1.Connect(), Adapter2.Connect());
            State = TransportComponentState.Connected;
            return result;
        }

        /// <summary>
        /// Disconnects the transport component.
        /// </summary>
        /// <returns></returns>
        public Task Disconnect()
        {
            var result = Task.WhenAll(Adapter1.Disconnect(), Adapter2.Disconnect());
            State = TransportComponentState.Disconnected;
            return result;
        }
    }
}