using System; using System.Collections.Generic; using System.Threading; using AppLimit.CloudComputing.SharpBox.Common; namespace AppLimit.CloudComputing.SharpBox { public partial class CloudStorage { #region Async Functions internal class BackgroundRequest : AsyncObjectRequest { public object OperationResult; public object OperationParameter; } internal class OpenRequest : BackgroundRequest { public ICloudStorageConfiguration config; public ICloudStorageAccessToken token; } /// /// This method implements a asyncallback for the open /// request, which is describe in BeginOpenRequest /// /// A reference to the start object private void OpenRequestCallback(object state) { // cast the request var req = state as OpenRequest; try { // perform the request req.OperationResult = Open(req.config, req.token); } catch (Exception e) { // failure to login var openRequest = req.result.AsyncState as BackgroundRequest; openRequest.OperationResult = null; openRequest.errorReason = e; } // call the async callback req.callback(req.result); } /// /// Starts the async open request /// /// /// /// /// public IAsyncResult BeginOpenRequest(AsyncCallback callback, ICloudStorageConfiguration configuration, ICloudStorageAccessToken token) { // build the request data structure var request = new OpenRequest { callback = callback }; request.result = new AsyncResultEx(request); request.config = configuration; request.token = token; // add to threadpool ThreadPool.QueueUserWorkItem(OpenRequestCallback, request); // return the result return request.result; } /// /// Finishs the async open request /// /// /// public ICloudStorageAccessToken EndOpenRequest(IAsyncResult asyncResult) { var req = asyncResult.AsyncState as OpenRequest; return req.OperationResult as ICloudStorageAccessToken; } /// /// This method implements a asyncallback for the get childs /// request, which is describe in BeginGetChildsRequest /// /// private static void GetChildsRequestCallback(object state) { // cast the request var req = state as BackgroundRequest; try { var retList = new List(); foreach (var e in req.OperationParameter as ICloudDirectoryEntry) { retList.Add(e); } req.OperationResult = retList; } catch (Exception e) { // failure to login var openRequest = req.result.AsyncState as BackgroundRequest; openRequest.OperationResult = null; openRequest.errorReason = e; } // call the async callback req.callback(req.result); } /// /// Starts the asyn get childs call /// /// /// /// public IAsyncResult BeginGetChildsRequest(AsyncCallback callback, ICloudDirectoryEntry parent) { // build the request data structure var request = new BackgroundRequest(); request.callback = callback; request.result = new AsyncResultEx(request); request.OperationParameter = parent; // add to threadpool ThreadPool.QueueUserWorkItem(GetChildsRequestCallback, request); // return the result return request.result; } /// /// Finishes the async get childs call /// /// /// public List EndGetChildsRequest(IAsyncResult asyncResult) { var req = asyncResult.AsyncState as BackgroundRequest; return req.OperationResult as List; } /// /// This method implements a asyncallback for the getroot /// request, which is describe in BeginGetRootRequest /// /// private void GetRootRequestCallback(object state) { // cast the request var req = state as BackgroundRequest; try { req.OperationResult = GetRoot(); } catch (Exception e) { // failure to login var openRequest = req.result.AsyncState as BackgroundRequest; openRequest.OperationResult = null; openRequest.errorReason = e; } // call the async callback req.callback(req.result); } /// /// Starts the asyn getRoot call /// /// /// public IAsyncResult BeginGetRootRequest(AsyncCallback callback) { // build the request data structure var request = new BackgroundRequest { callback = callback }; request.result = new AsyncResultEx(request); request.OperationParameter = null; // add to threadpool ThreadPool.QueueUserWorkItem(GetRootRequestCallback, request); // return the result return request.result; } /// /// Finishes the async getRoot call /// /// /// public ICloudDirectoryEntry EndGetRootRequest(IAsyncResult asyncResult) { var req = asyncResult.AsyncState as BackgroundRequest; return req.OperationResult as ICloudDirectoryEntry; } #endregion } }