GridManager.cs 42 KB

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