internal static int ParseHostList()

in MySQL.Data/src/Failover/FailoverManager.cs [248:336]


    internal static int ParseHostList(string hierPart, bool isXProtocol, bool connectionDataIsUri = true)
    {
      if (string.IsNullOrWhiteSpace(hierPart)) return -1;

      int hostCount = -1;
      FailoverMethod failoverMethod = FailoverMethod.Random;
      string[] hostArray = null;
      List<FailoverServer> hostList = new List<FailoverServer>();
      hierPart = hierPart.Replace(" ", "");

      if (!hierPart.StartsWith("(") && !hierPart.EndsWith(")"))
      {
        hostArray = hierPart.Split(',');
        if (hostArray.Length == 1)
          return 1;

        foreach (var host in hostArray)
          hostList.Add(ConvertToFailoverServer(host, connectionDataIsUri: connectionDataIsUri));

        hostCount = hostArray.Length;
      }
      else
      {
        string[] groups = hierPart.Split(new string[] { "),(" }, StringSplitOptions.RemoveEmptyEntries);
        bool? allHavePriority = null;
        int defaultPriority = -1;
        foreach (var group in groups)
        {
          // Remove leading parenthesis.
          var normalizedGroup = group;
          if (normalizedGroup.StartsWith("("))
            normalizedGroup = group.Substring(1);

          if (normalizedGroup.EndsWith(")"))
            normalizedGroup = normalizedGroup.Substring(0, normalizedGroup.Length - 1);

          string[] items = normalizedGroup.Split(',');
          string[] keyValuePairs = items[0].Split('=');
          if (keyValuePairs[0].ToLowerInvariant() != "address")
            throw new KeyNotFoundException(string.Format(ResourcesX.KeywordNotFound, "address"));

          string host = keyValuePairs[1];
          if (string.IsNullOrWhiteSpace(host))
            throw new ArgumentNullException("server");

          if (items.Length == 2)
          {
            if (allHavePriority != null && allHavePriority == false)
              throw new ArgumentException(ResourcesX.PriorityForAllOrNoHosts);

            allHavePriority = allHavePriority ?? true;
            keyValuePairs = items[1].Split('=');
            if (keyValuePairs[0].ToLowerInvariant() != "priority")
              throw new KeyNotFoundException(string.Format(ResourcesX.KeywordNotFound, "priority"));

            if (string.IsNullOrWhiteSpace(keyValuePairs[1]))
              throw new ArgumentNullException("priority");

            int priority = -1;
            Int32.TryParse(keyValuePairs[1], out priority);
            if (priority < 0 || priority > 100)
              throw new ArgumentException(ResourcesX.PriorityOutOfLimits);

            if (isXProtocol)
              hostList.Add(ConvertToFailoverServer(BaseSession.IsUnixSocket(host) ? BaseSession.NormalizeUnixSocket(host) : host, priority, connectionDataIsUri: connectionDataIsUri));
            else
              hostList.Add(ConvertToFailoverServer(host, priority));
          }
          else
          {
            if (allHavePriority != null && allHavePriority == true)
              throw new ArgumentException(ResourcesX.PriorityForAllOrNoHosts);

            allHavePriority = allHavePriority ?? false;

            hostList.Add(ConvertToFailoverServer(host, defaultPriority, connectionDataIsUri: connectionDataIsUri));
          }
        }

        hostCount = groups.Length;
        if (hostList.GroupBy(h => h.Priority).ToList().Count > 1)
          failoverMethod = FailoverMethod.Priority;
        else
          failoverMethod = FailoverMethod.Random;
      }

      SetHostList(hostList, failoverMethod);
      return hostCount;
    }