GridService.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. if (rdata.RegionID != regionID)
  245. {
  246. int flags = Convert.ToInt32(rdata.Data["flags"]);
  247. if ((flags & (int)Data.RegionFlags.Hyperlink) == 0) // no hyperlinks as neighbours
  248. rinfos.Add(RegionData2RegionInfo(rdata));
  249. }
  250. }
  251. m_log.DebugFormat("[GRID SERVICE]: region {0} has {1} neighours", region.RegionName, rinfos.Count);
  252. return rinfos;
  253. }
  254. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  255. {
  256. RegionData rdata = m_Database.Get(regionID, scopeID);
  257. if (rdata != null)
  258. return RegionData2RegionInfo(rdata);
  259. return null;
  260. }
  261. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  262. {
  263. int snapX = (int)(x / Constants.RegionSize) * (int)Constants.RegionSize;
  264. int snapY = (int)(y / Constants.RegionSize) * (int)Constants.RegionSize;
  265. RegionData rdata = m_Database.Get(snapX, snapY, scopeID);
  266. if (rdata != null)
  267. return RegionData2RegionInfo(rdata);
  268. return null;
  269. }
  270. public GridRegion GetRegionByName(UUID scopeID, string regionName)
  271. {
  272. List<RegionData> rdatas = m_Database.Get(regionName + "%", scopeID);
  273. if ((rdatas != null) && (rdatas.Count > 0))
  274. return RegionData2RegionInfo(rdatas[0]); // get the first
  275. return null;
  276. }
  277. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  278. {
  279. m_log.DebugFormat("[GRID SERVICE]: GetRegionsByName {0}", name);
  280. List<RegionData> rdatas = m_Database.Get("%" + name + "%", scopeID);
  281. int count = 0;
  282. List<GridRegion> rinfos = new List<GridRegion>();
  283. if (rdatas != null)
  284. {
  285. m_log.DebugFormat("[GRID SERVICE]: Found {0} regions", rdatas.Count);
  286. foreach (RegionData rdata in rdatas)
  287. {
  288. if (count++ < maxNumber)
  289. rinfos.Add(RegionData2RegionInfo(rdata));
  290. }
  291. }
  292. if (m_AllowHypergridMapSearch && (rdatas == null || (rdatas != null && rdatas.Count == 0)) && name.Contains("."))
  293. {
  294. GridRegion r = m_HypergridLinker.LinkRegion(scopeID, name);
  295. if (r != null)
  296. rinfos.Add(r);
  297. }
  298. return rinfos;
  299. }
  300. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  301. {
  302. int xminSnap = (int)(xmin / Constants.RegionSize) * (int)Constants.RegionSize;
  303. int xmaxSnap = (int)(xmax / Constants.RegionSize) * (int)Constants.RegionSize;
  304. int yminSnap = (int)(ymin / Constants.RegionSize) * (int)Constants.RegionSize;
  305. int ymaxSnap = (int)(ymax / Constants.RegionSize) * (int)Constants.RegionSize;
  306. List<RegionData> rdatas = m_Database.Get(xminSnap, yminSnap, xmaxSnap, ymaxSnap, scopeID);
  307. List<GridRegion> rinfos = new List<GridRegion>();
  308. foreach (RegionData rdata in rdatas)
  309. rinfos.Add(RegionData2RegionInfo(rdata));
  310. return rinfos;
  311. }
  312. #endregion
  313. #region Data structure conversions
  314. public RegionData RegionInfo2RegionData(GridRegion rinfo)
  315. {
  316. RegionData rdata = new RegionData();
  317. rdata.posX = (int)rinfo.RegionLocX;
  318. rdata.posY = (int)rinfo.RegionLocY;
  319. rdata.RegionID = rinfo.RegionID;
  320. rdata.RegionName = rinfo.RegionName;
  321. rdata.Data = rinfo.ToKeyValuePairs();
  322. rdata.Data["regionHandle"] = Utils.UIntsToLong((uint)rdata.posX, (uint)rdata.posY);
  323. rdata.Data["owner_uuid"] = rinfo.EstateOwner.ToString();
  324. return rdata;
  325. }
  326. public GridRegion RegionData2RegionInfo(RegionData rdata)
  327. {
  328. GridRegion rinfo = new GridRegion(rdata.Data);
  329. rinfo.RegionLocX = rdata.posX;
  330. rinfo.RegionLocY = rdata.posY;
  331. rinfo.RegionID = rdata.RegionID;
  332. rinfo.RegionName = rdata.RegionName;
  333. rinfo.ScopeID = rdata.ScopeID;
  334. return rinfo;
  335. }
  336. #endregion
  337. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  338. {
  339. List<GridRegion> ret = new List<GridRegion>();
  340. List<RegionData> regions = m_Database.GetDefaultRegions(scopeID);
  341. foreach (RegionData r in regions)
  342. {
  343. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Data.RegionFlags.RegionOnline) != 0)
  344. ret.Add(RegionData2RegionInfo(r));
  345. }
  346. m_log.DebugFormat("[GRID SERVICE]: GetDefaultRegions returning {0} regions", ret.Count);
  347. return ret;
  348. }
  349. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  350. {
  351. List<GridRegion> ret = new List<GridRegion>();
  352. List<RegionData> regions = m_Database.GetFallbackRegions(scopeID, x, y);
  353. foreach (RegionData r in regions)
  354. {
  355. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Data.RegionFlags.RegionOnline) != 0)
  356. ret.Add(RegionData2RegionInfo(r));
  357. }
  358. m_log.DebugFormat("[GRID SERVICE]: Fallback returned {0} regions", ret.Count);
  359. return ret;
  360. }
  361. public List<GridRegion> GetHyperlinks(UUID scopeID)
  362. {
  363. List<GridRegion> ret = new List<GridRegion>();
  364. List<RegionData> regions = m_Database.GetHyperlinks(scopeID);
  365. foreach (RegionData r in regions)
  366. {
  367. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Data.RegionFlags.RegionOnline) != 0)
  368. ret.Add(RegionData2RegionInfo(r));
  369. }
  370. m_log.DebugFormat("[GRID SERVICE]: Hyperlinks returned {0} regions", ret.Count);
  371. return ret;
  372. }
  373. public int GetRegionFlags(UUID scopeID, UUID regionID)
  374. {
  375. RegionData region = m_Database.Get(regionID, scopeID);
  376. if (region != null)
  377. {
  378. int flags = Convert.ToInt32(region.Data["flags"]);
  379. //m_log.DebugFormat("[GRID SERVICE]: Request for flags of {0}: {1}", regionID, flags);
  380. return flags;
  381. }
  382. else
  383. return -1;
  384. }
  385. private void HandleShowRegion(string module, string[] cmd)
  386. {
  387. if (cmd.Length != 3)
  388. {
  389. MainConsole.Instance.Output("Syntax: show region <region name>");
  390. return;
  391. }
  392. List<RegionData> regions = m_Database.Get(cmd[2], UUID.Zero);
  393. if (regions == null || regions.Count < 1)
  394. {
  395. MainConsole.Instance.Output("Region not found");
  396. return;
  397. }
  398. MainConsole.Instance.Output("Region Name Region UUID");
  399. MainConsole.Instance.Output("Location URI");
  400. MainConsole.Instance.Output("Owner ID Flags");
  401. MainConsole.Instance.Output("-------------------------------------------------------------------------------");
  402. foreach (RegionData r in regions)
  403. {
  404. OpenSim.Data.RegionFlags flags = (OpenSim.Data.RegionFlags)Convert.ToInt32(r.Data["flags"]);
  405. MainConsole.Instance.Output(String.Format("{0,-20} {1}\n{2,-20} {3}\n{4,-39} {5}\n\n",
  406. r.RegionName, r.RegionID,
  407. String.Format("{0},{1}", r.posX, r.posY), "http://" + r.Data["serverIP"].ToString() + ":" + r.Data["serverPort"].ToString(),
  408. r.Data["owner_uuid"].ToString(), flags.ToString()));
  409. }
  410. return;
  411. }
  412. private int ParseFlags(int prev, string flags)
  413. {
  414. OpenSim.Data.RegionFlags f = (OpenSim.Data.RegionFlags)prev;
  415. string[] parts = flags.Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
  416. foreach (string p in parts)
  417. {
  418. int val;
  419. try
  420. {
  421. if (p.StartsWith("+"))
  422. {
  423. val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p.Substring(1));
  424. f |= (OpenSim.Data.RegionFlags)val;
  425. }
  426. else if (p.StartsWith("-"))
  427. {
  428. val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p.Substring(1));
  429. f &= ~(OpenSim.Data.RegionFlags)val;
  430. }
  431. else
  432. {
  433. val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p);
  434. f |= (OpenSim.Data.RegionFlags)val;
  435. }
  436. }
  437. catch (Exception)
  438. {
  439. MainConsole.Instance.Output("Error in flag specification: " + p);
  440. }
  441. }
  442. return (int)f;
  443. }
  444. private void HandleSetFlags(string module, string[] cmd)
  445. {
  446. if (cmd.Length < 5)
  447. {
  448. MainConsole.Instance.Output("Syntax: set region flags <region name> <flags>");
  449. return;
  450. }
  451. List<RegionData> regions = m_Database.Get(cmd[3], UUID.Zero);
  452. if (regions == null || regions.Count < 1)
  453. {
  454. MainConsole.Instance.Output("Region not found");
  455. return;
  456. }
  457. foreach (RegionData r in regions)
  458. {
  459. int flags = Convert.ToInt32(r.Data["flags"]);
  460. flags = ParseFlags(flags, cmd[4]);
  461. r.Data["flags"] = flags.ToString();
  462. OpenSim.Data.RegionFlags f = (OpenSim.Data.RegionFlags)flags;
  463. MainConsole.Instance.Output(String.Format("Set region {0} to {1}", r.RegionName, f));
  464. m_Database.Store(r);
  465. }
  466. }
  467. }
  468. }