Connecting to the Cluster

To access the cluster, call the ICluster::Connect method and pass the computer name of the cluster's head node. If you are logged on to the head node, you can use "localhost" as the name; otherwise, you must specify the name of the head node. The name can be the head node's computer name or its fully qualified domain name.

The following C++ example shows how to connect to a cluster. The example assumes that pCluster points to a valid ICluster instance.



  if (ConnectToServer(pCluster, L"localhost"))
  {
    // Do something
  }


// Connect to the specified cluster. Set pClusterName to the name of 
// the cluster's head node.
BOOL ConnectToServer(ICluster* pCluster, LPWSTR pClusterName)
{
  HRESULT hr = S_OK;

  // Connect to the server. 
  hr = pCluster->Connect(_bstr_t(pClusterName));
  if (FAILED(hr))
  {
    wprintf(L"Failed to connect to cluster, %s.\n", pClusterName);
    return FALSE;
  }

  wprintf(L"Connected to cluster, %s.\n", pClusterName);

  return TRUE;
}


The following C# example shows how to connect to a cluster. The example assumes that cluster points to a valid ICluster instance and that the value of nodeName is the name of the head node or "localhost".



    try
    {
        cluster.Connect(nodeName);
    }
    catch
    {
        string message = "Failed to connect to the cluster, " + nodeName + ".\n\n";

        message = message + cluster.ErrorMessage + ".";

        MessageBox.Show(message, "Connection failure");
    }


If you do not know the names of the head nodes in a domain, you can use Active Directory to search for the names, as shown in the following example.



        // Reference System.DirectoryServices;
        private void LoadHeadNodes()
        {
            string dnsName;

            // Bind to current domain
            searchRoot = new DirectoryEntry("LDAP://rootDSE");
            domain = new DirectoryEntry("LDAP://" + searchRoot.Properties["defaultNamingContext"][0]);
            searchRoot.Dispose();

            string domainName = domain.Name.Substring(3);  // DC=name
            lblSelectDomain.Text = "Select the cluster to connect to from the " + domainName + " domain.";

            // Search for head nodes in current domain
            string[] propsToLoad = new string[] { "servicednsname" };
            string filterForAllHeadNodes = "(&(objectClass=ServiceConnectionPoint)(serviceClassName=MicrosoftComputeCluster))";
            searcher = new DirectorySearcher(domain, filterForAllHeadNodes, propsToLoad);

            SearchResultCollection results = searcher.FindAll();

            cbHeadNodes.BeginUpdate();

            // Enumerate head nodes and load list
            foreach (SearchResult result in results)
            {
                int index = 0;

                dnsName = (string)result.Properties["servicednsname"][0];

                // Get the name from the fully qualified path, if qualified.
                if ((index = dnsName.IndexOf('.')) > 0)
                    cbHeadNodes.Items.Add(dnsName.Substring(0, index));
                else
                    cbHeadNodes.Items.Add(dnsName);
            }

            results.Dispose();

            // Get name of local computer
            string localHost = System.Net.Dns.GetHostEntry("LocalHost").HostName;
            string filterForLocalHeadNode = "(&(objectClass=ServiceConnectionPoint)(serviceClassName=MicrosoftComputeCluster)(servicednsname=" + localHost + "))";

            // Change filter string filter on servicednsname too
            searcher.Filter = filterForLocalHeadNode;

            results = searcher.FindAll();

            // If the local computer is a head node, make its entry the selected item.
            if (results.Count > 0)
            {
                dnsName = (string)results[0].Properties["servicednsname"][0];
                cbHeadNodes.SelectedItem = dnsName.Substring(0, dnsName.IndexOf('.'));
            }
            else
            {
                cbHeadNodes.SelectedIndex = 0;
            }

            cbHeadNodes.EndUpdate();

            results.Dispose();
            domain.Dispose();
        }



Related topics

Using CCP

 

 

Show: