GridManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Reflection;
  6. using OpenGrid.Framework.Data;
  7. using OpenSim.Framework.Utilities;
  8. using OpenSim.Framework.Console;
  9. using OpenSim.Framework.Sims;
  10. using libsecondlife;
  11. using Nwc.XmlRpc;
  12. using System.Xml;
  13. namespace OpenGridServices.GridServer
  14. {
  15. class GridManager
  16. {
  17. Dictionary<string, IGridData> _plugins = new Dictionary<string, IGridData>();
  18. public OpenSim.Framework.Interfaces.GridConfig config;
  19. /// <summary>
  20. /// Adds a new grid server plugin - grid servers will be requested in the order they were loaded.
  21. /// </summary>
  22. /// <param name="FileName">The filename to the grid server plugin DLL</param>
  23. public void AddPlugin(string FileName)
  24. {
  25. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"Storage: Attempting to load " + FileName);
  26. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  27. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"Storage: Found " + pluginAssembly.GetTypes().Length + " interfaces.");
  28. foreach (Type pluginType in pluginAssembly.GetTypes())
  29. {
  30. if (!pluginType.IsAbstract)
  31. {
  32. Type typeInterface = pluginType.GetInterface("IGridData", true);
  33. if (typeInterface != null)
  34. {
  35. IGridData plug = (IGridData)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  36. plug.Initialise();
  37. this._plugins.Add(plug.getName(), plug);
  38. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"Storage: Added IGridData Interface");
  39. }
  40. typeInterface = null;
  41. }
  42. }
  43. pluginAssembly = null;
  44. }
  45. /// <summary>
  46. /// Returns a region by argument
  47. /// </summary>
  48. /// <param name="uuid">A UUID key of the region to return</param>
  49. /// <returns>A SimProfileData for the region</returns>
  50. public SimProfileData getRegion(libsecondlife.LLUUID uuid)
  51. {
  52. foreach(KeyValuePair<string,IGridData> kvp in _plugins) {
  53. try
  54. {
  55. return kvp.Value.GetProfileByLLUUID(uuid);
  56. }
  57. catch (Exception e)
  58. {
  59. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.NORMAL,"Storage: Unable to find region " + uuid.ToStringHyphenated() + " via " + kvp.Key);
  60. }
  61. }
  62. return null;
  63. }
  64. /// <summary>
  65. /// Returns a region by argument
  66. /// </summary>
  67. /// <param name="uuid">A regionHandle of the region to return</param>
  68. /// <returns>A SimProfileData for the region</returns>
  69. public SimProfileData getRegion(ulong handle)
  70. {
  71. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  72. {
  73. try
  74. {
  75. return kvp.Value.GetProfileByHandle(handle);
  76. }
  77. catch (Exception e)
  78. {
  79. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.NORMAL,"Storage: Unable to find region " + handle.ToString() + " via " + kvp.Key);
  80. }
  81. }
  82. return null;
  83. }
  84. public Dictionary<ulong, SimProfileData> getRegions(uint xmin, uint ymin, uint xmax, uint ymax)
  85. {
  86. Dictionary<ulong, SimProfileData> regions = new Dictionary<ulong, SimProfileData>();
  87. SimProfileData[] neighbours;
  88. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  89. {
  90. try
  91. {
  92. neighbours = kvp.Value.GetProfilesInRange(xmin, ymin, xmax, ymax);
  93. foreach (SimProfileData neighbour in neighbours)
  94. {
  95. regions[neighbour.regionHandle] = neighbour;
  96. }
  97. }
  98. catch (Exception e)
  99. {
  100. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.NORMAL, "Storage: Unable to query regionblock via " + kvp.Key);
  101. }
  102. }
  103. return regions;
  104. }
  105. /// <summary>
  106. /// Returns a XML String containing a list of the neighbouring regions
  107. /// </summary>
  108. /// <param name="reqhandle">The regionhandle for the center sim</param>
  109. /// <returns>An XML string containing neighbour entities</returns>
  110. public string GetXMLNeighbours(ulong reqhandle)
  111. {
  112. string response = "";
  113. SimProfileData central_region = getRegion(reqhandle);
  114. SimProfileData neighbour;
  115. for (int x = -1; x < 2; x++) for (int y = -1; y < 2; y++)
  116. {
  117. if (getRegion(Util.UIntsToLong((uint)((central_region.regionLocX + x) * 256), (uint)(central_region.regionLocY + y) * 256)) != null)
  118. {
  119. neighbour = getRegion(Util.UIntsToLong((uint)((central_region.regionLocX + x) * 256), (uint)(central_region.regionLocY + y) * 256));
  120. response += "<neighbour>";
  121. response += "<sim_ip>" + neighbour.serverIP + "</sim_ip>";
  122. response += "<sim_port>" + neighbour.serverPort.ToString() + "</sim_port>";
  123. response += "<locx>" + neighbour.regionLocX.ToString() + "</locx>";
  124. response += "<locy>" + neighbour.regionLocY.ToString() + "</locy>";
  125. response += "<regionhandle>" + neighbour.regionHandle.ToString() + "</regionhandle>";
  126. response += "</neighbour>";
  127. }
  128. }
  129. return response;
  130. }
  131. /// <summary>
  132. /// Performed when a region connects to the grid server initially.
  133. /// </summary>
  134. /// <param name="request">The XMLRPC Request</param>
  135. /// <returns>Startup parameters</returns>
  136. public XmlRpcResponse XmlRpcLoginToSimulatorMethod(XmlRpcRequest request)
  137. {
  138. XmlRpcResponse response = new XmlRpcResponse();
  139. Hashtable responseData = new Hashtable();
  140. response.Value = responseData;
  141. SimProfileData TheSim = null;
  142. Hashtable requestData = (Hashtable)request.Params[0];
  143. if (requestData.ContainsKey("UUID"))
  144. {
  145. TheSim = getRegion(new LLUUID((string)requestData["UUID"]));
  146. }
  147. else if (requestData.ContainsKey("region_handle"))
  148. {
  149. TheSim = getRegion((ulong)Convert.ToUInt64(requestData["region_handle"]));
  150. }
  151. if (TheSim == null)
  152. {
  153. responseData["error"] = "sim not found";
  154. }
  155. else
  156. {
  157. ArrayList SimNeighboursData = new ArrayList();
  158. SimProfileData neighbour;
  159. Hashtable NeighbourBlock;
  160. bool fastMode = false; // Only compatible with MySQL right now
  161. if (fastMode)
  162. {
  163. Dictionary<ulong, SimProfileData> neighbours = getRegions(TheSim.regionLocX - 1, TheSim.regionLocY - 1, TheSim.regionLocX + 1, TheSim.regionLocY + 1);
  164. foreach (KeyValuePair<ulong, SimProfileData> aSim in neighbours)
  165. {
  166. NeighbourBlock = new Hashtable();
  167. NeighbourBlock["sim_ip"] = aSim.Value.serverIP.ToString();
  168. NeighbourBlock["sim_port"] = aSim.Value.serverPort.ToString();
  169. NeighbourBlock["region_locx"] = aSim.Value.regionLocX.ToString();
  170. NeighbourBlock["region_locy"] = aSim.Value.regionLocY.ToString();
  171. NeighbourBlock["UUID"] = aSim.Value.UUID.ToString();
  172. if (aSim.Value.UUID != TheSim.UUID)
  173. SimNeighboursData.Add(NeighbourBlock);
  174. }
  175. }
  176. else
  177. {
  178. for (int x = -1; x < 2; x++) for (int y = -1; y < 2; y++)
  179. {
  180. if (getRegion(Helpers.UIntsToLong((uint)((TheSim.regionLocX + x) * 256), (uint)(TheSim.regionLocY + y) * 256)) != null)
  181. {
  182. neighbour = getRegion(Helpers.UIntsToLong((uint)((TheSim.regionLocX + x) * 256), (uint)(TheSim.regionLocY + y) * 256));
  183. NeighbourBlock = new Hashtable();
  184. NeighbourBlock["sim_ip"] = neighbour.serverIP;
  185. NeighbourBlock["sim_port"] = neighbour.serverPort.ToString();
  186. NeighbourBlock["region_locx"] = neighbour.regionLocX.ToString();
  187. NeighbourBlock["region_locy"] = neighbour.regionLocY.ToString();
  188. NeighbourBlock["UUID"] = neighbour.UUID.ToString();
  189. if (neighbour.UUID != TheSim.UUID) SimNeighboursData.Add(NeighbourBlock);
  190. }
  191. }
  192. }
  193. responseData["UUID"] = TheSim.UUID.ToString();
  194. responseData["region_locx"] = TheSim.regionLocX.ToString();
  195. responseData["region_locy"] = TheSim.regionLocY.ToString();
  196. responseData["regionname"] = TheSim.regionName;
  197. responseData["estate_id"] = "1";
  198. responseData["neighbours"] = SimNeighboursData;
  199. responseData["sim_ip"] = TheSim.serverIP;
  200. responseData["sim_port"] = TheSim.serverPort.ToString();
  201. responseData["asset_url"] = TheSim.regionAssetURI;
  202. responseData["asset_sendkey"] = TheSim.regionAssetSendKey;
  203. responseData["asset_recvkey"] = TheSim.regionAssetRecvKey;
  204. responseData["user_url"] = TheSim.regionUserURI;
  205. responseData["user_sendkey"] = TheSim.regionUserSendKey;
  206. responseData["user_recvkey"] = TheSim.regionUserRecvKey;
  207. responseData["authkey"] = TheSim.regionSecret;
  208. // New! If set, use as URL to local sim storage (ie http://remotehost/region.yap)
  209. responseData["data_uri"] = TheSim.regionDataURI;
  210. }
  211. return response;
  212. }
  213. public XmlRpcResponse XmlRpcMapBlockMethod(XmlRpcRequest request)
  214. {
  215. int xmin=980, ymin=980, xmax=1020, ymax=1020;
  216. Hashtable requestData = (Hashtable)request.Params[0];
  217. if (requestData.ContainsKey("xmin"))
  218. {
  219. xmin = (Int32)requestData["xmin"];
  220. }
  221. if (requestData.ContainsKey("ymin"))
  222. {
  223. ymin = (Int32)requestData["ymin"];
  224. }
  225. if (requestData.ContainsKey("xmax"))
  226. {
  227. xmax = (Int32)requestData["xmax"];
  228. }
  229. if (requestData.ContainsKey("ymax"))
  230. {
  231. ymax = (Int32)requestData["ymax"];
  232. }
  233. XmlRpcResponse response = new XmlRpcResponse();
  234. Hashtable responseData = new Hashtable();
  235. response.Value = responseData;
  236. IList simProfileList = new ArrayList();
  237. SimProfileData simProfile;
  238. for (int x = xmin; x < xmax; x++)
  239. {
  240. for (int y = ymin; y < ymax; y++)
  241. {
  242. simProfile = getRegion(Helpers.UIntsToLong((uint)(x * 256), (uint)(y * 256)));
  243. if (simProfile != null)
  244. {
  245. Hashtable simProfileBlock = new Hashtable();
  246. simProfileBlock["x"] = x;
  247. simProfileBlock["y"] = y;
  248. simProfileBlock["name"] = simProfile.regionName;
  249. simProfileBlock["access"] = 0;
  250. simProfileBlock["region-flags"] = 0;
  251. simProfileBlock["water-height"] = 20;
  252. simProfileBlock["agents"] = 1;
  253. simProfileBlock["map-image-id"] = simProfile.regionMapTextureID.ToString();
  254. simProfileList.Add(simProfileBlock);
  255. }
  256. }
  257. }
  258. responseData["sim-profiles"] = simProfileList;
  259. return response;
  260. }
  261. /// <summary>
  262. /// Performs a REST Get Operation
  263. /// </summary>
  264. /// <param name="request"></param>
  265. /// <param name="path"></param>
  266. /// <param name="param"></param>
  267. /// <returns></returns>
  268. public string RestGetRegionMethod(string request, string path, string param)
  269. {
  270. return RestGetSimMethod("", "/sims/", param);
  271. }
  272. /// <summary>
  273. /// Performs a REST Set Operation
  274. /// </summary>
  275. /// <param name="request"></param>
  276. /// <param name="path"></param>
  277. /// <param name="param"></param>
  278. /// <returns></returns>
  279. public string RestSetRegionMethod(string request, string path, string param)
  280. {
  281. return RestSetSimMethod("", "/sims/", param);
  282. }
  283. /// <summary>
  284. /// Returns information about a sim via a REST Request
  285. /// </summary>
  286. /// <param name="request"></param>
  287. /// <param name="path"></param>
  288. /// <param name="param"></param>
  289. /// <returns>Information about the sim in XML</returns>
  290. public string RestGetSimMethod(string request, string path, string param)
  291. {
  292. string respstring = String.Empty;
  293. SimProfileData TheSim;
  294. LLUUID UUID = new LLUUID(param);
  295. TheSim = getRegion(UUID);
  296. if (!(TheSim == null))
  297. {
  298. respstring = "<Root>";
  299. respstring += "<authkey>" + TheSim.regionSendKey + "</authkey>";
  300. respstring += "<sim>";
  301. respstring += "<uuid>" + TheSim.UUID.ToString() + "</uuid>";
  302. respstring += "<regionname>" + TheSim.regionName + "</regionname>";
  303. respstring += "<sim_ip>" + TheSim.serverIP + "</sim_ip>";
  304. respstring += "<sim_port>" + TheSim.serverPort.ToString() + "</sim_port>";
  305. respstring += "<region_locx>" + TheSim.regionLocX.ToString() + "</region_locx>";
  306. respstring += "<region_locy>" + TheSim.regionLocY.ToString() + "</region_locy>";
  307. respstring += "<estate_id>1</estate_id>";
  308. respstring += "</sim>";
  309. respstring += "</Root>";
  310. }
  311. return respstring;
  312. }
  313. /// <summary>
  314. /// Creates or updates a sim via a REST Method Request
  315. /// BROKEN with SQL Update
  316. /// </summary>
  317. /// <param name="request"></param>
  318. /// <param name="path"></param>
  319. /// <param name="param"></param>
  320. /// <returns>"OK" or an error</returns>
  321. public string RestSetSimMethod(string request, string path, string param)
  322. {
  323. Console.WriteLine("SimProfiles.cs:RestSetSimMethod() - processing request......");
  324. SimProfileData TheSim;
  325. TheSim = getRegion(new LLUUID(param));
  326. if ((TheSim) == null)
  327. {
  328. TheSim = new SimProfileData();
  329. LLUUID UUID = new LLUUID(param);
  330. TheSim.UUID = UUID;
  331. TheSim.regionRecvKey = config.SimRecvKey;
  332. }
  333. XmlDocument doc = new XmlDocument();
  334. doc.LoadXml(request);
  335. XmlNode rootnode = doc.FirstChild;
  336. XmlNode authkeynode = rootnode.ChildNodes[0];
  337. if (authkeynode.Name != "authkey")
  338. {
  339. return "ERROR! bad XML - expected authkey tag";
  340. }
  341. XmlNode simnode = rootnode.ChildNodes[1];
  342. if (simnode.Name != "sim")
  343. {
  344. return "ERROR! bad XML - expected sim tag";
  345. }
  346. if (authkeynode.InnerText != TheSim.regionRecvKey)
  347. {
  348. return "ERROR! invalid key";
  349. }
  350. //TheSim.regionSendKey = Cfg;
  351. TheSim.regionRecvKey = config.SimRecvKey;
  352. TheSim.regionSendKey = config.SimSendKey;
  353. TheSim.regionSecret = config.SimRecvKey;
  354. TheSim.regionDataURI = "";
  355. TheSim.regionAssetURI = config.DefaultAssetServer;
  356. TheSim.regionAssetRecvKey = config.AssetRecvKey;
  357. TheSim.regionAssetSendKey = config.AssetSendKey;
  358. TheSim.regionUserURI = config.DefaultUserServer;
  359. TheSim.regionUserSendKey = config.UserSendKey;
  360. TheSim.regionUserRecvKey = config.UserRecvKey;
  361. for (int i = 0; i < simnode.ChildNodes.Count; i++)
  362. {
  363. switch (simnode.ChildNodes[i].Name)
  364. {
  365. case "regionname":
  366. TheSim.regionName = simnode.ChildNodes[i].InnerText;
  367. break;
  368. case "sim_ip":
  369. TheSim.serverIP = simnode.ChildNodes[i].InnerText;
  370. break;
  371. case "sim_port":
  372. TheSim.serverPort = Convert.ToUInt32(simnode.ChildNodes[i].InnerText);
  373. break;
  374. case "region_locx":
  375. TheSim.regionLocX = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
  376. TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX * 256), (TheSim.regionLocY * 256));
  377. break;
  378. case "region_locy":
  379. TheSim.regionLocY = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
  380. TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX * 256), (TheSim.regionLocY * 256));
  381. break;
  382. }
  383. }
  384. TheSim.serverURI = "http://" + TheSim.serverIP + ":" + TheSim.serverPort + "/";
  385. bool requirePublic = false;
  386. if (requirePublic && (TheSim.serverIP.StartsWith("172.16") || TheSim.serverIP.StartsWith("192.168") || TheSim.serverIP.StartsWith("10.") || TheSim.serverIP.StartsWith("0.") || TheSim.serverIP.StartsWith("255.")))
  387. {
  388. return "ERROR! Servers must register with public addresses.";
  389. }
  390. try
  391. {
  392. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"Attempting to add a new region to the grid - " + _plugins.Count + " storage provider(s) registered.");
  393. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  394. {
  395. try
  396. {
  397. kvp.Value.AddProfile(TheSim);
  398. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"New sim added to grid (" + TheSim.regionName + ")");
  399. }
  400. catch (Exception e)
  401. {
  402. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"getRegionPlugin Handle " + kvp.Key + " unable to add new sim: " + e.ToString());
  403. }
  404. }
  405. return "OK";
  406. }
  407. catch (Exception e)
  408. {
  409. return "ERROR! Could not save to database! (" + e.ToString() + ")";
  410. }
  411. }
  412. }
  413. }