GridManager.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. */
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Reflection;
  32. using System.Xml;
  33. using libsecondlife;
  34. using Nwc.XmlRpc;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Framework.Data;
  38. using OpenSim.Framework.Servers;
  39. namespace OpenSim.Grid.GridServer
  40. {
  41. internal class GridManager
  42. {
  43. private Dictionary<string, IGridData> _plugins = new Dictionary<string, IGridData>();
  44. private Dictionary<string, ILogData> _logplugins = new Dictionary<string, ILogData>();
  45. // This is here so that the grid server can hand out MessageServer settings to regions on registration
  46. private List<MessageServerInfo> _MessageServers = new List<MessageServerInfo>();
  47. public GridConfig config;
  48. /// <summary>
  49. /// Adds a new grid server plugin - grid servers will be requested in the order they were loaded.
  50. /// </summary>
  51. /// <param name="FileName">The filename to the grid server plugin DLL</param>
  52. public void AddPlugin(string FileName)
  53. {
  54. MainLog.Instance.Verbose("DATA", "Attempting to load " + FileName);
  55. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  56. MainLog.Instance.Verbose("DATA", "Found " + pluginAssembly.GetTypes().Length + " interfaces.");
  57. foreach (Type pluginType in pluginAssembly.GetTypes())
  58. {
  59. if (!pluginType.IsAbstract)
  60. {
  61. // Regions go here
  62. Type typeInterface = pluginType.GetInterface("IGridData", true);
  63. if (typeInterface != null)
  64. {
  65. IGridData plug =
  66. (IGridData) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  67. plug.Initialise();
  68. _plugins.Add(plug.getName(), plug);
  69. MainLog.Instance.Verbose("DATA", "Added IGridData Interface");
  70. }
  71. typeInterface = null;
  72. // Logs go here
  73. typeInterface = pluginType.GetInterface("ILogData", true);
  74. if (typeInterface != null)
  75. {
  76. ILogData plug =
  77. (ILogData) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  78. plug.Initialise();
  79. _logplugins.Add(plug.getName(), plug);
  80. MainLog.Instance.Verbose("DATA", "Added ILogData Interface");
  81. }
  82. typeInterface = null;
  83. }
  84. }
  85. pluginAssembly = null;
  86. }
  87. /// <summary>
  88. /// Logs a piece of information to the database
  89. /// </summary>
  90. /// <param name="target">What you were operating on (in grid server, this will likely be the region UUIDs)</param>
  91. /// <param name="method">Which method is being called?</param>
  92. /// <param name="args">What arguments are being passed?</param>
  93. /// <param name="priority">How high priority is this? 1 = Max, 6 = Verbose</param>
  94. /// <param name="message">The message to log</param>
  95. private void logToDB(string target, string method, string args, int priority, string message)
  96. {
  97. foreach (KeyValuePair<string, ILogData> kvp in _logplugins)
  98. {
  99. try
  100. {
  101. kvp.Value.saveLog("Gridserver", target, method, args, priority, message);
  102. }
  103. catch (Exception)
  104. {
  105. MainLog.Instance.Warn("storage", "Unable to write log via " + kvp.Key);
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// Returns a region by argument
  111. /// </summary>
  112. /// <param name="uuid">A UUID key of the region to return</param>
  113. /// <returns>A SimProfileData for the region</returns>
  114. public RegionProfileData getRegion(LLUUID uuid)
  115. {
  116. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  117. {
  118. try
  119. {
  120. return kvp.Value.GetProfileByLLUUID(uuid);
  121. }
  122. catch (Exception e)
  123. {
  124. MainLog.Instance.Warn("storage", "getRegion - " + e.Message);
  125. }
  126. }
  127. return null;
  128. }
  129. /// <summary>
  130. /// Returns a region by argument
  131. /// </summary>
  132. /// <param name="uuid">A regionHandle of the region to return</param>
  133. /// <returns>A SimProfileData for the region</returns>
  134. public RegionProfileData getRegion(ulong handle)
  135. {
  136. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  137. {
  138. try
  139. {
  140. return kvp.Value.GetProfileByHandle(handle);
  141. }
  142. catch
  143. {
  144. MainLog.Instance.Warn("storage", "Unable to find region " + handle.ToString() + " via " + kvp.Key);
  145. }
  146. }
  147. return null;
  148. }
  149. public Dictionary<ulong, RegionProfileData> getRegions(uint xmin, uint ymin, uint xmax, uint ymax)
  150. {
  151. Dictionary<ulong, RegionProfileData> regions = new Dictionary<ulong, RegionProfileData>();
  152. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  153. {
  154. try
  155. {
  156. RegionProfileData[] neighbours = kvp.Value.GetProfilesInRange(xmin, ymin, xmax, ymax);
  157. foreach (RegionProfileData neighbour in neighbours)
  158. {
  159. regions[neighbour.regionHandle] = neighbour;
  160. }
  161. }
  162. catch
  163. {
  164. MainLog.Instance.Warn("storage", "Unable to query regionblock via " + kvp.Key);
  165. }
  166. }
  167. return regions;
  168. }
  169. /// <summary>
  170. /// Returns a XML String containing a list of the neighbouring regions
  171. /// </summary>
  172. /// <param name="reqhandle">The regionhandle for the center sim</param>
  173. /// <returns>An XML string containing neighbour entities</returns>
  174. public string GetXMLNeighbours(ulong reqhandle)
  175. {
  176. string response = "";
  177. RegionProfileData central_region = getRegion(reqhandle);
  178. RegionProfileData neighbour;
  179. for (int x = -1; x < 2; x++)
  180. for (int y = -1; y < 2; y++)
  181. {
  182. if (
  183. getRegion(
  184. Util.UIntsToLong((uint) ((central_region.regionLocX + x)*256),
  185. (uint) (central_region.regionLocY + y)*256)) != null)
  186. {
  187. neighbour =
  188. getRegion(
  189. Util.UIntsToLong((uint) ((central_region.regionLocX + x)*256),
  190. (uint) (central_region.regionLocY + y)*256));
  191. response += "<neighbour>";
  192. response += "<sim_ip>" + neighbour.serverIP + "</sim_ip>";
  193. response += "<sim_port>" + neighbour.serverPort.ToString() + "</sim_port>";
  194. response += "<locx>" + neighbour.regionLocX.ToString() + "</locx>";
  195. response += "<locy>" + neighbour.regionLocY.ToString() + "</locy>";
  196. response += "<regionhandle>" + neighbour.regionHandle.ToString() + "</regionhandle>";
  197. response += "</neighbour>";
  198. }
  199. }
  200. return response;
  201. }
  202. /// <summary>
  203. /// Performed when a region connects to the grid server initially.
  204. /// </summary>
  205. /// <param name="request">The XMLRPC Request</param>
  206. /// <returns>Startup parameters</returns>
  207. public XmlRpcResponse XmlRpcSimulatorLoginMethod(XmlRpcRequest request)
  208. {
  209. XmlRpcResponse response = new XmlRpcResponse();
  210. Hashtable responseData = new Hashtable();
  211. response.Value = responseData;
  212. RegionProfileData TheSim = null;
  213. RegionProfileData OldSim = null;
  214. Hashtable requestData = (Hashtable)request.Params[0];
  215. string myword;
  216. if (requestData.ContainsKey("UUID"))
  217. {
  218. TheSim = getRegion(new LLUUID((string)requestData["UUID"]));
  219. // logToDB((new LLUUID((string)requestData["UUID"])).ToString(),"XmlRpcSimulatorLoginMethod","", 5,"Region attempting login with UUID.");
  220. }
  221. else if (requestData.ContainsKey("region_handle"))
  222. {
  223. // TheSim = getRegion((ulong)Convert.ToUInt64(requestData["region_handle"]));
  224. // logToDB((string)requestData["region_handle"], "XmlRpcSimulatorLoginMethod", "", 5, "Region attempting login with regionHandle.");
  225. }
  226. else
  227. {
  228. responseData["error"] = "No UUID or region_handle passed to grid server - unable to connect you";
  229. return response;
  230. }
  231. if (TheSim == null) // Shouldnt this be in the REST Simulator Set method?
  232. {
  233. Console.WriteLine("NEW SIM");
  234. myword = "creation";
  235. }
  236. else
  237. {
  238. myword = "connection";
  239. }
  240. TheSim = new RegionProfileData();
  241. TheSim.regionRecvKey = config.SimRecvKey;
  242. TheSim.regionSendKey = config.SimSendKey;
  243. TheSim.regionSecret = config.SimRecvKey;
  244. TheSim.regionDataURI = "";
  245. TheSim.regionAssetURI = config.DefaultAssetServer;
  246. TheSim.regionAssetRecvKey = config.AssetRecvKey;
  247. TheSim.regionAssetSendKey = config.AssetSendKey;
  248. TheSim.regionUserURI = config.DefaultUserServer;
  249. TheSim.regionUserSendKey = config.UserSendKey;
  250. TheSim.regionUserRecvKey = config.UserRecvKey;
  251. TheSim.serverIP = (string)requestData["sim_ip"];
  252. TheSim.serverPort = Convert.ToUInt32((string)requestData["sim_port"]);
  253. TheSim.httpPort = Convert.ToUInt32((string)requestData["http_port"]);
  254. TheSim.remotingPort = Convert.ToUInt32((string)requestData["remoting_port"]);
  255. TheSim.regionLocX = Convert.ToUInt32((string)requestData["region_locx"]);
  256. TheSim.regionLocY = Convert.ToUInt32((string)requestData["region_locy"]);
  257. TheSim.regionLocZ = 0;
  258. TheSim.regionMapTextureID = new LLUUID((string)requestData["map-image-id"]);
  259. TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX * 256), (TheSim.regionLocY * 256));
  260. TheSim.serverURI = "http://" + TheSim.serverIP + ":" + TheSim.serverPort + "/";
  261. TheSim.httpServerURI = "http://" + TheSim.serverIP + ":" + TheSim.httpPort + "/";
  262. TheSim.regionName = (string)requestData["sim_name"];
  263. TheSim.UUID = new LLUUID((string)requestData["UUID"]);
  264. //make sure there is not an existing region at this location
  265. OldSim = getRegion(TheSim.regionHandle);
  266. if (OldSim == null || OldSim.UUID == TheSim.UUID)
  267. {
  268. Console.WriteLine("adding region " + TheSim.regionLocX + " , " + TheSim.regionLocY + " , " +
  269. TheSim.serverURI);
  270. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  271. {
  272. try
  273. {
  274. DataResponse insertResponse = kvp.Value.AddProfile(TheSim);
  275. switch (insertResponse)
  276. {
  277. case DataResponse.RESPONSE_OK:
  278. MainLog.Instance.Verbose("grid", "New sim " + myword + " successful: " + TheSim.regionName);
  279. break;
  280. case DataResponse.RESPONSE_ERROR:
  281. MainLog.Instance.Warn("storage", "New sim creation failed (Error): " + TheSim.regionName);
  282. break;
  283. case DataResponse.RESPONSE_INVALIDCREDENTIALS:
  284. MainLog.Instance.Warn("storage",
  285. "New sim creation failed (Invalid Credentials): " + TheSim.regionName);
  286. break;
  287. case DataResponse.RESPONSE_AUTHREQUIRED:
  288. MainLog.Instance.Warn("storage",
  289. "New sim creation failed (Authentication Required): " +
  290. TheSim.regionName);
  291. break;
  292. }
  293. }
  294. catch (Exception e)
  295. {
  296. MainLog.Instance.Warn("storage",
  297. "Unable to add region " + TheSim.UUID.ToString() + " via " + kvp.Key);
  298. MainLog.Instance.Warn("storage", e.ToString());
  299. }
  300. if (getRegion(TheSim.regionHandle) == null)
  301. {
  302. responseData["error"] = "Unable to add new region";
  303. return response;
  304. }
  305. }
  306. ArrayList SimNeighboursData = new ArrayList();
  307. RegionProfileData neighbour;
  308. Hashtable NeighbourBlock;
  309. bool fastMode = false; // Only compatible with MySQL right now
  310. if (fastMode)
  311. {
  312. Dictionary<ulong, RegionProfileData> neighbours =
  313. getRegions(TheSim.regionLocX - 1, TheSim.regionLocY - 1, TheSim.regionLocX + 1,
  314. TheSim.regionLocY + 1);
  315. foreach (KeyValuePair<ulong, RegionProfileData> aSim in neighbours)
  316. {
  317. NeighbourBlock = new Hashtable();
  318. NeighbourBlock["sim_ip"] = Util.GetHostFromDNS(aSim.Value.serverIP.ToString()).ToString();
  319. NeighbourBlock["sim_port"] = aSim.Value.serverPort.ToString();
  320. NeighbourBlock["region_locx"] = aSim.Value.regionLocX.ToString();
  321. NeighbourBlock["region_locy"] = aSim.Value.regionLocY.ToString();
  322. NeighbourBlock["UUID"] = aSim.Value.UUID.ToString();
  323. NeighbourBlock["regionHandle"] = aSim.Value.regionHandle.ToString();
  324. if (aSim.Value.UUID != TheSim.UUID)
  325. SimNeighboursData.Add(NeighbourBlock);
  326. }
  327. }
  328. else
  329. {
  330. for (int x = -1; x < 2; x++)
  331. for (int y = -1; y < 2; y++)
  332. {
  333. if (
  334. getRegion(
  335. Helpers.UIntsToLong((uint)((TheSim.regionLocX + x) * 256),
  336. (uint)(TheSim.regionLocY + y) * 256)) != null)
  337. {
  338. neighbour =
  339. getRegion(
  340. Helpers.UIntsToLong((uint)((TheSim.regionLocX + x) * 256),
  341. (uint)(TheSim.regionLocY + y) * 256));
  342. NeighbourBlock = new Hashtable();
  343. NeighbourBlock["sim_ip"] = Util.GetHostFromDNS(neighbour.serverIP).ToString();
  344. NeighbourBlock["sim_port"] = neighbour.serverPort.ToString();
  345. NeighbourBlock["region_locx"] = neighbour.regionLocX.ToString();
  346. NeighbourBlock["region_locy"] = neighbour.regionLocY.ToString();
  347. NeighbourBlock["UUID"] = neighbour.UUID.ToString();
  348. NeighbourBlock["regionHandle"] = neighbour.regionHandle.ToString();
  349. if (neighbour.UUID != TheSim.UUID) SimNeighboursData.Add(NeighbourBlock);
  350. }
  351. }
  352. }
  353. responseData["UUID"] = TheSim.UUID.ToString();
  354. responseData["region_locx"] = TheSim.regionLocX.ToString();
  355. responseData["region_locy"] = TheSim.regionLocY.ToString();
  356. responseData["regionname"] = TheSim.regionName;
  357. responseData["estate_id"] = "1";
  358. responseData["neighbours"] = SimNeighboursData;
  359. responseData["sim_ip"] = TheSim.serverIP;
  360. responseData["sim_port"] = TheSim.serverPort.ToString();
  361. responseData["asset_url"] = TheSim.regionAssetURI;
  362. responseData["asset_sendkey"] = TheSim.regionAssetSendKey;
  363. responseData["asset_recvkey"] = TheSim.regionAssetRecvKey;
  364. responseData["user_url"] = TheSim.regionUserURI;
  365. responseData["user_sendkey"] = TheSim.regionUserSendKey;
  366. responseData["user_recvkey"] = TheSim.regionUserRecvKey;
  367. responseData["authkey"] = TheSim.regionSecret;
  368. // New! If set, use as URL to local sim storage (ie http://remotehost/region.yap)
  369. responseData["data_uri"] = TheSim.regionDataURI;
  370. responseData["allow_forceful_banlines"] = config.AllowForcefulBanlines;
  371. // Instead of sending a multitude of message servers to the registering sim
  372. // we should probably be sending a single one and parhaps it's backup
  373. // that has responsibility over routing it's messages.
  374. // The Sim won't be contacting us again about any of the message server stuff during it's time up.
  375. responseData["messageserver_count"] = _MessageServers.Count;
  376. for (int i = 0; i < _MessageServers.Count; i++)
  377. {
  378. responseData["messageserver_uri" + i] = _MessageServers[i].URI;
  379. responseData["messageserver_sendkey" + i] = _MessageServers[i].sendkey;
  380. responseData["messageserver_recvkey" + i] = _MessageServers[i].recvkey;
  381. }
  382. return response;
  383. }
  384. else
  385. {
  386. MainLog.Instance.Warn("grid", "Failed to add new region " + TheSim.regionName + " at location " + TheSim.regionLocX + " " + TheSim.regionLocY + " currently occupied by " + OldSim.regionName);
  387. responseData["error"] = "Another region already exists at that location. Try another";
  388. return response;
  389. }
  390. }
  391. public XmlRpcResponse XmlRpcSimulatorDataRequestMethod(XmlRpcRequest request)
  392. {
  393. Hashtable requestData = (Hashtable) request.Params[0];
  394. Hashtable responseData = new Hashtable();
  395. RegionProfileData simData = null;
  396. if (requestData.ContainsKey("region_UUID"))
  397. {
  398. simData = getRegion(new LLUUID((string) requestData["region_UUID"]));
  399. }
  400. else if (requestData.ContainsKey("region_handle"))
  401. {
  402. //CFK: The if/else below this makes this message redundant.
  403. //CFK: Console.WriteLine("requesting data for region " + (string) requestData["region_handle"]);
  404. simData = getRegion(Convert.ToUInt64((string) requestData["region_handle"]));
  405. }
  406. if (simData == null)
  407. {
  408. //Sim does not exist
  409. Console.WriteLine("region not found");
  410. responseData["error"] = "Sim does not exist";
  411. }
  412. else
  413. {
  414. MainLog.Instance.Verbose("DATA", "found " + (string) simData.regionName + " regionHandle = " +
  415. (string) requestData["region_handle"]);
  416. responseData["sim_ip"] = Util.GetHostFromDNS(simData.serverIP).ToString();
  417. responseData["sim_port"] = simData.serverPort.ToString();
  418. responseData["http_port"] = simData.httpPort.ToString();
  419. responseData["remoting_port"] = simData.remotingPort.ToString();
  420. responseData["region_locx"] = simData.regionLocX.ToString();
  421. responseData["region_locy"] = simData.regionLocY.ToString();
  422. responseData["region_UUID"] = simData.UUID.UUID.ToString();
  423. responseData["region_name"] = simData.regionName;
  424. responseData["regionHandle"] = simData.regionHandle.ToString();
  425. }
  426. XmlRpcResponse response = new XmlRpcResponse();
  427. response.Value = responseData;
  428. return response;
  429. }
  430. public XmlRpcResponse XmlRpcMapBlockMethod(XmlRpcRequest request)
  431. {
  432. int xmin = 980, ymin = 980, xmax = 1020, ymax = 1020;
  433. Hashtable requestData = (Hashtable) request.Params[0];
  434. if (requestData.ContainsKey("xmin"))
  435. {
  436. xmin = (Int32) requestData["xmin"];
  437. }
  438. if (requestData.ContainsKey("ymin"))
  439. {
  440. ymin = (Int32) requestData["ymin"];
  441. }
  442. if (requestData.ContainsKey("xmax"))
  443. {
  444. xmax = (Int32) requestData["xmax"];
  445. }
  446. if (requestData.ContainsKey("ymax"))
  447. {
  448. ymax = (Int32) requestData["ymax"];
  449. }
  450. //CFK: The second MainLog is more meaningful and either standard or fast generally occurs.
  451. //CFK: MainLog.Instance.Verbose("MAP", "World map request for range (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  452. XmlRpcResponse response = new XmlRpcResponse();
  453. Hashtable responseData = new Hashtable();
  454. response.Value = responseData;
  455. IList simProfileList = new ArrayList();
  456. bool fastMode = false; // MySQL Only
  457. if (fastMode)
  458. {
  459. Dictionary<ulong, RegionProfileData> neighbours =
  460. getRegions((uint) xmin, (uint) ymin, (uint) xmax, (uint) ymax);
  461. foreach (KeyValuePair<ulong, RegionProfileData> aSim in neighbours)
  462. {
  463. Hashtable simProfileBlock = new Hashtable();
  464. simProfileBlock["x"] = aSim.Value.regionLocX.ToString();
  465. simProfileBlock["y"] = aSim.Value.regionLocY.ToString();
  466. Console.WriteLine("send neighbour info for " + aSim.Value.regionLocX.ToString() + " , " +
  467. aSim.Value.regionLocY.ToString());
  468. simProfileBlock["name"] = aSim.Value.regionName;
  469. simProfileBlock["access"] = 21;
  470. simProfileBlock["region-flags"] = 512;
  471. simProfileBlock["water-height"] = 0;
  472. simProfileBlock["agents"] = 1;
  473. simProfileBlock["map-image-id"] = aSim.Value.regionMapTextureID.ToString();
  474. // For Sugilite compatibility
  475. simProfileBlock["regionhandle"] = aSim.Value.regionHandle.ToString();
  476. simProfileBlock["sim_ip"] = aSim.Value.serverIP.ToString();
  477. simProfileBlock["sim_port"] = aSim.Value.serverPort.ToString();
  478. simProfileBlock["sim_uri"] = aSim.Value.serverURI.ToString();
  479. simProfileBlock["uuid"] = aSim.Value.UUID.ToString();
  480. simProfileBlock["remoting_port"] = aSim.Value.remotingPort;
  481. simProfileList.Add(simProfileBlock);
  482. }
  483. MainLog.Instance.Verbose("MAP", "Fast map " + simProfileList.Count.ToString() +
  484. " regions @ (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  485. }
  486. else
  487. {
  488. RegionProfileData simProfile;
  489. for (int x = xmin; x < xmax + 1; x++)
  490. {
  491. for (int y = ymin; y < ymax + 1; y++)
  492. {
  493. ulong regHandle = Helpers.UIntsToLong((uint) (x*256), (uint) (y*256));
  494. simProfile = getRegion(regHandle);
  495. if (simProfile != null)
  496. {
  497. Hashtable simProfileBlock = new Hashtable();
  498. simProfileBlock["x"] = x;
  499. simProfileBlock["y"] = y;
  500. simProfileBlock["name"] = simProfile.regionName;
  501. simProfileBlock["access"] = 0;
  502. simProfileBlock["region-flags"] = 0;
  503. simProfileBlock["water-height"] = 20;
  504. simProfileBlock["agents"] = 1;
  505. simProfileBlock["map-image-id"] = simProfile.regionMapTextureID.ToString();
  506. // For Sugilite compatibility
  507. simProfileBlock["regionhandle"] = simProfile.regionHandle.ToString();
  508. simProfileBlock["sim_ip"] = simProfile.serverIP.ToString();
  509. simProfileBlock["sim_port"] = simProfile.serverPort.ToString();
  510. simProfileBlock["sim_uri"] = simProfile.serverURI.ToString();
  511. simProfileBlock["uuid"] = simProfile.UUID.ToString();
  512. simProfileList.Add(simProfileBlock);
  513. }
  514. }
  515. }
  516. MainLog.Instance.Verbose("MAP", "Std map " + simProfileList.Count.ToString() +
  517. " regions @ (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  518. }
  519. responseData["sim-profiles"] = simProfileList;
  520. return response;
  521. }
  522. /// <summary>
  523. /// Performs a REST Get Operation
  524. /// </summary>
  525. /// <param name="request"></param>
  526. /// <param name="path"></param>
  527. /// <param name="param"></param>
  528. /// <returns></returns>
  529. public string RestGetRegionMethod(string request, string path, string param)
  530. {
  531. return RestGetSimMethod("", "/sims/", param);
  532. }
  533. /// <summary>
  534. /// Performs a REST Set Operation
  535. /// </summary>
  536. /// <param name="request"></param>
  537. /// <param name="path"></param>
  538. /// <param name="param"></param>
  539. /// <returns></returns>
  540. public string RestSetRegionMethod(string request, string path, string param)
  541. {
  542. return RestSetSimMethod("", "/sims/", param);
  543. }
  544. /// <summary>
  545. /// Returns information about a sim via a REST Request
  546. /// </summary>
  547. /// <param name="request"></param>
  548. /// <param name="path"></param>
  549. /// <param name="param"></param>
  550. /// <returns>Information about the sim in XML</returns>
  551. public string RestGetSimMethod(string request, string path, string param)
  552. {
  553. string respstring = String.Empty;
  554. RegionProfileData TheSim;
  555. LLUUID UUID = new LLUUID(param);
  556. TheSim = getRegion(UUID);
  557. if (!(TheSim == null))
  558. {
  559. respstring = "<Root>";
  560. respstring += "<authkey>" + TheSim.regionSendKey + "</authkey>";
  561. respstring += "<sim>";
  562. respstring += "<uuid>" + TheSim.UUID.ToString() + "</uuid>";
  563. respstring += "<regionname>" + TheSim.regionName + "</regionname>";
  564. respstring += "<sim_ip>" + Util.GetHostFromDNS(TheSim.serverIP).ToString() + "</sim_ip>";
  565. respstring += "<sim_port>" + TheSim.serverPort.ToString() + "</sim_port>";
  566. respstring += "<region_locx>" + TheSim.regionLocX.ToString() + "</region_locx>";
  567. respstring += "<region_locy>" + TheSim.regionLocY.ToString() + "</region_locy>";
  568. respstring += "<estate_id>1</estate_id>";
  569. respstring += "</sim>";
  570. respstring += "</Root>";
  571. }
  572. return respstring;
  573. }
  574. /// <summary>
  575. /// Creates or updates a sim via a REST Method Request
  576. /// BROKEN with SQL Update
  577. /// </summary>
  578. /// <param name="request"></param>
  579. /// <param name="path"></param>
  580. /// <param name="param"></param>
  581. /// <returns>"OK" or an error</returns>
  582. public string RestSetSimMethod(string request, string path, string param)
  583. {
  584. Console.WriteLine("Processing region update via REST method");
  585. RegionProfileData TheSim;
  586. TheSim = getRegion(new LLUUID(param));
  587. if ((TheSim) == null)
  588. {
  589. TheSim = new RegionProfileData();
  590. LLUUID UUID = new LLUUID(param);
  591. TheSim.UUID = UUID;
  592. TheSim.regionRecvKey = config.SimRecvKey;
  593. }
  594. XmlDocument doc = new XmlDocument();
  595. doc.LoadXml(request);
  596. XmlNode rootnode = doc.FirstChild;
  597. XmlNode authkeynode = rootnode.ChildNodes[0];
  598. if (authkeynode.Name != "authkey")
  599. {
  600. return "ERROR! bad XML - expected authkey tag";
  601. }
  602. XmlNode simnode = rootnode.ChildNodes[1];
  603. if (simnode.Name != "sim")
  604. {
  605. return "ERROR! bad XML - expected sim tag";
  606. }
  607. //TheSim.regionSendKey = Cfg;
  608. TheSim.regionRecvKey = config.SimRecvKey;
  609. TheSim.regionSendKey = config.SimSendKey;
  610. TheSim.regionSecret = config.SimRecvKey;
  611. TheSim.regionDataURI = "";
  612. TheSim.regionAssetURI = config.DefaultAssetServer;
  613. TheSim.regionAssetRecvKey = config.AssetRecvKey;
  614. TheSim.regionAssetSendKey = config.AssetSendKey;
  615. TheSim.regionUserURI = config.DefaultUserServer;
  616. TheSim.regionUserSendKey = config.UserSendKey;
  617. TheSim.regionUserRecvKey = config.UserRecvKey;
  618. for (int i = 0; i < simnode.ChildNodes.Count; i++)
  619. {
  620. switch (simnode.ChildNodes[i].Name)
  621. {
  622. case "regionname":
  623. TheSim.regionName = simnode.ChildNodes[i].InnerText;
  624. break;
  625. case "sim_ip":
  626. TheSim.serverIP = simnode.ChildNodes[i].InnerText;
  627. break;
  628. case "sim_port":
  629. TheSim.serverPort = Convert.ToUInt32(simnode.ChildNodes[i].InnerText);
  630. break;
  631. case "region_locx":
  632. TheSim.regionLocX = Convert.ToUInt32((string) simnode.ChildNodes[i].InnerText);
  633. TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX*256), (TheSim.regionLocY*256));
  634. break;
  635. case "region_locy":
  636. TheSim.regionLocY = Convert.ToUInt32((string) simnode.ChildNodes[i].InnerText);
  637. TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX*256), (TheSim.regionLocY*256));
  638. break;
  639. }
  640. }
  641. TheSim.serverURI = "http://" + TheSim.serverIP + ":" + TheSim.serverPort + "/";
  642. bool requirePublic = false;
  643. bool requireValid = true;
  644. if (requirePublic &&
  645. (TheSim.serverIP.StartsWith("172.16") || TheSim.serverIP.StartsWith("192.168") ||
  646. TheSim.serverIP.StartsWith("10.") || TheSim.serverIP.StartsWith("0.") ||
  647. TheSim.serverIP.StartsWith("255.")))
  648. {
  649. return "ERROR! Servers must register with public addresses.";
  650. }
  651. if (requireValid && (TheSim.serverIP.StartsWith("0.")))
  652. {
  653. return "ERROR! 0.*.*.* Addresses are invalid, please check your server config and try again";
  654. }
  655. try
  656. {
  657. MainLog.Instance.Verbose("DATA",
  658. "Updating / adding via " + _plugins.Count + " storage provider(s) registered.");
  659. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  660. {
  661. try
  662. {
  663. //Check reservations
  664. ReservationData reserveData =
  665. kvp.Value.GetReservationAtPoint(TheSim.regionLocX, TheSim.regionLocY);
  666. if ((reserveData != null && reserveData.gridRecvKey == TheSim.regionRecvKey) ||
  667. (reserveData == null && authkeynode.InnerText != TheSim.regionRecvKey))
  668. {
  669. kvp.Value.AddProfile(TheSim);
  670. MainLog.Instance.Verbose("grid", "New sim added to grid (" + TheSim.regionName + ")");
  671. logToDB(TheSim.UUID.ToString(), "RestSetSimMethod", "", 5,
  672. "Region successfully updated and connected to grid.");
  673. }
  674. else
  675. {
  676. MainLog.Instance.Warn("grid",
  677. "Unable to update region (RestSetSimMethod): Incorrect reservation auth key.");
  678. // Wanted: " + reserveData.gridRecvKey + ", Got: " + TheSim.regionRecvKey + ".");
  679. return "Unable to update region (RestSetSimMethod): Incorrect auth key.";
  680. }
  681. }
  682. catch (Exception e)
  683. {
  684. MainLog.Instance.Warn("GRID", "getRegionPlugin Handle " + kvp.Key + " unable to add new sim: " +
  685. e.ToString());
  686. }
  687. }
  688. return "OK";
  689. }
  690. catch (Exception e)
  691. {
  692. return "ERROR! Could not save to database! (" + e.ToString() + ")";
  693. }
  694. }
  695. public XmlRpcResponse XmlRPCRegisterMessageServer(XmlRpcRequest request)
  696. {
  697. XmlRpcResponse response = new XmlRpcResponse();
  698. Hashtable requestData = (Hashtable)request.Params[0];
  699. Hashtable responseData = new Hashtable();
  700. if (requestData.Contains("uri"))
  701. {
  702. string URI = (string)requestData["URI"];
  703. string sendkey = (string)requestData["sendkey"];
  704. string recvkey = (string)requestData["recvkey"];
  705. MessageServerInfo m = new MessageServerInfo();
  706. m.URI = URI;
  707. m.sendkey = sendkey;
  708. m.recvkey = recvkey;
  709. if (!_MessageServers.Contains(m))
  710. _MessageServers.Add(m);
  711. responseData["responsestring"] = "TRUE";
  712. response.Value = responseData;
  713. }
  714. return response;
  715. }
  716. public XmlRpcResponse XmlRPCDeRegisterMessageServer(XmlRpcRequest request)
  717. {
  718. XmlRpcResponse response = new XmlRpcResponse();
  719. Hashtable requestData = (Hashtable)request.Params[0];
  720. Hashtable responseData = new Hashtable();
  721. if (requestData.Contains("uri"))
  722. {
  723. string URI = (string)requestData["uri"];
  724. string sendkey = (string)requestData["sendkey"];
  725. string recvkey = (string)requestData["recvkey"];
  726. MessageServerInfo m = new MessageServerInfo();
  727. m.URI = URI;
  728. m.sendkey = sendkey;
  729. m.recvkey = recvkey;
  730. if (_MessageServers.Contains(m))
  731. _MessageServers.Remove(m);
  732. responseData["responsestring"] = "TRUE";
  733. response.Value = responseData;
  734. }
  735. return response;
  736. }
  737. }
  738. }