Exemple #1
0
        /// <summary>
        /// Expands the drop down list of the ComboBox to display available servers and endpoints.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void UrlCB_DropDown(object sender, EventArgs e)
        {
            // get discovery url
            string sUrl;

            // Check the text property of the Server textbox
            if (NodeTB.Text.Length == 0)
            {
                // Set the uri of the local discovery server by default.
                sUrl = "opc.tcp://localhost:4840";
            }
            else
            {
                // Has the port been entered by the user?
                char     seperator    = ':';
                string[] strPortCheck = NodeTB.Text.Split(seperator);
                if (strPortCheck.Length > 1)
                {
                    sUrl = NodeTB.Text;
                }
                else
                {
                    sUrl = NodeTB.Text + ":4840";
                }
            }

            // Set wait cursor.
            Cursor = Cursors.WaitCursor;

            // Clear all items of the ComboBox.
            UrlCB.Items.Clear();
            UrlCB.Text = "";

            // Look for servers
            List <ApplicationDescription> serverList = null;

            using (Discovery discovery = new Discovery(m_Application))
            {
                try
                {
                    serverList = discovery.FindServers(sUrl);
                }
                catch (Exception exception)
                {
                    // Update status label.
                    toolStripStatusLabel.Text  = "FindServers failed:" + exception.Message;
                    toolStripStatusLabel.Image = RabbitMQ_SendClient.Properties.Resources.error;
                    // Set default cursor.
                    Cursor = Cursors.Default;
                    return;
                }

                bool   bGetEndpointsError = false;
                bool   bIgnoreError       = false;
                string errorText          = "";
                List <EndpointWrapper> lstEndpointWrappers = new List <EndpointWrapper>();

                // Populate the drop down list with the endpoints for the available servers.
                foreach (ApplicationDescription server in serverList)
                {
                    if (server.ApplicationType == ApplicationType.Client || server.ApplicationType == ApplicationType.DiscoveryServer)
                    {
                        continue;
                    }

                    try
                    {
                        StringCollection lstEndpoint = new StringCollection();

                        foreach (string discoveryUrl in server.DiscoveryUrls)
                        {
                            // Call GetEndpoints
                            List <EndpointDescription> lstEndpoints = null;

                            try
                            {
                                lstEndpoints = discovery.GetEndpoints(discoveryUrl);

                                foreach (EndpointDescription endpoint in lstEndpoints)
                                {
                                    // build display name for combo
                                    EndpointWrapper endpointWrap = new EndpointWrapper(endpoint);
                                    if (!lstEndpoint.Contains(endpointWrap.ToString()))
                                    {
                                        lstEndpointWrappers.Add(endpointWrap);
                                        lstEndpoint.Add(endpointWrap.ToString());
                                        bIgnoreError = true;
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        // Update status label.
                        errorText          = "GetEndpoints failed. Error: " + exception.Message;
                        bGetEndpointsError = true;
                    }
                }

                // error occured during get endpoints
                if (bGetEndpointsError && !bIgnoreError)
                {
                    // Update status label.
                    toolStripStatusLabel.Text  = "GetEndpoints failed. Error: " + errorText;
                    toolStripStatusLabel.Image = RabbitMQ_SendClient.Properties.Resources.error;
                }
                // add list of endpoints
                else
                {
                    UrlCB.Items.AddRange(lstEndpointWrappers.ToArray());
                }
            }

            // Set default cursor.
            Cursor = Cursors.Default;
        }
Exemple #2
0
        /// <summary>
        /// Connect to server.
        /// </summary>
        private int Connect()
        {
            if (m_Session == null)
            {
                m_Session = new Session(m_Application);
                m_Session.UseDnsNameAndPortFromDiscoveryUrl = true;

                // Attach to events
                m_Session.ConnectionStatusUpdate += new ServerConnectionStatusUpdateEventHandler(Session_ServerConnectionStatusUpdate);
            }

            // Check the content of the combobox.
            if (UrlCB.Text.Length == 0)
            {
                return(-1);
            }

            // Set wait cursor.
            Cursor = Cursors.WaitCursor;
            int result = 0;

            try
            {
                string endpointUrl;

                // Extract Url from combobox text.
                object item = UrlCB.SelectedItem;
                if ((item == null) || (item.GetType() == typeof(string)))
                {
                    // The URL has been entered as text.
                    endpointUrl = UrlCB.Text;

                    // Call connect with URL
                    m_Session.Connect(endpointUrl, SecuritySelection.None);
                }
                else
                {
                    // The endpoint was provided through discovery.
                    EndpointWrapper endpoint = (EndpointWrapper)item;

                    // Call connect with endpoint
                    m_Session.Connect(endpoint.Endpoint, null);
                }
            }
            catch (Exception e)
            {
                result = -1;

                // Update status label.
                StatusException se = e as StatusException;

                if (se != null)
                {
                    toolStripStatusLabel.Text = String.Concat("Connect failed. Error [", se.StatusCode.ToString(), "] ", e.Message);
                }
                else
                {
                    toolStripStatusLabel.Text = "Connect failed. Error: " + e.Message;
                }

                toolStripStatusLabel.Image = RabbitMQ_SendClient.Properties.Resources.error;
            }

            // Set default cursor.
            Cursor = Cursors.Default;
            return(result);
        }