コード例 #1
0
        public void AddSegment(SegmentInformation segmentInfo)
        {
            var sap = segmentInfo.SegmentAlignedPosition;

            Preconditions.Assert(!_tokensDict.ContainsKey(sap), $"There arleady is segment of sap {sap}");

            RequiredSegmentSituation requiredSituation;
            bool fillingIsNecessary;

            if (segmentInfo.SegmentState == SegmentState.Active)
            {
                fillingIsNecessary = true;
                requiredSituation  = RequiredSegmentSituation.Filled;
            }
            else if (segmentInfo.SegmentState == SegmentState.Standby)
            {
                fillingIsNecessary = false;
                requiredSituation  = RequiredSegmentSituation.Filled;
            }
            else
            {
                fillingIsNecessary = false;
                requiredSituation  = RequiredSegmentSituation.Created;
            }
            var newToken = new SegmentGenerationProcessToken(SegmentGenerationProcessSituation.BeforeStartOfCreation, requiredSituation);

            _tokensDict[sap] = new SegmentGenerationProcessTokenWithFillingNecessity()
            {
                Token = newToken,
                FillingIsNecessary = fillingIsNecessary
            };
            _executor.ExecuteSegmentAction(newToken, sap);
        }
コード例 #2
0
        private async Task FillSegment(SegmentGenerationProcessToken token, IntVector2 sap)
        {
            Preconditions.Assert(token.CurrentSituation == SegmentGenerationProcessSituation.Created, "Unexpected situaton " + token.CurrentSituation);
            token.CurrentSituation = SegmentGenerationProcessSituation.DuringFilling;
            await _segmentFillingFunc(sap, _currentlyCreatedSegments[sap]);

            token.CurrentSituation = SegmentGenerationProcessSituation.Filled;
        }
コード例 #3
0
 private async Task RemoveSegment(SegmentGenerationProcessToken token, IntVector2 sap)
 {
     Preconditions.Assert(!token.ProcessIsOngoing, "Cannot remove while process in ongoing");
     Preconditions.Assert(token.RequiredSituation == RequiredSegmentSituation.Removed, "Required situation in not removed but " + token.RequiredSituation);
     if (_currentlyCreatedSegments.ContainsKey(sap))
     {
         var segment = _currentlyCreatedSegments[sap];
         _currentlyCreatedSegments.Remove(sap);
         await _segmentRemovalFunc(segment);
     }
 }
コード例 #4
0
 public async Task ExecuteSegmentAction(SegmentGenerationProcessToken token, IntVector2 sap)
 {
     if (!token.ProcessIsOngoing)
     {
         bool shouldContinue = true;
         while (shouldContinue)
         {
             shouldContinue = await SegmentProcessOneLoop(token, sap);
         }
     }
 }
コード例 #5
0
 public Task ExecuteSegmentAction(SegmentGenerationProcessToken token, IntVector2 sap)
 {
     if (token.RequiredSituation == RequiredSegmentSituation.Filled || token.RequiredSituation == RequiredSegmentSituation.Created)
     {
         return(_segmentFillingFunc(sap));
     }
     else
     {
         return(TaskUtils.EmptyCompleted());
     }
 }
コード例 #6
0
        private async Task GenerateSegment(SegmentGenerationProcessToken token, IntVector2 sap)
        {
            Preconditions.Assert(token.CurrentSituation == SegmentGenerationProcessSituation.BeforeStartOfCreation, "Unexpected situaton " + token.CurrentSituation);
            if (!_currentlyCreatedSegments.ContainsKey(sap))
            {
                token.CurrentSituation = SegmentGenerationProcessSituation.DuringCreation;
                var newSegment = await _segmentGeneratingFunc(sap);

                _currentlyCreatedSegments[sap] = newSegment;
            }
            token.CurrentSituation = SegmentGenerationProcessSituation.Created;
        }
コード例 #7
0
        private async Task <bool> SegmentProcessOneLoop(SegmentGenerationProcessToken token, IntVector2 sap)
        {
            switch (token.CurrentSituation)
            {
            case SegmentGenerationProcessSituation.BeforeStartOfCreation:
                switch (token.RequiredSituation)
                {
                case RequiredSegmentSituation.Created:
                    await GenerateSegment(token, sap);

                    return(true);

                case RequiredSegmentSituation.Filled:
                    if (!_currentlyCreatedSegments.ContainsKey(sap))
                    {
                        await GenerateSegment(token, sap);
                    }
                    else
                    {
                        await FillSegment(token, sap);
                    }
                    return(true);

                case RequiredSegmentSituation.Removed:
                    await RemoveSegment(token, sap);

                    return(false);

                default:
                    Preconditions.Fail("Unexpected required situation " + token.RequiredSituation);
                    return(false);
                }

            case SegmentGenerationProcessSituation.Created:
                switch (token.RequiredSituation)
                {
                case RequiredSegmentSituation.Created:
                    return(false);

                case RequiredSegmentSituation.Filled:
                    await FillSegment(token, sap);

                    return(true);

                case RequiredSegmentSituation.Removed:
                    await RemoveSegment(token, sap);

                    return(false);

                default:
                    Preconditions.Fail("Unexpected required situation " + token.RequiredSituation);
                    return(false);
                }

            case SegmentGenerationProcessSituation.Filled:
                switch (token.RequiredSituation)
                {
                case RequiredSegmentSituation.Created:
                    return(false);

                case RequiredSegmentSituation.Filled:
                    return(false);

                case RequiredSegmentSituation.Removed:
                    await RemoveSegment(token, sap);

                    return(false);

                default:
                    Preconditions.Fail("Unexpected required situation " + token.RequiredSituation);
                    return(false);
                }

            default:
                Preconditions.Fail("Unexpected situation " + token.CurrentSituation);
                return(false);
            }
        }
コード例 #8
0
 public void ExecuteSegmentAction(SegmentGenerationProcessToken token, IntVector2 sap)
 {
     PostPureAsyncAction(() => _executor.ExecuteSegmentAction(token, sap));
 }