NeighbourServiceConnector.cs 7.6 KB

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