/// <summary>
 /// An event handler called when the control completes an asynchronous request for a PlanetLab resource.
 /// </summary>
 /// <param name="response">The XML-RPC response.</param>
 /// <param name="state">The request state.</param>
 protected override void OnRequestResult(XmlRpcResponse response, RequestState state)
 {
     // If the request has not failed.
     if ((null == response.Fault) && (null != response.Value))
     {
         try
         {
             // Update the list of PlanetLab slices list for the given response.
             this.slices.CopyFrom(response.Value as XmlRpcArray);
             // Update the list view.
             this.OnUpdateList();
         }
         catch
         {
             // Update the status.
             this.labelStatus.Text = "Refresh failed.";
         }
     }
     else
     {
         // Update the status.
         this.labelStatus.Text = "Refresh failed. {0}".FormatWith(response.Fault);
     }
 }
 /// <summary>
 /// An event handler called when the request completes.
 /// </summary>
 /// <param name="response">The XML-RPC response.</param>
 /// <param name="state">The request state.</param>
 protected override void OnRequestResult(XmlRpcResponse response, RequestState state)
 {
     // Call the base class method.
     base.OnRequestResult(response, state);
     // If the request has not failed.
     if ((null == response.Fault) && (null != response.Value))
     {
         PlList<PlSlice> slices = null;
         // Create a PlanetLab nodes list for the given response.
         try { slices = PlList<PlSlice>.Create(response.Value as XmlRpcArray); }
         catch { }
         // If the nodes count is greater than zero.
         if ((null != slices) && (slices.Count > 0))
         {
             // Display the information for the first slice.
             this.Object = slices[0];
         }
         else
         {
             // Set the slice to null.
             this.Object = null;
         }
     }
 }
 /// <summary>
 /// An event handler called when the control completes an asynchronous request for the list of PlanetLab sites.
 /// </summary>
 /// <param name="response">The XML-RPC response.</param>
 /// <param name="state">The request state.</param>
 private void OnSitesRequestResult(XmlRpcResponse response, RequestState state)
 {
     // If the request has not failed.
     if ((null == response.Fault) && (null != response.Value))
     {
         try
         {
             // Update the list of PlanetLab sites list for the given response.
             this.sites.CopyFrom(response.Value as XmlRpcArray);
             // Update the sites list.
             this.OnUpdateSites();
         }
         catch
         {
             // Update the status.
             this.wizardPageSite.Status = "Refreshing the PlanetLab sites failed.";
         }
     }
     else
     {
         // Update the status.
         this.wizardPageSite.Status = "Refreshing the PlanetLab sites failed. {0}".FormatWith(response.Fault);
     }
 }
 /// <summary>
 /// An event handler called when the control completes an asynchronous request for a PlanetLab resource.
 /// </summary>
 /// <param name="response">The XML-RPC response.</param>
 /// <param name="state">The request state.</param>
 protected virtual void OnRequestResult(XmlRpcResponse response, RequestState state)
 {
     // If the request state is not null.
     if (null != state)
     {
         // If the delegate for this method is not null.
         if (null != state.ActionRequestResult)
         {
             // Call the delegate.
             state.ActionRequestResult(response, state);
         }
     }
 }
 /// <summary>
 /// An event handler called when the control completes an asynchronous request for refreshing the slice.
 /// </summary>
 /// <param name="response">The XML-RPC response.</param>
 /// <param name="state">The request state.</param>
 private void OnRefreshResult(XmlRpcResponse response, RequestState state)
 {
     // If the request has not failed.
     if ((null == response.Fault) && (null != response.Value))
     {
         try
         {
             // Get the response slices.
             XmlRpcArray slices = response.Value as XmlRpcArray;
             // Parse the slice.
             this.slice.Parse(slices.Values[0].Value as XmlRpcStruct);
             // Update the slice information.
             this.OnUpdateSlice();
             // Update the status.
             this.labelStatus.Text = "Ready.";
         }
         catch
         {
             // Update the status.
             this.labelStatus.Text = "Refresh failed.";
         }
     }
     else
     {
         // Update the status.
         this.labelStatus.Text = "Refresh failed. {0}".FormatWith(response.Fault);
     }
 }
 /// <summary>
 /// An event handler called when the control completes an asynchronous request for renewing the slice.
 /// </summary>
 /// <param name="response">The XML-RPC response.</param>
 /// <param name="state">The request state.</param>
 private void OnRenewResult(XmlRpcResponse response, RequestState state)
 {
     // If the request has not failed.
     if ((null == response.Fault) && (null != response.Value))
     {
         // Get the response value.
         int? code = response.Value.AsInt;
         if (code.HasValue ? 1 == code.Value : false)
         {
             this.labelStatus.Text = "Renew succeeded.";
             this.renewSuccess = true;
         }
         else
         {
             this.labelStatus.Text = "Renew failed.";
         }
     }
     else
     {
         this.labelStatus.Text = "Renew failed.";
     }
 }
        /// <summary>
        /// An event handler called when the control completes an asynchronous request for a PlanetLab resource.
        /// </summary>
        /// <param name="response">The XML-RPC response.</param>
        /// <param name="state">The request state.</param>
        protected override void OnRequestResult(XmlRpcResponse response, RequestState state)
        {
            // If the request has not failed.
            if ((null == response.Fault) && (null != response.Value))
            {
                try
                {
                    // Update the list of PlanetLab sites.
                    this.crawler.PlanetLab.Sites.CopyFrom(response.Value as XmlRpcArray);

                    // Log
                    this.crawler.Log.Add(
                        LogEventLevel.Verbose,
                        LogEventType.Success,
                        ControlSites.logSource,
                        "Refreshing the list of PlanetLab sites completed successfully.");
                }
                catch
                {
                    // Log
                    this.crawler.Log.Add(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        ControlSites.logSource,
                        "Refreshing the list of PlanetLab sites failed.");
                }
            }
            else
            {
                // Update the status.
                this.status.Send(ApplicationStatus.StatusType.Normal, "Refreshing the list of PlanetLab sites failed.", Resources.GlobeError_16);
                if (null == response.Fault)
                {
                    // Log
                    this.crawler.Log.Add(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        ControlSites.logSource,
                        "Refreshing the list of PlanetLab sites failed.");
                }
                else
                {
                    // Log
                    this.crawler.Log.Add(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        ControlSites.logSource,
                        "Refreshing the list of PlanetLab sites failed with code {0} ({1}).",
                        new object[] { response.Fault.FaultCode, response.Fault.FaultString });
                }
            }
        }
        /// <summary>
        /// An event handler called when the control completes an asynchronous request for a PlanetLab resource.
        /// </summary>
        /// <param name="response">The XML-RPC response.</param>
        /// <param name="state">The request state.</param>
        protected override void OnRequestResult(XmlRpcResponse response, RequestState state)
        {
            // If the request has not failed.
            if ((null == response.Fault) && (null != response.Value))
            {
                // Get the slices list.
                XmlRpcArray slices = response.Value as XmlRpcArray;

                // If the response list has one element.
                if ((null != slices) && (slices.Values.Length == 1))
                {
                    try
                    {
                        // Update the slice.
                        this.slice.Parse(slices.Values[0].Value as XmlRpcStruct);
                        // Update the slice nodes.
                        this.OnUpdateNodes();
                    }
                    catch
                    {
                        // Update the status.
                        this.labelStatus.Text = "Refresh failed.";
                    }
                }
            }
            else
            {
                // Update the status.
                this.labelStatus.Text = "Refresh failed. {0}".FormatWith(response.Fault);
            }
        }
        /// <summary>
        /// A method called when receiving the response to a slices refresh request.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="state">The request state.</param>
        private void OnRefreshSlicesRequestResult(XmlRpcResponse response, RequestState state)
        {
            // If the request has not failed.
            if ((null == response.Fault) && (null != response.Value))
            {
                // Get the slices array.
                XmlRpcArray slices = response.Value as XmlRpcArray;

                try
                {
                    // Update the list of PlanetLab local slices, filtering by the current person account.
                    this.crawler.PlanetLab.LocalSlices.CopyFrom(slices.Where((XmlRpcValue value) =>
                        {
                            XmlRpcStruct str = value.Value as XmlRpcStruct;
                            if (null == str) return false;

                            XmlRpcMember member = str[PlSlice.Fields.PersonIds.GetName()];
                            if (null == member) return false;

                            XmlRpcArray array = member.Value.Value as XmlRpcArray;
                            if (null == array) return false;

                            return array.Contains(CrawlerConfig.Static.PlanetLabPersonId);
                        }));

                    // Log
                    this.controlLog.Add(this.crawler.Log.Add(
                        LogEventLevel.Verbose,
                        LogEventType.Success,
                        ControlSlices.logSource,
                        "Refreshing the list of PlanetLab slices completed successfully."));
                    // Return.
                    return;
                }
                catch { }
            }

            // Update the status when the operation failed.
            this.status.Send(ApplicationStatus.StatusType.Normal, "Refreshing the list of PlanetLab slices failed.", Resources.GlobeError_16);
            if (null == response.Fault)
            {
                // Log
                this.controlLog.Add(this.crawler.Log.Add(
                    LogEventLevel.Important,
                    LogEventType.Error,
                    ControlSlices.logSource,
                    "Refreshing the list of PlanetLab slices failed."));
            }
            else
            {
                // Log
                this.controlLog.Add(this.crawler.Log.Add(
                    LogEventLevel.Important,
                    LogEventType.Error,
                    ControlSlices.logSource,
                    "Refreshing the list of PlanetLab slices failed with code {0} ({1}).",
                    new object[] { response.Fault.FaultCode, response.Fault.FaultString }));
            }
        }
        /// <summary>
        /// A method called when receiving the response to a remove slice to nodes request.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="state">The request state.</param>
        private void OnRemoveSliceFromNodesRequestResult(XmlRpcResponse response, RequestState state)
        {
            // Convert the request state.
            SliceIdsRequestState requestState = state as SliceIdsRequestState;
            // If the request has not failed.
            if ((null == response.Fault) && (null != response.Value))
            {
                // Get the response value.
                int? code = response.Value.AsInt;
                if (code.HasValue ? 1 == code.Value : false)
                {
                    // Update the status.
                    this.status.Send(
                        ApplicationStatus.StatusType.Normal,
                        "Showing {0} PlanetLab slices.".FormatWith(this.crawler.PlanetLab.LocalSlices.Count),
                        "Removing slice {0} from {1} PlanetLab node{2} succeeded.".FormatWith(requestState.Slice.Id, requestState.Ids.Length, requestState.Ids.Length.PluralSuffix()),
                        Resources.GlobeLab_16,
                        Resources.GlobeSuccess_16);

                    // Log
                    this.controlLog.Add(this.crawler.Log.Add(
                        LogEventLevel.Verbose,
                        LogEventType.Success,
                        ControlSlices.logSource,
                        "Removing slice {0} from {1} PlanetLab node{2} completed successfully.",
                        new object[] { requestState.Slice.Id, requestState.Ids.Length, requestState.Ids.Length.PluralSuffix() }));

                    // Set the request as successful.
                    requestState.Success = true;
                }
                else
                {
                    // Show a dialog.
                    MessageBox.Show(this,
                        "Cannot remove the slice {0} from {1} PlanetLab node{2}. The PlanetLab server responded, however the operation was not successful.".FormatWith(requestState.Slice.Id, requestState.Ids.Length, requestState.Ids.Length.PluralSuffix()),
                        "Remove Slice from Node",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
                    // Update the status.
                    this.status.Send(
                        ApplicationStatus.StatusType.Normal,
                        "Showing {0} PlanetLab slices.".FormatWith(this.crawler.PlanetLab.LocalSlices.Count),
                        "Removing slice {0} from {1} PlanetLab node{2} failed.".FormatWith(requestState.Slice.Id, requestState.Ids.Length, requestState.Ids.Length.PluralSuffix()),
                        Resources.GlobeLab_16,
                        Resources.GlobeWarning_16);
                }
            }
            else
            {
                // Update the status.
                this.status.Send(
                    ApplicationStatus.StatusType.Normal,
                    "Showing {0} PlanetLab slices.".FormatWith(this.crawler.PlanetLab.LocalSlices.Count),
                    "Removing slice {0} from {1} PlanetLab node{2} failed.".FormatWith(requestState.Slice.Id, requestState.Ids.Length, requestState.Ids.Length.PluralSuffix()),
                    Resources.GlobeLab_16,
                    Resources.GlobeError_16);
                if (null == response.Fault)
                {
                    // Log
                    this.controlLog.Add(this.crawler.Log.Add(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        ControlSlices.logSource,
                        "Removing slice {0} from {1} PlanetLab node{2} failed.",
                        new object[] { requestState.Slice.Id, requestState.Ids.Length, requestState.Ids.Length.PluralSuffix() }));
                }
                else
                {
                    // Log
                    this.controlLog.Add(this.crawler.Log.Add(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        ControlSlices.logSource,
                        "Removing slice {0} from {1} PlanetLab node{2} failed with code {3} ({4}).",
                        new object[] { requestState.Slice.Id, requestState.Ids.Length, requestState.Ids.Length.PluralSuffix(), response.Fault.FaultCode, response.Fault.FaultString }));
                }
            }
        }
        /// <summary>
        /// A method called when receiving the response to a refresh slice request.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="state">The request state.</param>
        private void OnRefreshSliceRequestResult(XmlRpcResponse response, RequestState state)
        {
            // Convert the request state.
            SliceRequestState requestState = state as SliceRequestState;
            // Get the slice.
            PlSlice slice = requestState.Slice;
            // If the request has not failed.
            if ((null == response.Fault) && (null != response.Value))
            {
                // Get the slices list.
                XmlRpcArray slices = response.Value as XmlRpcArray;

                // If the response list has one element.
                if ((null != slices) && (slices.Values.Length == 1))
                {
                    // Update the slice.
                    try { slice.Parse(slices.Values[0].Value as XmlRpcStruct); }
                    catch { }
                }
            }
        }
 /// <summary>
 /// An event handler called when the control completes an asynchronous request for a PlanetLab resource.
 /// </summary>
 /// <param name="response">The XML-RPC response.</param>
 /// <param name="state">The request state.</param>
 protected override void OnRequestResult(XmlRpcResponse response, RequestState state)
 {
     // If the response fault component is not null.
     if (response.Fault != null)
     {
         // Update the status.
         this.pictureBox.Image = Resources.GlobeWarning_32;
         this.labelMessage.Text = "Refreshing the PlanetLab information has failed.{0}{1}RPC code: {2}{3}{4}{5})".FormatWith(
             Environment.NewLine,
             Environment.NewLine,
             response.Fault.FaultCode,
             Environment.NewLine,
             Environment.NewLine,
             response.Fault.FaultString);
     }
     else if (response.Value == null)
     {
         // Update the status.
         this.labelMessage.Text = "The requested PlanetLab information is not available.";
     }
 }
        /// <summary>
        /// A method called when receiving the response to a slices refresh request.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="state">The request state.</param>
        private void OnRefreshSliceRequestResult(XmlRpcResponse response, RequestState state)
        {
            // If the request has not failed.
            if ((null == response.Fault) && (null != response.Value))
            {
                // Get the slices array.
                XmlRpcArray slices = response.Value as XmlRpcArray;

                // If the response array has one element.
                if ((null != slices) && (slices.Length == 1))
                {
                    try
                    {
                        // Update the current slice.
                        this.slice.Parse(slices.Values[0].Value as XmlRpcStruct);
                        // Log
                        this.controlLogList.Add(this.config.Log.Add(
                            LogEventLevel.Verbose,
                            LogEventType.Success,
                            ControlSlice.logSource.FormatWith(this.slice.Id),
                            "Refreshing the slice information completed successfully."));
                        // Return.
                        return;
                    }
                    catch { }
                }
            }
            // Update the status.
            this.status.Send(ApplicationStatus.StatusType.Normal, "Refreshing the slice information failed.", Resources.GlobeError_16);
            if (null == response.Fault)
            {
                // Log
                this.controlLogList.Add(this.config.Log.Add(
                    LogEventLevel.Important,
                    LogEventType.Error,
                    ControlSlice.logSource.FormatWith(this.slice.Id),
                    "Refreshing the slice information failed."));
            }
            else
            {
                // Log
                this.controlLogList.Add(this.config.Log.Add(
                    LogEventLevel.Important,
                    LogEventType.Error,
                    ControlSlice.logSource.FormatWith(this.slice.Id),
                    "Refreshing the slice information failed with code {0} ({1}).",
                    new object[] { response.Fault.FaultCode, response.Fault.FaultString }));
            }
        }
        /// <summary>
        /// A method called when receiving the response to a sites refresh request.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="state">The request state.</param>
        private void OnRefreshSitesRequestResult(XmlRpcResponse response, RequestState state)
        {
            // Convert the request state.
            IdsRequestState requestState = state as IdsRequestState;
            // If the request has not failed.
            if ((null == response.Fault) && (null != response.Value))
            {
                // Get the response array.
                XmlRpcArray array = response.Value as XmlRpcArray;

                // Check the array is not null.
                if (null == array)
                {
                    // Update the status.
                    this.status.Send(
                        ApplicationStatus.StatusType.Normal,
                        @"Slice '{0}' has {1} node{2}.".FormatWith(this.slice.Name, this.slice.NodeIds.Length, this.slice.NodeIds.Length.PluralSuffix()),
                        "Refreshing the sites information failed.",
                        Resources.GlobeLab_16,
                        Resources.GlobeError_16);
                    // Log
                    this.controlLogList.Add(this.config.Log.Add(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        ControlSlice.logSource.FormatWith(this.slice.Id),
                        "Refreshing the sites information failed. The received response was empty."));
                    // Return.
                    return;
                }

                // For each value in the response array.
                foreach (XmlRpcValue value in array.Values)
                {
                    // The PlanetLab site.
                    PlSite site = null;

                    // Try parse the structure to a PlanetLab node and add it to the sites list.
                    try { site = this.crawler.PlanetLab.Sites.Add(value.Value as XmlRpcStruct); }
                    catch { }

                    // If the object is null, continue.
                    if (null == site) continue;

                    // Find the list items corresponding to the site.
                    foreach (ListViewItem item in this.listViewNodes.Items.Where((ListViewItem it) =>
                        {
                            // Get the node info.
                            NodeInfo info = it.Tag as NodeInfo;
                            // Check the site ID.
                            return info.SiteId == site.Id;
                        }))
                    {
                        // Get the node info.
                        NodeInfo info = item.Tag as NodeInfo;

                        // If the site has not been set.
                        if (info.Site == null)
                        {
                            // Add a node event handler.
                            site.Changed += this.OnSiteChanged;
                            // Set the site.
                            info.Site = site;
                            // If the item does not have a marker and if the site has coordinates.
                            if ((null == info.Marker) && (null != info.Node) && site.Latitude.HasValue && site.Longitude.HasValue)
                            {
                                // Create a circular marker.
                                marker = new MapBulletMarker(new MapPoint(site.Longitude.Value, site.Latitude.Value));
                                marker.Name = "{0}{1}{2}".FormatWith(info.Node.Hostname, Environment.NewLine, site.Name);
                                // Add the marker to the map.
                                this.mapControl.Markers.Add(marker);
                                // Set the marker.
                                info.Marker = marker;
                            }
                        }
                    }
                }

                // Update the status.
                this.status.Send(
                    ApplicationStatus.StatusType.Normal,
                    @"Slice '{0}' has {1} node{2}.".FormatWith(this.slice.Name, this.slice.NodeIds.Length, this.slice.NodeIds.Length.PluralSuffix()),
                    "Refreshing the sites information completed successfully.",
                    Resources.GlobeLab_16,
                    Resources.GlobeSuccess_16);
                // Log
                this.controlLogList.Add(this.config.Log.Add(
                    LogEventLevel.Verbose,
                    LogEventType.Success,
                    ControlSlice.logSource.FormatWith(this.slice.Id),
                    "Refreshing the sites information completed successfully."));
            }
            else
            {
                // Update the status.
                this.status.Send(
                    ApplicationStatus.StatusType.Normal,
                    @"Slice '{0}' has {1} node{2}.".FormatWith(this.slice.Name, this.slice.NodeIds.Length, this.slice.NodeIds.Length.PluralSuffix()),
                    "Refreshing the sites information failed.",
                    Resources.GlobeLab_16,
                    Resources.GlobeError_16);
                if (null == response.Fault)
                {
                    // Log
                    this.controlLogList.Add(this.config.Log.Add(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        ControlSlice.logSource.FormatWith(this.slice.Id),
                        "Refreshing the sites information failed."));
                }
                else
                {
                    // Log
                    this.controlLogList.Add(this.config.Log.Add(
                        LogEventLevel.Important,
                        LogEventType.Error,
                        ControlSlice.logSource.FormatWith(this.slice.Id),
                        "Refreshing the sites information failed with code {0} ({1}).",
                        new object[] { response.Fault.FaultCode, response.Fault.FaultString }));
                }
            }
        }
        /// <summary>
        /// A method called when completing a PlanetLab request.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="state">The request state.</param>
        protected override void OnRequestResult(XmlRpcResponse response, RequestState state)
        {
            // Enable the validation button.
            this.OnChanged(this, EventArgs.Empty);
            // If the request has not failed.
            if ((null == response.Fault) && (null != response.Value))
            {
                try
                {
                    // Create a new persons list.
                    PlList<PlPerson> persons = new PlList<PlPerson>();
                    // Get the list of PlanetLab persons for the given response.
                    persons.Update(response.Value as XmlRpcArray);

                    // Check the number of persons returned in the response.
                    if (persons.Count == 1)
                    {
                        // If the number of persons is one, set it as the current person.
                        this.OnSetPerson(persons[0]);
                        // Save the credentials.
                        this.OnSaveCredentials(persons, persons[0]);
                    }
                    else if (persons.Count > 1)
                    {
                        // Show the select person dialog.
                        if (this.formSelectPerson.ShowDialog(this, this.textBoxUsername.Text, persons) == DialogResult.OK)
                        {
                            // Set the selected person as the current person.
                            this.OnSetPerson(this.formSelectPerson.Result);
                            // Save the credentials.
                            this.OnSaveCredentials(persons, this.formSelectPerson.Result);
                        }
                        else
                        {
                            // If a list was not selected, clear the selection.
                            this.OnSetPerson(UserState.NotSelected);
                        }
                    }
                    else
                    {
                        // If no results were returned, clear the selection.
                        this.OnSetPerson(UserState.Failed);
                    }
                }
                catch
                {
                    // If an error ocurred, clear the selection.
                    this.OnSetPerson(UserState.Failed);
                }
            }
            // Call the base class method.
            base.OnRequestResult(response, state);
        }