Beispiel #1
0
        public void ProcessCurrentState(IDownloader downloader, IUploader uploader)
        {
            if (!_timersManager.CheckTimerTimeout(DISABLE_UPLOAD_TIMER_NAME))
            {
                _uploadEnabled = true;
            }

            if (_stream.IsInFaultState && !_stream.Reopen())
            {
                Thread.Sleep(_lowLevelSettings.ConnectionRetryTime);
                return;
            }

            switch (_state)
            {
            case DataLinkStates.Upload:
                try
                {
                    _uploader.ExecuteUploadStep();
                    if (_uploader.Completed)
                    {
                        disableUpload(_lowLevelSettings.DelayUploadAfterUpload);
                        _state = DataLinkStates.Neutral;
                        raiseMessageUploadCompleted(new IOMessageEventArgs(_uploader.Message));
                    }
                }
                catch (DataLinkLayerException ex)
                {
                    _log.LogError(ex.Message);
                    if (ex.InnerException is ContentionErrorException)
                    {
                        _downloader.Reset();
                        _state = DataLinkStates.Download;
                    }
                    else
                    {
                        disableUpload(_lowLevelSettings.DelayUploadAfterUploadError);
                        _state = DataLinkStates.Neutral;
                        raiseMessageUploadCompleted(new IOMessageEventArgs(_uploader.Message, ex));
                    }
                }
                break;

            case DataLinkStates.Download:
                try
                {
                    _downloader.ExecuteDownloadStep();
                    if (_downloader.Completed)
                    {
                        disableUpload(_lowLevelSettings.DelayUploadAfterDownload);
                        _state = DataLinkStates.Neutral;
                        raiseMessageDownloadCompleted(new IOMessageEventArgs(_downloader.Message));
                    }
                }
                catch (TimeoutException)
                {
                    // The Data Link in the neutral state
                    _state = DataLinkStates.Neutral;
                }
                catch (DataLinkLayerException ex)
                {
                    _log.LogError(ex.Message);
                    _state = DataLinkStates.Neutral;
                    raiseMessageDownloadCompleted(new IOMessageEventArgs(_downloader.Message, ex));
                }
                break;

            case DataLinkStates.Neutral:
                if (_uploadQueue.Count > 0 && _uploadEnabled)
                {
                    var message = "";
                    lock (_uploadQueue)
                    {
                        message = _uploadQueue.Dequeue();
                    }
                    _uploader.ResetAndUpload(message);
                    _state = DataLinkStates.Upload;
                }
                else
                {
                    _downloader.Reset();
                    _state = DataLinkStates.Download;
                }
                break;
            }
        }