HGNetworkServersInfo.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2008, Contributors. All rights reserved.
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * * Neither the name of the Organizations nor the names of Individual
  14. * Contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  20. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  22. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  25. * OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System.Net;
  29. namespace OpenSim.Framework
  30. {
  31. public class HGNetworkServersInfo
  32. {
  33. public readonly string LocalAssetServerURI, LocalInventoryServerURI, LocalUserServerURI;
  34. private static HGNetworkServersInfo m_singleton;
  35. public static HGNetworkServersInfo Singleton
  36. {
  37. get { return m_singleton; }
  38. }
  39. public static void Init(string assetserver, string inventoryserver, string userserver)
  40. {
  41. m_singleton = new HGNetworkServersInfo(assetserver, inventoryserver, userserver);
  42. }
  43. private HGNetworkServersInfo(string a, string i, string u)
  44. {
  45. LocalAssetServerURI = ServerURI(a);
  46. LocalInventoryServerURI = ServerURI(i);
  47. LocalUserServerURI = ServerURI(u);
  48. }
  49. public bool IsLocalUser(string userserver)
  50. {
  51. string userServerURI = ServerURI(userserver);
  52. bool ret = (((userServerURI == null) || (userServerURI == "") || (userServerURI == LocalUserServerURI)));
  53. //Console.WriteLine("-------------> HGNetworkServersInfo.IsLocalUser? " + ret + "(userServer=" + userServerURI + "; localuserserver=" + LocalUserServerURI + ")");
  54. return ret;
  55. }
  56. public bool IsLocalUser(UserProfileData userData)
  57. {
  58. if (userData != null)
  59. {
  60. if (userData is ForeignUserProfileData)
  61. return IsLocalUser(((ForeignUserProfileData)userData).UserServerURI);
  62. else
  63. return true;
  64. }
  65. else
  66. // Something fishy; ignore it
  67. return true;
  68. }
  69. public static string ServerURI(string uri)
  70. {
  71. // Get rid of eventual slashes at the end
  72. try
  73. {
  74. if (uri.EndsWith("/"))
  75. uri = uri.Substring(0, uri.Length - 1);
  76. }
  77. catch { }
  78. IPAddress ipaddr1 = null;
  79. string port1 = "";
  80. try
  81. {
  82. ipaddr1 = Util.GetHostFromURL(uri);
  83. }
  84. catch { }
  85. try
  86. {
  87. port1 = uri.Split(new char[] { ':' })[2];
  88. }
  89. catch { }
  90. // We tried our best to convert the domain names to IP addresses
  91. return (ipaddr1 != null) ? "http://" + ipaddr1.ToString() + ":" + port1 : uri;
  92. }
  93. }
  94. }