NeighbourServicesConnector.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. namespace OpenSim.Services.Connectors
  42. {
  43. public class NeighbourServicesConnector : INeighbourService
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(
  47. MethodBase.GetCurrentMethod().DeclaringType);
  48. protected IGridService m_GridService = null;
  49. public NeighbourServicesConnector()
  50. {
  51. }
  52. public NeighbourServicesConnector(IGridService gridServices)
  53. {
  54. Initialise(gridServices);
  55. }
  56. public virtual void Initialise(IGridService gridServices)
  57. {
  58. m_GridService = gridServices;
  59. }
  60. public virtual GridRegion HelloNeighbour(ulong regionHandle, RegionInfo thisRegion)
  61. {
  62. uint x = 0, y = 0;
  63. Util.RegionHandleToWorldLoc(regionHandle, out x, out y);
  64. GridRegion regInfo = m_GridService.GetRegionByPosition(thisRegion.ScopeID, (int)x, (int)y);
  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. WebRequest helloNeighbourRequest;
  81. try
  82. {
  83. helloNeighbourRequest = WebRequest.Create(uri);
  84. }
  85. catch (Exception e)
  86. {
  87. m_log.Warn(string.Format(
  88. "[NEIGHBOUR SERVICES CONNECTOR]: Unable to parse uri {0} to send HelloNeighbour from {1} to {2}. Exception {3} ",
  89. uri, thisRegion.RegionName, region.RegionName, e.Message), e);
  90. return false;
  91. }
  92. helloNeighbourRequest.Method = "POST";
  93. helloNeighbourRequest.ContentType = "application/json";
  94. helloNeighbourRequest.Timeout = 10000;
  95. // Fill it in
  96. OSDMap args = null;
  97. try
  98. {
  99. args = thisRegion.PackRegionInfoData();
  100. }
  101. catch (Exception e)
  102. {
  103. m_log.Warn(string.Format(
  104. "[NEIGHBOUR SERVICES CONNECTOR]: PackRegionInfoData failed for HelloNeighbour from {0} to {1}. Exception {2} ",
  105. thisRegion.RegionName, region.RegionName, e.Message), e);
  106. return false;
  107. }
  108. // Add the regionhandle of the destination region
  109. args["destination_handle"] = OSD.FromString(region.RegionHandle.ToString());
  110. string strBuffer = "";
  111. byte[] buffer = new byte[1];
  112. try
  113. {
  114. strBuffer = OSDParser.SerializeJsonString(args);
  115. buffer = Util.UTF8NoBomEncoding.GetBytes(strBuffer);
  116. }
  117. catch (Exception e)
  118. {
  119. m_log.Warn(string.Format(
  120. "[NEIGHBOUR SERVICES CONNECTOR]: Exception thrown on serialization of HelloNeighbour from {0} to {1}. Exception {2} ",
  121. thisRegion.RegionName, region.RegionName, e.Message), e);
  122. return false;
  123. }
  124. Stream os = null;
  125. try
  126. { // send the Post
  127. helloNeighbourRequest.ContentLength = buffer.Length; //Count bytes to send
  128. os = helloNeighbourRequest.GetRequestStream();
  129. os.Write(buffer, 0, strBuffer.Length); //Send it
  130. //m_log.InfoFormat("[REST COMMS]: Posted HelloNeighbour request to remote sim {0}", uri);
  131. }
  132. catch (Exception e)
  133. {
  134. // m_log.WarnFormat(
  135. // "[NEIGHBOUR SERVICE CONNCTOR]: Unable to send HelloNeighbour from {0} to {1}. Exception {2}{3}",
  136. // thisRegion.RegionName, region.RegionName, e.Message, e.StackTrace);
  137. return false;
  138. }
  139. finally
  140. {
  141. if (os != null)
  142. os.Dispose();
  143. }
  144. // Let's wait for the response
  145. //m_log.Info("[REST COMMS]: Waiting for a reply after DoHelloNeighbourCall");
  146. try
  147. {
  148. using (WebResponse webResponse = helloNeighbourRequest.GetResponse())
  149. {
  150. if (webResponse == null)
  151. {
  152. m_log.DebugFormat(
  153. "[NEIGHBOUR SERVICES CONNECTOR]: Null reply on DoHelloNeighbourCall post from {0} to {1}",
  154. thisRegion.RegionName, region.RegionName);
  155. }
  156. using (Stream s = webResponse.GetResponseStream())
  157. {
  158. using (StreamReader sr = new StreamReader(s))
  159. {
  160. //reply = sr.ReadToEnd().Trim();
  161. sr.ReadToEnd().Trim();
  162. //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply);
  163. }
  164. }
  165. }
  166. }
  167. catch (Exception e)
  168. {
  169. m_log.Warn(string.Format(
  170. "[NEIGHBOUR SERVICES CONNECTOR]: Exception on reply of DoHelloNeighbourCall from {0} back to {1}. Exception {2} ",
  171. region.RegionName, thisRegion.RegionName, e.Message), e);
  172. return false;
  173. }
  174. return true;
  175. }
  176. }
  177. }