< 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:47
Uncovered lines:15
Coverable lines:62
Total lines:164
Line coverage:75.8% (47 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%20.9410052.17%
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
 54020        public Dictionary<MessagingBusType, MessagingBus> messagingBuses =
 21            new Dictionary<MessagingBusType, MessagingBus>();
 22
 23        public IMessageProcessHandler messageHandler;
 24        public int debugSceneNumber;
 54025        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
 54035        public MessagingController(IMessagingControllersManager messagingManager, IMessageProcessHandler messageHandler,
 36            int debugSceneNumber = -1)
 37        {
 54038            this.messagingManager = messagingManager;
 54039            this.debugSceneNumber = debugSceneNumber;
 54040            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
 54043            uiBus = AddMessageBus(MessagingBusType.UI);
 54044            initBus = AddMessageBus(MessagingBusType.INIT);
 54045            systemBus = AddMessageBus(MessagingBusType.SYSTEM);
 46
 54047            currentQueueState = QueueState.Init;
 48
 54049            StartBus(MessagingBusType.INIT);
 54050            StartBus(MessagingBusType.UI);
 54051        }
 52
 53        private MessagingBus AddMessageBus(MessagingBusType type)
 54        {
 162055            var newMessagingBus = new MessagingBus(type, messageHandler, this);
 162056            newMessagingBus.debugSceneNumber = debugSceneNumber;
 57
 162058            messagingBuses.Add(type, newMessagingBus);
 162059            return newMessagingBus;
 60        }
 61
 62        public void StartBus(MessagingBusType busType)
 63        {
 108464            if (messagingBuses.ContainsKey(busType))
 65            {
 108466                messagingBuses[busType].Start();
 67            }
 108468        }
 69
 70        public void StopBus(MessagingBusType busType)
 71        {
 272            if (messagingBuses.ContainsKey(busType))
 73            {
 274                messagingBuses[busType].Stop();
 75            }
 276        }
 77
 78        public void Stop()
 79        {
 102680            using (var iterator = messagingBuses.GetEnumerator())
 81            {
 410482                while (iterator.MoveNext())
 83                {
 307884                    iterator.Current.Value.Stop();
 85                }
 102686            }
 102687        }
 88
 89        public void Dispose()
 90        {
 53391            using (var iterator = messagingBuses.GetEnumerator())
 92            {
 213293                while (iterator.MoveNext())
 94                {
 159995                    iterator.Current.Value.Dispose();
 96                }
 53397            }
 53398        }
 99
 100        public void ForceEnqueue(MessagingBusType busType, QueuedSceneMessage queuedMessage)
 101        {
 33102            messagingBuses[busType].Enqueue(queuedMessage);
 33103        }
 104
 105        public void Enqueue(bool isUiBus, QueuedSceneMessage_Scene queuedMessage, out MessagingBusType busType)
 106        {
 2107            busType = MessagingBusType.NONE;
 108
 2109            QueueMode queueMode = QueueMode.Reliable;
 110
 111            // If current scene is the Global Scene, the bus id should be UI
 2112            if (isUiBus)
 0113                busType = MessagingBusType.UI;
 2114            else if (currentQueueState == QueueState.Init)
 2115                busType = MessagingBusType.INIT;
 116            else
 0117                busType = MessagingBusType.SYSTEM;
 118
 119            // Check if the message type is an EntityComponentCreateOrUpdate
 2120            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;
 129            }
 2130            else if (queuedMessage.payload is Protocol.QueryPayload)
 131            {
 0132                busType = MessagingBusType.UI;
 0133                queueMode = QueueMode.Lossy;
 134            }
 2135            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
 2140                currentQueueState = QueueState.Systems;
 141            }
 142
 2143            switch (busType)
 144            {
 145                case MessagingBusType.INIT:
 2146                    initBus.Enqueue(queuedMessage, queueMode);
 2147                    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}