MongoServerAddress.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Net;
  17. using System.Net.Sockets;
  18. using System.Text.RegularExpressions;
  19. using MongoDB.Bson.IO;
  20. namespace MongoDB.Driver
  21. {
  22. /// <summary>
  23. /// The address of a MongoDB server.
  24. /// </summary>
  25. #if NET452
  26. [Serializable]
  27. #endif
  28. public class MongoServerAddress : IEquatable<MongoServerAddress>
  29. {
  30. // private fields
  31. private string _host;
  32. private int _port;
  33. // constructors
  34. /// <summary>
  35. /// Initializes a new instance of MongoServerAddress.
  36. /// </summary>
  37. /// <param name="host">The server's host name.</param>
  38. public MongoServerAddress(string host)
  39. {
  40. _host = host;
  41. _port = 27017;
  42. }
  43. /// <summary>
  44. /// Initializes a new instance of MongoServerAddress.
  45. /// </summary>
  46. /// <param name="host">The server's host name.</param>
  47. /// <param name="port">The server's port number.</param>
  48. public MongoServerAddress(string host, int port)
  49. {
  50. _host = host;
  51. _port = port;
  52. }
  53. // factory methods
  54. /// <summary>
  55. /// Parses a string representation of a server address.
  56. /// </summary>
  57. /// <param name="value">The string representation of a server address.</param>
  58. /// <returns>A new instance of MongoServerAddress initialized with values parsed from the string.</returns>
  59. public static MongoServerAddress Parse(string value)
  60. {
  61. MongoServerAddress address;
  62. if (TryParse(value, out address))
  63. {
  64. return address;
  65. }
  66. else
  67. {
  68. var message = string.Format("'{0}' is not a valid server address.", value);
  69. throw new FormatException(message);
  70. }
  71. }
  72. /// <summary>
  73. /// Tries to parse a string representation of a server address.
  74. /// </summary>
  75. /// <param name="value">The string representation of a server address.</param>
  76. /// <param name="address">The server address (set to null if TryParse fails).</param>
  77. /// <returns>True if the string is parsed succesfully.</returns>
  78. public static bool TryParse(string value, out MongoServerAddress address)
  79. {
  80. // don't throw ArgumentNullException if value is null
  81. if (value != null)
  82. {
  83. Match match = Regex.Match(value, @"^(?<host>(\[[^]]+\]|[^:\[\]]+))(:(?<port>\d+))?$");
  84. if (match.Success)
  85. {
  86. string host = match.Groups["host"].Value;
  87. string portString = match.Groups["port"].Value;
  88. int port = (portString == "") ? 27017 : JsonConvert.ToInt32(portString);
  89. address = new MongoServerAddress(host, port);
  90. return true;
  91. }
  92. }
  93. address = null;
  94. return false;
  95. }
  96. // public properties
  97. /// <summary>
  98. /// Gets the server's host name.
  99. /// </summary>
  100. public string Host
  101. {
  102. get { return _host; }
  103. }
  104. /// <summary>
  105. /// Gets the server's port number.
  106. /// </summary>
  107. public int Port
  108. {
  109. get { return _port; }
  110. }
  111. // public operators
  112. /// <summary>
  113. /// Compares two server addresses.
  114. /// </summary>
  115. /// <param name="lhs">The first address.</param>
  116. /// <param name="rhs">The other address.</param>
  117. /// <returns>True if the two addresses are equal (or both are null).</returns>
  118. public static bool operator ==(MongoServerAddress lhs, MongoServerAddress rhs)
  119. {
  120. return object.Equals(lhs, rhs);
  121. }
  122. /// <summary>
  123. /// Compares two server addresses.
  124. /// </summary>
  125. /// <param name="lhs">The first address.</param>
  126. /// <param name="rhs">The other address.</param>
  127. /// <returns>True if the two addresses are not equal (or one is null and the other is not).</returns>
  128. public static bool operator !=(MongoServerAddress lhs, MongoServerAddress rhs)
  129. {
  130. return !(lhs == rhs);
  131. }
  132. // public methods
  133. /// <summary>
  134. /// Compares two server addresses.
  135. /// </summary>
  136. /// <param name="rhs">The other server address.</param>
  137. /// <returns>True if the two server addresses are equal.</returns>
  138. public bool Equals(MongoServerAddress rhs)
  139. {
  140. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  141. return _host.Equals(rhs._host, StringComparison.OrdinalIgnoreCase) && _port == rhs._port;
  142. }
  143. /// <summary>
  144. /// Compares two server addresses.
  145. /// </summary>
  146. /// <param name="obj">The other server address.</param>
  147. /// <returns>True if the two server addresses are equal.</returns>
  148. public override bool Equals(object obj)
  149. {
  150. return Equals(obj as MongoServerAddress); // works even if obj is null or of a different type
  151. }
  152. /// <summary>
  153. /// Gets the hash code for this object.
  154. /// </summary>
  155. /// <returns>The hash code.</returns>
  156. public override int GetHashCode()
  157. {
  158. // see Effective Java by Joshua Bloch
  159. int hash = 17;
  160. hash = 37 * hash + _host.ToLowerInvariant().GetHashCode();
  161. hash = 37 * hash + _port.GetHashCode();
  162. return hash;
  163. }
  164. /// <summary>
  165. /// Returns a string representation of the server address.
  166. /// </summary>
  167. /// <returns>A string representation of the server address.</returns>
  168. public override string ToString()
  169. {
  170. return string.Format("{0}:{1}", _host, _port);
  171. }
  172. }
  173. }