MessageRegionModule.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. using OpenSim.Services.Interfaces;
  42. using OpenSim.Services.Connectors;
  43. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  44. namespace OpenSim.Grid.MessagingServer.Modules
  45. {
  46. public class MessageRegionModule : IMessageRegionLookup
  47. {
  48. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private MessageServerConfig m_cfg;
  50. private IInterServiceUserService m_userServerModule;
  51. private IGridServiceCore m_messageCore;
  52. private IGridService m_GridService;
  53. // a dictionary of all current regions this server knows about
  54. private Dictionary<ulong, RegionProfileData> m_regionInfoCache = new Dictionary<ulong, RegionProfileData>();
  55. public MessageRegionModule(MessageServerConfig config, IGridServiceCore messageCore)
  56. {
  57. m_cfg = config;
  58. m_messageCore = messageCore;
  59. m_GridService = new GridServicesConnector(m_cfg.GridServerURL);
  60. }
  61. public void Initialise()
  62. {
  63. m_messageCore.RegisterInterface<IMessageRegionLookup>(this);
  64. }
  65. public void PostInitialise()
  66. {
  67. IInterServiceUserService messageUserServer;
  68. if (m_messageCore.TryGet<IInterServiceUserService>(out messageUserServer))
  69. {
  70. m_userServerModule = messageUserServer;
  71. }
  72. }
  73. public void RegisterHandlers()
  74. {
  75. //have these in separate method as some servers restart the http server and reregister all the handlers.
  76. }
  77. /// <summary>
  78. /// Gets and caches a RegionInfo object from the gridserver based on regionhandle
  79. /// if the regionhandle is already cached, use the cached values
  80. /// Gets called by lots of threads!!!!!
  81. /// </summary>
  82. /// <param name="regionhandle">handle to the XY of the region we're looking for</param>
  83. /// <returns>A RegionInfo object to stick in the presence info</returns>
  84. public RegionProfileData GetRegionInfo(ulong regionhandle)
  85. {
  86. RegionProfileData regionInfo = null;
  87. lock (m_regionInfoCache)
  88. {
  89. m_regionInfoCache.TryGetValue(regionhandle, out regionInfo);
  90. }
  91. if (regionInfo == null) // not found in cache
  92. {
  93. regionInfo = RequestRegionInfo(regionhandle);
  94. if (regionInfo != null) // lookup was successful
  95. {
  96. lock (m_regionInfoCache)
  97. {
  98. m_regionInfoCache[regionhandle] = regionInfo;
  99. }
  100. }
  101. }
  102. return regionInfo;
  103. }
  104. public int ClearRegionCache()
  105. {
  106. int cachecount = 0;
  107. lock (m_regionInfoCache)
  108. {
  109. cachecount = m_regionInfoCache.Count;
  110. m_regionInfoCache.Clear();
  111. }
  112. return cachecount;
  113. }
  114. /// <summary>
  115. /// Get RegionProfileData from the GridServer.
  116. /// We'll cache this information in GetRegionInfo and use it for presence updates
  117. /// </summary>
  118. /// <param name="regionHandle"></param>
  119. /// <returns></returns>
  120. public RegionProfileData RequestRegionInfo(ulong regionHandle)
  121. {
  122. uint x = 0, y = 0;
  123. Utils.LongToUInts(regionHandle, out x, out y);
  124. GridRegion region = m_GridService.GetRegionByPosition(UUID.Zero, (int)x, (int)y);
  125. if (region != null)
  126. return GridRegionToRegionProfile(region);
  127. else
  128. return null;
  129. }
  130. private RegionProfileData GridRegionToRegionProfile(GridRegion region)
  131. {
  132. RegionProfileData rprofile = new RegionProfileData();
  133. rprofile.httpPort = region.HttpPort;
  134. rprofile.httpServerURI = region.ServerURI;
  135. rprofile.regionLocX = (uint)(region.RegionLocX / Constants.RegionSize);
  136. rprofile.regionLocY = (uint)(region.RegionLocY / Constants.RegionSize);
  137. rprofile.RegionName = region.RegionName;
  138. rprofile.ServerHttpPort = region.HttpPort;
  139. rprofile.ServerIP = region.ExternalHostName;
  140. rprofile.ServerPort = (uint)region.ExternalEndPoint.Port;
  141. rprofile.Uuid = region.RegionID;
  142. return rprofile;
  143. }
  144. public XmlRpcResponse RegionStartup(XmlRpcRequest request, IPEndPoint remoteClient)
  145. {
  146. Hashtable requestData = (Hashtable)request.Params[0];
  147. Hashtable result = new Hashtable();
  148. result["success"] = "FALSE";
  149. if (m_userServerModule.SendToUserServer(requestData, "region_startup"))
  150. result["success"] = "TRUE";
  151. XmlRpcResponse response = new XmlRpcResponse();
  152. response.Value = result;
  153. return response;
  154. }
  155. public XmlRpcResponse RegionShutdown(XmlRpcRequest request, IPEndPoint remoteClient)
  156. {
  157. Hashtable requestData = (Hashtable)request.Params[0];
  158. Hashtable result = new Hashtable();
  159. result["success"] = "FALSE";
  160. if (m_userServerModule.SendToUserServer(requestData, "region_shutdown"))
  161. result["success"] = "TRUE";
  162. XmlRpcResponse response = new XmlRpcResponse();
  163. response.Value = result;
  164. return response;
  165. }
  166. }
  167. }