///
/// Copyright © 2003-2008 JetBrains s.r.o.
/// You may distribute under the terms of the GNU General Public License, as published by the Free Software Foundation, version 2 (see License.txt in the repository root folder).
///
using System;
using System.IO;
using System.Net;
using System.Windows.Forms;
using JetBrains.Omea.OpenAPI;
namespace JetBrains.Omea.SamplePlugins.Siam
{
///
/// Summary description for HttpDownload.
///
public class HttpDownload : AbstractNamedJob
{
public HttpDownload(string sUrl)
{
_sUrl = sUrl;
}
/*
///
/// Possible object state values
///
protected enum DownloadState { Idle, Busy }
///
/// Object state
///
protected DownloadState _state = DownloadState.Idle;
*/
///
/// The request to the server
///
protected WebRequest _request;
///
/// Represents the particular request being made and identifies it when information is retrieved from the request
///
protected IAsyncResult _asyncRequestResult;
///
/// URL being downloaded, as submitted to the constructor.
///
public string SUrl
{
get { return _sUrl; }
}
///
/// URL being downloaded
///
protected string _sUrl;
///
/// Delegate for the ContentDownloaded event.
///
public delegate void ContentDownloadedEventHandler(Stream streamData, HttpDownload session);
///
/// Fires when the content is downloaded successfully
///
public event ContentDownloadedEventHandler ContentDownloaded;
///
/// Executes the asynchronous job
///
protected override void Execute()
{
_request = WebRequest.Create( _sUrl );
_asyncRequestResult = _request.BeginGetResponse( null, null ); // Start the download
InvokeAfterWait( new MethodInvoker(OnHttpDownloaded), _asyncRequestResult.AsyncWaitHandle ); // Wait until it completes and call the handler
}
///
/// Download has completed
///
private void OnHttpDownloaded()
{
WebResponse response = _request.EndGetResponse( _asyncRequestResult );
if(response.GetType() == typeof(HttpWebResponse))
{
HttpWebResponse responseHttp = response as HttpWebResponse;
if(responseHttp.StatusCode != HttpStatusCode.OK)
throw new Exception("Failed to retrieve the sync data from HTTP.\n" + responseHttp.StatusDescription);
}
// Obtain the stream
Stream stream = response.GetResponseStream();
// Raise the event
ContentDownloaded(stream, this);
}
///
/// Job name.
///
public override string Name
{
get { return "Downloading SIAM sync data"; }
set { }
}
}
}