/// <summary>
        /// Begins a new request to the specified CDN Finder server.
        /// </summary>
        /// <param name="uri">The CDN Finder URI.</param>
        /// <param name="sites">The list of web sites.</param>
        /// <param name="callback">The callback method.</param>
        /// <param name="userState">The user state.</param>
        /// <returns>The result of the asynchronous web operation.</returns>
        public IAsyncResult Begin(Uri uri, IEnumerable<object> sites, AsyncWebRequestCallback callback, object userState = null)
        {
            // Create the request state.
            AsyncWebResult asyncState = new AsyncWebResult(uri, callback, userState);

            // Generate the request boundary.
            string boundary = "----InetAnalytics{0}".FormatWith(AsyncWeb.GenerateNonce());

            // Set the request headers.
            asyncState.Request.Method = "POST";
            asyncState.Request.Accept = "text/html,application/xhtml+xml,application/xml";
            asyncState.Request.ContentType = "multipart/form-data; boundary={0}".FormatWith(boundary);
            asyncState.Request.Timeout = this.config.Timeout;
            asyncState.Request.AllowAutoRedirect = this.config.AutoRedirect;

            // Compute the send data.
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("--{0}{1}", boundary, Environment.NewLine);
            builder.AppendLine("Content-Disposition: form-data; name=\"file\"; filename=\"sites\"");
            builder.AppendLine("Content-Type: text/plain");
            builder.AppendLine();
            foreach (object site in sites)
            {
                if (!string.IsNullOrWhiteSpace(site.ToString()))
                {
                    builder.AppendLine(site.ToString());
                }
            }
            builder.AppendFormat("--{0}{1}", boundary, Environment.NewLine);
            builder.AppendLine("Content-Disposition: form-data; name=\"format\"");
            builder.AppendLine();
            builder.AppendLine("xml");
            builder.AppendFormat("--{0}--{1}", boundary, Environment.NewLine);

            // Append the send data.
            asyncState.SendData.Append(builder.ToString(), Encoding.UTF8);

            // Begin the request.
            return base.Begin(asyncState);
        }
Пример #2
0
        /// <summary>
        /// Begins a new request to the specified Mercury server to upload a session.
        /// </summary>
        /// <param name="uri">The Mercury server URI.</param>
        /// <param name="id">The session identifier.</param>
        /// <param name="author">The session author.</param>
        /// <param name="description">The session description.</param>
        /// <param name="timestamp">The session timestamp.</param>
        /// <param name="callback">The callback method.</param>
        /// <param name="userState">The user state.</param>
        /// <returns>The result of the asynchronous web operation.</returns>
        public IAsyncResult BeginUploadSession(Uri uri, Guid id, string author, string description, DateTime timestamp, AsyncWebRequestCallback callback, object userState = null)
        {
            // Create the request state.
            AsyncWebResult asyncState = new AsyncWebResult(uri, callback, userState);

            // Set the request headers.
            asyncState.Request.Method = "POST";
            asyncState.Request.Accept = "text/html,application/xhtml+xml,application/xml";
            asyncState.Request.ContentType = "application/json;charset=UTF-8";

            // Create the traceroute JSON object.
            JObject obj = new JObject(
                new JProperty("sessionId", id.ToString()),
                new JProperty("author", author),
                new JProperty("description", description),
                new JProperty("dateStart", timestamp.ToUniversalTime().ToString(@"yyyy-MM-ddTHH:mm:ss.fffZ"))
                );

            // Append the send data.
            asyncState.SendData.Append(obj.ToString(), Encoding.UTF8);

            // Begin the request.
            return base.Begin(asyncState);
        }
Пример #3
0
        // Private methods.
        /// <summary>
        /// A callback method called when the control completes an asynchrnous request for a PlanetLab resource.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private void OnCallback(AsyncWebResult result)
        {
            lock (this.sync)
            {
                // If the current request is null, do nothing.
                if (null == this.request) return;

                try
                {
                    // The asynchronous web result.
                    AsyncWebResult asyncResult = null;
                    // Complete the asyncrhonous request.
                    XmlRpcResponse response = this.request.End(result, out asyncResult);

                    // If no fault occurred during the XML-RPC request.
                    if (response.Fault == null)
                    {
                        // Display a success notification message.
                        this.ShowMessage(
                            Resources.GlobeSuccess_48,
                            "PlanetLab Update",
                            "Refreshing the PlanetLab information completed successfully.",
                            false,
                            (int)ApplicationConfig.MessageCloseDelay.TotalMilliseconds,
                            (object[] parameters) =>
                                {
                                    // Call the complete request event handler.
                                    this.OnRequestResult(response, asyncResult.AsyncState as RequestState);
                                    // Call the end request event handler.
                                    this.OnRequestFinished(asyncResult.AsyncState as RequestState);
                                });
                    }
                    else
                    {
                        // Display an error notification message.
                        this.ShowMessage(
                            Resources.GlobeWarning_48,
                            "PlanetLab Error",
                            "Refreshing the PlanetLab information has failed (RPC code {0} {1})".FormatWith(response.Fault.FaultCode, response.Fault.FaultString),
                            false,
                            (int)ApplicationConfig.MessageCloseDelay.TotalMilliseconds,
                            (object[] paremeters) =>
                                {
                                    // Call the complete request event handler.
                                    this.OnRequestResult(response, asyncResult.AsyncState as RequestState);
                                    // Call the end request event handler.
                                    this.OnRequestFinished(asyncResult.AsyncState as RequestState);
                                });
                    }
                    // Set the current request to null.
                    this.request = null;
                    this.result = null;
                }
                catch (WebException exception)
                {
                    // Set the current request to null.
                    this.request = null;
                    this.result = null;
                    // If the exception status is canceled.
                    if (exception.Status == WebExceptionStatus.RequestCanceled)
                    {
                        // Hide the notification message.
                        this.HideMessage((object[] parameters) =>
                            {
                                // Call the cancel request handler.
                                this.OnRequestCanceled(result.AsyncState as RequestState);
                                // Call the end request handler.
                                this.OnRequestFinished(result.AsyncState as RequestState);
                            });
                        // Begin a pending request, if any.
                        this.BeginRequest();
                    }
                    else
                    {
                        // Display an error notification message.
                        this.ShowMessage(
                            Resources.GlobeError_48,
                            "PlanetLab Update",
                            "Refreshing the PlanetLab information has failed. {0}".FormatWith(exception.Message),
                            false,
                            (int)ApplicationConfig.MessageCloseDelay.TotalMilliseconds,
                            (object[] parameters) =>
                                {
                                    // Call the exception handler.
                                    this.OnRequestException(exception, result.AsyncState as RequestState);
                                    // Call the request finished event handler.
                                    this.OnRequestFinished(result.AsyncState as RequestState);
                                });
                    }
                }
                catch (Exception exception)
                {
                    // Set the current request to null.
                    this.request = null;
                    this.result = null;

                    // Display an error notification message.
                    this.ShowMessage(
                        Resources.GlobeError_48,
                        "PlanetLab Update",
                        "Refreshing the PlanetLab information has failed. {0}".FormatWith(exception.Message),
                        false,
                        (int)ApplicationConfig.MessageCloseDelay.TotalMilliseconds,
                        (object[] parameters) =>
                            {
                                // Call the exception handler.
                                this.OnRequestException(exception, result.AsyncState as RequestState);
                                // Call the request finished event handler.
                                this.OnRequestFinished(result.AsyncState as RequestState);
                            });
                }
            }
        }
 /// <summary>
 /// Completes an asynchronous request for updating the video categories.
 /// </summary>
 /// <param name="result">The asynchornous result.</param>
 private void OnEndRefreshCategories(AsyncWebResult result)
 {
     try
     {
         // Complete the asynchronous request.
         this.crawler.YouTube.Categories.EndRefresh(result);
         // Update the message that the operation completed successfully.
         this.ShowMessage(
             Resources.GlobeSuccess_48,
             "Video Categories",
             "Refreshing the list of YouTube categories completed successfully.",
             false);
         // Update the categories.
         this.OnOpenCategories(null, null);
     }
     catch (Exception exception)
     {
         // Update the message that the operation failed.
         this.ShowMessage(
             Resources.GlobeError_48,
             "Video Categories",
             "Refreshing the list of YouTube categories failed.{0}{1}".FormatWith(Environment.NewLine, exception.Message),
             false
             );
     }
     finally
     {
         // Delay the closing of the message.
         Thread.Sleep(this.crawler.Config.MessageCloseDelay);
         // Hide the message.
         this.HideMessage();
     }
 }
        /// <summary>
        /// A method called when receiving the web response.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private void OnCallback(AsyncWebResult result)
        {
            // Set the result to null.
            lock (this.sync)
            {
                this.result = null;
            }

            try
            {
                // Complete the request.
                this.request.End(result);
                // Update the status label.
                this.status.Send(
                    ApplicationStatus.StatusType.Normal,
                    "Uploading the traceroute to the Mercury server completed successfully.",
                    Resources.Success_16);
                // Show a message.
                this.ShowMessage(
                    Resources.GlobeSuccess_48,
                    "Mercury Server Request",
                    "Uploading the traceroute to the Mercury server successfully.",
                    false,
                    (int)this.config.Api.Config.MessageCloseDelay.TotalMilliseconds,
                    (object[] parameters) =>
                    {
                        // Call the request finished event handler.
                        this.OnRequestFinished();
                    });
                // Log the events.
                this.controlLog.Add(this.config.Api.Log(
                    LogEventLevel.Verbose,
                    LogEventType.Success,
                    @"Uploading the traceroute to the Mercury server '{0}' completed successfully.",
                    new object[] {  this.config.UploadTracerouteUrl }
                    ));
            }
            catch (WebException exception)
            {
                if (exception.Status == WebExceptionStatus.RequestCanceled)
                {
                    // Update the status label.
                    this.status.Send(ApplicationStatus.StatusType.Normal, "Uploading the traceroute to the Mercury server was canceled.", Resources.Canceled_16);
                    // Show a message.
                    this.ShowMessage(
                        Resources.GlobeCanceled_48,
                        "Mercury Server Request",
                        "Uploading the traceroute to the Mercury server was canceled.",
                        false,
                        (int)this.config.Api.Config.MessageCloseDelay.TotalMilliseconds,
                        (object[] parameters) =>
                        {
                            // Call the request finished event handler.
                            this.OnRequestFinished();
                        });
                    // Log the events.
                    this.controlLog.Add(this.config.Api.Log(
                        LogEventLevel.Normal,
                        LogEventType.Canceled,
                        @"Uploading the traceroute to the Mercury server '{0}' was canceled.",
                        new object[] { this.config.UploadTracerouteUrl }
                        ));
                }
                else
                {
                    // Update the status label.
                    this.status.Send(ApplicationStatus.StatusType.Normal, "Uploading the traceroute to the Mercury server failed. {0}".FormatWith(exception.Message), Resources.Error_16);
                    // Show a message.
                    this.ShowMessage(
                        Resources.GlobeError_48,
                        "Mercury Server Request",
                        "Uploading the traceroute to the Mercury server failed.{0}{1}".FormatWith(Environment.NewLine, exception.Message),
                        false,
                        (int)this.config.Api.Config.MessageCloseDelay.TotalMilliseconds,
                        (object[] parameters) =>
                        {
                            // Call the request finished event handler.
                            this.OnRequestFinished();
                        });
                    // Log the events.
                    this.controlLog.Add(this.config.Api.Log(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        @"Uploading the traceroute to the Mercury server '{0}' failed. {1}",
                        new object[] { this.config.UploadTracerouteUrl, exception.Message },
                        exception
                        ));
                }
            }
            catch (Exception exception)
            {
                // Update the status label.
                this.status.Send(ApplicationStatus.StatusType.Normal, "Uploading the traceroute to the Mercury server failed. {0}".FormatWith(exception.Message), Resources.Error_16);
                // Show a message.
                this.ShowMessage(
                    Resources.GlobeError_48,
                    "Mercury Server Request",
                    "Uploading the traceroute to the Mercury server failed.{0}{1}".FormatWith(Environment.NewLine, exception.Message),
                    false,
                    (int)this.config.Api.Config.MessageCloseDelay.TotalMilliseconds,
                    (object[] parameters) =>
                    {
                        // Call the request finished event handler.
                        this.OnRequestFinished();
                    });
                // Log the events.
                this.controlLog.Add(this.config.Api.Log(
                    LogEventLevel.Important,
                    LogEventType.Error,
                    @"Uploading the traceroute to the Mercury server '{0}' failed. {0}",
                    new object[] { this.config.UploadTracerouteUrl, exception.Message },
                    exception
                    ));
            }
        }
        /// <summary>
        /// A method called when receiving the web response.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private void OnCallback(AsyncWebResult result)
        {
            // Set the result to null.
            lock (this.sync)
            {
                this.asyncResult = null;
            }

            try
            {
                // Complete the request.
                CdnFinderSites sites = this.request.End(result);
                // Update the status label.
                this.status.Send(
                    ApplicationStatus.StatusType.Normal,
                    "Requesting the CDN Finder data completed successfully.",
                    "{0} web sites received.".FormatWith(sites.Count),
                    Resources.Success_16);
                // Show a message.
                this.ShowMessage(
                    Resources.GlobeSuccess_48,
                    "CDN Finder Request",
                    "Requesting the CDN Finder data completed successfully.",
                    false,
                    (int)this.config.Api.Config.MessageCloseDelay.TotalMilliseconds,
                    (object[] parameters) =>
                    {
                        // Call the request finished event handler.
                        this.OnRequestFinished();
                        // Update the site information.
                        this.OnUpdateDomains(sites);
                    });
                // Log the events.
                this.controlLog.Add(this.config.Api.Log(
                    LogEventLevel.Verbose,
                    LogEventType.Success,
                    "Requesting the CDN Finder data completed successfully."
                    ));
            }
            catch (WebException exception)
            {
                if (exception.Status == WebExceptionStatus.RequestCanceled)
                {
                    // Update the status label.
                    this.status.Send(ApplicationStatus.StatusType.Normal, "Requesting the CDN Finder data was canceled.", Resources.Canceled_16);
                    // Show a message.
                    this.ShowMessage(
                        Resources.GlobeCanceled_48,
                        "CDN Finder Request",
                        "Requesting the CDN Finder data was canceled.",
                        false,
                        (int)this.config.Api.Config.MessageCloseDelay.TotalMilliseconds,
                        (object[] parameters) =>
                        {
                            // Call the request finished event handler.
                            this.OnRequestFinished();
                        });
                    // Log the events.
                    this.controlLog.Add(this.config.Api.Log(
                        LogEventLevel.Normal,
                        LogEventType.Canceled,
                        "Requesting the CDN Finder data was canceled."
                        ));
                }
                else
                {
                    // Update the status label.
                    this.status.Send(ApplicationStatus.StatusType.Normal, "Requesting the CDN Finder data failed. {0}".FormatWith(exception.Message), Resources.Error_16);
                    // Show a message.
                    this.ShowMessage(
                        Resources.GlobeError_48,
                        "CDN Finder Request",
                        "Requesting the CDN Finder data failed.{0}{1}".FormatWith(Environment.NewLine, exception.Message),
                        false,
                        (int)this.config.Api.Config.MessageCloseDelay.TotalMilliseconds,
                        (object[] parameters) =>
                        {
                            // Call the request finished event handler.
                            this.OnRequestFinished();
                        });
                    // Log the events.
                    this.controlLog.Add(this.config.Api.Log(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        "Requesting the CDN Finder data failed. {0}",
                        new object[] { exception.Message },
                        exception
                        ));
                }
            }
            catch (Exception exception)
            {
                // Update the status label.
                this.status.Send(ApplicationStatus.StatusType.Normal, "Requesting the CDN Finder data failed. {0}".FormatWith(exception.Message), Resources.Error_16);
                // Show a message.
                this.ShowMessage(
                    Resources.GlobeError_48,
                    "CDN Finder Request",
                    "Requesting the CDN Finder data failed.{0}{1}".FormatWith(Environment.NewLine, exception.Message),
                    false,
                    (int)this.config.Api.Config.MessageCloseDelay.TotalMilliseconds,
                    (object[] parameters) =>
                    {
                        // Call the request finished event handler.
                        this.OnRequestFinished();
                    });
                // Log the events.
                this.controlLog.Add(this.config.Api.Log(
                    LogEventLevel.Important,
                    LogEventType.Error,
                    "Requesting the CDN Finder data failed. {0}",
                    new object[] { exception.Message },
                    exception
                    ));
            }
        }
        // Public methods.
        /// <summary>
        /// Adds the result of an asynchronous web operation to the collection of web requests.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="result">The result of the web request.</param>
        public AsyncWebOperation AddAsyncWeb(AsyncWebRequest request, AsyncWebResult result)
        {
            // Create the asynchronous web operation.
            AsyncWebOperation operation = new AsyncWebOperation(request, result);

            lock (this.sync)
            {
                // Add the result.
                this.asyncWeb.Add(operation);
            }

            // Return the web operation.
            return operation;
        }
Пример #8
0
        /// <summary>
        /// Begins a new request to the specified Mercury server to upload a traceroute.
        /// </summary>
        /// <param name="uri">The Mercury server URI.</param>
        /// <param name="traceroute">The traceroute to upload.</param>
        /// <param name="callback">The callback method.</param>
        /// <param name="userState">The user state.</param>
        /// <returns>The result of the asynchronous web operation.</returns>
        public IAsyncResult BeginUploadTraceroute(Uri uri, MercuryTraceroute traceroute, AsyncWebRequestCallback callback, object userState = null)
        {
            // Create the request state.
            AsyncWebResult asyncState = new AsyncWebResult(uri, callback, userState);

            // Set the request headers.
            asyncState.Request.Method = "POST";
            asyncState.Request.Accept = "text/html,application/xhtml+xml,application/xml";
            asyncState.Request.ContentType = "application/json;charset=UTF-8";

            // Create the traceroute JSON object.
            JObject obj = new JObject(
                new JProperty("sessionId", traceroute.Id.ToString()),
                new JProperty("srcIp", traceroute.SourceIp != null ? traceroute.SourceIp.ToString() : "none"),
                new JProperty("dstIp", traceroute.DestinationIp.ToString()),
                new JProperty("srcName", traceroute.SourceHostname),
                new JProperty("dstName", traceroute.DestinationHostname),
                new JProperty("hops",
                    new JArray(from hop in traceroute.Hops select new JObject(
                        new JProperty("id", hop.Number.ToString()),
                        new JProperty("ip", hop.Address != null ? hop.Address.ToString() : "none"),
                        new JProperty("asn", hop.AutonomousSystems != null ? new JArray(from asn in hop.AutonomousSystems select asn.ToString()) : new JArray()),
                        new JProperty("rtt", hop.Rtt != null ? new JArray(from rtt in hop.Rtt select rtt.ToString()) : new JArray())
                        )
                    )
                ));

            // Append the send data.
            asyncState.SendData.Append(obj.ToString(), Encoding.UTF8);

            // Begin the request.
            return base.Begin(asyncState);
        }