LocalBackEndServices.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 OpenSim 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.Reflection;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. namespace OpenSim.Region.Communications.Local
  36. {
  37. public class LocalBackEndServices : IGridServices
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. protected Dictionary<ulong, RegionInfo> m_regions = new Dictionary<ulong, RegionInfo>();
  41. protected Dictionary<ulong, RegionCommsListener> m_regionListeners =
  42. new Dictionary<ulong, RegionCommsListener>();
  43. // private Dictionary<ulong, RegionInfo> m_remoteRegionInfoCache = new Dictionary<ulong, RegionInfo>();
  44. private Dictionary<string, string> m_queuedGridSettings = new Dictionary<string, string>();
  45. public string _gdebugRegionName = String.Empty;
  46. public bool RegionLoginsEnabled
  47. {
  48. get { return m_regionLoginsEnabled; }
  49. set { m_regionLoginsEnabled = value; }
  50. }
  51. private bool m_regionLoginsEnabled;
  52. public bool CheckRegion(string address, uint port)
  53. {
  54. return true;
  55. }
  56. public string gdebugRegionName
  57. {
  58. get { return _gdebugRegionName; }
  59. set { _gdebugRegionName = value; }
  60. }
  61. public string _rdebugRegionName = String.Empty;
  62. public string rdebugRegionName
  63. {
  64. get { return _rdebugRegionName; }
  65. set { _rdebugRegionName = value; }
  66. }
  67. /// <summary>
  68. /// Register a region method with the BackEnd Services.
  69. /// </summary>
  70. /// <param name="regionInfo"></param>
  71. /// <returns></returns>
  72. public RegionCommsListener RegisterRegion(RegionInfo regionInfo)
  73. {
  74. //m_log.Debug("CommsManager - Region " + regionInfo.RegionHandle + " , " + regionInfo.RegionLocX + " , "+ regionInfo.RegionLocY +" is registering");
  75. if (!m_regions.ContainsKey(regionInfo.RegionHandle))
  76. {
  77. //m_log.Debug("CommsManager - Adding Region " + regionInfo.RegionHandle);
  78. m_regions.Add(regionInfo.RegionHandle, regionInfo);
  79. RegionCommsListener regionHost = new RegionCommsListener();
  80. if (m_regionListeners.ContainsKey(regionInfo.RegionHandle))
  81. {
  82. m_log.Error("[INTERREGION STANDALONE]: " +
  83. "Error:Region registered twice as an Events listener for Interregion Communications but not as a listed region. " +
  84. "In Standalone mode this will cause BIG issues. In grid mode, it means a region went down and came back up.");
  85. m_regionListeners.Remove(regionInfo.RegionHandle);
  86. }
  87. m_regionListeners.Add(regionInfo.RegionHandle, regionHost);
  88. return regionHost;
  89. }
  90. else
  91. {
  92. // Already in our list, so the region went dead and restarted.
  93. // don't replace the old regioninfo.. this might be a locking issue.. however we need to
  94. // remove it and let it add normally below or we get extremely strange and intermittant
  95. // connectivity errors.
  96. // Don't change this line below to 'm_regions[regionInfo.RegionHandle] = regionInfo' unless you
  97. // *REALLY* know what you are doing here.
  98. m_regions[regionInfo.RegionHandle] = regionInfo;
  99. m_log.Warn("[INTERREGION STANDALONE]: Region registered twice. Region went down and came back up.");
  100. RegionCommsListener regionHost = new RegionCommsListener();
  101. if (m_regionListeners.ContainsKey(regionInfo.RegionHandle))
  102. {
  103. m_regionListeners.Remove(regionInfo.RegionHandle);
  104. }
  105. m_regionListeners.Add(regionInfo.RegionHandle, regionHost);
  106. return regionHost;
  107. }
  108. }
  109. public bool DeregisterRegion(RegionInfo regionInfo)
  110. {
  111. if (m_regions.ContainsKey(regionInfo.RegionHandle))
  112. {
  113. m_regions.Remove(regionInfo.RegionHandle);
  114. if (m_regionListeners.ContainsKey(regionInfo.RegionHandle))
  115. {
  116. m_regionListeners.Remove(regionInfo.RegionHandle);
  117. }
  118. return true;
  119. }
  120. return false;
  121. }
  122. /// <summary>
  123. /// </summary>
  124. /// <param name="regionInfo"></param>
  125. /// <returns></returns>
  126. public List<SimpleRegionInfo> RequestNeighbours(uint x, uint y)
  127. {
  128. // m_log.Debug("Finding Neighbours to " + regionInfo.RegionHandle);
  129. List<SimpleRegionInfo> neighbours = new List<SimpleRegionInfo>();
  130. foreach (RegionInfo reg in m_regions.Values)
  131. {
  132. // m_log.Debug("CommsManager- RequestNeighbours() checking region " + reg.RegionLocX + " , "+ reg.RegionLocY);
  133. if (reg.RegionLocX != x || reg.RegionLocY != y)
  134. {
  135. //m_log.Debug("CommsManager- RequestNeighbours() - found a different region in list, checking location");
  136. if ((reg.RegionLocX > (x - 2)) && (reg.RegionLocX < (x + 2)))
  137. {
  138. if ((reg.RegionLocY > (y - 2)) && (reg.RegionLocY < (y + 2)))
  139. {
  140. neighbours.Add(reg);
  141. }
  142. }
  143. }
  144. }
  145. return neighbours;
  146. }
  147. /// <summary>
  148. /// Get information about a neighbouring region
  149. /// </summary>
  150. /// <param name="regionHandle"></param>
  151. /// <returns></returns>
  152. public RegionInfo RequestNeighbourInfo(ulong regionHandle)
  153. {
  154. if (m_regions.ContainsKey(regionHandle))
  155. {
  156. return m_regions[regionHandle];
  157. }
  158. return null;
  159. }
  160. /// <summary>
  161. /// Get information about a neighbouring region
  162. /// </summary>
  163. /// <param name="regionHandle"></param>
  164. /// <returns></returns>
  165. public RegionInfo RequestNeighbourInfo(UUID regionID)
  166. {
  167. // TODO add a dictionary for faster lookup
  168. foreach (RegionInfo info in m_regions.Values)
  169. {
  170. if (info.RegionID == regionID)
  171. return info;
  172. }
  173. return null;
  174. }
  175. /// <summary>
  176. /// Get information about the closet region given a region name.
  177. /// </summary>
  178. /// <param name="regionName"></param>
  179. /// <returns></returns>
  180. public RegionInfo RequestClosestRegion(string regionName)
  181. {
  182. foreach (RegionInfo regInfo in m_regions.Values)
  183. {
  184. if (regInfo.RegionName == regionName)
  185. return regInfo;
  186. }
  187. return null;
  188. }
  189. /// <summary>
  190. ///
  191. /// </summary>
  192. /// <param name="minX"></param>
  193. /// <param name="minY"></param>
  194. /// <param name="maxX"></param>
  195. /// <param name="maxY"></param>
  196. /// <returns></returns>
  197. public List<MapBlockData> RequestNeighbourMapBlocks(int minX, int minY, int maxX, int maxY)
  198. {
  199. List<MapBlockData> mapBlocks = new List<MapBlockData>();
  200. foreach (RegionInfo regInfo in m_regions.Values)
  201. {
  202. if (((regInfo.RegionLocX >= minX) && (regInfo.RegionLocX <= maxX)) &&
  203. ((regInfo.RegionLocY >= minY) && (regInfo.RegionLocY <= maxY)))
  204. {
  205. MapBlockData map = new MapBlockData();
  206. map.Name = regInfo.RegionName;
  207. map.X = (ushort) regInfo.RegionLocX;
  208. map.Y = (ushort) regInfo.RegionLocY;
  209. map.WaterHeight = (byte) regInfo.RegionSettings.WaterHeight;
  210. map.MapImageId = regInfo.RegionSettings.TerrainImageID;
  211. map.Agents = 1;
  212. map.RegionFlags = 72458694;
  213. map.Access = 13;
  214. mapBlocks.Add(map);
  215. }
  216. }
  217. return mapBlocks;
  218. }
  219. // This function is only here to keep this class in line with the Grid Interface.
  220. // It never gets called.
  221. public virtual Dictionary<string, string> GetGridSettings()
  222. {
  223. Dictionary<string, string> returnGridSettings = new Dictionary<string, string>();
  224. lock (m_queuedGridSettings)
  225. {
  226. returnGridSettings = m_queuedGridSettings;
  227. m_queuedGridSettings.Clear();
  228. }
  229. return returnGridSettings;
  230. }
  231. public virtual void SetForcefulBanlistsDisallowed()
  232. {
  233. m_queuedGridSettings.Add("allow_forceful_banlines", "FALSE");
  234. }
  235. /// <summary>
  236. /// Is a Sandbox mode method, used by the local Login server to inform a region of a connection user/session
  237. /// </summary>
  238. /// <param name="regionHandle"></param>
  239. /// <param name="loginData"></param>
  240. /// <returns></returns>
  241. public void AddNewSession(ulong regionHandle, Login loginData)
  242. {
  243. AgentCircuitData agent = new AgentCircuitData();
  244. agent.AgentID = loginData.Agent;
  245. agent.firstname = loginData.First;
  246. agent.lastname = loginData.Last;
  247. agent.SessionID = loginData.Session;
  248. agent.SecureSessionID = loginData.SecureSession;
  249. agent.circuitcode = loginData.CircuitCode;
  250. agent.BaseFolder = loginData.BaseFolder;
  251. agent.InventoryFolder = loginData.InventoryFolder;
  252. agent.startpos = loginData.StartPos;
  253. agent.CapsPath = loginData.CapsPath;
  254. if (loginData.Appearance != null)
  255. agent.Appearance = loginData.Appearance;
  256. else
  257. {
  258. m_log.WarnFormat("[INTER]: Appearance not found for {0} {1}. Creating default.", agent.firstname, agent.lastname);
  259. agent.Appearance = new AvatarAppearance();
  260. }
  261. TriggerExpectUser(regionHandle, agent);
  262. }
  263. public void TriggerExpectUser(ulong regionHandle, AgentCircuitData agent)
  264. {
  265. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: Other region is sending child agent our way: " + agent.firstname + " " + agent.lastname);
  266. if (m_regionListeners.ContainsKey(regionHandle))
  267. {
  268. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: FoundLocalRegion To send it to: " + agent.firstname + " " + agent.lastname);
  269. m_regionListeners[regionHandle].TriggerExpectUser(agent);
  270. }
  271. }
  272. public void TriggerLogOffUser(ulong regionHandle, UUID agentID, UUID RegionSecret, string message)
  273. {
  274. if (m_regionListeners.ContainsKey(regionHandle))
  275. {
  276. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: FoundLocalRegion To send it to: " + agent.firstname + " " + agent.lastname);
  277. m_regionListeners[regionHandle].TriggerLogOffUser(agentID, RegionSecret, message);
  278. }
  279. }
  280. public void PingCheckReply(Hashtable respData)
  281. {
  282. foreach (ulong region in m_regions.Keys)
  283. {
  284. Hashtable regData = new Hashtable();
  285. RegionInfo reg = m_regions[region];
  286. regData["status"] = "active";
  287. regData["handle"] = region.ToString();
  288. respData[reg.RegionID.ToString()] = regData;
  289. }
  290. }
  291. public LandData RequestLandData (ulong regionHandle, uint x, uint y)
  292. {
  293. m_log.DebugFormat("[INTERREGION STANDALONE] requests land data in {0}, at {1}, {2}",
  294. regionHandle, x, y);
  295. if (m_regionListeners.ContainsKey(regionHandle))
  296. {
  297. LandData land = m_regionListeners[regionHandle].TriggerGetLandData(x, y);
  298. return land;
  299. }
  300. m_log.Debug("[INTERREGION STANDALONE] didn't find land data locally.");
  301. return null;
  302. }
  303. public List<RegionInfo> RequestNamedRegions (string name, int maxNumber)
  304. {
  305. List<RegionInfo> regions = new List<RegionInfo>();
  306. foreach (RegionInfo info in m_regions.Values)
  307. {
  308. if (info.RegionName.StartsWith(name))
  309. {
  310. regions.Add(info);
  311. if (regions.Count >= maxNumber) break;
  312. }
  313. }
  314. return regions;
  315. }
  316. }
  317. }