NeighbourServiceConnector.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.Framework.Servers.HttpServer;
  39. using OpenSim.Services.Interfaces;
  40. using OpenMetaverse;
  41. using OpenMetaverse.StructuredData;
  42. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  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. uint x = 0, y = 0;
  65. Utils.LongToUInts(regionHandle, out x, out y);
  66. GridRegion regInfo = m_GridService.GetRegionByPosition(UUID.Zero, (int)x, (int)y);
  67. if ((regInfo != null) &&
  68. // Don't remote-call this instance; that's a startup hickup
  69. !((regInfo.ExternalHostName == thisRegion.ExternalHostName) && (regInfo.HttpPort == thisRegion.HttpPort)))
  70. {
  71. if (!DoHelloNeighbourCall(regInfo, thisRegion))
  72. return null;
  73. }
  74. else
  75. return null;
  76. return regInfo;
  77. }
  78. public bool DoHelloNeighbourCall(GridRegion region, RegionInfo thisRegion)
  79. {
  80. string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/region/" + thisRegion.RegionID + "/";
  81. //m_log.Debug(" >>> DoHelloNeighbourCall <<< " + uri);
  82. WebRequest HelloNeighbourRequest = WebRequest.Create(uri);
  83. HelloNeighbourRequest.Method = "POST";
  84. HelloNeighbourRequest.ContentType = "application/json";
  85. HelloNeighbourRequest.Timeout = 10000;
  86. // Fill it in
  87. OSDMap args = null;
  88. try
  89. {
  90. args = thisRegion.PackRegionInfoData();
  91. }
  92. catch (Exception e)
  93. {
  94. m_log.Debug("[REST COMMS]: PackRegionInfoData failed with exception: " + e.Message);
  95. return false;
  96. }
  97. // Add the regionhandle of the destination region
  98. args["destination_handle"] = OSD.FromString(region.RegionHandle.ToString());
  99. string strBuffer = "";
  100. byte[] buffer = new byte[1];
  101. try
  102. {
  103. strBuffer = OSDParser.SerializeJsonString(args);
  104. UTF8Encoding str = new UTF8Encoding();
  105. buffer = str.GetBytes(strBuffer);
  106. }
  107. catch (Exception e)
  108. {
  109. m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of HelloNeighbour: {0}", e.Message);
  110. return false;
  111. }
  112. Stream os = null;
  113. try
  114. { // send the Post
  115. HelloNeighbourRequest.ContentLength = buffer.Length; //Count bytes to send
  116. os = HelloNeighbourRequest.GetRequestStream();
  117. os.Write(buffer, 0, strBuffer.Length); //Send it
  118. //m_log.InfoFormat("[REST COMMS]: Posted HelloNeighbour request to remote sim {0}", uri);
  119. }
  120. catch (Exception ex)
  121. {
  122. m_log.InfoFormat("[REST COMMS]: Unable to send HelloNeighbour to {0}: {1}", region.RegionName, ex.Message);
  123. return false;
  124. }
  125. finally
  126. {
  127. if (os != null)
  128. os.Close();
  129. }
  130. // Let's wait for the response
  131. //m_log.Info("[REST COMMS]: Waiting for a reply after DoHelloNeighbourCall");
  132. StreamReader sr = null;
  133. try
  134. {
  135. WebResponse webResponse = HelloNeighbourRequest.GetResponse();
  136. if (webResponse == null)
  137. {
  138. m_log.Info("[REST COMMS]: Null reply on DoHelloNeighbourCall post");
  139. }
  140. sr = new StreamReader(webResponse.GetResponseStream());
  141. //reply = sr.ReadToEnd().Trim();
  142. sr.ReadToEnd().Trim();
  143. //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply);
  144. }
  145. catch (Exception ex)
  146. {
  147. m_log.InfoFormat("[REST COMMS]: exception on reply of DoHelloNeighbourCall {0}", ex.Message);
  148. return false;
  149. }
  150. finally
  151. {
  152. if (sr != null)
  153. sr.Close();
  154. }
  155. return true;
  156. }
  157. }
  158. }