NeighbourServiceConnector.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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 IGridServices m_MapService = null;
  50. public NeighbourServicesConnector()
  51. {
  52. }
  53. public NeighbourServicesConnector(IGridServices gridServices)
  54. {
  55. Initialise(gridServices);
  56. }
  57. public virtual void Initialise(IGridServices gridServices)
  58. {
  59. m_MapService = gridServices;
  60. }
  61. public virtual bool HelloNeighbour(ulong regionHandle, RegionInfo thisRegion)
  62. {
  63. RegionInfo regInfo = m_MapService.RequestNeighbourInfo(regionHandle);
  64. if ((regInfo != null) &&
  65. // Don't remote-call this instance; that's a startup hickup
  66. !((regInfo.ExternalHostName == thisRegion.ExternalHostName) && (regInfo.HttpPort == thisRegion.HttpPort)))
  67. {
  68. return DoHelloNeighbourCall(regInfo, thisRegion);
  69. }
  70. //else
  71. // m_log.Warn("[REST COMMS]: Region not found " + regionHandle);
  72. return false;
  73. }
  74. public bool DoHelloNeighbourCall(RegionInfo region, RegionInfo thisRegion)
  75. {
  76. string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/region/" + thisRegion.RegionID + "/";
  77. //m_log.Debug(" >>> DoHelloNeighbourCall <<< " + uri);
  78. WebRequest HelloNeighbourRequest = WebRequest.Create(uri);
  79. HelloNeighbourRequest.Method = "POST";
  80. HelloNeighbourRequest.ContentType = "application/json";
  81. HelloNeighbourRequest.Timeout = 10000;
  82. // Fill it in
  83. OSDMap args = null;
  84. try
  85. {
  86. args = thisRegion.PackRegionInfoData();
  87. }
  88. catch (Exception e)
  89. {
  90. m_log.Debug("[REST COMMS]: PackRegionInfoData failed with exception: " + e.Message);
  91. }
  92. // Add the regionhandle of the destination region
  93. args["destination_handle"] = OSD.FromString(region.RegionHandle.ToString());
  94. string strBuffer = "";
  95. byte[] buffer = new byte[1];
  96. try
  97. {
  98. strBuffer = OSDParser.SerializeJsonString(args);
  99. UTF8Encoding str = new UTF8Encoding();
  100. buffer = str.GetBytes(strBuffer);
  101. }
  102. catch (Exception e)
  103. {
  104. m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of HelloNeighbour: {0}", e.Message);
  105. // ignore. buffer will be empty, caller should check.
  106. }
  107. Stream os = null;
  108. try
  109. { // send the Post
  110. HelloNeighbourRequest.ContentLength = buffer.Length; //Count bytes to send
  111. os = HelloNeighbourRequest.GetRequestStream();
  112. os.Write(buffer, 0, strBuffer.Length); //Send it
  113. os.Close();
  114. //m_log.InfoFormat("[REST COMMS]: Posted HelloNeighbour request to remote sim {0}", uri);
  115. }
  116. //catch (WebException ex)
  117. catch
  118. {
  119. //m_log.InfoFormat("[REST COMMS]: Bad send on HelloNeighbour {0}", ex.Message);
  120. return false;
  121. }
  122. // Let's wait for the response
  123. //m_log.Info("[REST COMMS]: Waiting for a reply after DoHelloNeighbourCall");
  124. try
  125. {
  126. WebResponse webResponse = HelloNeighbourRequest.GetResponse();
  127. if (webResponse == null)
  128. {
  129. m_log.Info("[REST COMMS]: Null reply on DoHelloNeighbourCall post");
  130. }
  131. StreamReader sr = new StreamReader(webResponse.GetResponseStream());
  132. //reply = sr.ReadToEnd().Trim();
  133. sr.ReadToEnd().Trim();
  134. sr.Close();
  135. //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply);
  136. }
  137. catch (WebException ex)
  138. {
  139. m_log.InfoFormat("[REST COMMS]: exception on reply of DoHelloNeighbourCall {0}", ex.Message);
  140. // ignore, really
  141. }
  142. return true;
  143. }
  144. }
  145. }