Пример #1
0
        /// <summary>
        /// Invokes the current action based on the provided message.
        /// </summary>
        /// <param name="message">The message upon which the action acts.</param>
        public void Invoke(ICommunicationMessage message)
        {
            var msg = message as DataDownloadRequestMessage;

            if (msg == null)
            {
                Debug.Assert(false, "The message is of the incorrect type.");
                return;
            }

            if (!m_Uploads.HasRegistration(msg.Token))
            {
                m_Diagnostics.Log(
                    LevelToLog.Warn,
                    CommunicationConstants.DefaultLogTextPrefix,
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "No file was registered for uploading with token {0}",
                        msg.Token));

                SendMessage(msg, new FailureMessage(m_Layer.Id, msg.Id));
                return;
            }

            var filePath    = m_Uploads.Deregister(msg.Token);
            var tokenSource = new CancellationTokenSource();

            m_Diagnostics.Log(
                LevelToLog.Debug,
                CommunicationConstants.DefaultLogTextPrefix,
                string.Format(
                    CultureInfo.InvariantCulture,
                    "Transferring file: {0}",
                    filePath));
            var task = m_Layer.UploadData(msg.Sender, filePath, tokenSource.Token, m_Scheduler);

            ICommunicationMessage returnMsg;

            try
            {
                task.Wait();
                returnMsg = new SuccessMessage(m_Layer.Id, msg.Id);
            }
            catch (AggregateException e)
            {
                m_Diagnostics.Log(
                    LevelToLog.Error,
                    CommunicationConstants.DefaultLogTextPrefix,
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "An error occurred during the transfer of the file {0}. Exception was: {1}",
                        filePath,
                        e));

                returnMsg = new FailureMessage(m_Layer.Id, msg.Id);
                m_Uploads.Reregister(msg.Token, filePath);
            }

            SendMessage(msg, returnMsg);
        }