< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Tuio.TuioBroadcast
Assembly: ReFlex.Core.Tuio
File(s): D:\a\reflex\reflex\library\src\Core\Tuio\TuioBroadcast.cs
Line coverage
100%
Covered lines: 63
Uncovered lines: 0
Coverable lines: 63
Total lines: 192
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
get_IsConfigured()100%11100%
get_IsSending()100%11100%
get_Configuration()100%11100%
get_FrameId()100%11100%
.ctor()100%11100%
Configure()100%66100%
Broadcast()100%1010100%
Dispose()100%11100%
CreateOscBundle(...)100%11100%
CreateOscMessages(...)100%22100%

File(s)

D:\a\reflex\reflex\library\src\Core\Tuio\TuioBroadcast.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Net;
 4using System.Runtime.CompilerServices;
 5using System.Threading.Tasks;
 6using CoreOSC;
 7using NLog;
 8using ReFlex.Core.Common.Components;
 9using ReFlex.Core.Tuio.Components;
 10using ReFlex.Core.Tuio.Interfaces;
 11using ReFlex.Core.Tuio.Util;
 12
 13[assembly: InternalsVisibleTo("ReFlex.Tuio.Test")]
 14namespace ReFlex.Core.Tuio
 15{
 16    public class TuioBroadcast : ITuioBroadcast, IDisposable
 17    {
 18        #region Fields
 19
 220        private static readonly Logger Log = LogManager.GetCurrentClassLogger();
 21
 22        private readonly ITuioMessageBuilder _builder;
 23        private readonly ITuioSender _sender;
 24
 2625        private string _localAddress = "Localhost";
 26
 27        #endregion
 28
 29        #region Properties
 30
 31        /// <summary>
 32        /// Specify whether the instance has a valid configuration
 33        /// </summary>
 13634        public bool IsConfigured { get; private set; }
 35
 36        /// <summary>
 37        /// Specifies whether the service is currently sending data.
 38        /// </summary>
 11639        public bool IsSending { get; private set; }
 40
 41        /// <summary>
 42        /// Current Configuration for sending
 43        /// </summary>
 15444        public TuioConfiguration Configuration { get; private set; }
 45
 46        /// <summary>
 47        /// Storage for Frame Id. Is incremented when <see cref="Broadcast"/> was successful.
 48        /// </summary>
 5849        public int FrameId { get; private set; }
 50
 51        #endregion
 52
 53        #region Constructor
 54
 55        /// <summary>
 56        /// internal Constructor for Testing purposes
 57        /// </summary>
 58        /// <param name="messageBuilder">constructs the TUIO messages</param>
 59        /// <param name="sender">sends the data to TUIO server</param>
 2660        internal TuioBroadcast(ITuioMessageBuilder messageBuilder, ITuioSender sender)
 2661        {
 2662            _builder = messageBuilder;
 2663            _sender = sender;
 2664        }
 65
 66        /// <summary>
 67        /// initializes the Broadcast service.
 68        /// Creates a <see cref="TuioMessageBuilder"/> for generating messages according to TUIO specification.
 69        /// Creates a <see cref="TuioSender"/> for sending the data.
 70        /// Needs to call <see cref="Configure"/> before sending is enabled.
 71        /// </summary>
 272        public TuioBroadcast() : this(new TuioMessageBuilder(), new TuioSender())
 273        {
 274        }
 75
 76        #endregion
 77
 78        #region public Methods
 79
 80        /// <summary>
 81        /// Initializes the service with the provided configuration.
 82        /// First, all open connections are closed on <see cref="TuioSender"/>
 83        /// then current hostname is retrieved and stored as local Address for TUIO identification
 84        /// <see cref="Configuration"/> is set
 85        /// <see cref="IsConfigured"/> is set to true, if <see cref="Configuration"/> is not null and <see cref="TuioSen
 86        /// </summary>
 87        /// <param name="config">Configuration containing (at least) server address, port and <see cref="TransportProtoc
 88        /// <returns>completed Task</returns>
 89        public async Task Configure(TuioConfiguration config)
 2090        {
 2091            await _sender.StopAllConnections();
 92
 2093            _sender.Initialize(config);
 94
 95            // Get the Name of HOST
 2096            var hostName = Dns.GetHostName();
 97
 98            // Get the IP from GetHostByName method of dns class.
 2099            var entry = await Dns.GetHostEntryAsync(hostName);
 100
 20101            _localAddress = entry.AddressList[0].ToString();
 102
 20103            Configuration = config;
 104
 20105            IsConfigured = Configuration != null && _sender.IsInitialized;
 106
 20107            Log.Info($"Sucessfully configured {nameof(TuioBroadcast)}: {config?.GetTuioConfigurationString() ?? "No conf
 20108        }
 109
 110        /// <summary>
 111        /// Broadcast data according to the current configuration to TUIO server
 112        /// If <see cref="Configuration"/> is null or  <see cref="IsConfigured"/> is false, nothing is sent.
 113        /// Also checks, if <see cref="IsSending"/> is true and only send if this is not the case.
 114        /// Constructs <see cref="TuioParameters"/> from <see cref="interactions"/>, current <see cref="Configuration"/>
 115        /// Sends data based on specified <see cref="TransportProtocol"/> in <see cref="Configuration"/> and increases f
 116        /// </summary>
 117        /// <param name="interactions">interactions that are transformed into TUIO packages</param>
 118        /// <returns>completed Task</returns>
 119        public async Task<string> Broadcast(List<Interaction> interactions)
 16120        {
 16121            if (Configuration == null || !IsConfigured)
 2122            {
 2123                Log.Warn(
 2124                    $"Cannot {nameof(Broadcast)} interactions, because {nameof(TuioBroadcast)} is has no valid configura
 2125                return "";
 126            }
 127
 128            // skip if last message is still being sent
 14129            if (IsSending)
 2130                return "";
 131
 12132            var tuioParams = new TuioParameters(_localAddress, Configuration, interactions, FrameId);
 133
 12134            var bndl = CreateOscBundle(tuioParams);
 135
 12136            IsSending = true;
 137
 12138            switch (Configuration.Transport)
 139            {
 140                case TransportProtocol.Udp:
 6141                    await _sender.SendUdp(bndl);
 6142                    break;
 143                case TransportProtocol.Tcp:
 2144                    await _sender.SendTcp(bndl);
 2145                    break;
 146                case TransportProtocol.WebSocket:
 2147                    await _sender.SendWebSocket(bndl);
 2148                    break;
 149                default:
 2150                    IsSending = false;
 2151                    throw new ArgumentOutOfRangeException();
 152            }
 153
 10154            FrameId++;
 155
 10156            IsSending = false;
 157
 10158            return _builder.GenerateStringRepresentation(bndl);
 14159        }
 160
 161        /// <summary>
 162        /// resets <see cref="IsConfigured"/>, <see cref="IsSending"/>, frame Id.
 163        /// Also stops all connections on <see cref="TuioSender"/>
 164        /// </summary>
 165        public async void Dispose()
 26166        {
 26167            IsConfigured = false;
 26168            FrameId = 0;
 26169            IsSending = false;
 26170            IsConfigured = false;
 26171            await _sender.StopAllConnections();
 26172        }
 173
 174        #endregion
 175
 176        #region private Methods
 177
 178        private OscBundle CreateOscBundle(TuioParameters parameters)
 12179        {
 12180            return new OscBundle(parameters.Time, CreateOscMessages(Configuration.Protocol, parameters));
 12181        }
 182
 183        private IEnumerable<OscMessage> CreateOscMessages(ProtocolVersion tuioVersion, TuioParameters parameters)
 12184        {
 12185            return tuioVersion == ProtocolVersion.TUIO_VERSION_1_1
 12186                ? _builder.CreateTuio11Messages(parameters, Configuration.Interpretation)
 12187                : _builder.CreateTuio20Messages(parameters, Configuration.Interpretation);
 12188        }
 189
 190        #endregion
 191    }
 192}