コード例 #1
0
        private CaptureInfo ProcessResponse(string response)
        {
            // Parse the response XML
            XDocument xml;

            try
            {
                xml = XDocument.Parse(response);
            }
            catch (System.Xml.XmlException ex)
            {
                throw new SimTemplateException("Failed to parse response XML", ex);
            }

            CaptureInfo capture;
            // Expect one capture element (or none)
            XElement captureEl = xml.Elements("capture").SingleOrDefault();

            if (captureEl != null)
            {
                capture = SimTemplateServerHelper.ToCaptureInfo(captureEl);
            }
            else
            {
                throw new SimTemplateException("XML response contained no capture results");
            }
            return(capture);
        }
コード例 #2
0
        protected override void StartSaveTask(long dbId, byte[] template, Guid guid, CancellationToken token)
        {
            // Make HTTP request
            HttpContent content = new ByteArrayContent(template);
            Task <HttpResponseMessage> saveResponse = m_Client.PostAsync(SimTemplateServerHelper.SaveTemplateUri(dbId), content);

            // When task is complete, raise GetCaptureComplete event
            // Pass the task the cancellation token so that this action may be skipped
            saveResponse.ContinueWith((Task <HttpResponseMessage> sTT) =>
            {
                // Check for cancellation (race)
                if (!token.IsCancellationRequested)
                {
                    // HTTP requests are not cancellable
                    IntegrityCheck.IsFalse(sTT.IsCanceled);
                    if (sTT.IsCompleted)
                    {
                        HttpResponseMessage response = sTT.Result;
                        // Do some dealing with the response to check it was successful
                        OnSaveTemplateComplete(
                            new SaveTemplateEventArgs(guid, DataRequestResult.Success));
                    }
                    else if (sTT.IsFaulted)
                    {
                        // An exception was thrown during the request.
                        Log.Error("Save Template task failed: " + sTT.Exception.Message, sTT.Exception);
                        OnSaveTemplateComplete(
                            new SaveTemplateEventArgs(guid, DataRequestResult.TaskFailed));
                    }
                }
            }, token);
        }
コード例 #3
0
        protected override void StartCaptureTask(ScannerType scannerType, Guid guid, CancellationToken token)
        {
            Log.DebugFormat("Starting task with Guid={0}", guid);

            // Make HTTP request
            Task <string> responseText = m_Client.GetStringAsync(
                SimTemplateServerHelper.GetCaptureRequestUri(scannerType));

            // When task is complete, raise GetCaptureComplete event
            // Pass the task the cancellation token so that this action may be skipped
            responseText.ContinueWith((Task <string> gCT) =>
            {
                // Check for cancellation (race)
                if (!token.IsCancellationRequested)
                {
                    // HTTP requests are not cancellable
                    IntegrityCheck.IsFalse(gCT.IsCanceled);
                    if (gCT.IsCompleted)
                    {
                        CaptureInfo capture;
                        try
                        {
                            capture = ProcessResponse(gCT.Result);
                            OnGetCaptureComplete(
                                new GetCaptureCompleteEventArgs(capture, guid, DataRequestResult.Success));
                        }
                        catch (SimTemplateException ex)
                        {
                            Log.Error("Failed to process xml response:", ex);
                            OnGetCaptureComplete(
                                new GetCaptureCompleteEventArgs(null, guid, DataRequestResult.Failed));
                        }
                    }
                    else if (gCT.IsFaulted)
                    {
                        // An exception was thrown during the request.
                        Log.Error("GetCapture task failed: " + gCT.Exception.Message, gCT.Exception);
                        OnGetCaptureComplete(
                            new GetCaptureCompleteEventArgs(null, guid, DataRequestResult.TaskFailed));
                    }
                }
            }, token);
        }