NeighbourServicesConnector.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using log4net;
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Net;
  33. using System.Reflection;
  34. using System.Text;
  35. using Nini.Config;
  36. using OpenSim.Framework;
  37. using OpenSim.Services.Interfaces;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  41. using System.Net.Http;
  42. using System.Threading;
  43. namespace OpenSim.Services.Connectors
  44. {
  45. public class NeighbourServicesConnector : INeighbourService
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(
  49. MethodBase.GetCurrentMethod().DeclaringType);
  50. protected IGridService m_GridService = null;
  51. public NeighbourServicesConnector()
  52. {
  53. }
  54. public NeighbourServicesConnector(IGridService gridServices)
  55. {
  56. Initialise(gridServices);
  57. }
  58. public virtual void Initialise(IGridService gridServices)
  59. {
  60. m_GridService = gridServices;
  61. }
  62. public virtual GridRegion HelloNeighbour(ulong regionHandle, RegionInfo thisRegion)
  63. {
  64. GridRegion regInfo = m_GridService.GetRegionByHandle(thisRegion.ScopeID, regionHandle);
  65. if ((regInfo != null) &&
  66. // Don't remote-call this instance; that's a startup hickup
  67. !((regInfo.ExternalHostName == thisRegion.ExternalHostName) && (regInfo.HttpPort == thisRegion.HttpPort)))
  68. {
  69. if (!DoHelloNeighbourCall(regInfo, thisRegion))
  70. return null;
  71. }
  72. else
  73. return null;
  74. return regInfo;
  75. }
  76. public bool DoHelloNeighbourCall(GridRegion region, RegionInfo thisRegion)
  77. {
  78. string uri = region.ServerURI + "region/" + thisRegion.RegionID + "/";
  79. //m_log.Debug(" >>> DoHelloNeighbourCall <<< " + uri);
  80. byte[] buffer;
  81. try
  82. {
  83. OSDMap args = thisRegion.PackRegionInfoData();
  84. args["destination_handle"] = OSD.FromString(region.RegionHandle.ToString());
  85. buffer = OSDParser.SerializeJsonToBytes(args);
  86. }
  87. catch (Exception e)
  88. {
  89. m_log.WarnFormat("[NEIGHBOUR SERVICES CONNECTOR]: PackRegionInfoData failed for HelloNeighbour from {0} to {1}. Exception: {2} ",
  90. thisRegion.RegionName, region.RegionName, e.Message);
  91. return false;
  92. }
  93. if(buffer is null || buffer.Length == 0)
  94. return false;
  95. HttpResponseMessage responseMessage = null;
  96. HttpRequestMessage request = null;
  97. HttpClient client = null;
  98. try
  99. {
  100. client = WebUtil.GetNewGlobalHttpClient(10000);
  101. request = new(HttpMethod.Post, uri);
  102. request.Headers.ExpectContinue = false;
  103. request.Headers.TransferEncodingChunked = false;
  104. //if (keepalive)
  105. {
  106. request.Headers.TryAddWithoutValidation("Keep-Alive", "timeout=30, max=10");
  107. request.Headers.TryAddWithoutValidation("Connection", "Keep-Alive");
  108. }
  109. //else
  110. // request.Headers.TryAddWithoutValidation("Connection", "close");
  111. request.Content = new ByteArrayContent(buffer);
  112. request.Content.Headers.TryAddWithoutValidation("Content-Type", "application/json");
  113. request.Content.Headers.TryAddWithoutValidation("Content-Length", buffer.Length.ToString());
  114. //m_log.InfoFormat("[REST COMMS]: Posted HelloNeighbour request to remote sim {0}", uri);
  115. responseMessage = client.Send(request, HttpCompletionOption.ResponseContentRead);
  116. responseMessage.EnsureSuccessStatusCode();
  117. //using StreamReader sr = new(responseMessage.Content.ReadAsStream());
  118. //sr.ReadToEnd(); // just try to read
  119. //string reply = sr.ReadToEnd();
  120. //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply);
  121. return true;
  122. }
  123. catch (Exception e)
  124. {
  125. m_log.WarnFormat(
  126. "[NEIGHBOUR SERVICES CONNECTOR]: Exception on DoHelloNeighbourCall from {0} back to {1}. Exception: {2} ",
  127. region.RegionName, thisRegion.RegionName, e.Message);
  128. }
  129. finally
  130. {
  131. request?.Dispose();
  132. responseMessage?.Dispose();
  133. client?.Dispose();
  134. }
  135. return false;
  136. }
  137. }
  138. }