< Summary

Class:DCL.MessagingController
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Messaging/MessagingController.cs
Covered lines:32
Uncovered lines:32
Coverable lines:64
Total lines:164
Line coverage:50% (32 of 64)
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%6200%
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/WorldRuntime/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
 69920        public Dictionary<MessagingBusType, MessagingBus> messagingBuses =
 21            new Dictionary<MessagingBusType, MessagingBus>();
 22
 23        public IMessageProcessHandler messageHandler;
 24        public string debugTag;
 69925        public bool enabled = true;
 26
 27        private QueueState currentQueueState;
 28
 29        public readonly MessagingBus initBus;
 30        public readonly MessagingBus systemBus;
 31        public readonly MessagingBus uiBus;
 32
 33        public IMessagingControllersManager messagingManager;
 34
 69935        public MessagingController(IMessagingControllersManager messagingManager, IMessageProcessHandler messageHandler,
 36            string debugTag = null)
 37        {
 69938            this.messagingManager = messagingManager;
 69939            this.debugTag = debugTag;
 69940            this.messageHandler = messageHandler;
 41
 42            //TODO(Brian): This is too hacky, most of the controllers won't be using this system. Refactor this in the f
 69943            uiBus = AddMessageBus(MessagingBusType.UI);
 69944            initBus = AddMessageBus(MessagingBusType.INIT);
 69945            systemBus = AddMessageBus(MessagingBusType.SYSTEM);
 46
 69947            currentQueueState = QueueState.Init;
 48
 69949            StartBus(MessagingBusType.INIT);
 69950            StartBus(MessagingBusType.UI);
 69951        }
 52
 53        private MessagingBus AddMessageBus(MessagingBusType type)
 54        {
 209755            var newMessagingBus = new MessagingBus(type, messageHandler, this);
 209756            newMessagingBus.debugTag = debugTag;
 57
 209758            messagingBuses.Add(type, newMessagingBus);
 209759            return newMessagingBus;
 60        }
 61
 62        public void StartBus(MessagingBusType busType)
 63        {
 139864            if (messagingBuses.ContainsKey(busType))
 65            {
 139866                messagingBuses[busType].Start();
 67            }
 139868        }
 69
 70        public void StopBus(MessagingBusType busType)
 71        {
 072            if (messagingBuses.ContainsKey(busType))
 73            {
 074                messagingBuses[busType].Stop();
 75            }
 076        }
 77
 78        public void Stop()
 79        {
 135380            using (var iterator = messagingBuses.GetEnumerator())
 81            {
 541282                while (iterator.MoveNext())
 83                {
 405984                    iterator.Current.Value.Stop();
 85                }
 135386            }
 135387        }
 88
 89        public void Dispose()
 90        {
 69291            using (var iterator = messagingBuses.GetEnumerator())
 92            {
 276893                while (iterator.MoveNext())
 94                {
 207695                    iterator.Current.Value.Dispose();
 96                }
 69297            }
 69298        }
 99
 100        public void ForceEnqueue(MessagingBusType busType, QueuedSceneMessage queuedMessage)
 101        {
 27102            messagingBuses[busType].Enqueue(queuedMessage);
 27103        }
 104
 105        public void Enqueue(bool isUiBus, QueuedSceneMessage_Scene queuedMessage, out MessagingBusType busType)
 106        {
 0107            busType = MessagingBusType.NONE;
 108
 0109            QueueMode queueMode = QueueMode.Reliable;
 110
 111            // If current scene is the Global Scene, the bus id should be UI
 0112            if (isUiBus)
 0113                busType = MessagingBusType.UI;
 0114            else if (currentQueueState == QueueState.Init)
 0115                busType = MessagingBusType.INIT;
 116            else
 0117                busType = MessagingBusType.SYSTEM;
 118
 119            // Check if the message type is an EntityComponentCreateOrUpdate
 0120            if (queuedMessage.payload is Protocol.EntityComponentCreateOrUpdate)
 121            {
 122                // We need to extract the entityId and the classId from the tag.
 123                // The tag format is "entityId_classId", i.e: "E1_2".
 0124                GetEntityIdAndClassIdFromTag(queuedMessage.tag, out int classId);
 125
 126                // If it is a transform update, the queue mode is Lossy
 0127                if (classId == (int) CLASS_ID_COMPONENT.TRANSFORM)
 0128                    queueMode = QueueMode.Lossy;
 0129            }
 0130            else if (queuedMessage.payload is Protocol.QueryPayload)
 131            {
 0132                busType = MessagingBusType.UI;
 0133                queueMode = QueueMode.Lossy;
 0134            }
 0135            else if (queuedMessage.payload is Protocol.SceneReady)
 136            {
 137                // When a INIT DONE message is enqueued, the next messages should be
 138                // enqueued in SYSTEM message bus, but we don't process them until
 139                // scene started has been processed
 0140                currentQueueState = QueueState.Systems;
 141            }
 142
 0143            switch (busType)
 144            {
 145                case MessagingBusType.INIT:
 0146                    initBus.Enqueue(queuedMessage, queueMode);
 0147                    break;
 148                case MessagingBusType.SYSTEM:
 0149                    systemBus.Enqueue(queuedMessage, queueMode);
 0150                    break;
 151                case MessagingBusType.UI:
 0152                    uiBus.Enqueue(queuedMessage, queueMode);
 153                    break;
 154            }
 0155        }
 156
 157        private void GetEntityIdAndClassIdFromTag(string tag, out int classId)
 158        {
 0159            int lastSeparator = tag.LastIndexOf(SEPARATOR);
 0160            if (!int.TryParse(tag.Substring(lastSeparator + 1), out classId))
 0161                Debug.LogError("Couldn't parse classId string to int");
 0162        }
 163    }
 164}