< Summary

Class:DCL.MessagingController
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/Messaging/MessagingController.cs
Covered lines:33
Uncovered lines:29
Coverable lines:62
Total lines:155
Line coverage:53.2% (33 of 62)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MessagingController(...)0%110100%
AddMessageBus(...)0%110100%
StartBus(...)0%220100%
StopBus(...)0%220100%
Stop()0%220100%
Dispose()0%220100%
ForceEnqueue(...)0%110100%
Enqueue(...)0%1101000%
GetEntityIdAndClassIdFromTag(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/Messaging/MessagingController.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Models;
 3using System;
 4using System.Collections.Generic;
 5using UnityEngine;
 6using UnityEngine.Profiling;
 7
 8namespace DCL
 9{
 10    public class MessagingController : IDisposable
 11    {
 12        const char SEPARATOR = '_';
 13
 14        public enum QueueState
 15        {
 16            Init,
 17            Systems,
 18        }
 19
 137120        public Dictionary<MessagingBusType, MessagingBus> messagingBuses = new Dictionary<MessagingBusType, MessagingBus
 21        public IMessageProcessHandler messageHandler;
 22        public string debugTag;
 137123        public bool enabled = true;
 24
 25        private QueueState currentQueueState;
 26
 27        public readonly MessagingBus initBus;
 28        public readonly MessagingBus systemBus;
 29        public readonly MessagingBus uiBus;
 30
 137131        public MessagingController(IMessageProcessHandler messageHandler, string debugTag = null)
 32        {
 137133            this.debugTag = debugTag;
 137134            this.messageHandler = messageHandler;
 35
 36            //TODO(Brian): This is too hacky, most of the controllers won't be using this system. Refactor this in the f
 137137            uiBus = AddMessageBus(MessagingBusType.UI);
 137138            initBus = AddMessageBus(MessagingBusType.INIT);
 137139            systemBus = AddMessageBus(MessagingBusType.SYSTEM);
 40
 137141            currentQueueState = QueueState.Init;
 42
 137143            StartBus(MessagingBusType.INIT);
 137144            StartBus(MessagingBusType.UI);
 137145        }
 46
 47        private MessagingBus AddMessageBus(MessagingBusType type)
 48        {
 411349            var newMessagingBus = new MessagingBus(type, messageHandler, this);
 411350            newMessagingBus.debugTag = debugTag;
 51
 411352            messagingBuses.Add(type, newMessagingBus);
 411353            return newMessagingBus;
 54        }
 55
 56        public void StartBus(MessagingBusType busType)
 57        {
 275858            if (messagingBuses.ContainsKey(busType))
 59            {
 275860                messagingBuses[busType].Start();
 61            }
 275862        }
 63
 64        public void StopBus(MessagingBusType busType)
 65        {
 866            if (messagingBuses.ContainsKey(busType))
 67            {
 868                messagingBuses[busType].Stop();
 69            }
 870        }
 71
 72        public void Stop()
 73        {
 272874            using (var iterator = messagingBuses.GetEnumerator())
 75            {
 1091276                while (iterator.MoveNext())
 77                {
 818478                    iterator.Current.Value.Stop();
 79                }
 272880            }
 272881        }
 82
 83        public void Dispose()
 84        {
 137185            using (var iterator = messagingBuses.GetEnumerator())
 86            {
 548487                while (iterator.MoveNext())
 88                {
 411389                    iterator.Current.Value.Dispose();
 90                }
 137191            }
 137192        }
 93
 5494        public void ForceEnqueue(MessagingBusType busType, QueuedSceneMessage queuedMessage) { messagingBuses[busType].E
 95
 96        public void Enqueue(bool isUiBus, QueuedSceneMessage_Scene queuedMessage, out MessagingBusType busType)
 97        {
 098            busType = MessagingBusType.NONE;
 99
 0100            QueueMode queueMode = QueueMode.Reliable;
 101
 102            // If current scene is the Global Scene, the bus id should be UI
 0103            if (isUiBus)
 0104                busType = MessagingBusType.UI;
 0105            else if (currentQueueState == QueueState.Init)
 0106                busType = MessagingBusType.INIT;
 107            else
 0108                busType = MessagingBusType.SYSTEM;
 109
 110            // Check if the message type is an EntityComponentCreateOrUpdate
 0111            if (queuedMessage.payload is Protocol.EntityComponentCreateOrUpdate)
 112            {
 113                // We need to extract the entityId and the classId from the tag.
 114                // The tag format is "entityId_classId", i.e: "E1_2".
 0115                GetEntityIdAndClassIdFromTag(queuedMessage.tag, out int classId);
 116
 117                // If it is a transform update, the queue mode is Lossy
 0118                if (classId == (int) CLASS_ID_COMPONENT.TRANSFORM)
 0119                    queueMode = QueueMode.Lossy;
 0120            }
 0121            else if (queuedMessage.payload is Protocol.QueryPayload)
 122            {
 0123                busType = MessagingBusType.UI;
 0124                queueMode = QueueMode.Lossy;
 0125            }
 0126            else if (queuedMessage.payload is Protocol.SceneReady)
 127            {
 128                // When a INIT DONE message is enqueued, the next messages should be
 129                // enqueued in SYSTEM message bus, but we don't process them until
 130                // scene started has been processed
 0131                currentQueueState = QueueState.Systems;
 132            }
 133
 0134            switch (busType)
 135            {
 136                case MessagingBusType.INIT:
 0137                    initBus.Enqueue(queuedMessage, queueMode);
 0138                    break;
 139                case MessagingBusType.SYSTEM:
 0140                    systemBus.Enqueue(queuedMessage, queueMode);
 0141                    break;
 142                case MessagingBusType.UI:
 0143                    uiBus.Enqueue(queuedMessage, queueMode);
 144                    break;
 145            }
 0146        }
 147
 148        private void GetEntityIdAndClassIdFromTag(string tag, out int classId)
 149        {
 0150            int lastSeparator = tag.LastIndexOf(SEPARATOR);
 0151            if (!int.TryParse(tag.Substring(lastSeparator + 1), out classId))
 0152                Debug.LogError("Couldn't parse classId string to int");
 0153        }
 154    }
 155}