MessageRegionModule.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Threading;
  33. using System.Timers;
  34. using log4net;
  35. using Nwc.XmlRpc;
  36. using OpenMetaverse;
  37. using OpenSim.Data;
  38. using OpenSim.Framework;
  39. using OpenSim.Grid.Framework;
  40. using Timer = System.Timers.Timer;
  41. namespace OpenSim.Grid.MessagingServer.Modules
  42. {
  43. public class MessageRegionModule : IMessageRegionLookup
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private MessageServerConfig m_cfg;
  47. private IInterServiceUserService m_userServerModule;
  48. private IGridServiceCore m_messageCore;
  49. // a dictionary of all current regions this server knows about
  50. private Dictionary<ulong, RegionProfileData> m_regionInfoCache = new Dictionary<ulong, RegionProfileData>();
  51. public MessageRegionModule(MessageServerConfig config, IGridServiceCore messageCore)
  52. {
  53. m_cfg = config;
  54. m_messageCore = messageCore;
  55. }
  56. public void Initialise()
  57. {
  58. m_messageCore.RegisterInterface<IMessageRegionLookup>(this);
  59. }
  60. public void PostInitialise()
  61. {
  62. IInterServiceUserService messageUserServer;
  63. if (m_messageCore.TryGet<IInterServiceUserService>(out messageUserServer))
  64. {
  65. m_userServerModule = messageUserServer;
  66. }
  67. }
  68. public void RegisterHandlers()
  69. {
  70. //have these in separate method as some servers restart the http server and reregister all the handlers.
  71. }
  72. /// <summary>
  73. /// Gets and caches a RegionInfo object from the gridserver based on regionhandle
  74. /// if the regionhandle is already cached, use the cached values
  75. /// Gets called by lots of threads!!!!!
  76. /// </summary>
  77. /// <param name="regionhandle">handle to the XY of the region we're looking for</param>
  78. /// <returns>A RegionInfo object to stick in the presence info</returns>
  79. public RegionProfileData GetRegionInfo(ulong regionhandle)
  80. {
  81. RegionProfileData regionInfo = null;
  82. lock (m_regionInfoCache)
  83. {
  84. m_regionInfoCache.TryGetValue(regionhandle, out regionInfo);
  85. }
  86. if (regionInfo == null) // not found in cache
  87. {
  88. regionInfo = RequestRegionInfo(regionhandle);
  89. if (regionInfo != null) // lookup was successful
  90. {
  91. lock (m_regionInfoCache)
  92. {
  93. m_regionInfoCache[regionhandle] = regionInfo;
  94. }
  95. }
  96. }
  97. return regionInfo;
  98. }
  99. public int ClearRegionCache()
  100. {
  101. int cachecount = 0;
  102. lock (m_regionInfoCache)
  103. {
  104. cachecount = m_regionInfoCache.Count;
  105. m_regionInfoCache.Clear();
  106. }
  107. return cachecount;
  108. }
  109. /// <summary>
  110. /// Get RegionProfileData from the GridServer.
  111. /// We'll cache this information in GetRegionInfo and use it for presence updates
  112. /// </summary>
  113. /// <param name="regionHandle"></param>
  114. /// <returns></returns>
  115. public RegionProfileData RequestRegionInfo(ulong regionHandle)
  116. {
  117. RegionProfileData regionProfile = null;
  118. try
  119. {
  120. Hashtable requestData = new Hashtable();
  121. requestData["region_handle"] = regionHandle.ToString();
  122. requestData["authkey"] = m_cfg.GridSendKey;
  123. ArrayList SendParams = new ArrayList();
  124. SendParams.Add(requestData);
  125. XmlRpcRequest GridReq = new XmlRpcRequest("simulator_data_request", SendParams);
  126. XmlRpcResponse GridResp = GridReq.Send(m_cfg.GridServerURL, 3000);
  127. Hashtable responseData = (Hashtable)GridResp.Value;
  128. if (responseData.ContainsKey("error"))
  129. {
  130. m_log.Error("[GRID]: error received from grid server" + responseData["error"]);
  131. return null;
  132. }
  133. uint regX = Convert.ToUInt32((string)responseData["region_locx"]);
  134. uint regY = Convert.ToUInt32((string)responseData["region_locy"]);
  135. string internalIpStr = (string)responseData["sim_ip"];
  136. regionProfile = new RegionProfileData();
  137. regionProfile.httpPort = (uint)Convert.ToInt32((string)responseData["http_port"]);
  138. regionProfile.httpServerURI = "http://" + internalIpStr + ":" + regionProfile.httpPort + "/";
  139. regionProfile.regionHandle = Utils.UIntsToLong((regX * Constants.RegionSize), (regY * Constants.RegionSize));
  140. regionProfile.regionLocX = regX;
  141. regionProfile.regionLocY = regY;
  142. regionProfile.remotingPort = Convert.ToUInt32((string)responseData["remoting_port"]);
  143. regionProfile.UUID = new UUID((string)responseData["region_UUID"]);
  144. regionProfile.regionName = (string)responseData["region_name"];
  145. }
  146. catch (WebException)
  147. {
  148. m_log.Error("[GRID]: " +
  149. "Region lookup failed for: " + regionHandle.ToString() +
  150. " - Is the GridServer down?");
  151. }
  152. return regionProfile;
  153. }
  154. public XmlRpcResponse RegionStartup(XmlRpcRequest request, IPEndPoint remoteClient)
  155. {
  156. Hashtable requestData = (Hashtable)request.Params[0];
  157. Hashtable result = new Hashtable();
  158. result["success"] = "FALSE";
  159. if (m_userServerModule.SendToUserServer(requestData, "region_startup"))
  160. result["success"] = "TRUE";
  161. XmlRpcResponse response = new XmlRpcResponse();
  162. response.Value = result;
  163. return response;
  164. }
  165. public XmlRpcResponse RegionShutdown(XmlRpcRequest request, IPEndPoint remoteClient)
  166. {
  167. Hashtable requestData = (Hashtable)request.Params[0];
  168. Hashtable result = new Hashtable();
  169. result["success"] = "FALSE";
  170. if (m_userServerModule.SendToUserServer(requestData, "region_shutdown"))
  171. result["success"] = "TRUE";
  172. XmlRpcResponse response = new XmlRpcResponse();
  173. response.Value = result;
  174. return response;
  175. }
  176. }
  177. }