LocalBackEndServices.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 OpenMetaverse;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. namespace OpenSim.Region.Communications.Local
  36. {
  37. public class LocalBackEndServices : IGridServices, IInterRegionCommunications
  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. //Console.WriteLine("CommsManager - Region " + regionInfo.RegionHandle + " , " + regionInfo.RegionLocX + " , "+ regionInfo.RegionLocY +" is registering");
  75. if (!m_regions.ContainsKey(regionInfo.RegionHandle))
  76. {
  77. //Console.WriteLine("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. // Console.WriteLine("Finding Neighbours to " + regionInfo.RegionHandle);
  129. List<SimpleRegionInfo> neighbours = new List<SimpleRegionInfo>();
  130. foreach (RegionInfo reg in m_regions.Values)
  131. {
  132. // Console.WriteLine("CommsManager- RequestNeighbours() checking region " + reg.RegionLocX + " , "+ reg.RegionLocY);
  133. if (reg.RegionLocX != x || reg.RegionLocY != y)
  134. {
  135. //Console.WriteLine("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. public bool TellRegionToCloseChildConnection(ulong regionHandle, UUID agentID)
  220. {
  221. if (m_regionListeners.ContainsKey(regionHandle))
  222. {
  223. return m_regionListeners[regionHandle].TriggerTellRegionToCloseChildConnection(agentID);
  224. }
  225. return false;
  226. }
  227. public virtual bool RegionUp(SerializableRegionInfo sregion, ulong regionhandle)
  228. {
  229. RegionInfo region = new RegionInfo(sregion);
  230. //region.RegionLocX = sregion.X;
  231. //region.RegionLocY = sregion.Y;
  232. //region.SetEndPoint(sregion.IPADDR, sregion.PORT);
  233. //sregion);
  234. if (m_regionListeners.ContainsKey(regionhandle))
  235. {
  236. return m_regionListeners[regionhandle].TriggerRegionUp(region);
  237. }
  238. return false;
  239. }
  240. public virtual bool ChildAgentUpdate(ulong regionHandle, ChildAgentDataUpdate cAgentData)
  241. {
  242. if (m_regionListeners.ContainsKey(regionHandle))
  243. {
  244. // Console.WriteLine("CommsManager- Informing a region to expect child agent");
  245. m_regionListeners[regionHandle].TriggerChildAgentUpdate(cAgentData);
  246. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: Got Listener trigginering local event: " + agentData.firstname + " " + agentData.lastname);
  247. return true;
  248. }
  249. return false;
  250. }
  251. // This function is only here to keep this class in line with the Grid Interface.
  252. // It never gets called.
  253. public virtual Dictionary<string, string> GetGridSettings()
  254. {
  255. Dictionary<string, string> returnGridSettings = new Dictionary<string, string>();
  256. lock (m_queuedGridSettings)
  257. {
  258. returnGridSettings = m_queuedGridSettings;
  259. m_queuedGridSettings.Clear();
  260. }
  261. return returnGridSettings;
  262. }
  263. public virtual void SetForcefulBanlistsDisallowed()
  264. {
  265. m_queuedGridSettings.Add("allow_forceful_banlines", "FALSE");
  266. }
  267. public bool TriggerRegionUp(RegionInfo region, ulong regionhandle)
  268. {
  269. if (m_regionListeners.ContainsKey(regionhandle))
  270. {
  271. return m_regionListeners[regionhandle].TriggerRegionUp(region);
  272. }
  273. return false;
  274. }
  275. public bool TriggerChildAgentUpdate(ulong regionHandle, ChildAgentDataUpdate cAgentData)
  276. {
  277. if (m_regionListeners.ContainsKey(regionHandle))
  278. {
  279. return m_regionListeners[regionHandle].TriggerChildAgentUpdate(cAgentData);
  280. }
  281. return false;
  282. }
  283. public bool TriggerTellRegionToCloseChildConnection(ulong regionHandle, UUID agentID)
  284. {
  285. if (m_regionListeners.ContainsKey(regionHandle))
  286. {
  287. return m_regionListeners[regionHandle].TriggerTellRegionToCloseChildConnection(agentID);
  288. }
  289. return false;
  290. }
  291. /// <summary>
  292. /// Tell a region to expect a new client connection.
  293. /// </summary>
  294. /// <param name="regionHandle"></param>
  295. /// <param name="agentData"></param>
  296. /// <returns></returns>
  297. public bool InformRegionOfChildAgent(ulong regionHandle, AgentCircuitData agentData)
  298. // TODO: should change from agentCircuitData
  299. {
  300. //Console.WriteLine("CommsManager- Trying to Inform a region to expect child agent");
  301. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: Trying to inform region of child agent: " + agentData.firstname + " " + agentData.lastname);
  302. if (m_regionListeners.ContainsKey(regionHandle))
  303. {
  304. // Console.WriteLine("CommsManager- Informing a region to expect child agent");
  305. m_regionListeners[regionHandle].TriggerExpectUser(agentData);
  306. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: Got Listener trigginering local event: " + agentData.firstname + " " + agentData.lastname);
  307. return true;
  308. }
  309. return false;
  310. }
  311. /// <summary>
  312. /// Tell a region to expect the crossing in of a new prim.
  313. /// </summary>
  314. /// <param name="regionHandle"></param>
  315. /// <param name="primID"></param>
  316. /// <param name="objData"></param>
  317. /// <param name="XMLMethod"></param>
  318. /// <returns></returns>
  319. public bool InformRegionOfPrimCrossing(ulong regionHandle, UUID primID, string objData, int XMLMethod)
  320. {
  321. if (m_regionListeners.ContainsKey(regionHandle))
  322. {
  323. m_regionListeners[regionHandle].TriggerExpectPrim(primID, objData, XMLMethod);
  324. return true;
  325. }
  326. return false;
  327. }
  328. /// <summary>
  329. /// Tell a region to get prepare for an avatar to cross into it.
  330. /// </summary>
  331. /// <param name="regionHandle"></param>
  332. /// <param name="agentID"></param>
  333. /// <param name="position"></param>
  334. /// <returns></returns>
  335. public bool ExpectAvatarCrossing(ulong regionHandle, UUID agentID, Vector3 position, bool isFlying)
  336. {
  337. if (m_regionListeners.ContainsKey(regionHandle))
  338. {
  339. // Console.WriteLine("CommsManager- Informing a region to expect avatar crossing");
  340. m_regionListeners[regionHandle].TriggerExpectAvatarCrossing(agentID, position, isFlying);
  341. return true;
  342. }
  343. return false;
  344. }
  345. public bool ExpectPrimCrossing(ulong regionHandle, UUID primID, Vector3 position, bool isPhysical)
  346. {
  347. if (m_regionListeners.ContainsKey(regionHandle))
  348. {
  349. m_regionListeners[regionHandle].TriggerExpectPrimCrossing(primID, position, isPhysical);
  350. return true;
  351. }
  352. return false;
  353. }
  354. public bool AcknowledgeAgentCrossed(ulong regionHandle, UUID agentId)
  355. {
  356. if (m_regionListeners.ContainsKey(regionHandle))
  357. {
  358. return true;
  359. }
  360. return false;
  361. }
  362. public bool AcknowledgePrimCrossed(ulong regionHandle, UUID primID)
  363. {
  364. if (m_regionListeners.ContainsKey(regionHandle))
  365. {
  366. return true;
  367. }
  368. return false;
  369. }
  370. /// <summary>
  371. /// Is a Sandbox mode method, used by the local Login server to inform a region of a connection user/session
  372. /// </summary>
  373. /// <param name="regionHandle"></param>
  374. /// <param name="loginData"></param>
  375. /// <returns></returns>
  376. public void AddNewSession(ulong regionHandle, Login loginData)
  377. {
  378. AgentCircuitData agent = new AgentCircuitData();
  379. agent.AgentID = loginData.Agent;
  380. agent.firstname = loginData.First;
  381. agent.lastname = loginData.Last;
  382. agent.SessionID = loginData.Session;
  383. agent.SecureSessionID = loginData.SecureSession;
  384. agent.circuitcode = loginData.CircuitCode;
  385. agent.BaseFolder = loginData.BaseFolder;
  386. agent.InventoryFolder = loginData.InventoryFolder;
  387. agent.startpos = loginData.StartPos;
  388. agent.CapsPath = loginData.CapsPath;
  389. TriggerExpectUser(regionHandle, agent);
  390. }
  391. public void TriggerExpectUser(ulong regionHandle, AgentCircuitData agent)
  392. {
  393. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: Other region is sending child agent our way: " + agent.firstname + " " + agent.lastname);
  394. if (m_regionListeners.ContainsKey(regionHandle))
  395. {
  396. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: FoundLocalRegion To send it to: " + agent.firstname + " " + agent.lastname);
  397. m_regionListeners[regionHandle].TriggerExpectUser(agent);
  398. }
  399. }
  400. public void TriggerLogOffUser(ulong regionHandle, UUID agentID, UUID RegionSecret, string message)
  401. {
  402. if (m_regionListeners.ContainsKey(regionHandle))
  403. {
  404. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: FoundLocalRegion To send it to: " + agent.firstname + " " + agent.lastname);
  405. m_regionListeners[regionHandle].TriggerLogOffUser(agentID, RegionSecret, message);
  406. }
  407. }
  408. public void TriggerExpectPrim(ulong regionHandle, UUID primID, string objData, int XMLMethod)
  409. {
  410. if (m_regionListeners.ContainsKey(regionHandle))
  411. {
  412. m_regionListeners[regionHandle].TriggerExpectPrim(primID, objData, XMLMethod);
  413. }
  414. }
  415. public void PingCheckReply(Hashtable respData)
  416. {
  417. foreach (ulong region in m_regions.Keys)
  418. {
  419. Hashtable regData = new Hashtable();
  420. RegionInfo reg = m_regions[region];
  421. regData["status"] = "active";
  422. regData["handle"] = region.ToString();
  423. respData[reg.RegionID.ToString()] = regData;
  424. }
  425. }
  426. public bool TriggerExpectAvatarCrossing(ulong regionHandle, UUID agentID, Vector3 position, bool isFlying)
  427. {
  428. if (m_regionListeners.ContainsKey(regionHandle))
  429. {
  430. return m_regionListeners[regionHandle].TriggerExpectAvatarCrossing(agentID, position, isFlying);
  431. }
  432. return false;
  433. }
  434. public bool TriggerExpectPrimCrossing(ulong regionHandle, UUID primID, Vector3 position, bool isPhysical)
  435. {
  436. if (m_regionListeners.ContainsKey(regionHandle))
  437. {
  438. return
  439. m_regionListeners[regionHandle].TriggerExpectPrimCrossing(primID, position, isPhysical);
  440. }
  441. return false;
  442. }
  443. public bool IncomingChildAgent(ulong regionHandle, AgentCircuitData agentData)
  444. {
  445. // m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: Other local region is sending child agent our way: " + agentData.firstname + " " + agentData.lastname);
  446. if (m_regionListeners.ContainsKey(regionHandle))
  447. {
  448. //m_log.Info("[INTER]: " + rdebugRegionName + ":Local BackEnd: found local region to trigger event on: " + agentData.firstname + " " + agentData.lastname);
  449. TriggerExpectUser(regionHandle, agentData);
  450. return true;
  451. }
  452. return false;
  453. }
  454. public LandData RequestLandData (ulong regionHandle, uint x, uint y)
  455. {
  456. m_log.DebugFormat("[INTERREGION STANDALONE] requests land data in {0}, at {1}, {2}",
  457. regionHandle, x, y);
  458. if (m_regionListeners.ContainsKey(regionHandle))
  459. {
  460. LandData land = m_regionListeners[regionHandle].TriggerGetLandData(x, y);
  461. return land;
  462. }
  463. m_log.Debug("[INTERREGION STANDALONE] didn't find land data locally.");
  464. return null;
  465. }
  466. public List<RegionInfo> RequestNamedRegions (string name, int maxNumber)
  467. {
  468. List<RegionInfo> regions = new List<RegionInfo>();
  469. foreach (RegionInfo info in m_regions.Values)
  470. {
  471. if (info.RegionName.StartsWith(name))
  472. {
  473. regions.Add(info);
  474. if (regions.Count >= maxNumber) break;
  475. }
  476. }
  477. return regions;
  478. }
  479. public List<UUID> InformFriendsInOtherRegion(UUID agentId, ulong destRegionHandle, List<UUID> friends, bool online)
  480. {
  481. // if we get to here, something is wrong: We are in standalone mode, but have users that are not on our server?
  482. m_log.WarnFormat("[INTERREGION STANDALONE] Did find {0} users on a region not on our server: {1} ???",
  483. friends.Count, destRegionHandle);
  484. return new List<UUID>();
  485. }
  486. public bool TriggerTerminateFriend (ulong regionHandle, UUID agentID, UUID exFriendID)
  487. {
  488. // if we get to here, something is wrong: We are in standalone mode, but have users that are not on our server?
  489. m_log.WarnFormat("[INTERREGION STANDALONE] Did find user {0} on a region not on our server: {1} ???",
  490. agentID, regionHandle);
  491. return true;
  492. }
  493. }
  494. }