LocalBackEndServices.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.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 a neighbouring region
  177. /// </summary>
  178. /// <param name="regionHandle"></param>
  179. /// <returns></returns>
  180. public RegionInfo RequestNeighbourInfo(string name)
  181. {
  182. foreach (RegionInfo info in m_regions.Values)
  183. {
  184. if (info.RegionName == name)
  185. return info;
  186. }
  187. return null;
  188. }
  189. /// <summary>
  190. /// Get information about a neighbouring region
  191. /// </summary>
  192. /// <param name="regionHandle"></param>
  193. /// <returns></returns>
  194. public RegionInfo RequestNeighbourInfo(string host, uint port)
  195. {
  196. foreach (RegionInfo info in m_regions.Values)
  197. {
  198. if ((info.ExternalHostName == host) && (info.HttpPort == port))
  199. return info;
  200. }
  201. return null;
  202. }
  203. /// <summary>
  204. /// Get information about the closet region given a region name.
  205. /// </summary>
  206. /// <param name="regionName"></param>
  207. /// <returns></returns>
  208. public RegionInfo RequestClosestRegion(string regionName)
  209. {
  210. foreach (RegionInfo regInfo in m_regions.Values)
  211. {
  212. if (regInfo.RegionName == regionName)
  213. return regInfo;
  214. }
  215. return null;
  216. }
  217. /// <summary>
  218. ///
  219. /// </summary>
  220. /// <param name="minX"></param>
  221. /// <param name="minY"></param>
  222. /// <param name="maxX"></param>
  223. /// <param name="maxY"></param>
  224. /// <returns></returns>
  225. public List<MapBlockData> RequestNeighbourMapBlocks(int minX, int minY, int maxX, int maxY)
  226. {
  227. List<MapBlockData> mapBlocks = new List<MapBlockData>();
  228. foreach (RegionInfo regInfo in m_regions.Values)
  229. {
  230. if (((regInfo.RegionLocX >= minX) && (regInfo.RegionLocX <= maxX)) &&
  231. ((regInfo.RegionLocY >= minY) && (regInfo.RegionLocY <= maxY)))
  232. {
  233. MapBlockData map = new MapBlockData();
  234. map.Name = regInfo.RegionName;
  235. map.X = (ushort) regInfo.RegionLocX;
  236. map.Y = (ushort) regInfo.RegionLocY;
  237. map.WaterHeight = (byte) regInfo.RegionSettings.WaterHeight;
  238. map.MapImageId = regInfo.RegionSettings.TerrainImageID;
  239. map.Agents = 1;
  240. map.RegionFlags = 72458694;
  241. map.Access = regInfo.AccessLevel;
  242. mapBlocks.Add(map);
  243. }
  244. }
  245. return mapBlocks;
  246. }
  247. // This function is only here to keep this class in line with the Grid Interface.
  248. // It never gets called.
  249. public virtual Dictionary<string, string> GetGridSettings()
  250. {
  251. Dictionary<string, string> returnGridSettings = new Dictionary<string, string>();
  252. lock (m_queuedGridSettings)
  253. {
  254. returnGridSettings = m_queuedGridSettings;
  255. m_queuedGridSettings.Clear();
  256. }
  257. return returnGridSettings;
  258. }
  259. public virtual void SetForcefulBanlistsDisallowed()
  260. {
  261. m_queuedGridSettings.Add("allow_forceful_banlines", "FALSE");
  262. }
  263. /// <summary>
  264. /// Is a Sandbox mode method, used by the local Login server to inform a region of a connection user/session
  265. /// </summary>
  266. /// <param name="regionHandle"></param>
  267. /// <param name="loginData"></param>
  268. /// <returns></returns>
  269. public void AddNewSession(ulong regionHandle, Login loginData)
  270. {
  271. AgentCircuitData agent = new AgentCircuitData();
  272. agent.AgentID = loginData.Agent;
  273. agent.firstname = loginData.First;
  274. agent.lastname = loginData.Last;
  275. agent.SessionID = loginData.Session;
  276. agent.SecureSessionID = loginData.SecureSession;
  277. agent.circuitcode = loginData.CircuitCode;
  278. agent.BaseFolder = loginData.BaseFolder;
  279. agent.InventoryFolder = loginData.InventoryFolder;
  280. agent.startpos = loginData.StartPos;
  281. agent.CapsPath = loginData.CapsPath;
  282. if (loginData.Appearance != null)
  283. agent.Appearance = loginData.Appearance;
  284. else
  285. {
  286. m_log.WarnFormat("[INTER]: Appearance not found for {0} {1}. Creating default.", agent.firstname, agent.lastname);
  287. agent.Appearance = new AvatarAppearance(agent.AgentID);
  288. }
  289. TriggerExpectUser(regionHandle, agent);
  290. }
  291. public void TriggerExpectUser(ulong regionHandle, AgentCircuitData agent)
  292. {
  293. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: Other region is sending child agent our way: " + agent.firstname + " " + agent.lastname);
  294. if (m_regionListeners.ContainsKey(regionHandle))
  295. {
  296. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: FoundLocalRegion To send it to: " + agent.firstname + " " + agent.lastname);
  297. m_regionListeners[regionHandle].TriggerExpectUser(agent);
  298. }
  299. }
  300. public void TriggerLogOffUser(ulong regionHandle, UUID agentID, UUID RegionSecret, string message)
  301. {
  302. if (m_regionListeners.ContainsKey(regionHandle))
  303. {
  304. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: FoundLocalRegion To send it to: " + agent.firstname + " " + agent.lastname);
  305. m_regionListeners[regionHandle].TriggerLogOffUser(agentID, RegionSecret, message);
  306. }
  307. }
  308. public void PingCheckReply(Hashtable respData)
  309. {
  310. foreach (ulong region in m_regions.Keys)
  311. {
  312. Hashtable regData = new Hashtable();
  313. RegionInfo reg = m_regions[region];
  314. regData["status"] = "active";
  315. regData["handle"] = region.ToString();
  316. respData[reg.RegionID.ToString()] = regData;
  317. }
  318. }
  319. public LandData RequestLandData (ulong regionHandle, uint x, uint y)
  320. {
  321. m_log.DebugFormat("[INTERREGION STANDALONE] requests land data in {0}, at {1}, {2}",
  322. regionHandle, x, y);
  323. if (m_regionListeners.ContainsKey(regionHandle))
  324. {
  325. LandData land = m_regionListeners[regionHandle].TriggerGetLandData(x, y);
  326. return land;
  327. }
  328. m_log.Debug("[INTERREGION STANDALONE] didn't find land data locally.");
  329. return null;
  330. }
  331. public List<RegionInfo> RequestNamedRegions (string name, int maxNumber)
  332. {
  333. List<RegionInfo> lowercase_regions = new List<RegionInfo>();
  334. List<RegionInfo> regions = new List<RegionInfo>();
  335. foreach (RegionInfo info in m_regions.Values)
  336. {
  337. // Prioritizes exact match
  338. if (info.RegionName.StartsWith(name))
  339. {
  340. regions.Add(info);
  341. if (regions.Count >= maxNumber) break;
  342. }
  343. // But still saves lower case matches
  344. else if (info.RegionName.ToLower().StartsWith(name))
  345. {
  346. if (lowercase_regions.Count < maxNumber)
  347. {
  348. lowercase_regions.Add(info);
  349. }
  350. }
  351. }
  352. // If no exact matches found, return lowercase matches (libOMV compatiblity)
  353. if (regions.Count == 0 && lowercase_regions.Count != 0)
  354. {
  355. return lowercase_regions;
  356. }
  357. return regions;
  358. }
  359. }
  360. }