/// <summary>
    /// Sets the callback function of the BackgroundWorker.
    /// </summary>
    /// <param name="task">The callback function that contains the task that should be performed asynchronously.</param>
    /// <param name="finished">The callback function that should be called with the result of the task function.</param>
    /// <param name="progressChanged">The callback function for the progress changed handling.</param>
    /// <param name="supportCancellation">Flag to determine if the asynchronous task could be canceled.</param>
    public void SetCallbackFunctions(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished, AsyncProgressChangedHandler progressChanged = null, bool supportCancellation = false)
    {
        if (task == null)
        {
            throw new ArgumentNullException();
        }

        mResultCall          = finished;
        mRunCall             = task;
        mProgressChangedCall = progressChanged;
        mException           = null;

        mWorker = new BackgroundWorker();
        mWorker.WorkerSupportsCancellation = supportCancellation;

        if (mResultCall != null)
        {
            mWorker.DoWork += new DoWorkEventHandler(Run);
        }
        if (mResultCall != null)
        {
            mWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Finish);
        }

        if (progressChanged != null)
        {
            mWorker.WorkerReportsProgress = true;
            mWorker.ProgressChanged      += new ProgressChangedEventHandler(ProgressChanged);
        }
    }
Esempio n. 2
0
        public void Select(string url, string collection,
                           string where, string sortKey,
                           int rangeStart, int rangeCount,
                           AsyncResultHandler callback)
        {
            JProperty propWhere   = new JProperty("where", where);
            JProperty propSort    = new JProperty("sortKey", sortKey);
            JProperty propRange   = new JProperty("range", new JArray(rangeStart, rangeCount));
            JObject   jsonRequest = new JObject()
            {
                { "cmd", "select" },
                { "collection", collection },
                propWhere, propSort, propRange
            };

            if (where == null)
            {
                propWhere.Remove();
            }
            if (sortKey == null)
            {
                propSort.Remove();
            }

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 3
0
        public void Hello(string url, AsyncResultHandler callback)
        {
            JObject jsonRequest = new JObject()
            {
                { "cmd", "hello" }
            };

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 4
0
        public void SchemeList(string url, AsyncResultHandler callback)
        {
            JObject jsonRequest = new JObject()
            {
                { "cmd", "schemeList" }
            };

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 5
0
        public void CollectionList(string url, string scheme, AsyncResultHandler callback)
        {
            JObject jsonRequest = new JObject()
            {
                { "cmd", "collectionList" },
                { "scheme", scheme }
            };

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 6
0
        public void DropScheme(string url, string schemeName, AsyncResultHandler callback)
        {
            JObject jsonRequest = new JObject()
            {
                { "cmd", "dropScheme" },
                { "scheme", schemeName }
            };

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 7
0
        /// <summary>
        /// Starts a async download of a mod from KSP Spaceport.
        /// </summary>
        /// <param name="modInfo"></param>
        /// <param name="finished"></param>
        /// <param name="progressChanged"></param>
        public static void DownloadModAsync(string downloadURL, ref ModInfo modInfo, AsyncResultHandler <bool> finished = null, AsyncProgressChangedHandler progressChanged = null)
        {
            // get save path
            int    start    = downloadURL.LastIndexOf("/") + 1;
            string filename = downloadURL.Substring(start, downloadURL.Length - start);

            modInfo.LocalPath = Path.Combine(downloadURL, filename);

            AsyncTask <bool> asyncJob = new AsyncTask <bool>();

            asyncJob.SetDownloadCallbackFunctions(modInfo.SpaceportURL, modInfo.LocalPath, finished, progressChanged);
            asyncJob.RunDownload();;
        }
Esempio n. 8
0
        public void SendAsync(string url, JToken requestToken, AsyncResultHandler callback)
        {
            WebExceptionHandling(() =>
            {
                var uri = new Uri(url);
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpRequest.Method         = "POST";
                httpRequest.ServicePoint.Expect100Continue = false;
                httpRequest.Timeout     = RequestTimeout;
                httpRequest.ContentType = "application/json; charset=UTF-8";

                string request   = requestToken.ToString(Newtonsoft.Json.Formatting.None);
                byte[] byteArray = Encoding.UTF8.GetBytes(request);
                var dataStream   = httpRequest.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();


                httpRequest.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult result)
                {
                    WebExceptionHandling(() =>
                    {
                        using (var response = (HttpWebResponse)httpRequest.EndGetResponse(result))
                            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"), true))
                            {
                                string resultString = reader.ReadToEnd();
                                _workQueue.Enqueue(() =>
                                {
                                    try
                                    {
                                        callback(new RoseResult(resultString));
                                    }
                                    catch (Exception e)
                                    {
                                        callback(new RoseResult(RoseResult.UnknownError, e.Message));
                                    }
                                });
                            }
                    }, (e, errorCode) =>
                    {
                        callback(new RoseResult(errorCode, e.Message));
                    });
                }), httpRequest);
            }, (e, errorCode) =>
            {
                callback(new RoseResult(errorCode, e.Message));
            });
        }
Esempio n. 9
0
        public void Delete(string url, string collection,
                           string where, AsyncResultHandler callback)
        {
            JProperty propWhere   = new JProperty("where", where);
            JObject   jsonRequest = new JObject()
            {
                { "cmd", "delete" },
                { "collection", collection },
                propWhere
            };

            if (where == null)
            {
                propWhere.Remove();
            }

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 10
0
    /// <summary>
    /// Sets the download callback function of the BackgroundWorker.
    /// </summary>
    /// <param name="url">Download link</param>
    /// <param name="downloadPath">Path and filename to download to.</param>
    /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;bool&gt;(bool result, Exception ex)")</param>
    /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
    public void SetDownloadCallbackFunctions(string url, string downloadPath, AsyncResultHandler <bool> finished, AsyncProgressChangedHandler progressChanged = null)
    {
        mURL                 = url;
        mDownloadPath        = downloadPath;
        mDownloadFinished    = finished;
        mProgressChangedCall = progressChanged;
        mException           = null;

        mWebClient             = new WebClient();
        mWebClient.Credentials = CredentialCache.DefaultCredentials;

        if (finished != null)
        {
            mWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFinished);
        }

        if (progressChanged != null)
        {
            mWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
        }
    }
Esempio n. 11
0
        public void Insert(string url, string collection, string uniqueFor, string onDuplicate,
                           JToken data, AsyncResultHandler callback)
        {
            JProperty propUniqueFor   = new JProperty("uniqueFor", uniqueFor);
            JProperty propOnDuplicate = new JProperty("onDuplicate", onDuplicate);
            JObject   jsonRequest     = new JObject()
            {
                { "cmd", "insert" },
                { "collection", collection },
                propUniqueFor, propOnDuplicate,
                { "data", data }
            };

            if (uniqueFor == null)
            {
                propUniqueFor.Remove();
            }
            if (onDuplicate == null)
            {
                propOnDuplicate.Remove();
            }

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 12
0
 public void Update(string url, string collection,
                    string where, string data, AsyncResultHandler callback)
 {
     Update(url, collection, where, JToken.Parse(data), callback);
 }
Esempio n. 13
0
 public void Insert(string url, string collection, string uniqueFor, string onDuplicate,
                    string data, AsyncResultHandler callback)
 {
     Insert(url, collection, uniqueFor, onDuplicate, JToken.Parse(data), callback);
 }
Esempio n. 14
0
 /// <summary>
 /// Instantiates the AsyncTask class
 /// </summary>
 /// <param name="task">The task function to execute asynchrony. (A function with the signature "T_RETURN_VALUE FunctionName&lt;T_RETURN_VALUE&gt;()")</param>
 /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;T_RETURN_VALUE&gt;(T_RETURN_VALUE result, Exception ex)")</param>
 /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
 /// <param name="supportCancellation">Flag to determine if cancellation is needed.</param>
 public AsyncTask(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished, AsyncProgressChangedHandler progressChanged = null, bool supportCancellation = false)
 {
     SetCallbackFunctions(task, finished, progressChanged, supportCancellation);
 }
Esempio n. 15
0
        public void DropCollection(string url, string schemeName, string collectionName, AsyncResultHandler callback)
        {
            JObject jsonRequest = new JObject()
            {
                { "cmd", "dropCollection" },
                { "collection", $"{schemeName}.{collectionName}" }
            };

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 16
0
        public void CreateCollection(string url, string schemeName, string collectionName, AsyncResultHandler callback)
        {
            JObject jsonRequest = new JObject()
            {
                { "cmd", "createCollection" },
                { "collection", $"{schemeName}.{collectionName}" },
                { "justInCache", false }
            };

            SendAsync(url, jsonRequest, callback);
        }
Esempio n. 17
0
    /// <summary>
    /// Starts a asynchrony download.
    /// </summary>
    /// <param name="url">Download link</param>
    /// <param name="downloadPath">Path and filename to download to.</param>
    /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;bool&gt;(bool result, Exception ex)")</param>
    /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
    public static void RunDownload(string url, string downloadPath, AsyncResultHandler <bool> finished = null, AsyncProgressChangedHandler progressChanged = null)
    {
        AsyncTask <bool> asyncTask = new AsyncTask <bool>(url, downloadPath, finished, progressChanged);

        asyncTask.RunDownload();
    }
Esempio n. 18
0
 /// <summary>
 /// Instantiates the AsyncTask class for a async download.
 /// </summary>
 /// <param name="url">Download link</param>
 /// <param name="downloadPath">Path and filename to download to.</param>
 /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;bool&gt;(bool result, Exception ex)")</param>
 /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
 public AsyncTask(string url, string downloadPath, AsyncResultHandler <bool> finished, AsyncProgressChangedHandler progressChanged)
 {
     SetDownloadCallbackFunctions(url, downloadPath, finished, progressChanged);
 }
        /// <summary>
        /// Starts a async download of a mod from KSP Spaceport.
        /// </summary>
        /// <param name="modInfo"></param>
        /// <param name="finished"></param>
        /// <param name="progressChanged"></param>
        public static void DownloadModAsync(string downloadURL, ref ModInfo modInfo, AsyncResultHandler<bool> finished = null, AsyncProgressChangedHandler progressChanged = null)
        {
            // get save path 
            int start = downloadURL.LastIndexOf("/") + 1;
            string filename = downloadURL.Substring(start, downloadURL.Length - start);
            modInfo.LocalPath = Path.Combine(downloadURL, filename);

            AsyncTask<bool> asyncJob = new AsyncTask<bool>();
            asyncJob.SetDownloadCallbackFunctions(modInfo.SpaceportURL, modInfo.LocalPath, finished, progressChanged);
            asyncJob.RunDownload(); ;
        }
Esempio n. 20
0
    /// <summary>
    /// This function calls the passed task function asynchronously.
    /// </summary>
    /// <param name="task">The task function to execute asynchrony. (A function with the signature "T_RETURN_VALUE FunctionName&lt;T_RETURN_VALUE&gt;()")</param>
    /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;T_RETURN_VALUE&gt;(T_RETURN_VALUE result, Exception ex)")</param>
    /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
    public static void DoWork(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished = null, AsyncProgressChangedHandler progressChanged = null)
    {
        AsyncTask <T_RETURN_VALUE> asyncTask = new AsyncTask <T_RETURN_VALUE>(task, finished, progressChanged);

        asyncTask.Run();
    }