Esempio n. 1
0
        /// <summary>
        /// Handles Machinations tasks.
        /// </summary>
        public void ProcessSchedule()
        {
            try
            {
                //Execute any updates from the back-end.
                while (_updatesFromBackEnd.Count > 0)
                {
                    MnDataLayer.UpdateSourcesWithValuesFromMachinations(_updatesFromBackEnd[0].Key, _updatesFromBackEnd[0].Value);
                    _updatesFromBackEnd.RemoveAt(0);
                }

                //Rudimentary State Machine:

                //Socket not yet ready?
                if (!_socketClient.IsInitialized)
                {
                    L.D("Machinations Service SocketIO Scheduler: Waiting for Socket Connection. Current State: " + _currentState);
                    if (_currentState != State.WaitingForSocketReady)
                    {
                        L.E("Invalid state given the fact that the socket is not even initialized.");
                        FreshStart();
                    }

                    return;
                }

                //If the socket isn't yet ready, starting the Auth process.
                if (_currentState == State.WaitingForSocketReady)
                {
                    L.D("Machinations Service SocketIO Scheduler: WaitingForSocketReady -> AuthRequested.");
                    _currentState = State.AuthRequested;
                    _socketClient.EmitAuthRequest();
                    return;
                }

                //Wait for Auth.
                if (_currentState == State.AuthRequested)
                {
                    L.D("Machinations Service SocketIO Scheduler: Waiting for Auth Response.");
                    return;
                }

                //Wait for Init.
                if (_currentState == State.InitRequested)
                {
                    L.D("Machinations Service SocketIO Scheduler: Waiting for Sync Init Response.");
                    return;
                }

                //When everything is connected, switching through the following states:
                switch (_currentState)
                {
                case State.AuthSuccess:
                    L.D("Machinations Service SocketIO Scheduler: Auth Success. Idling.");
                    _currentState = State.Idling;
                    //Make sure we request init at first start.
                    if (!HasPerformedFullDiagramInit)
                    {
                        _initRequested = true;
                    }
                    break;

                case State.Idling:
                    if (_initRequested)
                    {
                        L.D("Machinations Service SocketIO Scheduler: Init Requested: Idling -> PreparingForInitRequest.");
                        _currentState = State.PreparingForInitRequest;
                    }

                    break;

                //Wait at least 1 Timer interval before making the Sync request.
                case State.PreparingForInitRequest:
                    L.D("Machinations Service SocketIO Scheduler: Init Requested.");
                    _currentState  = State.InitRequested;
                    _initRequested = false;
                    //The first time we get here, we will perform a FULL init request.
                    _socketClient.EmitDiagramInitRequest(HasPerformedFullDiagramInit);
                    HasPerformedFullDiagramInit = true;     //But subsequent times, we will only ask for whatever is new.
                    break;

                case State.InitComplete:
                    L.D("Machinations Service SocketIO Scheduler: Init Complete. " + _currentState + " -> Idling.");
                    _currentState = State.Idling;
                    MnDataLayer.UpdateSourcesWithValuesFromMachinations(_diagramElementsFromBackEnd);
                    MnDataLayer.SyncComplete();
                    break;
                }
            }
            catch (Exception ex)
            {
                L.ToLogFile("MachinationsService Scheduler Exception Caught:");
                L.ExToLogFile(ex);
            }
        }