GridService.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using Nini.Config;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Data;
  36. using OpenSim.Server.Base;
  37. using OpenSim.Services.Interfaces;
  38. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  39. using OpenMetaverse;
  40. namespace OpenSim.Services.GridService
  41. {
  42. public class GridService : GridServiceBase, IGridService
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private bool m_DeleteOnUnregister = true;
  48. private static GridService m_RootInstance = null;
  49. protected IConfigSource m_config;
  50. protected static HypergridLinker m_HypergridLinker;
  51. protected IAuthenticationService m_AuthenticationService = null;
  52. protected bool m_AllowDuplicateNames = false;
  53. protected bool m_AllowHypergridMapSearch = false;
  54. public GridService(IConfigSource config)
  55. : base(config)
  56. {
  57. m_log.DebugFormat("[GRID SERVICE]: Starting...");
  58. m_config = config;
  59. IConfig gridConfig = config.Configs["GridService"];
  60. if (gridConfig != null)
  61. {
  62. m_DeleteOnUnregister = gridConfig.GetBoolean("DeleteOnUnregister", true);
  63. string authService = gridConfig.GetString("AuthenticationService", String.Empty);
  64. if (authService != String.Empty)
  65. {
  66. Object[] args = new Object[] { config };
  67. m_AuthenticationService = ServerUtils.LoadPlugin<IAuthenticationService>(authService, args);
  68. }
  69. m_AllowDuplicateNames = gridConfig.GetBoolean("AllowDuplicateNames", m_AllowDuplicateNames);
  70. m_AllowHypergridMapSearch = gridConfig.GetBoolean("AllowHypergridMapSearch", m_AllowHypergridMapSearch);
  71. }
  72. if (m_RootInstance == null)
  73. {
  74. m_RootInstance = this;
  75. if (MainConsole.Instance != null)
  76. {
  77. MainConsole.Instance.Commands.AddCommand("grid", true,
  78. "show region",
  79. "show region <Region name>",
  80. "Show details on a region",
  81. String.Empty,
  82. HandleShowRegion);
  83. MainConsole.Instance.Commands.AddCommand("grid", true,
  84. "set region flags",
  85. "set region flags <Region name> <flags>",
  86. "Set database flags for region",
  87. String.Empty,
  88. HandleSetFlags);
  89. }
  90. m_HypergridLinker = new HypergridLinker(m_config, this, m_Database);
  91. }
  92. }
  93. #region IGridService
  94. public string RegisterRegion(UUID scopeID, GridRegion regionInfos)
  95. {
  96. IConfig gridConfig = m_config.Configs["GridService"];
  97. // This needs better sanity testing. What if regionInfo is registering in
  98. // overlapping coords?
  99. RegionData region = m_Database.Get(regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID);
  100. if (region != null)
  101. {
  102. // There is a preexisting record
  103. //
  104. // Get it's flags
  105. //
  106. OpenSim.Data.RegionFlags rflags = (OpenSim.Data.RegionFlags)Convert.ToInt32(region.Data["flags"]);
  107. // Is this a reservation?
  108. //
  109. if ((rflags & OpenSim.Data.RegionFlags.Reservation) != 0)
  110. {
  111. // Regions reserved for the null key cannot be taken.
  112. if ((string)region.Data["PrincipalID"] == UUID.Zero.ToString())
  113. return "Region location is reserved";
  114. // Treat it as an auth request
  115. //
  116. // NOTE: Fudging the flags value here, so these flags
  117. // should not be used elsewhere. Don't optimize
  118. // this with the later retrieval of the same flags!
  119. rflags |= OpenSim.Data.RegionFlags.Authenticate;
  120. }
  121. if ((rflags & OpenSim.Data.RegionFlags.Authenticate) != 0)
  122. {
  123. // Can we authenticate at all?
  124. //
  125. if (m_AuthenticationService == null)
  126. return "No authentication possible";
  127. if (!m_AuthenticationService.Verify(new UUID(region.Data["PrincipalID"].ToString()), regionInfos.Token, 30))
  128. return "Bad authentication";
  129. }
  130. }
  131. if ((region != null) && (region.RegionID != regionInfos.RegionID))
  132. {
  133. m_log.WarnFormat("[GRID SERVICE]: Region {0} tried to register in coordinates {1}, {2} which are already in use in scope {3}.",
  134. regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID);
  135. return "Region overlaps another region";
  136. }
  137. if ((region != null) && (region.RegionID == regionInfos.RegionID) &&
  138. ((region.posX != regionInfos.RegionLocX) || (region.posY != regionInfos.RegionLocY)))
  139. {
  140. if ((Convert.ToInt32(region.Data["flags"]) & (int)OpenSim.Data.RegionFlags.NoMove) != 0)
  141. return "Can't move this region";
  142. // Region reregistering in other coordinates. Delete the old entry
  143. m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) was previously registered at {2}-{3}. Deleting old entry.",
  144. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY);
  145. try
  146. {
  147. m_Database.Delete(regionInfos.RegionID);
  148. }
  149. catch (Exception e)
  150. {
  151. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  152. }
  153. }
  154. if (!m_AllowDuplicateNames)
  155. {
  156. List<RegionData> dupe = m_Database.Get(regionInfos.RegionName, scopeID);
  157. if (dupe != null && dupe.Count > 0)
  158. {
  159. foreach (RegionData d in dupe)
  160. {
  161. if (d.RegionID != regionInfos.RegionID)
  162. {
  163. m_log.WarnFormat("[GRID SERVICE]: Region {0} tried to register duplicate name with ID {1}.",
  164. regionInfos.RegionName, regionInfos.RegionID);
  165. return "Duplicate region name";
  166. }
  167. }
  168. }
  169. }
  170. // Everything is ok, let's register
  171. RegionData rdata = RegionInfo2RegionData(regionInfos);
  172. rdata.ScopeID = scopeID;
  173. if (region != null)
  174. {
  175. int oldFlags = Convert.ToInt32(region.Data["flags"]);
  176. if ((oldFlags & (int)OpenSim.Data.RegionFlags.LockedOut) != 0)
  177. return "Region locked out";
  178. oldFlags &= ~(int)OpenSim.Data.RegionFlags.Reservation;
  179. rdata.Data["flags"] = oldFlags.ToString(); // Preserve flags
  180. }
  181. else
  182. {
  183. rdata.Data["flags"] = "0";
  184. if ((gridConfig != null) && rdata.RegionName != string.Empty)
  185. {
  186. int newFlags = 0;
  187. string regionName = rdata.RegionName.Trim().Replace(' ', '_');
  188. newFlags = ParseFlags(newFlags, gridConfig.GetString("DefaultRegionFlags", String.Empty));
  189. newFlags = ParseFlags(newFlags, gridConfig.GetString("Region_" + regionName, String.Empty));
  190. newFlags = ParseFlags(newFlags, gridConfig.GetString("Region_" + rdata.RegionID.ToString(), String.Empty));
  191. rdata.Data["flags"] = newFlags.ToString();
  192. }
  193. }
  194. int flags = Convert.ToInt32(rdata.Data["flags"]);
  195. flags |= (int)OpenSim.Data.RegionFlags.RegionOnline;
  196. rdata.Data["flags"] = flags.ToString();
  197. try
  198. {
  199. rdata.Data["last_seen"] = Util.UnixTimeSinceEpoch();
  200. m_Database.Store(rdata);
  201. }
  202. catch (Exception e)
  203. {
  204. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  205. }
  206. m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) registered successfully at {2}-{3}",
  207. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY);
  208. return String.Empty;
  209. }
  210. public bool DeregisterRegion(UUID regionID)
  211. {
  212. m_log.DebugFormat("[GRID SERVICE]: Region {0} deregistered", regionID);
  213. RegionData region = m_Database.Get(regionID, UUID.Zero);
  214. if (region == null)
  215. return false;
  216. int flags = Convert.ToInt32(region.Data["flags"]);
  217. if (!m_DeleteOnUnregister || (flags & (int)OpenSim.Data.RegionFlags.Persistent) != 0)
  218. {
  219. flags &= ~(int)OpenSim.Data.RegionFlags.RegionOnline;
  220. region.Data["flags"] = flags.ToString();
  221. region.Data["last_seen"] = Util.UnixTimeSinceEpoch();
  222. try
  223. {
  224. m_Database.Store(region);
  225. }
  226. catch (Exception e)
  227. {
  228. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  229. }
  230. return true;
  231. }
  232. return m_Database.Delete(regionID);
  233. }
  234. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  235. {
  236. List<GridRegion> rinfos = new List<GridRegion>();
  237. RegionData region = m_Database.Get(regionID, scopeID);
  238. if (region != null)
  239. {
  240. // Not really? Maybe?
  241. List<RegionData> rdatas = m_Database.Get(region.posX - (int)Constants.RegionSize - 1, region.posY - (int)Constants.RegionSize - 1,
  242. region.posX + (int)Constants.RegionSize + 1, region.posY + (int)Constants.RegionSize + 1, scopeID);
  243. foreach (RegionData rdata in rdatas)
  244. {
  245. if (rdata.RegionID != regionID)
  246. {
  247. int flags = Convert.ToInt32(rdata.Data["flags"]);
  248. if ((flags & (int)Data.RegionFlags.Hyperlink) == 0) // no hyperlinks as neighbours
  249. rinfos.Add(RegionData2RegionInfo(rdata));
  250. }
  251. }
  252. m_log.DebugFormat("[GRID SERVICE]: region {0} has {1} neighbours", region.RegionName, rinfos.Count);
  253. }
  254. else
  255. {
  256. m_log.WarnFormat(
  257. "[GRID SERVICE]: GetNeighbours() called for scope {0}, region {1} but no such region found",
  258. scopeID, regionID);
  259. }
  260. return rinfos;
  261. }
  262. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  263. {
  264. RegionData rdata = m_Database.Get(regionID, scopeID);
  265. if (rdata != null)
  266. return RegionData2RegionInfo(rdata);
  267. return null;
  268. }
  269. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  270. {
  271. int snapX = (int)(x / Constants.RegionSize) * (int)Constants.RegionSize;
  272. int snapY = (int)(y / Constants.RegionSize) * (int)Constants.RegionSize;
  273. RegionData rdata = m_Database.Get(snapX, snapY, scopeID);
  274. if (rdata != null)
  275. return RegionData2RegionInfo(rdata);
  276. return null;
  277. }
  278. public GridRegion GetRegionByName(UUID scopeID, string regionName)
  279. {
  280. List<RegionData> rdatas = m_Database.Get(regionName + "%", scopeID);
  281. if ((rdatas != null) && (rdatas.Count > 0))
  282. return RegionData2RegionInfo(rdatas[0]); // get the first
  283. return null;
  284. }
  285. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  286. {
  287. m_log.DebugFormat("[GRID SERVICE]: GetRegionsByName {0}", name);
  288. List<RegionData> rdatas = m_Database.Get(name + "%", scopeID);
  289. int count = 0;
  290. List<GridRegion> rinfos = new List<GridRegion>();
  291. if (rdatas != null)
  292. {
  293. m_log.DebugFormat("[GRID SERVICE]: Found {0} regions", rdatas.Count);
  294. foreach (RegionData rdata in rdatas)
  295. {
  296. if (count++ < maxNumber)
  297. rinfos.Add(RegionData2RegionInfo(rdata));
  298. }
  299. }
  300. if (m_AllowHypergridMapSearch && (rdatas == null || (rdatas != null && rdatas.Count == 0)) && name.Contains("."))
  301. {
  302. GridRegion r = m_HypergridLinker.LinkRegion(scopeID, name);
  303. if (r != null)
  304. rinfos.Add(r);
  305. }
  306. return rinfos;
  307. }
  308. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  309. {
  310. int xminSnap = (int)(xmin / Constants.RegionSize) * (int)Constants.RegionSize;
  311. int xmaxSnap = (int)(xmax / Constants.RegionSize) * (int)Constants.RegionSize;
  312. int yminSnap = (int)(ymin / Constants.RegionSize) * (int)Constants.RegionSize;
  313. int ymaxSnap = (int)(ymax / Constants.RegionSize) * (int)Constants.RegionSize;
  314. List<RegionData> rdatas = m_Database.Get(xminSnap, yminSnap, xmaxSnap, ymaxSnap, scopeID);
  315. List<GridRegion> rinfos = new List<GridRegion>();
  316. foreach (RegionData rdata in rdatas)
  317. rinfos.Add(RegionData2RegionInfo(rdata));
  318. return rinfos;
  319. }
  320. #endregion
  321. #region Data structure conversions
  322. public RegionData RegionInfo2RegionData(GridRegion rinfo)
  323. {
  324. RegionData rdata = new RegionData();
  325. rdata.posX = (int)rinfo.RegionLocX;
  326. rdata.posY = (int)rinfo.RegionLocY;
  327. rdata.RegionID = rinfo.RegionID;
  328. rdata.RegionName = rinfo.RegionName;
  329. rdata.Data = rinfo.ToKeyValuePairs();
  330. rdata.Data["regionHandle"] = Utils.UIntsToLong((uint)rdata.posX, (uint)rdata.posY);
  331. rdata.Data["owner_uuid"] = rinfo.EstateOwner.ToString();
  332. return rdata;
  333. }
  334. public GridRegion RegionData2RegionInfo(RegionData rdata)
  335. {
  336. GridRegion rinfo = new GridRegion(rdata.Data);
  337. rinfo.RegionLocX = rdata.posX;
  338. rinfo.RegionLocY = rdata.posY;
  339. rinfo.RegionID = rdata.RegionID;
  340. rinfo.RegionName = rdata.RegionName;
  341. rinfo.ScopeID = rdata.ScopeID;
  342. return rinfo;
  343. }
  344. #endregion
  345. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  346. {
  347. List<GridRegion> ret = new List<GridRegion>();
  348. List<RegionData> regions = m_Database.GetDefaultRegions(scopeID);
  349. foreach (RegionData r in regions)
  350. {
  351. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Data.RegionFlags.RegionOnline) != 0)
  352. ret.Add(RegionData2RegionInfo(r));
  353. }
  354. m_log.DebugFormat("[GRID SERVICE]: GetDefaultRegions returning {0} regions", ret.Count);
  355. return ret;
  356. }
  357. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  358. {
  359. List<GridRegion> ret = new List<GridRegion>();
  360. List<RegionData> regions = m_Database.GetFallbackRegions(scopeID, x, y);
  361. foreach (RegionData r in regions)
  362. {
  363. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Data.RegionFlags.RegionOnline) != 0)
  364. ret.Add(RegionData2RegionInfo(r));
  365. }
  366. m_log.DebugFormat("[GRID SERVICE]: Fallback returned {0} regions", ret.Count);
  367. return ret;
  368. }
  369. public List<GridRegion> GetHyperlinks(UUID scopeID)
  370. {
  371. List<GridRegion> ret = new List<GridRegion>();
  372. List<RegionData> regions = m_Database.GetHyperlinks(scopeID);
  373. foreach (RegionData r in regions)
  374. {
  375. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Data.RegionFlags.RegionOnline) != 0)
  376. ret.Add(RegionData2RegionInfo(r));
  377. }
  378. m_log.DebugFormat("[GRID SERVICE]: Hyperlinks returned {0} regions", ret.Count);
  379. return ret;
  380. }
  381. public int GetRegionFlags(UUID scopeID, UUID regionID)
  382. {
  383. RegionData region = m_Database.Get(regionID, scopeID);
  384. if (region != null)
  385. {
  386. int flags = Convert.ToInt32(region.Data["flags"]);
  387. //m_log.DebugFormat("[GRID SERVICE]: Request for flags of {0}: {1}", regionID, flags);
  388. return flags;
  389. }
  390. else
  391. return -1;
  392. }
  393. private void HandleShowRegion(string module, string[] cmd)
  394. {
  395. if (cmd.Length != 3)
  396. {
  397. MainConsole.Instance.Output("Syntax: show region <region name>");
  398. return;
  399. }
  400. List<RegionData> regions = m_Database.Get(cmd[2], UUID.Zero);
  401. if (regions == null || regions.Count < 1)
  402. {
  403. MainConsole.Instance.Output("Region not found");
  404. return;
  405. }
  406. MainConsole.Instance.Output("Region Name Region UUID");
  407. MainConsole.Instance.Output("Location URI");
  408. MainConsole.Instance.Output("Owner ID Flags");
  409. MainConsole.Instance.Output("-------------------------------------------------------------------------------");
  410. foreach (RegionData r in regions)
  411. {
  412. OpenSim.Data.RegionFlags flags = (OpenSim.Data.RegionFlags)Convert.ToInt32(r.Data["flags"]);
  413. MainConsole.Instance.Output(String.Format("{0,-20} {1}\n{2,-20} {3}\n{4,-39} {5}\n\n",
  414. r.RegionName, r.RegionID,
  415. String.Format("{0},{1}", r.posX, r.posY), r.Data["serverURI"],
  416. r.Data["owner_uuid"].ToString(), flags.ToString()));
  417. }
  418. return;
  419. }
  420. private int ParseFlags(int prev, string flags)
  421. {
  422. OpenSim.Data.RegionFlags f = (OpenSim.Data.RegionFlags)prev;
  423. string[] parts = flags.Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
  424. foreach (string p in parts)
  425. {
  426. int val;
  427. try
  428. {
  429. if (p.StartsWith("+"))
  430. {
  431. val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p.Substring(1));
  432. f |= (OpenSim.Data.RegionFlags)val;
  433. }
  434. else if (p.StartsWith("-"))
  435. {
  436. val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p.Substring(1));
  437. f &= ~(OpenSim.Data.RegionFlags)val;
  438. }
  439. else
  440. {
  441. val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p);
  442. f |= (OpenSim.Data.RegionFlags)val;
  443. }
  444. }
  445. catch (Exception)
  446. {
  447. MainConsole.Instance.Output("Error in flag specification: " + p);
  448. }
  449. }
  450. return (int)f;
  451. }
  452. private void HandleSetFlags(string module, string[] cmd)
  453. {
  454. if (cmd.Length < 5)
  455. {
  456. MainConsole.Instance.Output("Syntax: set region flags <region name> <flags>");
  457. return;
  458. }
  459. List<RegionData> regions = m_Database.Get(cmd[3], UUID.Zero);
  460. if (regions == null || regions.Count < 1)
  461. {
  462. MainConsole.Instance.Output("Region not found");
  463. return;
  464. }
  465. foreach (RegionData r in regions)
  466. {
  467. int flags = Convert.ToInt32(r.Data["flags"]);
  468. flags = ParseFlags(flags, cmd[4]);
  469. r.Data["flags"] = flags.ToString();
  470. OpenSim.Data.RegionFlags f = (OpenSim.Data.RegionFlags)flags;
  471. MainConsole.Instance.Output(String.Format("Set region {0} to {1}", r.RegionName, f));
  472. m_Database.Store(r);
  473. }
  474. }
  475. }
  476. }